All files / node.ts

50.00% Branches 4/8
94.94% Lines 413/435
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
562
x1
 
x3
 
 
x3
x3
x3
x256
x256
x256
x256
x256
x256
x256
 
x6
x23
x23
 
x3
x3
x13
x13
 
x3
x5
x5
 
x3
 
x256
x256
x257
x257
 
x256
x256
x257
x257
 
x256
x256
x261
x261
 
x256
x258
x258
 
x256
 
x256
x256
x260
x260
 
x256
x258
x258
 
x256
 
x256
x256
x256
x259
x259
 
x256
x258
x258
 
x256
x256
x285
x285
 
x256
x258
x258
 
x256
 
x256
x256
x265
 
 
 
 
 
 
x265
x265
 
x256
x258
x258
 
x256
x256
x264
x264
 
x256
x258
x258
 
x515
 
x3
x3
x7
x7
 
x3
x5
x5
 
x3
x3
x7
x7
 
x3
x5
x5
 
x3
x3
 
x6
 
x3
x5
x5
 
x3
x3
 
x6
 
x3
x5
x5
 
x3
x3
x11
x11
x11
x12
x12
x18
x18
 
 
 
x18
x18
 
x18
x18
x11
 
x3
 
 
 
 
 
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x5
x5
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x4
x4
 
x3
x3
x3
x4
x4
 
x3
x3
x3
x4
x4
 
x3
x3
x3
x4
x4
 
x3
x3
x6
x6
 
x3
x5
x5
 
 
 
 
 
 
 
 
x3
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
 
x3
x6
x6
 
x3
x5
x5
 
x3
x9
x9
 
x3
x5
x5
x3
 
x3
x3
x3
x3
x13
x13
 
x3
x3
x3
x4
x4
x4
x3





















































































I











































I








I















I




















































































































































































































































































































































































































// Imports
import type { Nullable } from "@libs/typing"
import { type _Node, type _NodeList, illegal, Indexable, internal, unimplemented } from "./_.ts"
import type { Document } from "./document.ts"

/** https://developer.mozilla.org/en-US/docs/Web/API/Node */
export class Node extends EventTarget implements _Node {
  constructor({ ownerDocument = null, nodeName = "", nodeValue = null, parentNode = null } = {} as { [internal]?: boolean; ownerDocument?: Nullable<Document>; parentNode?: Nullable<ParentNode>; nodeName?: string; nodeValue?: Nullable<string> }) {
    super()
    illegal(arguments)
    this.#ownerDocument = ownerDocument
    this.#parentNode = parentNode
    this.#nodeName = nodeName!.toLocaleUpperCase()
    this.#nodeValue = nodeValue
  }

  get [Symbol.toStringTag](): string {
    return this.constructor.name
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument
  get ownerDocument(): Nullable<Document> {
    return this.#ownerDocument
  }

  set ownerDocument(_: Nullable<Document>) {
    return
  }

  #ownerDocument = null as Nullable<Document>

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected
  get isConnected(): boolean {
    return unimplemented.getter<"immutable">()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/baseURI
  get baseURI(): string {
    return unimplemented.getter<"immutable">()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName
  get nodeName(): string {
    return this.#nodeName
  }

  set nodeName(_: string) {
    return
  }

  readonly #nodeName

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue
  get nodeValue(): Nullable<string> {
    return this.#nodeValue
  }

  set nodeValue(_: Nullable<string>) {
    return
  }

  readonly #nodeValue

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent
  // Note: to override in child classes
  get textContent(): Nullable<string> {
    return null
  }

  set textContent(_: Nullable<string>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode
  get parentNode(): Nullable<ParentNode> {
    return this.#parentNode
  }

  set parentNode(_: Nullable<ParentNode>) {
    return
  }

  #parentNode

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement
  get parentElement(): Nullable<HTMLElement> {
    let node = this.parentNode
    while (node) {
      if (node instanceof Element) {
        return node as HTMLElement
      }
      node = node.parentNode
    }
    return null
  }

  set parentElement(_: Nullable<HTMLElement>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes
  get childNodes(): NodeListOf<ChildNode> {
    return this.#childNodes
  }

  set childNodes(_: NodeListOf<ChildNode>) {
    return
  }

  readonly #childNodes = new NodeList<ChildNode>({ [internal]: true })

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/firstChild
  get firstChild(): Nullable<ChildNode> {
    return this.#childNodes.item(0)
  }

  set firstChild(_: Nullable<ChildNode>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild
  get lastChild(): Nullable<ChildNode> {
    return this.#childNodes.item(this.#childNodes.length - 1)
  }

  set lastChild(_: Nullable<ChildNode>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling
  get previousSibling(): Nullable<ChildNode> {
    return this.parentElement?.childNodes[(this.parentElement!.childNodes as NodeList<ChildNode>)[internal].indexOf(this as unknown as ChildNode) - 1] ?? null
  }

  set previousSibling(_: Nullable<ChildNode>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling
  get nextSibling(): Nullable<ChildNode> {
    return this.parentElement?.childNodes.item((this.parentElement!.childNodes as NodeList<ChildNode>)[internal].indexOf(this as unknown as ChildNode) + 1) ?? null
  }

  set nextSibling(_: Nullable<ChildNode>) {
    return
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild
  appendChild<T extends _Node>(child: T): T {
    let ancestor = child as unknown as Nullable<HTMLElement | Node>
    while (ancestor) {
      if (ancestor === this) {
        throw new DOMException("The new child is an ancestor of the parent.", "HierarchyRequestError")
      }
      ancestor = ancestor.parentNode as Nullable<HTMLElement | Node>
    }
    if (child.parentNode) {
      child.parentNode.removeChild(child)
    }
    ;(child as unknown as Node).#parentNode = this as unknown as ParentNode
    this.#childNodes[internal].push(child as unknown as ChildNode)

    // TODO(@lowlighter): dispatch mutation, set parent
    return child
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild
  removeChild<T extends _Node>(child: T): T {
    ;(child as unknown as Node).#parentNode = null
    this.#childNodes[internal].splice(this.#childNodes[internal].indexOf(child as unknown as ChildNode), 1)
    return child
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild
  replaceChild<T extends _Node>(_newChild: _Node, _oldChild: T): T {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore
  insertBefore<T extends _Node>(_newNode: T, _referenceNode: Nullable<_Node>): T {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/cloneNode
  cloneNode(_deep?: boolean): _Node {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes
  hasChildNodes(): boolean {
    return this.#childNodes.length > 0
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
  contains(_other: Nullable<_Node>): boolean {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode
  isEqualNode(_other: Nullable<_Node>): boolean {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/isSameNode
  isSameNode(_other: Nullable<_Node>): boolean {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/isDefaultNamespace
  isDefaultNamespace(_namespace: string): boolean {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupPrefix
  lookupPrefix(_namespace: Nullable<string>): Nullable<string> {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/lookupNamespaceURI
  lookupNamespaceURI(_prefix: Nullable<string>): Nullable<string> {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
  // Depends: none
  compareDocumentPosition(_other: _Node): number {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode
  // Depends: none
  getRootNode(_options?: GetRootNodeOptions): _Node {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize
  // Depends: TextNode implementation
  normalize(): void {
    return unimplemented()
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType
  get nodeType(): number {
    return NaN
  }

  set nodeType(_: number) {
    return
  }

  /** Internal acessor. */
  get [internal](): { parent: (parent: Node["parentNode"]) => void } {
    return {
      parent: (parent: Node["parentNode"]) => this.#parentNode = parent,
    }
  }

  // Enum ========================================================================================

  get ELEMENT_NODE(): 1 {
    return Node.ELEMENT_NODE
  }

  set ELEMENT_NODE(_: 1) {
    return
  }

  static get ELEMENT_NODE(): 1 {
    return 1
  }

  static set ELEMENT_NODE(_: 1) {
    return
  }

  get ATTRIBUTE_NODE(): 2 {
    return Node.ATTRIBUTE_NODE
  }

  set ATTRIBUTE_NODE(_: 2) {
    return
  }

  static get ATTRIBUTE_NODE(): 2 {
    return 2
  }

  static set ATTRIBUTE_NODE(_: 2) {
    return
  }

  get TEXT_NODE(): 3 {
    return Node.TEXT_NODE
  }

  set TEXT_NODE(_: 3) {
    return
  }

  static get TEXT_NODE(): 3 {
    return 3
  }

  static set TEXT_NODE(_: 3) {
    return
  }

  get CDATA_SECTION_NODE(): 4 {
    return Node.CDATA_SECTION_NODE
  }

  set CDATA_SECTION_NODE(_: 4) {
    return
  }

  static get CDATA_SECTION_NODE(): 4 {
    return 4
  }

  static set CDATA_SECTION_NODE(_: 4) {
    return
  }

  get ENTITY_REFERENCE_NODE(): 5 {
    return Node.ENTITY_REFERENCE_NODE
  }

  set ENTITY_REFERENCE_NODE(_: 5) {
    return
  }

  static get ENTITY_REFERENCE_NODE(): 5 {
    return 5
  }

  static set ENTITY_REFERENCE_NODE(_: 5) {
    return
  }

  get ENTITY_NODE(): 6 {
    return Node.ENTITY_NODE
  }

  set ENTITY_NODE(_: 6) {
    return
  }

  static get ENTITY_NODE(): 6 {
    return 6
  }

  static set ENTITY_NODE(_: 6) {
    return
  }

  get PROCESSING_INSTRUCTION_NODE(): 7 {
    return Node.PROCESSING_INSTRUCTION_NODE
  }

  set PROCESSING_INSTRUCTION_NODE(_: 7) {
    return
  }

  static get PROCESSING_INSTRUCTION_NODE(): 7 {
    return 7
  }

  static set PROCESSING_INSTRUCTION_NODE(_: 7) {
    return
  }

  get COMMENT_NODE(): 8 {
    return Node.COMMENT_NODE
  }

  set COMMENT_NODE(_: 8) {
    return
  }

  static get COMMENT_NODE(): 8 {
    return 8
  }

  static set COMMENT_NODE(_: 8) {
    return
  }

  get DOCUMENT_NODE(): 9 {
    return Node.DOCUMENT_NODE
  }

  set DOCUMENT_NODE(_: 9) {
    return
  }

  static get DOCUMENT_NODE(): 9 {
    return 9
  }

  static set DOCUMENT_NODE(_: 9) {
    return
  }

  get DOCUMENT_TYPE_NODE(): 10 {
    return Node.DOCUMENT_TYPE_NODE
  }

  set DOCUMENT_TYPE_NODE(_: 10) {
    return
  }

  static get DOCUMENT_TYPE_NODE(): 10 {
    return 10
  }

  static set DOCUMENT_TYPE_NODE(_: 10) {
    return
  }

  get DOCUMENT_FRAGMENT_NODE(): 11 {
    return Node.DOCUMENT_FRAGMENT_NODE
  }

  set DOCUMENT_FRAGMENT_NODE(_: 11) {
    return
  }

  static get DOCUMENT_FRAGMENT_NODE(): 11 {
    return 11
  }

  static set DOCUMENT_FRAGMENT_NODE(_: 11) {
    return
  }

  get NOTATION_NODE(): 12 {
    return Node.NOTATION_NODE
  }

  set NOTATION_NODE(_: 12) {
    return
  }

  static get NOTATION_NODE(): 12 {
    return 12
  }

  static set NOTATION_NODE(_: 12) {
    return
  }

  get DOCUMENT_POSITION_DISCONNECTED(): 1 {
    return Node.DOCUMENT_POSITION_DISCONNECTED
  }

  set DOCUMENT_POSITION_DISCONNECTED(_: 1) {
    return
  }

  static get DOCUMENT_POSITION_DISCONNECTED(): 1 {
    return 1
  }

  static set DOCUMENT_POSITION_DISCONNECTED(_: 1) {
    return
  }

  get DOCUMENT_POSITION_PRECEDING(): 2 {
    return Node.DOCUMENT_POSITION_PRECEDING
  }

  set DOCUMENT_POSITION_PRECEDING(_: 2) {
    return
  }

  static get DOCUMENT_POSITION_PRECEDING(): 2 {
    return 2
  }

  static set DOCUMENT_POSITION_PRECEDING(_: 2) {
    return
  }

  get DOCUMENT_POSITION_FOLLOWING(): 4 {
    return Node.DOCUMENT_POSITION_FOLLOWING
  }

  set DOCUMENT_POSITION_FOLLOWING(_: 4) {
    return
  }

  static get DOCUMENT_POSITION_FOLLOWING(): 4 {
    return 4
  }

  static set DOCUMENT_POSITION_FOLLOWING(_: 4) {
    return
  }

  get DOCUMENT_POSITION_CONTAINS(): 8 {
    return Node.DOCUMENT_POSITION_CONTAINS
  }

  set DOCUMENT_POSITION_CONTAINS(_: 8) {
    return
  }

  static get DOCUMENT_POSITION_CONTAINS(): 8 {
    return 8
  }

  static set DOCUMENT_POSITION_CONTAINS(_: 8) {
    return
  }

  get DOCUMENT_POSITION_CONTAINED_BY(): 16 {
    return Node.DOCUMENT_POSITION_CONTAINED_BY
  }

  set DOCUMENT_POSITION_CONTAINED_BY(_: 16) {
    return
  }

  static get DOCUMENT_POSITION_CONTAINED_BY(): 16 {
    return 16
  }

  static set DOCUMENT_POSITION_CONTAINED_BY(_: 16) {
    return
  }

  get DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(): 32 {
    return Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
  }

  set DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(_: 32) {
    return
  }

  static get DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(): 32 {
    return 32
  }

  static set DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC(_: 32) {
    return
  }
}

/** https://developer.mozilla.org/en-US/docs/Web/API/NodeList */
export class NodeList<T extends _Node> extends Indexable<T> implements _NodeList, NodeListOf<T> {
  // https://developer.mozilla.org/en-US/docs/Web/API/NodeList/item
  item(index: number): T {
    return this[index] ?? null
  }

  // https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach
  // deno-lint-ignore no-explicit-any
  override forEach(callbackfn: (value: T, index: number, array: this) => void, thisArg?: any): void {
    // deno-lint-ignore no-explicit-any
    return super.forEach(callbackfn as any, thisArg)
  }
}