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