Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

179 řádky
5.5 KiB

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