index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. var defaultTreeAdapter = require('../tree_adapters/default'),
  3. mergeOptions = require('../utils/merge_options'),
  4. doctype = require('../common/doctype'),
  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. treeAdapter: defaultTreeAdapter
  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. //Serializer
  20. var Serializer = module.exports = function (node, options) {
  21. this.options = mergeOptions(DEFAULT_OPTIONS, options);
  22. this.treeAdapter = this.options.treeAdapter;
  23. this.html = '';
  24. this.startNode = node;
  25. };
  26. // NOTE: exported as static method for the testing purposes
  27. Serializer.escapeString = function (str, attrMode) {
  28. str = str
  29. .replace(AMP_REGEX, '&amp;')
  30. .replace(NBSP_REGEX, '&nbsp;');
  31. if (attrMode)
  32. str = str.replace(DOUBLE_QUOTE_REGEX, '&quot;');
  33. else {
  34. str = str
  35. .replace(LT_REGEX, '&lt;')
  36. .replace(GT_REGEX, '&gt;');
  37. }
  38. return str;
  39. };
  40. //API
  41. Serializer.prototype.serialize = function () {
  42. this._serializeChildNodes(this.startNode);
  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. var childNodesHolder = tn === $.TEMPLATE && ns === NS.HTML ?
  73. this.treeAdapter.getTemplateContent(node) :
  74. node;
  75. this._serializeChildNodes(childNodesHolder);
  76. this.html += '</' + tn + '>';
  77. }
  78. };
  79. Serializer.prototype._serializeAttributes = function (node) {
  80. var attrs = this.treeAdapter.getAttrList(node);
  81. for (var i = 0, attrsLength = attrs.length; i < attrsLength; i++) {
  82. var attr = attrs[i],
  83. value = Serializer.escapeString(attr.value, true);
  84. this.html += ' ';
  85. if (!attr.namespace)
  86. this.html += attr.name;
  87. else if (attr.namespace === NS.XML)
  88. this.html += 'xml:' + attr.name;
  89. else if (attr.namespace === NS.XMLNS) {
  90. if (attr.name !== 'xmlns')
  91. this.html += 'xmlns:';
  92. this.html += attr.name;
  93. }
  94. else if (attr.namespace === NS.XLINK)
  95. this.html += 'xlink:' + attr.name;
  96. else
  97. this.html += attr.namespace + ':' + attr.name;
  98. this.html += '="' + value + '"';
  99. }
  100. };
  101. Serializer.prototype._serializeTextNode = function (node) {
  102. var content = this.treeAdapter.getTextNodeContent(node),
  103. parent = this.treeAdapter.getParentNode(node),
  104. parentTn = void 0;
  105. if (parent && this.treeAdapter.isElementNode(parent))
  106. parentTn = this.treeAdapter.getTagName(parent);
  107. if (parentTn === $.STYLE || parentTn === $.SCRIPT || parentTn === $.XMP || parentTn === $.IFRAME ||
  108. parentTn === $.NOEMBED || parentTn === $.NOFRAMES || parentTn === $.PLAINTEXT || parentTn === $.NOSCRIPT)
  109. this.html += content;
  110. else
  111. this.html += Serializer.escapeString(content, false);
  112. };
  113. Serializer.prototype._serializeCommentNode = function (node) {
  114. this.html += '<!--' + this.treeAdapter.getCommentNodeContent(node) + '-->';
  115. };
  116. Serializer.prototype._serializeDocumentTypeNode = function (node) {
  117. var name = this.treeAdapter.getDocumentTypeNodeName(node);
  118. this.html += '<' + doctype.serializeContent(name, null, null) + '>';
  119. };