-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Document.ts
1359 lines (1230 loc) · 38.9 KB
/
Document.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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Element from '../element/Element.js';
import * as PropertySymbol from '../../PropertySymbol.js';
import BrowserWindow from '../../window/BrowserWindow.js';
import Node from '../node/Node.js';
import NodeIterator from '../../tree-walker/NodeIterator.js';
import TreeWalker from '../../tree-walker/TreeWalker.js';
import DocumentFragment from '../document-fragment/DocumentFragment.js';
import XMLParser from '../../xml-parser/XMLParser.js';
import Event from '../../event/Event.js';
import DOMImplementation from '../../dom-implementation/DOMImplementation.js';
import INodeFilter from '../../tree-walker/INodeFilter.js';
import NamespaceURI from '../../config/NamespaceURI.js';
import DocumentType from '../document-type/DocumentType.js';
import ParentNodeUtility from '../parent-node/ParentNodeUtility.js';
import QuerySelector from '../../query-selector/QuerySelector.js';
import CSSStyleSheet from '../../css/CSSStyleSheet.js';
import DOMException from '../../exception/DOMException.js';
import HTMLScriptElement from '../html-script-element/HTMLScriptElement.js';
import HTMLElement from '../html-element/HTMLElement.js';
import Comment from '../comment/Comment.js';
import Text from '../text/Text.js';
import NodeList from '../node/NodeList.js';
import HTMLCollection from '../element/HTMLCollection.js';
import HTMLLinkElement from '../html-link-element/HTMLLinkElement.js';
import HTMLStyleElement from '../html-style-element/HTMLStyleElement.js';
import DocumentReadyStateEnum from './DocumentReadyStateEnum.js';
import Location from '../../location/Location.js';
import Selection from '../../selection/Selection.js';
import ShadowRoot from '../shadow-root/ShadowRoot.js';
import Range from '../../range/Range.js';
import HTMLBaseElement from '../html-base-element/HTMLBaseElement.js';
import Attr from '../attr/Attr.js';
import ProcessingInstruction from '../processing-instruction/ProcessingInstruction.js';
import ElementUtility from '../element/ElementUtility.js';
import VisibilityStateEnum from './VisibilityStateEnum.js';
import NodeTypeEnum from '../node/NodeTypeEnum.js';
import CookieStringUtility from '../../cookie/urilities/CookieStringUtility.js';
import IBrowserFrame from '../../browser/types/IBrowserFrame.js';
import NodeFactory from '../NodeFactory.js';
import { URL } from 'url';
import IHTMLElementTagNameMap from '../../config/IHTMLElementTagNameMap.js';
import ISVGElementTagNameMap from '../../config/ISVGElementTagNameMap.js';
import SVGElement from '../svg-element/SVGElement.js';
import HTMLFormElement from '../html-form-element/HTMLFormElement.js';
import HTMLAnchorElement from '../html-anchor-element/HTMLAnchorElement.js';
import HTMLElementConfig from '../../config/HTMLElementConfig.js';
const PROCESSING_INSTRUCTION_TARGET_REGEXP = /^[a-z][a-z0-9-]+$/;
/**
* Document.
*/
export default class Document extends Node {
// Internal properties
public [PropertySymbol.children]: HTMLCollection<Element> = new HTMLCollection<Element>();
public [PropertySymbol.activeElement]: HTMLElement | SVGElement = null;
public [PropertySymbol.nextActiveElement]: HTMLElement | SVGElement = null;
public [PropertySymbol.currentScript]: HTMLScriptElement = null;
public [PropertySymbol.rootNode] = this;
// Used as an unique identifier which is updated whenever the DOM gets modified.
public [PropertySymbol.cacheID] = 0;
public [PropertySymbol.isFirstWrite] = true;
public [PropertySymbol.isFirstWriteAfterOpen] = false;
public [PropertySymbol.nodeType] = NodeTypeEnum.documentNode;
public [PropertySymbol.isConnected] = true;
public [PropertySymbol.adoptedStyleSheets]: CSSStyleSheet[] = [];
public [PropertySymbol.implementation] = new DOMImplementation(this);
public [PropertySymbol.readyState] = DocumentReadyStateEnum.interactive;
public [PropertySymbol.referrer] = '';
public [PropertySymbol.defaultView]: BrowserWindow | null = null;
public [PropertySymbol.ownerWindow]: BrowserWindow;
// Private properties
#selection: Selection = null;
#browserFrame: IBrowserFrame;
// Events
public onreadystatechange: (event: Event) => void = null;
public onpointerlockchange: (event: Event) => void = null;
public onpointerlockerror: (event: Event) => void = null;
public onbeforecopy: (event: Event) => void = null;
public onbeforecut: (event: Event) => void = null;
public onbeforepaste: (event: Event) => void = null;
public onfreeze: (event: Event) => void = null;
public onresume: (event: Event) => void = null;
public onsearch: (event: Event) => void = null;
public onvisibilitychange: (event: Event) => void = null;
public onfullscreenchange: (event: Event) => void = null;
public onfullscreenerror: (event: Event) => void = null;
public onwebkitfullscreenchange: (event: Event) => void = null;
public onwebkitfullscreenerror: (event: Event) => void = null;
public onbeforexrselect: (event: Event) => void = null;
public onabort: (event: Event) => void = null;
public onbeforeinput: (event: Event) => void = null;
public onblur: (event: Event) => void = null;
public oncancel: (event: Event) => void = null;
public oncanplay: (event: Event) => void = null;
public oncanplaythrough: (event: Event) => void = null;
public onchange: (event: Event) => void = null;
public onclick: (event: Event) => void = null;
public onclose: (event: Event) => void = null;
public oncontextlost: (event: Event) => void = null;
public oncontextmenu: (event: Event) => void = null;
public oncontextrestored: (event: Event) => void = null;
public oncuechange: (event: Event) => void = null;
public ondblclick: (event: Event) => void = null;
public ondrag: (event: Event) => void = null;
public ondragend: (event: Event) => void = null;
public ondragenter: (event: Event) => void = null;
public ondragleave: (event: Event) => void = null;
public ondragover: (event: Event) => void = null;
public ondragstart: (event: Event) => void = null;
public ondrop: (event: Event) => void = null;
public ondurationchange: (event: Event) => void = null;
public onemptied: (event: Event) => void = null;
public onended: (event: Event) => void = null;
public onerror: (event: Event) => void = null;
public onfocus: (event: Event) => void = null;
public onformdata: (event: Event) => void = null;
public oninput: (event: Event) => void = null;
public oninvalid: (event: Event) => void = null;
public onkeydown: (event: Event) => void = null;
public onkeypress: (event: Event) => void = null;
public onkeyup: (event: Event) => void = null;
public onload: (event: Event) => void = null;
public onloadeddata: (event: Event) => void = null;
public onloadedmetadata: (event: Event) => void = null;
public onloadstart: (event: Event) => void = null;
public onmousedown: (event: Event) => void = null;
public onmouseenter: (event: Event) => void = null;
public onmouseleave: (event: Event) => void = null;
public onmousemove: (event: Event) => void = null;
public onmouseout: (event: Event) => void = null;
public onmouseover: (event: Event) => void = null;
public onmouseup: (event: Event) => void = null;
public onmousewheel: (event: Event) => void = null;
public onpause: (event: Event) => void = null;
public onplay: (event: Event) => void = null;
public onplaying: (event: Event) => void = null;
public onprogress: (event: Event) => void = null;
public onratechange: (event: Event) => void = null;
public onreset: (event: Event) => void = null;
public onresize: (event: Event) => void = null;
public onscroll: (event: Event) => void = null;
public onsecuritypolicyviolation: (event: Event) => void = null;
public onseeked: (event: Event) => void = null;
public onseeking: (event: Event) => void = null;
public onselect: (event: Event) => void = null;
public onslotchange: (event: Event) => void = null;
public onstalled: (event: Event) => void = null;
public onsubmit: (event: Event) => void = null;
public onsuspend: (event: Event) => void = null;
public ontimeupdate: (event: Event) => void = null;
public ontoggle: (event: Event) => void = null;
public onvolumechange: (event: Event) => void = null;
public onwaiting: (event: Event) => void = null;
public onwebkitanimationend: (event: Event) => void = null;
public onwebkitanimationiteration: (event: Event) => void = null;
public onwebkitanimationstart: (event: Event) => void = null;
public onwebkittransitionend: (event: Event) => void = null;
public onwheel: (event: Event) => void = null;
public onauxclick: (event: Event) => void = null;
public ongotpointercapture: (event: Event) => void = null;
public onlostpointercapture: (event: Event) => void = null;
public onpointerdown: (event: Event) => void = null;
public onpointermove: (event: Event) => void = null;
public onpointerrawupdate: (event: Event) => void = null;
public onpointerup: (event: Event) => void = null;
public onpointercancel: (event: Event) => void = null;
public onpointerover: (event: Event) => void = null;
public onpointerout: (event: Event) => void = null;
public onpointerenter: (event: Event) => void = null;
public onpointerleave: (event: Event) => void = null;
public onselectstart: (event: Event) => void = null;
public onselectionchange: (event: Event) => void = null;
public onanimationend: (event: Event) => void = null;
public onanimationiteration: (event: Event) => void = null;
public onanimationstart: (event: Event) => void = null;
public ontransitionrun: (event: Event) => void = null;
public ontransitionstart: (event: Event) => void = null;
public ontransitionend: (event: Event) => void = null;
public ontransitioncancel: (event: Event) => void = null;
public oncopy: (event: Event) => void = null;
public oncut: (event: Event) => void = null;
public onpaste: (event: Event) => void = null;
public onbeforematch: (event: Event) => void = null;
/**
* Constructor.
*
* @param injected Injected properties.
* @param injected.browserFrame Browser frame.
* @param injected.window Window.
*/
constructor(injected: { browserFrame: IBrowserFrame; window: BrowserWindow }) {
super();
this.#browserFrame = injected.browserFrame;
this[PropertySymbol.ownerWindow] = injected.window;
}
/**
* Returns adopted style sheets.
*
* @returns Adopted style sheets.
*/
public get adoptedStyleSheets(): CSSStyleSheet[] {
return this[PropertySymbol.adoptedStyleSheets];
}
/**
* Sets adopted style sheets.
*
* @param value Adopted style sheets.
*/
public set adoptedStyleSheets(value: CSSStyleSheet[]) {
this[PropertySymbol.adoptedStyleSheets] = value;
}
/**
* Returns DOM implementation.
*
* @returns DOM implementation.
*/
public get implementation(): DOMImplementation {
return this[PropertySymbol.implementation];
}
/**
* Returns document ready state.
*
* @returns Document ready state.
*/
public get readyState(): DocumentReadyStateEnum {
return this[PropertySymbol.readyState];
}
/**
* Returns referrer.
*
* @returns Referrer.
*/
public get referrer(): string {
return this[PropertySymbol.referrer];
}
/**
* Returns default view.
*
* @returns Default view.
*/
public get defaultView(): BrowserWindow | null {
return this[PropertySymbol.defaultView];
}
/**
* Returns document children.
*/
public get children(): HTMLCollection<Element> {
return this[PropertySymbol.children];
}
/**
* Returns character set.
*
* @deprecated
* @returns Character set.
*/
public get charset(): string {
return this.characterSet;
}
/**
* Returns character set.
*
* @returns Character set.
*/
public get characterSet(): string {
const charset = this.querySelector('meta[charset]')?.getAttributeNS(null, 'charset');
return charset ? charset : 'UTF-8';
}
/**
* Returns title.
*
* @returns Title.
*/
public get title(): string {
const element = ParentNodeUtility.getElementByTagName(this, 'title');
if (element) {
return element.textContent;
}
return '';
}
/**
* Returns set title.
*
*/
public set title(title: string) {
const element = ParentNodeUtility.getElementByTagName(this, 'title');
if (element) {
element.textContent = title;
} else {
const newElement = this.createElement('title');
newElement.textContent = title;
this.head.appendChild(newElement);
}
}
/**
* Returns a collection of all area elements and a elements in a document with a value for the href attribute.
*/
public get links(): NodeList<HTMLAnchorElement | HTMLElement> {
return <NodeList<HTMLElement>>this.querySelectorAll('a[href],area[href]');
}
/**
* Returns a collection of all form elements in a document.
*/
public get forms(): NodeList<HTMLFormElement> {
return this.querySelectorAll('form');
}
/**
* Last element child.
*
* @returns Element.
*/
public get childElementCount(): number {
return this[PropertySymbol.children].length;
}
/**
* First element child.
*
* @returns Element.
*/
public get firstElementChild(): Element {
return this[PropertySymbol.children][0] ?? null;
}
/**
* Last element child.
*
* @returns Element.
*/
public get lastElementChild(): Element {
return this[PropertySymbol.children][this[PropertySymbol.children].length - 1] ?? null;
}
/**
* Returns cookie string.
*
* @returns Cookie.
*/
public get cookie(): string {
return CookieStringUtility.cookiesToString(
this.#browserFrame.page.context.cookieContainer.getCookies(
new URL(this[PropertySymbol.ownerWindow].location.href),
true
)
);
}
/**
* Sets a cookie string.
*
* @param cookie Cookie string.
*/
public set cookie(cookie: string) {
this.#browserFrame.page.context.cookieContainer.addCookies([
CookieStringUtility.stringToCookie(
new URL(this[PropertySymbol.ownerWindow].location.href),
cookie
)
]);
}
/**
* Node name.
*
* @returns Node name.
*/
public get nodeName(): string {
return '#document';
}
/**
* Returns <html> element.
*
* @returns Element.
*/
public get documentElement(): HTMLElement {
return <HTMLElement>ParentNodeUtility.getElementByTagName(this, 'html');
}
/**
* Returns document type element.
*
* @returns Document type.
*/
public get doctype(): DocumentType {
for (const node of this[PropertySymbol.childNodes]) {
if (node instanceof DocumentType) {
return node;
}
}
return null;
}
/**
* Returns <body> element.
*
* @returns Element.
*/
public get body(): HTMLElement {
return <HTMLElement>ParentNodeUtility.getElementByTagName(this, 'body');
}
/**
* Returns <head> element.
*
* @returns Element.
*/
public get head(): HTMLElement {
return <HTMLElement>ParentNodeUtility.getElementByTagName(this, 'head');
}
/**
* Returns CSS style sheets.
*
* @returns CSS style sheets.
*/
public get styleSheets(): CSSStyleSheet[] {
const styles = <NodeList<HTMLLinkElement | HTMLStyleElement>>(
this.querySelectorAll('link[rel="stylesheet"][href],style')
);
const styleSheets = [];
for (const style of styles) {
const sheet = style.sheet;
if (sheet) {
styleSheets.push(sheet);
}
}
return styleSheets;
}
/**
* Returns active element.
*
* @returns Active element.
*/
public get activeElement(): HTMLElement | SVGElement {
if (
this[PropertySymbol.activeElement] &&
!this[PropertySymbol.activeElement][PropertySymbol.isConnected]
) {
this[PropertySymbol.activeElement] = null;
}
if (
this[PropertySymbol.activeElement] &&
this[PropertySymbol.activeElement] instanceof Element
) {
let rootNode: ShadowRoot | Document = <ShadowRoot | Document>(
this[PropertySymbol.activeElement].getRootNode()
);
let activeElement = this[PropertySymbol.activeElement];
while (rootNode !== this) {
activeElement = <HTMLElement | SVGElement>(<ShadowRoot>rootNode).host;
rootNode = activeElement ? <ShadowRoot | Document>activeElement.getRootNode() : this;
}
return activeElement;
}
return this[PropertySymbol.activeElement] || this.body || this.documentElement || null;
}
/**
* Returns scrolling element.
*
* @returns Scrolling element.
*/
public get scrollingElement(): HTMLElement {
return this.documentElement;
}
/**
* Returns location.
*
* @returns Location.
*/
public get location(): Location {
return this[PropertySymbol.ownerWindow].location;
}
/**
* Returns scripts.
*
* @returns Scripts.
*/
public get scripts(): HTMLCollection<HTMLScriptElement> {
return this.getElementsByTagName('script');
}
/**
* Returns base URI.
*
* @override
* @returns Base URI.
*/
public get baseURI(): string {
const element = <HTMLBaseElement | null>ParentNodeUtility.getElementByTagName(this, 'base');
if (element) {
return element.href;
}
return this[PropertySymbol.ownerWindow].location.href;
}
/**
* Returns URL.
*
* @returns the URL of the current document.
* */
public get URL(): string {
return this[PropertySymbol.ownerWindow].location.href;
}
/**
* Returns document URI.
*
* @returns the URL of the current document.
* */
public get documentURI(): string {
return this.URL;
}
/**
* Returns document visibility state.
*
* @returns the visibility state of the current document.
* */
public get visibilityState(): VisibilityStateEnum {
if (this.defaultView) {
return VisibilityStateEnum.visible;
}
return VisibilityStateEnum.hidden;
}
/**
* Returns document hidden state.
*
* @returns the hidden state of the current document.
* */
public get hidden(): boolean {
if (this.defaultView) {
return false;
}
return true;
}
/**
* Gets the currently executing script element.
*
* @returns the currently executing script element.
*/
public get currentScript(): HTMLScriptElement {
return this[PropertySymbol.currentScript];
}
/**
* Inserts a set of Node objects or DOMString objects after the last child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public append(...nodes: (Node | string)[]): void {
ParentNodeUtility.append(this, ...nodes);
}
/**
* Inserts a set of Node objects or DOMString objects before the first child of the ParentNode. DOMString objects are inserted as equivalent Text nodes.
*
* @param nodes List of Node or DOMString.
*/
public prepend(...nodes: (Node | string)[]): void {
ParentNodeUtility.prepend(this, ...nodes);
}
/**
* Replaces the existing children of a node with a specified new set of children.
*
* @param nodes List of Node or DOMString.
*/
public replaceChildren(...nodes: (Node | string)[]): void {
ParentNodeUtility.replaceChildren(this, ...nodes);
}
/**
* Query CSS selector to find matching nodes.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll<K extends keyof IHTMLElementTagNameMap>(
selector: K
): NodeList<IHTMLElementTagNameMap[K]>;
/**
* Query CSS selector to find matching elments.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll<K extends keyof ISVGElementTagNameMap>(
selector: K
): NodeList<ISVGElementTagNameMap[K]>;
/**
* Query CSS selector to find matching elments.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll(selector: string): NodeList<Element>;
/**
* Query CSS selector to find matching elments.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll(selector: string): NodeList<Element> {
return QuerySelector.querySelectorAll(this, selector);
}
/**
* Query CSS Selector to find matching node.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector<K extends keyof IHTMLElementTagNameMap>(
selector: K
): IHTMLElementTagNameMap[K] | null;
/**
* Query CSS Selector to find matching node.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector<K extends keyof ISVGElementTagNameMap>(
selector: K
): ISVGElementTagNameMap[K] | null;
/**
* Query CSS Selector to find matching node.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): Element | null;
/**
* Query CSS Selector to find matching node.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): Element | null {
return QuerySelector.querySelector(this, selector);
}
/**
* Returns an elements by class name.
*
* @param className Tag name.
* @returns Matching element.
*/
public getElementsByClassName(className: string): HTMLCollection<Element> {
return ParentNodeUtility.getElementsByClassName(this, className);
}
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName<K extends keyof IHTMLElementTagNameMap>(
tagName: K
): HTMLCollection<IHTMLElementTagNameMap[K]>;
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName<K extends keyof ISVGElementTagNameMap>(
tagName: K
): HTMLCollection<ISVGElementTagNameMap[K]>;
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName(tagName: string): HTMLCollection<Element>;
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName(tagName: string): HTMLCollection<Element> {
return ParentNodeUtility.getElementsByTagName(this, tagName);
}
/**
* Returns an elements by tag name and namespace.
*
* @param namespaceURI Namespace URI.
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagNameNS<K extends keyof IHTMLElementTagNameMap>(
namespaceURI: 'http://www.w3.org/1999/xhtml',
tagName: K
): HTMLCollection<IHTMLElementTagNameMap[K]>;
/**
* Returns an elements by tag name and namespace.
*
* @param namespaceURI Namespace URI.
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagNameNS<K extends keyof ISVGElementTagNameMap>(
namespaceURI: 'http://www.w3.org/2000/svg',
tagName: K
): HTMLCollection<ISVGElementTagNameMap[K]>;
/**
* Returns an elements by tag name and namespace.
*
* @param namespaceURI Namespace URI.
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagNameNS(namespaceURI: string, tagName: string): HTMLCollection<Element>;
/**
* Returns an elements by tag name and namespace.
*
* @param namespaceURI Namespace URI.
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagNameNS(namespaceURI: string, tagName: string): HTMLCollection<Element> {
return ParentNodeUtility.getElementsByTagNameNS(this, namespaceURI, tagName);
}
/**
* Returns an element by ID.
*
* @param id ID.
* @returns Matching element.
*/
public getElementById(id: string): Element {
return <Element>ParentNodeUtility.getElementById(this, id);
}
/**
* Returns an element by Name.
*
* @returns Matching element.
* @param name
*/
public getElementsByName(name: string): NodeList<Element> {
const getElementsByName = (
parentNode: Element | DocumentFragment | Document,
name: string
): NodeList<Element> => {
const matches = new NodeList<Element>();
for (const child of (<Element | Document>parentNode)[PropertySymbol.children]) {
if (child.getAttributeNS(null, 'name') === name) {
matches.push(child);
}
for (const match of getElementsByName(<Element>child, name)) {
matches.push(match);
}
}
return matches;
};
return getElementsByName(this, name);
}
/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): Document {
const clone = <Document>super.cloneNode(deep);
if (deep) {
for (const node of clone[PropertySymbol.childNodes]) {
if (node[PropertySymbol.nodeType] === NodeTypeEnum.elementNode) {
clone[PropertySymbol.children].push(<Element>node);
}
}
}
return clone;
}
/**
* @override
*/
public override appendChild(node: Node): Node {
// We do not call super here as this will be handled by ElementUtility to improve performance by avoiding validation and other checks.
return ElementUtility.appendChild(this, node);
}
/**
* @override
*/
public override removeChild(node: Node): Node {
// We do not call super here as this will be handled by ElementUtility to improve performance by avoiding validation and other checks.
return ElementUtility.removeChild(this, node);
}
/**
* @override
*/
public override insertBefore(newNode: Node, referenceNode: Node | null): Node {
if (arguments.length < 2) {
throw new TypeError(
`Failed to execute 'insertBefore' on 'Node': 2 arguments required, but only ${arguments.length} present.`
);
}
// We do not call super here as this will be handled by ElementUtility to improve performance by avoiding validation and other checks.
return ElementUtility.insertBefore(this, newNode, referenceNode);
}
/**
* Replaces the document HTML with new HTML.
*
* @param html HTML.
*/
public write(html: string): void {
const root = <DocumentFragment>XMLParser.parse(this, html, { evaluateScripts: true });
if (this[PropertySymbol.isFirstWrite] || this[PropertySymbol.isFirstWriteAfterOpen]) {
if (this[PropertySymbol.isFirstWrite]) {
if (!this[PropertySymbol.isFirstWriteAfterOpen]) {
this.open();
}
this[PropertySymbol.isFirstWrite] = false;
}
this[PropertySymbol.isFirstWriteAfterOpen] = false;
let documentElement = null;
let documentTypeNode = null;
for (const node of root[PropertySymbol.childNodes]) {
if (node['tagName'] === 'HTML') {
documentElement = node;
} else if (node[PropertySymbol.nodeType] === NodeTypeEnum.documentTypeNode) {
documentTypeNode = node;
}
if (documentElement && documentTypeNode) {
break;
}
}
if (documentElement) {
if (!this.documentElement) {
if (documentTypeNode) {
this.appendChild(documentTypeNode);
}
this.appendChild(documentElement);
const head = <Element>ParentNodeUtility.getElementByTagName(this, 'head');
let body = <Element>ParentNodeUtility.getElementByTagName(this, 'body');
if (!body) {
body = this.createElement('body');
documentElement.appendChild(this.createElement('body'));
}
if (!head) {
documentElement.insertBefore(this.createElement('head'), body);
}
} else {
const rootBody = <Element>ParentNodeUtility.getElementByTagName(root, 'body');
const body = ParentNodeUtility.getElementByTagName(this, 'body');
if (rootBody && body) {
for (const child of rootBody[PropertySymbol.childNodes].slice()) {
body.appendChild(child);
}
}
}
// Remaining nodes outside the <html> element are added to the <body> element.
const body = ParentNodeUtility.getElementByTagName(this, 'body');
if (body) {
for (const child of root[PropertySymbol.childNodes].slice()) {
if (
child['tagName'] !== 'HTML' &&
child[PropertySymbol.nodeType] !== NodeTypeEnum.documentTypeNode
) {
body.appendChild(child);
}
}
}
} else {
const documentElement = this.createElement('html');
const bodyElement = this.createElement('body');
const headElement = this.createElement('head');
for (const child of root[PropertySymbol.childNodes].slice()) {
bodyElement.appendChild(child);
}
documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
this.appendChild(documentElement);
}
} else {
const bodyNode = ParentNodeUtility.getElementByTagName(root, 'body');
const body = ParentNodeUtility.getElementByTagName(this, 'body');
for (const child of (<Element>(bodyNode || root))[PropertySymbol.childNodes].slice()) {
body.appendChild(child);
}
}
}
/**
* Opens the document.
*
* @returns Document.
*/
public open(): Document {
this[PropertySymbol.isFirstWriteAfterOpen] = true;
for (const eventType of Object.keys(this[PropertySymbol.listeners])) {
const listeners = this[PropertySymbol.listeners][eventType];
if (listeners) {
for (const listener of listeners) {
this.removeEventListener(eventType, listener);
}
}
}
for (const child of this[PropertySymbol.childNodes].slice()) {
this.removeChild(child);
}
return this;
}
/**
* Closes the document.
*/
public close(): void {}
/* eslint-disable jsdoc/valid-types */
/**
* Creates an element.
*
* @param qualifiedName Tag name.
* @param [options] Options.
* @param [options.is] Tag name of a custom element previously defined via customElements.define().
* @returns Element.
*/
public createElement<K extends keyof IHTMLElementTagNameMap>(
qualifiedName: K,
options?: { is?: string }
): IHTMLElementTagNameMap[K];
/**
* Creates an element.
*
* @param qualifiedName Tag name.
* @param [options] Options.
* @param [options.is] Tag name of a custom element previously defined via customElements.define().
* @returns Element.