forked from capricorn86/happy-dom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeUtility.ts
276 lines (238 loc) · 6.47 KB
/
NodeUtility.ts
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import IText from '../text/IText';
import IComment from '../comment/IComment';
import INode from './INode';
import NodeTypeEnum from './NodeTypeEnum';
import IElement from '../element/IElement';
import IDocumentType from '../document-type/IDocumentType';
import IAttr from '../attr/IAttr';
import IProcessingInstruction from '../processing-instruction/IProcessingInstruction';
/**
* Node utility.
*/
export default class NodeUtility {
/**
* Returns boolean indicating if nodeB is an inclusive ancestor of nodeA.
*
* Based on:
* https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/helpers/node.js
*
* @see https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
* @param ancestorNode Ancestor node.
* @param referenceNode Reference node.
* @returns "true" if following.
*/
public static isInclusiveAncestor(ancestorNode: INode, referenceNode: INode): boolean {
let parent: INode = referenceNode;
while (parent) {
if (ancestorNode === parent) {
return true;
}
parent = parent.parentNode;
}
return false;
}
/**
* Returns boolean indicating if nodeB is following nodeA in the document tree.
*
* Based on:
* https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/helpers/node.js
*
* @see https://dom.spec.whatwg.org/#concept-tree-following
* @param nodeA Node A.
* @param nodeB Node B.
* @returns "true" if following.
*/
public static isFollowing(nodeA: INode, nodeB: INode): boolean {
if (nodeA === nodeB) {
return false;
}
let current: INode = nodeB;
while (current) {
current = this.following(current);
if (current === nodeA) {
return true;
}
}
return false;
}
/**
* Node length.
*
* Based on:
* https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/helpers/node.js
*
* @see https://dom.spec.whatwg.org/#concept-node-length
* @param node Node.
* @returns Node length.
*/
public static getNodeLength(node: INode): number {
switch (node.nodeType) {
case NodeTypeEnum.documentTypeNode:
return 0;
case NodeTypeEnum.textNode:
case NodeTypeEnum.processingInstructionNode:
case NodeTypeEnum.commentNode:
return (<IText | IComment>node).data.length;
default:
return node.childNodes.length;
}
}
/**
* Returns boolean indicating if nodeB is following nodeA in the document tree.
*
* Based on:
* https://github.com/jsdom/js-symbol-tree/blob/master/lib/SymbolTree.js#L220
*
* @param node Node.
* @param [root] Root.
* @returns Following node.
*/
public static following(node: INode, root?: INode): INode {
const firstChild = node.firstChild;
if (firstChild) {
return firstChild;
}
let current = node;
while (current) {
if (current === root) {
return null;
}
const nextSibling = current.nextSibling;
if (nextSibling) {
return nextSibling;
}
current = current.parentNode;
}
return null;
}
/**
* Returns the next sibling or parents sibling.
*
* @param node Node.
* @returns Next decentant node.
*/
public static nextDecendantNode(node: INode): INode {
while (node && !node.nextSibling) {
node = node.parentNode;
}
if (!node) {
return null;
}
return node.nextSibling;
}
/**
* Needed by https://dom.spec.whatwg.org/#concept-node-equals
*
* @param elementA
* @param elementB
*/
public static attributeListsEqual(elementA: IElement, elementB: IElement): boolean {
const listA = <Array<IAttr>>Object.values(elementA['_attributes']);
const listB = <Array<IAttr>>Object.values(elementB['_attributes']);
const lengthA = listA.length;
const lengthB = listB.length;
if (lengthA !== lengthB) {
return false;
}
for (let i = 0; i < lengthA; ++i) {
const attrA = listA[i];
if (
!listB.some((attrB) => {
return (
(typeof attrA === 'number' && typeof attrB === 'number' && attrA === attrB) ||
(typeof attrA === 'object' &&
typeof attrB === 'object' &&
NodeUtility.nodeEquals(attrA, attrB))
);
})
) {
return false;
}
}
return true;
}
/**
* Check if node nodeA equals node nodeB.
* Reference: https://dom.spec.whatwg.org/#concept-node-equals
*
* @param nodeA Node A.
* @param nodeB Node B.
*/
public static nodeEquals(nodeA: INode, nodeB: INode): boolean {
if (nodeA.nodeType !== nodeB.nodeType) {
return false;
}
switch (nodeA.nodeType) {
case NodeTypeEnum.documentTypeNode:
const documentTypeA = <IDocumentType>nodeA;
const documentTypeB = <IDocumentType>nodeB;
if (
documentTypeA.name !== documentTypeB.name ||
documentTypeA.publicId !== documentTypeB.publicId ||
documentTypeA.systemId !== documentTypeB.systemId
) {
return false;
}
break;
case NodeTypeEnum.elementNode:
const elementA = <IElement>nodeA;
const elementB = <IElement>nodeB;
if (
elementA.namespaceURI !== elementB.namespaceURI ||
elementA.prefix !== elementB.prefix ||
elementA.localName !== elementB.localName ||
elementA.attributes.length !== elementB.attributes.length
) {
return false;
}
break;
case NodeTypeEnum.attributeNode:
const attributeA = <IAttr>nodeA;
const attributeB = <IAttr>nodeB;
if (
attributeA.namespaceURI !== attributeB.namespaceURI ||
attributeA.localName !== attributeB.localName ||
attributeA.value !== attributeB.value
) {
return false;
}
break;
case NodeTypeEnum.processingInstructionNode:
const processingInstructionA = <IProcessingInstruction>nodeA;
const processingInstructionB = <IProcessingInstruction>nodeB;
if (
processingInstructionA.target !== processingInstructionB.target ||
processingInstructionA.data !== processingInstructionB.data
) {
return false;
}
break;
case NodeTypeEnum.textNode:
case NodeTypeEnum.commentNode:
type TextOrComment = IText | IComment;
const textOrCommentA = <TextOrComment>nodeA;
const textOrCommentB = <TextOrComment>nodeB;
if (textOrCommentA.data !== textOrCommentB.data) {
return false;
}
break;
}
if (
nodeA.nodeType === NodeTypeEnum.elementNode &&
!NodeUtility.attributeListsEqual(<IElement>nodeA, <IElement>nodeB)
) {
return false;
}
if (nodeA.childNodes.length !== nodeB.childNodes.length) {
return false;
}
for (let i = 0; i < nodeA.childNodes.length; i++) {
const childNodeA = nodeA.childNodes[i];
const childNodeB = nodeB.childNodes[i];
if (!NodeUtility.nodeEquals(childNodeA, childNodeB)) {
return false;
}
}
return true;
}
}