index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.adapter = void 0;
  4. exports.serializeDoctypeContent = serializeDoctypeContent;
  5. const parse5_1 = require("parse5");
  6. const domhandler_1 = require("domhandler");
  7. function enquoteDoctypeId(id) {
  8. const quote = id.includes('"') ? "'" : '"';
  9. return quote + id + quote;
  10. }
  11. /** @internal */
  12. function serializeDoctypeContent(name, publicId, systemId) {
  13. let str = '!DOCTYPE ';
  14. if (name) {
  15. str += name;
  16. }
  17. if (publicId) {
  18. str += ` PUBLIC ${enquoteDoctypeId(publicId)}`;
  19. }
  20. else if (systemId) {
  21. str += ' SYSTEM';
  22. }
  23. if (systemId) {
  24. str += ` ${enquoteDoctypeId(systemId)}`;
  25. }
  26. return str;
  27. }
  28. exports.adapter = {
  29. // Re-exports from domhandler
  30. isCommentNode: domhandler_1.isComment,
  31. isElementNode: domhandler_1.isTag,
  32. isTextNode: domhandler_1.isText,
  33. //Node construction
  34. createDocument() {
  35. const node = new domhandler_1.Document([]);
  36. node['x-mode'] = parse5_1.html.DOCUMENT_MODE.NO_QUIRKS;
  37. return node;
  38. },
  39. createDocumentFragment() {
  40. return new domhandler_1.Document([]);
  41. },
  42. createElement(tagName, namespaceURI, attrs) {
  43. const attribs = Object.create(null);
  44. const attribsNamespace = Object.create(null);
  45. const attribsPrefix = Object.create(null);
  46. for (let i = 0; i < attrs.length; i++) {
  47. const attrName = attrs[i].name;
  48. attribs[attrName] = attrs[i].value;
  49. attribsNamespace[attrName] = attrs[i].namespace;
  50. attribsPrefix[attrName] = attrs[i].prefix;
  51. }
  52. const node = new domhandler_1.Element(tagName, attribs, []);
  53. node.namespace = namespaceURI;
  54. node['x-attribsNamespace'] = attribsNamespace;
  55. node['x-attribsPrefix'] = attribsPrefix;
  56. return node;
  57. },
  58. createCommentNode(data) {
  59. return new domhandler_1.Comment(data);
  60. },
  61. createTextNode(value) {
  62. return new domhandler_1.Text(value);
  63. },
  64. //Tree mutation
  65. appendChild(parentNode, newNode) {
  66. const prev = parentNode.children[parentNode.children.length - 1];
  67. if (prev) {
  68. prev.next = newNode;
  69. newNode.prev = prev;
  70. }
  71. parentNode.children.push(newNode);
  72. newNode.parent = parentNode;
  73. },
  74. insertBefore(parentNode, newNode, referenceNode) {
  75. const insertionIdx = parentNode.children.indexOf(referenceNode);
  76. const { prev } = referenceNode;
  77. if (prev) {
  78. prev.next = newNode;
  79. newNode.prev = prev;
  80. }
  81. referenceNode.prev = newNode;
  82. newNode.next = referenceNode;
  83. parentNode.children.splice(insertionIdx, 0, newNode);
  84. newNode.parent = parentNode;
  85. },
  86. setTemplateContent(templateElement, contentElement) {
  87. exports.adapter.appendChild(templateElement, contentElement);
  88. },
  89. getTemplateContent(templateElement) {
  90. return templateElement.children[0];
  91. },
  92. setDocumentType(document, name, publicId, systemId) {
  93. const data = serializeDoctypeContent(name, publicId, systemId);
  94. let doctypeNode = document.children.find((node) => (0, domhandler_1.isDirective)(node) && node.name === '!doctype');
  95. if (doctypeNode) {
  96. doctypeNode.data = data !== null && data !== void 0 ? data : null;
  97. }
  98. else {
  99. doctypeNode = new domhandler_1.ProcessingInstruction('!doctype', data);
  100. exports.adapter.appendChild(document, doctypeNode);
  101. }
  102. doctypeNode['x-name'] = name;
  103. doctypeNode['x-publicId'] = publicId;
  104. doctypeNode['x-systemId'] = systemId;
  105. },
  106. setDocumentMode(document, mode) {
  107. document['x-mode'] = mode;
  108. },
  109. getDocumentMode(document) {
  110. return document['x-mode'];
  111. },
  112. detachNode(node) {
  113. if (node.parent) {
  114. const idx = node.parent.children.indexOf(node);
  115. const { prev, next } = node;
  116. node.prev = null;
  117. node.next = null;
  118. if (prev) {
  119. prev.next = next;
  120. }
  121. if (next) {
  122. next.prev = prev;
  123. }
  124. node.parent.children.splice(idx, 1);
  125. node.parent = null;
  126. }
  127. },
  128. insertText(parentNode, text) {
  129. const lastChild = parentNode.children[parentNode.children.length - 1];
  130. if (lastChild && (0, domhandler_1.isText)(lastChild)) {
  131. lastChild.data += text;
  132. }
  133. else {
  134. exports.adapter.appendChild(parentNode, exports.adapter.createTextNode(text));
  135. }
  136. },
  137. insertTextBefore(parentNode, text, referenceNode) {
  138. const prevNode = parentNode.children[parentNode.children.indexOf(referenceNode) - 1];
  139. if (prevNode && (0, domhandler_1.isText)(prevNode)) {
  140. prevNode.data += text;
  141. }
  142. else {
  143. exports.adapter.insertBefore(parentNode, exports.adapter.createTextNode(text), referenceNode);
  144. }
  145. },
  146. adoptAttributes(recipient, attrs) {
  147. for (let i = 0; i < attrs.length; i++) {
  148. const attrName = attrs[i].name;
  149. if (recipient.attribs[attrName] === undefined) {
  150. recipient.attribs[attrName] = attrs[i].value;
  151. recipient['x-attribsNamespace'][attrName] = attrs[i].namespace;
  152. recipient['x-attribsPrefix'][attrName] = attrs[i].prefix;
  153. }
  154. }
  155. },
  156. //Tree traversing
  157. getFirstChild(node) {
  158. return node.children[0];
  159. },
  160. getChildNodes(node) {
  161. return node.children;
  162. },
  163. getParentNode(node) {
  164. return node.parent;
  165. },
  166. getAttrList(element) {
  167. return element.attributes;
  168. },
  169. //Node data
  170. getTagName(element) {
  171. return element.name;
  172. },
  173. getNamespaceURI(element) {
  174. return element.namespace;
  175. },
  176. getTextNodeContent(textNode) {
  177. return textNode.data;
  178. },
  179. getCommentNodeContent(commentNode) {
  180. return commentNode.data;
  181. },
  182. getDocumentTypeNodeName(doctypeNode) {
  183. var _a;
  184. return (_a = doctypeNode['x-name']) !== null && _a !== void 0 ? _a : '';
  185. },
  186. getDocumentTypeNodePublicId(doctypeNode) {
  187. var _a;
  188. return (_a = doctypeNode['x-publicId']) !== null && _a !== void 0 ? _a : '';
  189. },
  190. getDocumentTypeNodeSystemId(doctypeNode) {
  191. var _a;
  192. return (_a = doctypeNode['x-systemId']) !== null && _a !== void 0 ? _a : '';
  193. },
  194. //Node types
  195. isDocumentTypeNode(node) {
  196. return (0, domhandler_1.isDirective)(node) && node.name === '!doctype';
  197. },
  198. // Source code location
  199. setNodeSourceCodeLocation(node, location) {
  200. if (location) {
  201. node.startIndex = location.startOffset;
  202. node.endIndex = location.endOffset;
  203. }
  204. node.sourceCodeLocation = location;
  205. },
  206. getNodeSourceCodeLocation(node) {
  207. return node.sourceCodeLocation;
  208. },
  209. updateNodeSourceCodeLocation(node, endLocation) {
  210. if (endLocation.endOffset != null)
  211. node.endIndex = endLocation.endOffset;
  212. node.sourceCodeLocation = Object.assign(Object.assign({}, node.sourceCodeLocation), endLocation);
  213. },
  214. };