forked from ProjectEvergreen/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom-shim.js
285 lines (238 loc) · 8.26 KB
/
dom-shim.js
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
/* eslint-disable no-warning-comments */
import { parseFragment, serialize } from 'parse5';
// TODO Should go into utils file?
function isShadowRoot(element) {
return Object.getPrototypeOf(element).constructor.name === 'ShadowRoot';
}
// Deep clone for cloneNode(deep) - TODO should this go into a utils file?
// structuredClone doesn't work with functions. TODO This works with
// all current tests but would it be worth considering a lightweight
// library here to better cover edge cases?
function deepClone(obj, map = new WeakMap()) {
if (obj === null || typeof obj !== 'object') {
return obj; // Return primitives or functions as-is
}
if (typeof obj === 'function') {
const clonedFn = obj.bind({});
Object.assign(clonedFn, obj);
return clonedFn;
}
if (map.has(obj)) {
return map.get(obj);
}
const result = Array.isArray(obj) ? [] : {};
map.set(obj, result);
for (const key of Object.keys(obj)) {
result[key] = deepClone(obj[key], map);
}
return result;
}
// Creates an empty parse5 element without the parse5 overhead. Results in 2-10x better performance.
// TODO Should this go into a utils files?
function getParse5ElementDefaults(element, tagName) {
return {
addEventListener: noop,
attrs: [],
parentNode: element.parentNode,
childNodes: [],
nodeName: tagName,
tagName: tagName,
namespaceURI: 'http://www.w3.org/1999/xhtml',
// eslint-disable-next-line no-extra-parens
...(tagName === 'template' ? { content: { nodeName: '#document-fragment', childNodes: [] } } : {})
};
}
function noop() { }
// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/CSSStyleSheet
class CSSStyleSheet {
insertRule() { }
deleteRule() { }
replace() { }
replaceSync() { }
}
// https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
class EventTarget {
constructor() {
this.addEventListener = noop;
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/Node
// EventTarget <- Node
// TODO should be an interface?
class Node extends EventTarget {
constructor() {
super();
// Parse5 properties
this.attrs = [];
this.parentNode = null;
this.childNodes = [];
}
cloneNode(deep) {
return deep ? deepClone(this) : Object.assign({}, this);
}
appendChild(node) {
const childNodes = (this.nodeName === 'template' ? this.content : this).childNodes;
if (node.parentNode) {
node.parentNode?.removeChild?.(node); // Remove from current parent
}
if (node.nodeName === 'template') {
if (isShadowRoot(this) && this.mode) {
node.attrs = [{ name: 'shadowrootmode', value: this.mode }];
childNodes.push(node);
node.parentNode = this;
} else {
this.childNodes = [...this.childNodes, ...node.content.childNodes];
}
} else {
childNodes.push(node);
node.parentNode = this;
}
return node;
}
removeChild(node) {
const childNodes = (this.nodeName === 'template' ? this.content : this).childNodes;
if (!childNodes || !childNodes.length) {
return null;
}
const index = childNodes.indexOf(node);
if (index === -1) {
return null;
}
childNodes.splice(index, 1);
node.parentNode = null;
return node;
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/Element
// EventTarget <- Node <- Element
class Element extends Node {
constructor() {
super();
}
attachShadow(options) {
this.shadowRoot = new ShadowRoot(options);
this.shadowRoot.parentNode = this;
return this.shadowRoot;
}
set innerHTML(html) {
(this.nodeName === 'template' ? this.content : this).childNodes = parseFragment(html).childNodes; // Replace content's child nodes
}
// Serialize the content of the DocumentFragment when getting innerHTML
get innerHTML() {
const childNodes = (this.nodeName === 'template' ? this.content : this).childNodes;
return childNodes ? serialize({ childNodes }) : '';
}
setAttribute(name, value) {
// Modified attribute handling to work with parse5
const attr = this.attrs?.find((attr) => attr.name === name);
if (attr) {
attr.value = value;
} else {
this.attrs?.push({ name, value });
}
}
getAttribute(name) {
// Modified attribute handling to work with parse5
const attr = this.attrs.find((attr) => attr.name === name);
return attr ? attr.value : null;
}
hasAttribute(name) {
// Modified attribute handling to work with parse5
return this.attrs.some((attr) => attr.name === name);
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/Document
// EventTarget <- Node <- Document
class Document extends Node {
createElement(tagName) {
switch (tagName) {
case 'template':
return new HTMLTemplateElement();
default:
return new HTMLElement(tagName);
}
}
createDocumentFragment(html) {
return new DocumentFragment(html);
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement
// EventTarget <- Node <- Element <- HTMLElement
class HTMLElement extends Element {
constructor(tagName) {
super();
Object.assign(this, getParse5ElementDefaults(this, tagName));
}
connectedCallback() { }
}
// https://developer.mozilla.org/en-US/docs/Web/API/DocumentFragment
// EventTarget <- Node <- DocumentFragment
class DocumentFragment extends Node { }
// https://developer.mozilla.org/en-US/docs/Web/API/ShadowRoot
// EventTarget <- Node <- DocumentFragment <- ShadowRoot
class ShadowRoot extends DocumentFragment {
constructor(options) {
super();
this.mode = options.mode || 'closed';
this.adoptedStyleSheets = [];
}
get innerHTML() {
return this.childNodes ? serialize({ childNodes: this.childNodes }) : '';
}
set innerHTML(html) {
// Replaces auto wrapping functionality that was previously done
// in HTMLTemplateElement. This allows parse5 to add declarative
// shadow roots when necessary. To pass tests that wrap innerHTML
// in a template, we only wrap when if a template isn't found at the
// start of the html string (this can be removed if those tests are
// changed)
html = html.trim().toLowerCase().startsWith('<template')
? html
: `<template shadowrootmode="${this.mode}">${html}</template>`;
this.childNodes = parseFragment(html).childNodes;
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLTemplateElement
// EventTarget <- Node <- Element <- HTMLElement <- HTMLTemplateElement
class HTMLTemplateElement extends HTMLElement {
constructor() {
super();
// Gets element defaults for template element instead of parsing a
// <template></template> with parse5. Results in 2-5x better performance
// when creating templates
Object.assign(this, getParse5ElementDefaults(this, 'template'));
this.content.cloneNode = this.cloneNode.bind(this);
}
}
// https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry
class CustomElementsRegistry {
constructor() {
// TODO this should probably be a set or otherwise follow the spec?
// https://github.com/ProjectEvergreen/wcc/discussions/145
this.customElementsRegistry = new Map();
}
define(tagName, BaseClass) {
// TODO Should we throw an error here when a tagName is already defined?
// Would require altering tests
// if (this.customElementsRegistry.has(tagName)) {
// throw new Error(
// `Custom element with tag name ${tagName} is already defined.`
// );
// }
// TODO this should probably fail as per the spec...
// e.g. if(this.customElementsRegistry.get(tagName))
// https://github.com/ProjectEvergreen/wcc/discussions/145
this.customElementsRegistry.set(tagName, BaseClass);
}
get(tagName) {
return this.customElementsRegistry.get(tagName);
}
}
// mock top level aliases (globalThis === window)
// https://developer.mozilla.org/en-US/docs/Web/API/Window
// make this "idempotent" for now until a better idea comes along - https://github.com/ProjectEvergreen/wcc/discussions/145
globalThis.addEventListener = globalThis.addEventListener ?? noop;
globalThis.document = globalThis.document ?? new Document();
globalThis.customElements = globalThis.customElements ?? new CustomElementsRegistry();
globalThis.HTMLElement = globalThis.HTMLElement ?? HTMLElement;
globalThis.CSSStyleSheet = globalThis.CSSStyleSheet ?? CSSStyleSheet;