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