-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
DocumentFragment.ts
241 lines (216 loc) · 6.5 KB
/
DocumentFragment.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
import Node from '../node/Node.js';
import * as PropertySymbol from '../../PropertySymbol.js';
import Element from '../element/Element.js';
import QuerySelector from '../../query-selector/QuerySelector.js';
import ParentNodeUtility from '../parent-node/ParentNodeUtility.js';
import HTMLCollection from '../element/HTMLCollection.js';
import ElementUtility from '../element/ElementUtility.js';
import NodeList from '../node/NodeList.js';
import NodeTypeEnum from '../node/NodeTypeEnum.js';
import IHTMLElementTagNameMap from '../../config/IHTMLElementTagNameMap.js';
import ISVGElementTagNameMap from '../../config/ISVGElementTagNameMap.js';
/**
* DocumentFragment.
*/
export default class DocumentFragment extends Node {
public readonly [PropertySymbol.children]: HTMLCollection<Element> = new HTMLCollection();
public [PropertySymbol.rootNode]: Node = this;
public [PropertySymbol.nodeType] = NodeTypeEnum.documentFragmentNode;
public cloneNode: (deep?: boolean) => DocumentFragment;
/**
* Returns the document fragment children.
*/
public get children(): HTMLCollection<Element> {
return this[PropertySymbol.children];
}
/**
* 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;
}
/**
* Get text value of children.
*
* @returns Text content.
*/
public get textContent(): string {
let result = '';
for (const childNode of this[PropertySymbol.childNodes]) {
if (
childNode[PropertySymbol.nodeType] === NodeTypeEnum.elementNode ||
childNode[PropertySymbol.nodeType] === NodeTypeEnum.textNode
) {
result += childNode.textContent;
}
}
return result;
}
/**
* Sets text content.
*
* @param textContent Text content.
*/
public set textContent(textContent: string) {
for (const child of this[PropertySymbol.childNodes].slice()) {
this.removeChild(child);
}
if (textContent) {
this.appendChild(this[PropertySymbol.ownerDocument].createTextNode(textContent));
}
}
/**
* 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 a matching element.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector<K extends keyof ISVGElementTagNameMap>(
selector: K
): ISVGElementTagNameMap[K] | null;
/**
* Query CSS Selector to find a matching element.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): Element | null;
/**
* Query CSS Selector to find a matching element.
*
* @param selector CSS selector.
* @returns Matching element.
*/
public querySelector(selector: string): Element | null {
return QuerySelector.querySelector(this, selector);
}
/**
* Returns an element by ID.
*
* @param id ID.
* @returns Matching element.
*/
public getElementById(id: string): Element {
return ParentNodeUtility.getElementById(this, id);
}
/**
* @override
*/
public override [PropertySymbol.cloneNode](deep = false): DocumentFragment {
const clone = <DocumentFragment>super[PropertySymbol.cloneNode](deep);
if (deep) {
for (const node of clone[PropertySymbol.childNodes]) {
if (node[PropertySymbol.nodeType] === NodeTypeEnum.elementNode) {
clone[PropertySymbol.children].push(<Element>node);
}
}
}
return <DocumentFragment>clone;
}
/**
* @override
*/
public override [PropertySymbol.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 [PropertySymbol.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 [PropertySymbol.insertBefore](newNode: Node, referenceNode: Node | null): 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.insertBefore(this, newNode, referenceNode);
}
}