/
Document.ts
831 lines (740 loc) · 20.7 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
import Element from '../element/Element';
import HTMLUnknownElement from '../html-unknown-element/HTMLUnknownElement';
import Text from '../text/Text';
import Comment from '../comment/Comment';
import IWindow from '../../window/IWindow';
import Node from '../node/Node';
import TreeWalker from '../../tree-walker/TreeWalker';
import DocumentFragment from '../document-fragment/DocumentFragment';
import XMLParser from '../../xml-parser/XMLParser';
import Event from '../../event/Event';
import DOMImplementation from '../../dom-implementation/DOMImplementation';
import ElementTag from '../../config/ElementTag';
import INodeFilter from '../../tree-walker/INodeFilter';
import Attr from '../../attribute/Attr';
import NamespaceURI from '../../config/NamespaceURI';
import DocumentType from '../document-type/DocumentType';
import ParentNodeUtility from '../parent-node/ParentNodeUtility';
import QuerySelector from '../../query-selector/QuerySelector';
import IDocument from './IDocument';
import CSSStyleSheet from '../../css/CSSStyleSheet';
import DOMException from '../../exception/DOMException';
import CookieUtility from '../../cookie/CookieUtility';
import IElement from '../element/IElement';
import IHTMLScriptElement from '../html-script-element/IHTMLScriptElement';
import IHTMLElement from '../html-element/IHTMLElement';
import IDocumentType from '../document-type/IDocumentType';
import INode from '../node/INode';
import IComment from '../comment/IComment';
import IText from '../text/IText';
import IDocumentFragment from '../document-fragment/IDocumentFragment';
import INodeList from '../node/INodeList';
import IHTMLCollection from '../element/IHTMLCollection';
import HTMLCollectionFactory from '../element/HTMLCollectionFactory';
import IHTMLLinkElement from '../html-link-element/IHTMLLinkElement';
import IHTMLStyleElement from '../html-style-element/IHTMLStyleElement';
import DocumentReadyStateEnum from './DocumentReadyStateEnum';
import DocumentReadyStateManager from './DocumentReadyStateManager';
import Location from '../../location/Location';
import Selection from '../../selection/Selection';
import IShadowRoot from '../shadow-root/IShadowRoot';
import Range from '../../range/Range';
import IHTMLBaseElement from '../html-base-element/IHTMLBaseElement';
/**
* Document.
*/
export default class Document extends Node implements IDocument {
public onreadystatechange: (event: Event) => void = null;
public nodeType = Node.DOCUMENT_NODE;
public adoptedStyleSheets: CSSStyleSheet[] = [];
public implementation: DOMImplementation;
public readonly children: IHTMLCollection<IElement> = HTMLCollectionFactory.create();
public readonly readyState = DocumentReadyStateEnum.interactive;
public readonly isConnected: boolean = true;
public _readyStateManager: DocumentReadyStateManager = null;
public _activeElement: IHTMLElement = null;
protected _isFirstWrite = true;
protected _isFirstWriteAfterOpen = false;
private _defaultView: IWindow = null;
private _cookie = '';
/**
* Creates an instance of Document.
*/
constructor() {
super();
this.implementation = new DOMImplementation();
this.implementation._ownerDocument = this;
const doctype = this.implementation.createDocumentType('html', '', '');
const documentElement = this.createElement('html');
const bodyElement = this.createElement('body');
const headElement = this.createElement('head');
this.appendChild(doctype);
this.appendChild(documentElement);
documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
}
/**
* 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 default view.
*
* @returns Default view.
*/
public get defaultView(): IWindow {
return this._defaultView;
}
/**
* Sets a default view.
*
* @param defaultView Default view.
*/
public set defaultView(defaultView: IWindow) {
this._defaultView = defaultView;
this._readyStateManager = new DocumentReadyStateManager(defaultView);
this._readyStateManager.whenComplete().then(() => {
(<DocumentReadyStateEnum>this.readyState) = DocumentReadyStateEnum.complete;
this.dispatchEvent(new Event('readystatechange'));
});
}
/**
* Last element child.
*
* @returns Element.
*/
public get childElementCount(): number {
return this.children.length;
}
/**
* First element child.
*
* @returns Element.
*/
public get firstElementChild(): IElement {
return this.children ? this.children[0] || null : null;
}
/**
* Last element child.
*
* @returns Element.
*/
public get lastElementChild(): IElement {
return this.children ? this.children[this.children.length - 1] || null : null;
}
/**
* Returns cookie string.
*
* @returns Cookie.
*/
public get cookie(): string {
return this._cookie;
}
/**
* Sets a cookie string.
*
* @param cookie Cookie string.
*/
public set cookie(cookie: string) {
this._cookie = CookieUtility.getCookieString(this.defaultView.location, this._cookie, cookie);
}
/**
* Node name.
*
* @returns Node name.
*/
public get nodeName(): string {
return '#document';
}
/**
* Returns <html> element.
*
* @returns Element.
*/
public get documentElement(): IHTMLElement {
return <IHTMLElement>ParentNodeUtility.getElementByTagName(this, 'html');
}
/**
* Returns document type element.
*
* @returns Document type.
*/
public get doctype(): IDocumentType {
for (const node of this.childNodes) {
if (node instanceof DocumentType) {
return node;
}
}
return null;
}
/**
* Returns <body> element.
*
* @returns Element.
*/
public get body(): IHTMLElement {
return <IHTMLElement>ParentNodeUtility.getElementByTagName(this, 'body');
}
/**
* Returns <head> element.
*
* @returns Element.
*/
public get head(): IHTMLElement {
return <IHTMLElement>ParentNodeUtility.getElementByTagName(this, 'head');
}
/**
* Returns CSS style sheets.
*
* @returns CSS style sheets.
*/
public get styleSheets(): CSSStyleSheet[] {
const styles = <INodeList<IHTMLLinkElement | IHTMLStyleElement>>(
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(): IHTMLElement {
if (this._activeElement) {
let rootNode: IShadowRoot | IDocument = <IShadowRoot | IDocument>(
this._activeElement.getRootNode()
);
let activeElement: IHTMLElement = this._activeElement;
while (rootNode !== this) {
activeElement = <IHTMLElement>(<IShadowRoot>rootNode).host;
rootNode = <IShadowRoot | IDocument>activeElement.getRootNode();
}
return activeElement;
}
return this._activeElement || this.body || this.documentElement || null;
}
/**
* Returns scrolling element.
*
* @returns Scrolling element.
*/
public get scrollingElement(): IHTMLElement {
return this.documentElement;
}
/**
* Returns location.
*
* @returns Location.
*/
public get location(): Location {
return this._defaultView.location;
}
/**
* Returns scripts.
*
* @returns Scripts.
*/
public get scripts(): IHTMLCollection<IHTMLScriptElement> {
return <IHTMLCollection<IHTMLScriptElement>>this.getElementsByTagName('script');
}
/**
* Returns base URI.
*
* @override
* @returns Base URI.
*/
public get baseURI(): string {
const base = <IHTMLBaseElement>this.querySelector('base');
if (base) {
return base.href;
}
return this.defaultView.location.href;
}
/**
* 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: (INode | 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: (INode | 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: (INode | string)[]): void {
ParentNodeUtility.replaceChildren(this, ...nodes);
}
/**
* Query CSS selector to find matching elments.
*
* @param selector CSS selector.
* @returns Matching elements.
*/
public querySelectorAll(selector: string): INodeList<IElement> {
return QuerySelector.querySelectorAll(this, selector);
}
/**
* Query CSS Selector to find a matching element.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): IElement {
return QuerySelector.querySelector(this, selector);
}
/**
* Returns an elements by class name.
*
* @param className Tag name.
* @returns Matching element.
*/
public getElementsByClassName(className: string): IHTMLCollection<IElement> {
return ParentNodeUtility.getElementsByClassName(this, className);
}
/**
* Returns an elements by tag name.
*
* @param tagName Tag name.
* @returns Matching element.
*/
public getElementsByTagName(tagName: string): IHTMLCollection<IElement> {
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(namespaceURI: string, tagName: string): IHTMLCollection<IElement> {
return ParentNodeUtility.getElementsByTagNameNS(this, namespaceURI, tagName);
}
/**
* Returns an element by ID.
*
* @param id ID.
* @returns Matching element.
*/
public getElementById(id: string): IElement {
return <Element>ParentNodeUtility.getElementById(this, id);
}
/**
* Returns an element by Name.
*
* @returns Matching element.
* @param name
*/
public getElementsByName(name: string): INodeList<IElement> {
const _getElementsByName = (
_parentNode: IElement | IDocumentFragment | IDocument,
_name: string
): INodeList<IElement> => {
const matches = HTMLCollectionFactory.create();
for (const child of _parentNode.children) {
if ((child.getAttributeNS(null, 'name') || '') === _name) {
matches.push(child);
}
for (const match of _getElementsByName(<IElement>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): IDocument {
const clone = <Document>super.cloneNode(deep);
if (deep) {
for (const node of clone.childNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
clone.children.push(<IElement>node);
}
}
}
clone.defaultView = this.defaultView;
return clone;
}
/**
* Append a child node to childNodes.
*
* @override
* @param node Node to append.
* @returns Appended node.
*/
public appendChild(node: INode): INode {
// If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
if (node.parentNode && node.parentNode['children']) {
const index = node.parentNode['children'].indexOf(node);
if (index !== -1) {
node.parentNode['children'].splice(index, 1);
}
}
if (node !== this && node.nodeType === Node.ELEMENT_NODE) {
this.children.push(<Element>node);
}
}
return super.appendChild(node);
}
/**
* Remove Child element from childNodes array.
*
* @override
* @param node Node to remove.
*/
public removeChild(node: INode): INode {
if (node.nodeType === Node.ELEMENT_NODE) {
const index = this.children.indexOf(<Element>node);
if (index !== -1) {
this.children.splice(index, 1);
}
}
return super.removeChild(node);
}
/**
* Inserts a node before another.
*
* @override
* @param newNode Node to insert.
* @param [referenceNode] Node to insert before.
* @returns Inserted node.
*/
public insertBefore(newNode: INode, referenceNode?: INode): INode {
const returnValue = super.insertBefore(newNode, referenceNode);
// If the type is DocumentFragment, then the child nodes of if it should be moved instead of the actual node.
// See: https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
if (newNode.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
if (newNode.parentNode && newNode.parentNode['children']) {
const index = newNode.parentNode['children'].indexOf(newNode);
if (index !== -1) {
newNode.parentNode['children'].splice(index, 1);
}
}
this.children.length = 0;
for (const node of this.childNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
this.children.push(<IElement>node);
}
}
}
return returnValue;
}
/**
* Replaces the document HTML with new HTML.
*
* @param html HTML.
*/
public write(html: string): void {
const root = XMLParser.parse(this, html, true);
if (this._isFirstWrite || this._isFirstWriteAfterOpen) {
if (this._isFirstWrite) {
if (!this._isFirstWriteAfterOpen) {
this.open();
}
this._isFirstWrite = false;
}
this._isFirstWriteAfterOpen = false;
let documentElement = null;
let documentTypeNode = null;
for (const node of root.childNodes) {
if (node['tagName'] === 'HTML') {
documentElement = node;
} else if (node.nodeType === Node.DOCUMENT_TYPE_NODE) {
documentTypeNode = node;
}
if (documentElement && documentTypeNode) {
break;
}
}
if (documentElement) {
if (!this.documentElement) {
if (documentTypeNode) {
this.appendChild(documentTypeNode);
}
this.appendChild(documentElement);
} else {
const rootBody = root.querySelector('body');
const body = this.querySelector('body');
if (rootBody && body) {
for (const child of rootBody.childNodes.slice()) {
body.appendChild(child);
}
}
}
const body = this.querySelector('body');
if (body) {
for (const child of root.childNodes.slice()) {
if (child['tagName'] !== 'HTML' && child.nodeType !== Node.DOCUMENT_TYPE_NODE) {
body.appendChild(child);
}
}
}
} else {
const documentElement = this.createElement('html');
const bodyElement = this.createElement('body');
const headElement = this.createElement('head');
for (const child of root.childNodes.slice()) {
bodyElement.appendChild(child);
}
documentElement.appendChild(headElement);
documentElement.appendChild(bodyElement);
this.appendChild(documentElement);
}
} else {
const bodyNode = root.querySelector('body');
for (const child of (bodyNode || root).childNodes.slice()) {
this.body.appendChild(child);
}
}
}
/**
* Opens the document.
*
* @returns Document.
*/
public open(): IDocument {
this._isFirstWriteAfterOpen = true;
for (const eventType of Object.keys(this._listeners)) {
const listeners = this._listeners[eventType];
if (listeners) {
for (const listener of listeners) {
this.removeEventListener(eventType, listener);
}
}
}
for (const child of this.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(qualifiedName: string, options?: { is?: string }): IElement {
return this.createElementNS(NamespaceURI.html, qualifiedName, options);
}
/**
* Creates an element with the specified namespace URI and qualified name.
*
* @param namespaceURI Namespace URI.
* @param qualifiedName Tag name.
* @param [options] Options.
* @param [options.is] Tag name of a custom element previously defined via customElements.define().
* @returns Element.
*/
public createElementNS(
namespaceURI: string,
qualifiedName: string,
options?: { is?: string }
): IElement {
const tagName = String(qualifiedName).toUpperCase();
let customElementClass;
if (this.defaultView && options && options.is) {
customElementClass = this.defaultView.customElements.get(String(options.is));
} else if (this.defaultView) {
customElementClass = this.defaultView.customElements.get(tagName);
}
const elementClass = customElementClass || ElementTag[tagName] || HTMLUnknownElement;
elementClass.ownerDocument = this;
const element = new elementClass();
element.tagName = tagName;
element.ownerDocument = this;
element.namespaceURI = namespaceURI;
if (element instanceof Element && options && options.is) {
element._isValue = String(options.is);
}
return element;
}
/* eslint-enable jsdoc/valid-types */
/**
* Creates a text node.
*
* @param [data] Text data.
* @returns Text node.
*/
public createTextNode(data?: string): IText {
Text.ownerDocument = this;
return new Text(data);
}
/**
* Creates a comment node.
*
* @param [data] Text data.
* @returns Text node.
*/
public createComment(data?: string): IComment {
Comment.ownerDocument = this;
return new Comment(data);
}
/**
* Creates a document fragment.
*
* @returns Document fragment.
*/
public createDocumentFragment(): IDocumentFragment {
DocumentFragment.ownerDocument = this;
return new DocumentFragment();
}
/**
* Creates a Tree Walker.
*
* @param root Root.
* @param [whatToShow] What to show.
* @param [filter] Filter.
*/
public createTreeWalker(root: INode, whatToShow = -1, filter: INodeFilter = null): TreeWalker {
return new TreeWalker(root, whatToShow, filter);
}
/**
* Creates an event.
*
* @deprecated
* @param type Type.
* @returns Event.
*/
public createEvent(type: string): Event {
if (this.defaultView[type]) {
return new this.defaultView[type]('init');
}
return new Event('init');
}
/**
* Creates an Attr node.
*
* @param name Name.
* @returns Attribute.
*/
public createAttribute(name: string): Attr {
const attribute = new Attr();
attribute.name = name.toLowerCase();
(<IDocument>attribute.ownerDocument) = this;
return attribute;
}
/**
* Creates a namespaced Attr node.
*
* @param namespaceURI Namespace URI.
* @param qualifiedName Qualified name.
* @returns Element.
*/
public createAttributeNS(namespaceURI: string, qualifiedName: string): Attr {
const attribute = new Attr();
attribute.namespaceURI = namespaceURI;
attribute.name = qualifiedName;
(<IDocument>attribute.ownerDocument) = this;
return attribute;
}
/**
* Imports a node.
*
* @param node Node to import.
* @param [deep=false] Set to "true" if the clone should be deep.
* @param Imported Node.
*/
public importNode(node: INode, deep = false): INode {
if (!(node instanceof Node)) {
throw new DOMException('Parameter 1 was not of type Node.');
}
const clone = node.cloneNode(deep);
(<Document>clone.ownerDocument) = this;
return clone;
}
/**
* Creates a range.
*
* @returns Range.
*/
public createRange(): Range {
return new Range();
}
/**
* Adopts a node.
*
* @param node Node to adopt.
* @returns Adopted node.
*/
public adoptNode(node: INode): INode {
if (!(node instanceof Node)) {
throw new DOMException('Parameter 1 was not of type Node.');
}
const adopted = node.parentNode ? node.parentNode.removeChild(node) : node;
(<Document>adopted.ownerDocument) = this;
return adopted;
}
/**
* Returns selection.
*
* @returns Selection.
*/
public getSelection(): Selection {
return new Selection();
}
/**
* Returns a boolean value indicating whether the document or any element inside the document has focus.
*
* @returns "true" if the document has focus.
*/
public hasFocus(): boolean {
return !!this.activeElement;
}
/**
* @override
*/
public dispatchEvent(event: Event): boolean {
const returnValue = super.dispatchEvent(event);
if (event.bubbles && !event._propagationStopped) {
return this.defaultView.dispatchEvent(event);
}
return returnValue;
}
}