You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
5.1 KiB

  1. 'use strict';
  2. const defaultTreeAdapter = require('../tree-adapters/default');
  3. const mergeOptions = require('../utils/merge-options');
  4. const doctype = require('../common/doctype');
  5. const HTML = require('../common/html');
  6. //Aliases
  7. const $ = HTML.TAG_NAMES;
  8. const NS = HTML.NAMESPACES;
  9. //Default serializer options
  10. const DEFAULT_OPTIONS = {
  11. treeAdapter: defaultTreeAdapter
  12. };
  13. //Escaping regexes
  14. const AMP_REGEX = /&/g;
  15. const NBSP_REGEX = /\u00a0/g;
  16. const DOUBLE_QUOTE_REGEX = /"/g;
  17. const LT_REGEX = /</g;
  18. const GT_REGEX = />/g;
  19. //Serializer
  20. class Serializer {
  21. constructor(node, options) {
  22. this.options = mergeOptions(DEFAULT_OPTIONS, options);
  23. this.treeAdapter = this.options.treeAdapter;
  24. this.html = '';
  25. this.startNode = node;
  26. }
  27. //API
  28. serialize() {
  29. this._serializeChildNodes(this.startNode);
  30. return this.html;
  31. }
  32. //Internals
  33. _serializeChildNodes(parentNode) {
  34. const childNodes = this.treeAdapter.getChildNodes(parentNode);
  35. if (childNodes) {
  36. for (let i = 0, cnLength = childNodes.length; i < cnLength; i++) {
  37. const currentNode = childNodes[i];
  38. if (this.treeAdapter.isElementNode(currentNode)) {
  39. this._serializeElement(currentNode);
  40. } else if (this.treeAdapter.isTextNode(currentNode)) {
  41. this._serializeTextNode(currentNode);
  42. } else if (this.treeAdapter.isCommentNode(currentNode)) {
  43. this._serializeCommentNode(currentNode);
  44. } else if (this.treeAdapter.isDocumentTypeNode(currentNode)) {
  45. this._serializeDocumentTypeNode(currentNode);
  46. }
  47. }
  48. }
  49. }
  50. _serializeElement(node) {
  51. const tn = this.treeAdapter.getTagName(node);
  52. const ns = this.treeAdapter.getNamespaceURI(node);
  53. this.html += '<' + tn;
  54. this._serializeAttributes(node);
  55. this.html += '>';
  56. if (
  57. tn !== $.AREA &&
  58. tn !== $.BASE &&
  59. tn !== $.BASEFONT &&
  60. tn !== $.BGSOUND &&
  61. tn !== $.BR &&
  62. tn !== $.COL &&
  63. tn !== $.EMBED &&
  64. tn !== $.FRAME &&
  65. tn !== $.HR &&
  66. tn !== $.IMG &&
  67. tn !== $.INPUT &&
  68. tn !== $.KEYGEN &&
  69. tn !== $.LINK &&
  70. tn !== $.META &&
  71. tn !== $.PARAM &&
  72. tn !== $.SOURCE &&
  73. tn !== $.TRACK &&
  74. tn !== $.WBR
  75. ) {
  76. const childNodesHolder =
  77. tn === $.TEMPLATE && ns === NS.HTML ? this.treeAdapter.getTemplateContent(node) : node;
  78. this._serializeChildNodes(childNodesHolder);
  79. this.html += '</' + tn + '>';
  80. }
  81. }
  82. _serializeAttributes(node) {
  83. const attrs = this.treeAdapter.getAttrList(node);
  84. for (let i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
  85. const attr = attrs[i];
  86. const value = Serializer.escapeString(attr.value, true);
  87. this.html += ' ';
  88. if (!attr.namespace) {
  89. this.html += attr.name;
  90. } else if (attr.namespace === NS.XML) {
  91. this.html += 'xml:' + attr.name;
  92. } else if (attr.namespace === NS.XMLNS) {
  93. if (attr.name !== 'xmlns') {
  94. this.html += 'xmlns:';
  95. }
  96. this.html += attr.name;
  97. } else if (attr.namespace === NS.XLINK) {
  98. this.html += 'xlink:' + attr.name;
  99. } else {
  100. this.html += attr.prefix + ':' + attr.name;
  101. }
  102. this.html += '="' + value + '"';
  103. }
  104. }
  105. _serializeTextNode(node) {
  106. const content = this.treeAdapter.getTextNodeContent(node);
  107. const parent = this.treeAdapter.getParentNode(node);
  108. let parentTn = void 0;
  109. if (parent && this.treeAdapter.isElementNode(parent)) {
  110. parentTn = this.treeAdapter.getTagName(parent);
  111. }
  112. if (
  113. parentTn === $.STYLE ||
  114. parentTn === $.SCRIPT ||
  115. parentTn === $.XMP ||
  116. parentTn === $.IFRAME ||
  117. parentTn === $.NOEMBED ||
  118. parentTn === $.NOFRAMES ||
  119. parentTn === $.PLAINTEXT ||
  120. parentTn === $.NOSCRIPT
  121. ) {
  122. this.html += content;
  123. } else {
  124. this.html += Serializer.escapeString(content, false);
  125. }
  126. }
  127. _serializeCommentNode(node) {
  128. this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
  129. }
  130. _serializeDocumentTypeNode(node) {
  131. const name = this.treeAdapter.getDocumentTypeNodeName(node);
  132. this.html += '<' + doctype.serializeContent(name, null, null) + '>';
  133. }
  134. }
  135. // NOTE: used in tests and by rewriting stream
  136. Serializer.escapeString = function(str, attrMode) {
  137. str = str.replace(AMP_REGEX, '&amp;').replace(NBSP_REGEX, '&nbsp;');
  138. if (attrMode) {
  139. str = str.replace(DOUBLE_QUOTE_REGEX, '&quot;');
  140. } else {
  141. str = str.replace(LT_REGEX, '&lt;').replace(GT_REGEX, '&gt;');
  142. }
  143. return str;
  144. };
  145. module.exports = Serializer;