CreateHTML.js 846 B

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