CreateHTML.js 927 B

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var GetIntrinsic = require('get-intrinsic');
  3. var $TypeError = GetIntrinsic('%TypeError%');
  4. var callBound = require('call-bind/callBound');
  5. var $replace = callBound('String.prototype.replace');
  6. var RequireObjectCoercible = require('./RequireObjectCoercible');
  7. var ToString = require('./ToString');
  8. var Type = require('./Type');
  9. // https://ecma-international.org/ecma-262/6.0/#sec-createhtml
  10. module.exports = function CreateHTML(string, tag, attribute, value) {
  11. if (Type(tag) !== 'String' || Type(attribute) !== 'String') {
  12. throw new $TypeError('Assertion failed: `tag` and `attribute` must be strings');
  13. }
  14. var str = RequireObjectCoercible(string);
  15. var S = ToString(str);
  16. var p1 = '<' + tag;
  17. if (attribute !== '') {
  18. var V = ToString(value);
  19. var escapedV = $replace(V, /\x22/g, '&quot;');
  20. p1 += '\x20' + attribute + '\x3D\x22' + escapedV + '\x22';
  21. }
  22. return p1 + '>' + S + '</' + tag + '>';
  23. };