-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
ShadowRoot.ts
84 lines (76 loc) · 1.96 KB
/
ShadowRoot.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
import DocumentFragment from '../document-fragment/DocumentFragment';
import XMLParser from '../../xml-parser/XMLParser';
import XMLSerializer from '../../xml-serializer/XMLSerializer';
import IElement from '../element/IElement';
import CSSStyleSheet from '../../css/CSSStyleSheet';
import IShadowRoot from './IShadowRoot';
import IHTMLElement from '../../nodes/html-element/IHTMLElement';
import Event from '../../event/Event';
/**
* ShadowRoot.
*/
export default class ShadowRoot extends DocumentFragment implements IShadowRoot {
public readonly mode = 'open';
public readonly host: IElement = null;
public adoptedStyleSheets: CSSStyleSheet[] = [];
// Events
public onslotchange: (event: Event) => void | null = null;
/**
* Returns inner HTML.
*
* @returns HTML.
*/
public get innerHTML(): string {
const xmlSerializer = new XMLSerializer();
let xml = '';
for (const node of this.childNodes) {
xml += xmlSerializer.serializeToString(node);
}
return xml;
}
/**
* Sets inner HTML.
*
* @param html HTML.
*/
public set innerHTML(html: string) {
for (const child of this.childNodes.slice()) {
this.removeChild(child);
}
for (const node of XMLParser.parse(this.ownerDocument, html).childNodes.slice()) {
this.appendChild(node);
}
}
/**
* Returns active element.
*
* @returns Active element.
*/
public get activeElement(): IHTMLElement {
const activeElement: IHTMLElement = this.ownerDocument['_activeElement'];
if (activeElement && activeElement.isConnected && activeElement.getRootNode() === this) {
return activeElement;
}
return null;
}
/**
* Converts to string.
*
* @returns String.
*/
public toString(): string {
return this.innerHTML;
}
/**
* Clones a node.
*
* @override
* @param [deep=false] "true" to clone deep.
* @returns Cloned node.
*/
public cloneNode(deep = false): IShadowRoot {
const clone = <ShadowRoot>super.cloneNode(deep);
(<string>clone.mode) = this.mode;
return clone;
}
}