-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathLocation.ts
339 lines (291 loc) · 9.7 KB
/
Location.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import type ConstructedStyleSheet from './ConstructedStyleSheet';
import {
addAdopterLocation,
getAdopterByLocation,
isCSSStyleSheetInstance,
isNonConstructedStyleSheetInstance,
removeAdopterLocation,
restyleAdopter,
} from './ConstructedStyleSheet';
import {defineProperty, forEach, hasShadyCss} from './shared';
import {
diff,
getShadowRoot,
isElementConnected,
removeNode,
unique,
} from './utils';
const defaultObserverOptions: MutationObserverInit = {
childList: true,
subtree: true,
};
const locations = new WeakMap<ShadowRoot | Document, Location>();
/**
* Searches for the location associated with the element. If no location found,
* creates a new one and adds it to the registry.
*/
export function getAssociatedLocation(
element: ShadowRoot | Document,
): Location {
let location = locations.get(element);
if (!location) {
location = new Location(element);
locations.set(element, location);
}
return location;
}
/**
* Adds an `adoptedStyleSheets` accessors for the received class prototype.
*/
export function attachAdoptedStyleSheetProperty(
constructor: typeof Document | typeof ShadowRoot,
) {
defineProperty(constructor.prototype, 'adoptedStyleSheets', {
configurable: true,
enumerable: true,
get(): readonly ConstructedStyleSheet[] {
return getAssociatedLocation(this).sheets;
},
set(sheets: readonly ConstructedStyleSheet[]) {
getAssociatedLocation(this).update(sheets);
},
});
}
/**
* Traverses through the node's subtree in order to find web components with
* Shadow DOM initialized and runs a `callback` for their ShadowRoot.
*/
function traverseWebComponents(
node: Node,
callback: (node: ShadowRoot) => void,
): void {
const iter = document.createNodeIterator(
node,
NodeFilter.SHOW_ELEMENT,
(foundNode: Element) =>
getShadowRoot(foundNode)
? NodeFilter.FILTER_ACCEPT
: NodeFilter.FILTER_REJECT,
// @ts-expect-error: IE createNodeIterator method accepts 5 args
null,
false,
);
for (let next: Node | null; (next = iter.nextNode()); ) {
// Type Note: we already checked for ShadowRoot above
callback(getShadowRoot(next as Element)!);
}
}
/*
* Private properties
*/
/**
* A root element (either ShadowRoot or Document) that has the
* `adoptedStyleSheets` property set.
*/
const $element = new WeakMap<Location, ShadowRoot | Document>();
/**
* A result of [...new Set(...this.sheets)] operation.
*/
const $uniqueSheets = new WeakMap<Location, readonly ConstructedStyleSheet[]>();
/**
* An observer that adds and restores `<style>` adopters to the location
* element. It also runs a `connect` for a location associated with the added
* element, and a `disconnect` function for a location of removed element.
*/
const $observer = new WeakMap<Location, MutationObserver>();
/*
* Private methods
*/
/**
* Checks if the element is an adopter that presents in the current set of
* constructed style sheets.
*/
function isExistingAdopter(self: Location, element: Node): boolean {
return (
element instanceof HTMLStyleElement &&
$uniqueSheets.get(self)!.some((sheet) => getAdopterByLocation(sheet, self))
);
}
/**
* Gets an element that serves as a container for `<style>` adopters. For
* Document, it is `document.body`, for ShadowRoot - the ShadowRoot itself.
*/
function getAdopterContainer(self: Location): Element | ShadowRoot {
const element = $element.get(self)!;
return element instanceof Document ? element.body : element;
}
/**
* Runs the adoption algorithm: re-adds all the `<style>` adopters to the
* location.
*/
function adopt(self: Location): void {
const styleList = document.createDocumentFragment();
const sheets = $uniqueSheets.get(self)!;
const observer = $observer.get(self)!;
const container = getAdopterContainer(self);
// Adding a `<style>` element to document fragment removes that element from
// the location, so we need to pause watching when it happens to avoid calling
// observer callback.
observer.disconnect();
sheets.forEach((sheet) => {
styleList.appendChild(
getAdopterByLocation(sheet, self) || addAdopterLocation(sheet, self),
);
});
// Inserting in the end of the location
container.insertBefore(styleList, null);
// Re-start the observation.
observer.observe(container, defaultObserverOptions);
// Now we have all the sheets of `<style>` elements available (because
// `adopt` is not supposed to run while location is disconnected).
sheets.forEach((sheet) => {
// Mote: we just defined adopter above.
restyleAdopter(sheet, getAdopterByLocation(sheet, self)!);
});
}
/**
* A wrapper for any element that is allowed to set the `adoptedStyleSheets`
* property. Watches its internal DOM for new elements; when they appear, it
* connects them by adding a `<style>` adopters that provides all the styles
* from the applied ConstructedStyleSheets instances.
*/
declare class Location {
/**
* A list of constructable style sheets added to the current location via
* `adoptedStyleSheets` property.
*/
public sheets: readonly ConstructedStyleSheet[];
constructor(element: Document | ShadowRoot);
/**
* The `connectedCallback` method for a location. Runs when the location is
* connected to the DOM.
*/
connect(): void;
/**
* The `disconnectedCallback` method for a location. Runs when the location is
* disconnected from the DOM.
*/
disconnect(): void;
/**
* Checks if the current location is connected to the DOM.
*/
isConnected(): boolean;
/**
* Called when the new set of constructed style sheets is set to the
* `adoptedStyleSheets` property of Document or ShadowRoot.
*/
update(sheets: readonly ConstructedStyleSheet[]): void;
}
function Location(this: Location, element: Document | ShadowRoot) {
const self = this;
self.sheets = [];
// Initialize private properties
$element.set(self, element);
$uniqueSheets.set(self, []);
$observer.set(
self,
new MutationObserver((mutations, observer) => {
// Workaround for
// https://github.com/calebdwilliams/construct-style-sheets/pull/63
if (!document) {
observer.disconnect();
return;
}
mutations.forEach((mutation) => {
if (!hasShadyCss) {
// When the new custom element is added to the observing location, we
// need to connect all web components. However, since any added node
// may contain custom elements deeply nested we need to explore the
// whole tree.
forEach.call(mutation.addedNodes, (node: Node) => {
if (!(node instanceof Element)) {
return;
}
traverseWebComponents(node, (root) => {
getAssociatedLocation(root).connect();
});
});
}
// When any `<style>` adopter is removed, we need to re-adopt all the
// styles because otherwise we can break the order of appended styles
// which affects the rules overriding.
forEach.call(mutation.removedNodes, (node: Node) => {
if (!(node instanceof Element)) {
return;
}
if (isExistingAdopter(self, node)) {
adopt(self);
}
// We have to stop observers for disconnected nodes.
if (!hasShadyCss) {
traverseWebComponents(node, (root) => {
getAssociatedLocation(root).disconnect();
});
}
});
});
}),
);
}
// @ts-expect-error: too generic for TypeScript
Location.prototype = {
isConnected() {
const element = $element.get(this)!;
return element instanceof Document
? element.readyState !== 'loading'
: isElementConnected(element.host);
},
connect() {
const container = getAdopterContainer(this);
$observer.get(this)!.observe(container, defaultObserverOptions);
if ($uniqueSheets.get(this)!.length > 0) {
adopt(this);
}
traverseWebComponents(container, (root) => {
getAssociatedLocation(root).connect();
});
},
disconnect() {
$observer.get(this)!.disconnect();
},
update(sheets: readonly ConstructedStyleSheet[]) {
const self = this;
const locationType =
$element.get(self) === document ? 'Document' : 'ShadowRoot';
if (!Array.isArray(sheets)) {
// document.adoptedStyleSheets = new CSSStyleSheet();
throw new TypeError(
`Failed to set the 'adoptedStyleSheets' property on ${locationType}: Iterator getter is not callable.`,
);
}
if (!sheets.every(isCSSStyleSheetInstance)) {
// document.adoptedStyleSheets = ['non-CSSStyleSheet value'];
throw new TypeError(
`Failed to set the 'adoptedStyleSheets' property on ${locationType}: Failed to convert value to 'CSSStyleSheet'`,
);
}
if (sheets.some(isNonConstructedStyleSheetInstance)) {
// document.adoptedStyleSheets = [document.styleSheets[0]];
throw new TypeError(
`Failed to set the 'adoptedStyleSheets' property on ${locationType}: Can't adopt non-constructed stylesheets`,
);
}
self.sheets = sheets;
const oldUniqueSheets = $uniqueSheets.get(self)!;
const uniqueSheets = unique(sheets);
// Style sheets that existed in the old sheet list but was excluded in the
// new one.
const removedSheets = diff(oldUniqueSheets, uniqueSheets);
removedSheets.forEach((sheet) => {
// Type Note: any removed sheet is already initialized, so there cannot be
// missing adopter for this location.
removeNode(getAdopterByLocation(sheet, self)!);
removeAdopterLocation(sheet, self);
});
$uniqueSheets.set(self, uniqueSheets);
if (self.isConnected() && uniqueSheets.length > 0) {
adopt(self);
}
},
};
export default Location;