default.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.defaultTreeAdapter = void 0;
  4. const html_js_1 = require("../common/html.js");
  5. exports.defaultTreeAdapter = {
  6. //Node construction
  7. createDocument() {
  8. return {
  9. nodeName: '#document',
  10. mode: html_js_1.DOCUMENT_MODE.NO_QUIRKS,
  11. childNodes: [],
  12. };
  13. },
  14. createDocumentFragment() {
  15. return {
  16. nodeName: '#document-fragment',
  17. childNodes: [],
  18. };
  19. },
  20. createElement(tagName, namespaceURI, attrs) {
  21. return {
  22. nodeName: tagName,
  23. tagName,
  24. attrs,
  25. namespaceURI,
  26. childNodes: [],
  27. parentNode: null,
  28. };
  29. },
  30. createCommentNode(data) {
  31. return {
  32. nodeName: '#comment',
  33. data,
  34. parentNode: null,
  35. };
  36. },
  37. createTextNode(value) {
  38. return {
  39. nodeName: '#text',
  40. value,
  41. parentNode: null,
  42. };
  43. },
  44. //Tree mutation
  45. appendChild(parentNode, newNode) {
  46. parentNode.childNodes.push(newNode);
  47. newNode.parentNode = parentNode;
  48. },
  49. insertBefore(parentNode, newNode, referenceNode) {
  50. const insertionIdx = parentNode.childNodes.indexOf(referenceNode);
  51. parentNode.childNodes.splice(insertionIdx, 0, newNode);
  52. newNode.parentNode = parentNode;
  53. },
  54. setTemplateContent(templateElement, contentElement) {
  55. templateElement.content = contentElement;
  56. },
  57. getTemplateContent(templateElement) {
  58. return templateElement.content;
  59. },
  60. setDocumentType(document, name, publicId, systemId) {
  61. const doctypeNode = document.childNodes.find((node) => node.nodeName === '#documentType');
  62. if (doctypeNode) {
  63. doctypeNode.name = name;
  64. doctypeNode.publicId = publicId;
  65. doctypeNode.systemId = systemId;
  66. }
  67. else {
  68. const node = {
  69. nodeName: '#documentType',
  70. name,
  71. publicId,
  72. systemId,
  73. parentNode: null,
  74. };
  75. exports.defaultTreeAdapter.appendChild(document, node);
  76. }
  77. },
  78. setDocumentMode(document, mode) {
  79. document.mode = mode;
  80. },
  81. getDocumentMode(document) {
  82. return document.mode;
  83. },
  84. detachNode(node) {
  85. if (node.parentNode) {
  86. const idx = node.parentNode.childNodes.indexOf(node);
  87. node.parentNode.childNodes.splice(idx, 1);
  88. node.parentNode = null;
  89. }
  90. },
  91. insertText(parentNode, text) {
  92. if (parentNode.childNodes.length > 0) {
  93. const prevNode = parentNode.childNodes[parentNode.childNodes.length - 1];
  94. if (exports.defaultTreeAdapter.isTextNode(prevNode)) {
  95. prevNode.value += text;
  96. return;
  97. }
  98. }
  99. exports.defaultTreeAdapter.appendChild(parentNode, exports.defaultTreeAdapter.createTextNode(text));
  100. },
  101. insertTextBefore(parentNode, text, referenceNode) {
  102. const prevNode = parentNode.childNodes[parentNode.childNodes.indexOf(referenceNode) - 1];
  103. if (prevNode && exports.defaultTreeAdapter.isTextNode(prevNode)) {
  104. prevNode.value += text;
  105. }
  106. else {
  107. exports.defaultTreeAdapter.insertBefore(parentNode, exports.defaultTreeAdapter.createTextNode(text), referenceNode);
  108. }
  109. },
  110. adoptAttributes(recipient, attrs) {
  111. const recipientAttrsMap = new Set(recipient.attrs.map((attr) => attr.name));
  112. for (let j = 0; j < attrs.length; j++) {
  113. if (!recipientAttrsMap.has(attrs[j].name)) {
  114. recipient.attrs.push(attrs[j]);
  115. }
  116. }
  117. },
  118. //Tree traversing
  119. getFirstChild(node) {
  120. return node.childNodes[0];
  121. },
  122. getChildNodes(node) {
  123. return node.childNodes;
  124. },
  125. getParentNode(node) {
  126. return node.parentNode;
  127. },
  128. getAttrList(element) {
  129. return element.attrs;
  130. },
  131. //Node data
  132. getTagName(element) {
  133. return element.tagName;
  134. },
  135. getNamespaceURI(element) {
  136. return element.namespaceURI;
  137. },
  138. getTextNodeContent(textNode) {
  139. return textNode.value;
  140. },
  141. getCommentNodeContent(commentNode) {
  142. return commentNode.data;
  143. },
  144. getDocumentTypeNodeName(doctypeNode) {
  145. return doctypeNode.name;
  146. },
  147. getDocumentTypeNodePublicId(doctypeNode) {
  148. return doctypeNode.publicId;
  149. },
  150. getDocumentTypeNodeSystemId(doctypeNode) {
  151. return doctypeNode.systemId;
  152. },
  153. //Node types
  154. isTextNode(node) {
  155. return node.nodeName === '#text';
  156. },
  157. isCommentNode(node) {
  158. return node.nodeName === '#comment';
  159. },
  160. isDocumentTypeNode(node) {
  161. return node.nodeName === '#documentType';
  162. },
  163. isElementNode(node) {
  164. return Object.prototype.hasOwnProperty.call(node, 'tagName');
  165. },
  166. // Source code location
  167. setNodeSourceCodeLocation(node, location) {
  168. node.sourceCodeLocation = location;
  169. },
  170. getNodeSourceCodeLocation(node) {
  171. return node.sourceCodeLocation;
  172. },
  173. updateNodeSourceCodeLocation(node, endLocation) {
  174. node.sourceCodeLocation = Object.assign(Object.assign({}, node.sourceCodeLocation), endLocation);
  175. },
  176. };