-
Notifications
You must be signed in to change notification settings - Fork 382
/
static-html-collection.ts
151 lines (144 loc) · 4.3 KB
/
static-html-collection.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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { ArrayMap, create, defineProperty, fields, forEach, setPrototypeOf } from '@lwc/shared';
const { createFieldName, getHiddenField, setHiddenField } = fields;
const Items = createFieldName('items');
function isValidHTMLCollectionName(name) {
return name !== 'length' && isNaN(name);
}
function getNodeHTMLCollectionName(node) {
return node.getAttribute('id') || node.getAttribute('name');
}
function StaticHTMLCollection() {
throw new TypeError('Illegal constructor');
}
StaticHTMLCollection.prototype = create(HTMLCollection.prototype, {
constructor: {
writable: true,
configurable: true,
value: StaticHTMLCollection,
},
item: {
writable: true,
enumerable: true,
configurable: true,
value(index: number) {
return this[index];
},
},
length: {
enumerable: true,
configurable: true,
get() {
return getHiddenField(this, Items).length;
},
},
// https://dom.spec.whatwg.org/#dom-htmlcollection-nameditem-key
namedItem: {
writable: true,
enumerable: true,
configurable: true,
value(name: string) {
if (isValidHTMLCollectionName(name) && this[name]) {
return this[name];
}
const items = getHiddenField(this, Items);
// Note: loop in reverse so that the first named item matches the named property
for (let len = items.length - 1; len >= 0; len -= 1) {
const item = items[len];
const nodeName = getNodeHTMLCollectionName(item);
if (nodeName === name) {
return item;
}
}
return null;
},
},
// Iterator protocol
forEach: {
writable: true,
enumerable: true,
configurable: true,
value(cb, thisArg) {
forEach.call(getHiddenField(this, Items), cb, thisArg);
},
},
entries: {
writable: true,
enumerable: true,
configurable: true,
value() {
return ArrayMap.call(getHiddenField(this, Items), (v: any, i: number) => [i, v]);
},
},
keys: {
writable: true,
enumerable: true,
configurable: true,
value() {
return ArrayMap.call(getHiddenField(this, Items), (v: any, i: number) => i);
},
},
values: {
writable: true,
enumerable: true,
configurable: true,
value() {
return getHiddenField(this, Items);
},
},
[Symbol.iterator]: {
writable: true,
configurable: true,
value() {
let nextIndex = 0;
return {
next: () => {
const items = getHiddenField(this, Items);
return nextIndex < items.length
? {
value: items[nextIndex++],
done: false,
}
: {
done: true,
};
},
};
},
},
[Symbol.toStringTag]: {
configurable: true,
get() {
return 'HTMLCollection';
},
},
// IE11 doesn't support Symbol.toStringTag, in which case we
// provide the regular toString method.
toString: {
writable: true,
configurable: true,
value() {
return '[object HTMLCollection]';
},
},
});
// prototype inheritance dance
setPrototypeOf(StaticHTMLCollection, HTMLCollection);
export function createStaticHTMLCollection<T extends Element>(items: T[]): HTMLCollectionOf<T> {
const collection: HTMLCollectionOf<T> = create(StaticHTMLCollection.prototype);
setHiddenField(collection, Items, items);
// setting static indexes
forEach.call(items, (item: T, index: number) => {
defineProperty(collection, index, {
value: item,
enumerable: true,
configurable: true,
});
});
return collection;
}