-
Notifications
You must be signed in to change notification settings - Fork 43
/
node.ts
562 lines (467 loc) · 15.2 KB
/
node.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
import { CTOR_KEY } from "../constructor-lock.ts";
import { NodeList, NodeListMutator, nodeListMutatorSym } from "./node-list.ts";
import { HTMLCollection, HTMLCollectionMutator, HTMLCollectionMutatorSym } from "./html-collection.ts";
import type { Element } from "./element.ts";
import type { Document } from "./document.ts";
export class EventTarget {
addEventListener() {
// TODO
}
removeEventListener() {
// TODO
}
dispatchEvent() {
// TODO
}
}
export enum NodeType {
ELEMENT_NODE = 1,
ATTRIBUTE_NODE = 2,
TEXT_NODE = 3,
CDATA_SECTION_NODE = 4,
ENTITY_REFERENCE_NODE = 5,
ENTITY_NODE = 6,
PROCESSING_INSTRUCTION_NODE = 7,
COMMENT_NODE = 8,
DOCUMENT_NODE = 9,
DOCUMENT_TYPE_NODE = 10,
DOCUMENT_FRAGMENT_NODE = 11,
NOTATION_NODE = 12,
}
const nodesAndTextNodes = (nodes: (Node | any)[], parentNode: Node) => {
return nodes.map(n => {
let node = n;
if (!(n instanceof Node)) {
node = new Text("" + n);
}
node._setParent(parentNode, true);
return node;
});
}
export class Node extends EventTarget {
public nodeValue: string | null;
public childNodes: NodeList;
public parentNode: Node | null = null;
public parentElement: Element | null;
#childNodesMutator: NodeListMutator;
#ownerDocument: Document | null = null;
private _ancestors = new Set<Node>();
// Instance constants defined after Node
// class body below to avoid clutter
static ELEMENT_NODE = NodeType.ELEMENT_NODE;
static ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE;
static TEXT_NODE = NodeType.TEXT_NODE;
static CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE;
static ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE;
static ENTITY_NODE = NodeType.ENTITY_NODE;
static PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE;
static COMMENT_NODE = NodeType.COMMENT_NODE;
static DOCUMENT_NODE = NodeType.DOCUMENT_NODE;
static DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE;
static DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE;
static NOTATION_NODE = NodeType.NOTATION_NODE;
constructor(
public nodeName: string,
public nodeType: NodeType,
parentNode: Node | null,
key: typeof CTOR_KEY,
) {
if (key !== CTOR_KEY) {
throw new TypeError("Illegal constructor.");
}
super();
this.nodeValue = null;
this.childNodes = new NodeList();
this.#childNodesMutator = this.childNodes[nodeListMutatorSym]();
this.parentElement = <Element> parentNode;
if (parentNode) {
parentNode.appendChild(this);
}
}
_getChildNodesMutator(): NodeListMutator {
return this.#childNodesMutator;
}
_setParent(newParent: Node | null, force = false) {
const sameParent = this.parentNode === newParent;
const shouldUpdateParentAndAncestors = !sameParent || force;
if (shouldUpdateParentAndAncestors) {
this.parentNode = newParent;
if (newParent) {
if (!sameParent) {
// If this a document node or another non-element node
// then parentElement should be set to null
if (newParent.nodeType === NodeType.ELEMENT_NODE) {
this.parentElement = newParent as unknown as Element;
} else {
this.parentElement = null;
}
this._setOwnerDocument(newParent.#ownerDocument);
}
// Add parent chain to ancestors
this._ancestors = new Set(newParent._ancestors);
this._ancestors.add(newParent);
} else {
this.parentElement = null;
this._ancestors.clear();
}
// Update ancestors for child nodes
for (const child of this.childNodes) {
child._setParent(this, shouldUpdateParentAndAncestors);
}
}
}
_assertNotAncestor(child: Node) {
// Check this child isn't an ancestor
if (child.contains(this)) {
throw new DOMException("The new child is an ancestor of the parent");
}
}
_setOwnerDocument(document: Document | null) {
if (this.#ownerDocument !== document) {
this.#ownerDocument = document;
for (const child of this.childNodes) {
child._setOwnerDocument(document);
}
}
}
contains(child: Node) {
return child._ancestors.has(this) || child === this;
}
get ownerDocument() {
return this.#ownerDocument;
}
get textContent(): string {
let out = "";
for (const child of this.childNodes) {
switch (child.nodeType) {
case NodeType.TEXT_NODE:
out += child.nodeValue;
break;
case NodeType.ELEMENT_NODE:
out += child.textContent;
break;
}
}
return out;
}
set textContent(content: string) {
for (const child of this.childNodes) {
child._setParent(null);
}
this._getChildNodesMutator().splice(0, this.childNodes.length);
this.appendChild(new Text(content));
}
get firstChild() {
return this.childNodes[0] || null;
}
get lastChild() {
return this.childNodes[this.childNodes.length - 1] || null;
}
hasChildNodes() {
return this.firstChild !== null;
}
cloneNode(deep: boolean = false): this {
const copy = this._shallowClone();
copy._setOwnerDocument(this.ownerDocument);
if (deep) {
for (const child of this.childNodes) {
copy.appendChild(child.cloneNode(true));
}
}
return copy as this;
}
_shallowClone(): Node {
throw new Error("Illegal invocation");
}
remove() {
const parent = this.parentNode;
if (parent) {
const nodeList = parent._getChildNodesMutator();
const idx = nodeList.indexOf(this);
nodeList.splice(idx, 1);
this._setParent(null);
}
}
appendChild(child: Node): Node {
return child._appendTo(this);
}
_appendTo(parentNode: Node) {
parentNode._assertNotAncestor(this); // FIXME: Should this really be a method?
const oldParentNode = this.parentNode;
// Check if we already own this child
if (oldParentNode === parentNode) {
if (parentNode._getChildNodesMutator().indexOf(this) !== -1) {
return this;
}
} else if (oldParentNode) {
this.remove();
}
this._setParent(parentNode, true);
parentNode._getChildNodesMutator().push(this);
return this;
}
removeChild(child: Node) {
// Just copy Firefox's error messages
if (child && typeof child === "object") {
if (child.parentNode === this) {
return child.remove();
} else {
throw new DOMException("Node.removeChild: The node to be removed is not a child of this node");
}
} else {
throw new TypeError("Node.removeChild: Argument 1 is not an object.");
}
}
replaceChild(newChild: Node, oldChild: Node): Node {
if (oldChild.parentNode !== this) {
throw new Error("Old child's parent is not the current node.");
}
oldChild.replaceWith(newChild);
return oldChild;
}
private insertBeforeAfter(nodes: (Node | string)[], side: number) {
const parentNode = this.parentNode!;
const mutator = parentNode._getChildNodesMutator();
const index = mutator.indexOf(this);
nodes = nodesAndTextNodes(nodes, parentNode);
mutator.splice(index + side, 0, ...(<Node[]> nodes));
}
before(...nodes: (Node | string)[]) {
if (this.parentNode) {
this.insertBeforeAfter(nodes, 0);
}
}
after(...nodes: (Node | string)[]) {
if (this.parentNode) {
this.insertBeforeAfter(nodes, 1);
}
}
insertBefore(newNode: Node, refNode: Node | null): Node {
this._assertNotAncestor(newNode);
const mutator = this._getChildNodesMutator();
if (refNode === null) {
this.appendChild(newNode);
return newNode;
}
const index = mutator.indexOf(refNode);
if (index === -1) {
throw new Error("DOMException: Child to insert before is not a child of this node");
}
const oldParentNode = newNode.parentNode;
newNode._setParent(this, oldParentNode !== this);
mutator.splice(index, 0, newNode);
return newNode;
}
replaceWith(...nodes: (Node | string)[]) {
if (this.parentNode) {
const parentNode = this.parentNode;
const mutator = parentNode._getChildNodesMutator();
const index = mutator.indexOf(this);
nodes = nodesAndTextNodes(nodes, parentNode);
mutator.splice(index, 1, ...(<Node[]> nodes));
this._setParent(null);
}
}
get children(): HTMLCollection {
const collection = new HTMLCollection();
const mutator = collection[HTMLCollectionMutatorSym]();
for (const child of this.childNodes) {
if (child.nodeType === NodeType.ELEMENT_NODE) {
mutator.push(<Element> child);
}
}
return collection;
}
get nextSibling(): Node | null {
const parent = this.parentNode;
if (!parent) {
return null;
}
const index = parent._getChildNodesMutator().indexOf(this);
let next: Node | null = this.childNodes[index + 1] || null;
return next;
}
get previousSibling(): Node | null {
const parent = this.parentNode;
if (!parent) {
return null;
}
const index = parent._getChildNodesMutator().indexOf(this);
let prev: Node | null = this.childNodes[index - 1] || null;
return prev;
}
// Node.compareDocumentPosition()'s bitmask values
public static DOCUMENT_POSITION_DISCONNECTED = 1 as const;
public static DOCUMENT_POSITION_PRECEDING = 2 as const;
public static DOCUMENT_POSITION_FOLLOWING = 4 as const;
public static DOCUMENT_POSITION_CONTAINS = 8 as const;
public static DOCUMENT_POSITION_CONTAINED_BY = 16 as const;
public static DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32 as const;
/**
* FIXME: Does not implement attribute node checks
* ref: https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
* MDN: https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
*/
compareDocumentPosition(other: Node) {
if (other === this) {
return 0;
}
// Note: major browser implementations differ in their rejection error of
// non-Node or nullish values so we just copy the most relevant error message
// from Firefox
if (!(other instanceof Node)) {
throw new TypeError("Node.compareDocumentPosition: Argument 1 does not implement interface Node.");
}
let node1Root = other;
let node2Root = this as Node;
const node1Hierarchy = [node1Root];
const node2Hierarchy = [node2Root];
while (node1Root.parentNode ?? node2Root.parentNode) {
node1Root = node1Root.parentNode ? (node1Hierarchy.push(node1Root.parentNode), node1Root.parentNode) : node1Root;
node2Root = node2Root.parentNode ? (node2Hierarchy.push(node2Root.parentNode), node2Root.parentNode) : node2Root;
}
// Check if they don't share the same root node
if (node1Root !== node2Root) {
return Node.DOCUMENT_POSITION_DISCONNECTED
| Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
| Node.DOCUMENT_POSITION_PRECEDING;
}
const longerHierarchy = node1Hierarchy.length > node2Hierarchy.length
? node1Hierarchy
: node2Hierarchy;
const shorterHierarchy = longerHierarchy === node1Hierarchy
? node2Hierarchy
: node1Hierarchy;
// Check if either is a container of the other
if (longerHierarchy[longerHierarchy.length - shorterHierarchy.length] === shorterHierarchy[0]) {
return longerHierarchy === node1Hierarchy
// other is a child of this
? Node.DOCUMENT_POSITION_CONTAINED_BY | Node.DOCUMENT_POSITION_FOLLOWING
// this is a child of other
: Node.DOCUMENT_POSITION_CONTAINS | Node.DOCUMENT_POSITION_PRECEDING;
}
// Find their first common ancestor and see whether they
// are preceding or following
const longerStart = longerHierarchy.length - shorterHierarchy.length;
for (let i = shorterHierarchy.length - 1; i >= 0; i--) {
const shorterHierarchyNode = shorterHierarchy[i];
const longerHierarchyNode = longerHierarchy[longerStart + i];
// We found the first common ancestor
if (longerHierarchyNode !== shorterHierarchyNode) {
const siblings = shorterHierarchyNode.parentNode!._getChildNodesMutator();
if (siblings.indexOf(shorterHierarchyNode) < siblings.indexOf(longerHierarchyNode)) {
// Shorter is before longer
if (shorterHierarchy === node1Hierarchy) {
// Other is before this
return Node.DOCUMENT_POSITION_PRECEDING;
} else {
// This is before other
return Node.DOCUMENT_POSITION_FOLLOWING;
}
} else {
// Longer is before shorter
if (longerHierarchy === node1Hierarchy) {
// Other is before this
return Node.DOCUMENT_POSITION_PRECEDING;
} else {
// Other is after this
return Node.DOCUMENT_POSITION_FOLLOWING;
}
}
}
}
// FIXME: Should probably throw here because this
// point should be unreachable code as per the
// intended logic
return Node.DOCUMENT_POSITION_FOLLOWING;
}
}
// Node instance `nodeType` enum constants
export interface Node {
ELEMENT_NODE: NodeType;
ATTRIBUTE_NODE: NodeType;
TEXT_NODE: NodeType;
CDATA_SECTION_NODE: NodeType;
ENTITY_REFERENCE_NODE: NodeType;
ENTITY_NODE: NodeType;
PROCESSING_INSTRUCTION_NODE: NodeType;
COMMENT_NODE: NodeType;
DOCUMENT_NODE: NodeType;
DOCUMENT_TYPE_NODE: NodeType;
DOCUMENT_FRAGMENT_NODE: NodeType;
NOTATION_NODE: NodeType;
}
Node.prototype.ELEMENT_NODE = NodeType.ELEMENT_NODE;
Node.prototype.ATTRIBUTE_NODE = NodeType.ATTRIBUTE_NODE;
Node.prototype.TEXT_NODE = NodeType.TEXT_NODE;
Node.prototype.CDATA_SECTION_NODE = NodeType.CDATA_SECTION_NODE;
Node.prototype.ENTITY_REFERENCE_NODE = NodeType.ENTITY_REFERENCE_NODE;
Node.prototype.ENTITY_NODE = NodeType.ENTITY_NODE;
Node.prototype.PROCESSING_INSTRUCTION_NODE = NodeType.PROCESSING_INSTRUCTION_NODE;
Node.prototype.COMMENT_NODE = NodeType.COMMENT_NODE;
Node.prototype.DOCUMENT_NODE = NodeType.DOCUMENT_NODE;
Node.prototype.DOCUMENT_TYPE_NODE = NodeType.DOCUMENT_TYPE_NODE;
Node.prototype.DOCUMENT_FRAGMENT_NODE = NodeType.DOCUMENT_FRAGMENT_NODE;
Node.prototype.NOTATION_NODE = NodeType.NOTATION_NODE;
export class CharacterData extends Node {
constructor(
public data: string,
nodeName: string,
nodeType: NodeType,
parentNode: Node | null,
key: typeof CTOR_KEY,
) {
super(
nodeName,
nodeType,
parentNode,
key,
);
this.nodeValue = data;
}
get length(): number {
return this.data.length;
}
// TODO: Implement NonDocumentTypeChildNode.nextElementSibling, etc
// ref: https://developer.mozilla.org/en-US/docs/Web/API/CharacterData
}
export class Text extends CharacterData {
constructor(
text: string = "",
) {
super(
text,
"#text",
NodeType.TEXT_NODE,
null,
CTOR_KEY,
);
this.nodeValue = text;
}
_shallowClone(): Node {
return new Text(this.textContent);
}
get textContent(): string {
return <string> this.nodeValue;
}
}
export class Comment extends CharacterData {
constructor(
text: string = "",
) {
super(
text,
"#comment",
NodeType.COMMENT_NODE,
null,
CTOR_KEY,
);
this.nodeValue = text;
}
_shallowClone(): Node {
return new Comment(this.textContent);
}
get textContent(): string {
return <string> this.nodeValue;
}
}