/
node-list.ts
223 lines (189 loc) · 5.56 KB
/
node-list.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
import { Node } from "./node.ts";
import { HTMLCollection } from "./html-collection.ts";
const NodeListFakeClass: any = (() => {
return class NodeList {
constructor() {
throw new TypeError("Illegal constructor");
}
static [Symbol.hasInstance](value: any) {
return value.constructor === NodeListClass;
}
};
})();
export const nodeListMutatorSym = Symbol();
const nodeListCachedMutator = Symbol();
// Array methods that we need for NodeList mutator implementation
const { push, splice, slice, indexOf, filter } = Array.prototype;
// Implementation of a NodeList mutator
class NodeListMutatorImpl {
// There should only ever be one elementView per element. Element views
// are basically just the source of HTMLCollections/.children properties
// on elements that are always in sync with their .childNodes counterpart.
elementViews: any[][] = [];
constructor(
public arrayInstance: any[],
) {}
push(...items: any[]) {
// Copy the new items to the element view (if any)
for (const view of this.elementViews) {
for (const item of items) {
if (item.nodeType === Node.ELEMENT_NODE) {
push.call(view, item);
}
}
}
return push.call(this.arrayInstance, ...items);
}
splice(index: number, deleteCount = 0, ...items: any[]) {
// Delete and insert new elements in an element view (if any)
for (const view of this.elementViews) {
const toDelete = filter.call(
slice.call(this.arrayInstance, index, index + deleteCount),
(item) => item.nodeType === Node.ELEMENT_NODE,
);
const toInsert = items.filter((item) =>
item.nodeType === Node.ELEMENT_NODE
);
// Find where to start splicing in the element view
let elementViewSpliceIndex = -1;
for (let idx = index; idx < this.arrayInstance.length; idx++) {
const item = this.arrayInstance[idx];
if (item.nodeType === Node.ELEMENT_NODE) {
elementViewSpliceIndex = indexOf.call(view, item);
break;
}
}
// If no element is found just do everything at the end
// of the view
if (elementViewSpliceIndex === -1) {
elementViewSpliceIndex = view.length;
}
if (toDelete.length) {
splice.call(view, elementViewSpliceIndex, toDelete.length);
}
// Finally, insert all the found elements
splice.call(view, elementViewSpliceIndex, 0, ...toInsert);
}
return splice.call(this.arrayInstance, index, deleteCount, ...items);
}
indexOf(item: any, fromIndex = 0) {
return indexOf.call(this.arrayInstance, item, fromIndex);
}
indexOfElementsView(item: any, fromIndex = 0) {
return indexOf.call(this.elementsView(), item, fromIndex);
}
// Return the elements-only view for this NodeList. Creates one if
// it doesn't already exist.
elementsView() {
let view = this.elementViews[0];
if (!view) {
view = new HTMLCollection() as any as any[];
this.elementViews.push(view);
push.call(
view,
...filter.call(
this.arrayInstance,
(item) => item.nodeType === Node.ELEMENT_NODE,
),
);
}
return view;
}
}
// We define the `NodeList` inside a closure to ensure that its
// `.name === "NodeList"` property stays intact, as we need to manipulate
// its prototype and completely change its TypeScript-recognized type.
const NodeListClass: any = (() => {
// @ts-ignore
class NodeList extends Array<Node> {
// @ts-ignore
forEach(
cb: (node: Node, index: number, nodeList: Node[]) => void,
thisArg: NodeList | undefined = undefined,
) {
super.forEach(cb, thisArg);
}
item(index: number): Node | null {
return this[index] ?? null;
}
[nodeListMutatorSym]() {
const cachedMutator = (this as any)[nodeListCachedMutator];
if (cachedMutator) {
return cachedMutator;
} else {
const cachedMutator = new NodeListMutatorImpl(this);
(this as any)[nodeListCachedMutator] = cachedMutator;
return cachedMutator;
}
}
toString() {
return "[object NodeList]";
}
}
return NodeList;
})();
for (
const staticMethod of [
"from",
"isArray",
"of",
]
) {
NodeListClass[staticMethod] = undefined;
}
for (
const instanceMethod of [
"concat",
"copyWithin",
"every",
"fill",
"filter",
"find",
"findIndex",
"flat",
"flatMap",
"includes",
"indexOf",
"join",
"lastIndexOf",
"map",
"pop",
"push",
"reduce",
"reduceRight",
"reverse",
"shift",
"slice",
"some",
"sort",
"splice",
"toLocaleString",
"unshift",
]
) {
NodeListClass.prototype[instanceMethod] = undefined;
}
export interface NodeList {
new (): NodeList;
readonly [index: number]: Node;
readonly length: number;
[Symbol.iterator](): Generator<Node>;
item(index: number): Node;
forEach(
cb: (node: Node, index: number, nodeList: Node[]) => void,
thisArg?: NodeList | undefined,
): void;
[nodeListMutatorSym](): NodeListMutator;
}
export interface NodeListPublic extends NodeList {
[nodeListMutatorSym]: never;
}
export interface NodeListMutator {
push(...nodes: Node[]): number;
splice(start: number, deleteCount?: number, ...items: Node[]): Node[];
indexOf(node: Node, fromIndex?: number | undefined): number;
indexOfElementsView(node: Node, fromIndex?: number | undefined): number;
elementsView(): HTMLCollection;
}
export const NodeList = <NodeList> NodeListClass;
export const NodeListPublic = <NodeListPublic> NodeListFakeClass;