@master/css-runtime 2.0.0-rc.7 → 2.0.0-rc.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -8
- package/dist/class-tracker.d.ts +12 -0
- package/dist/core.d.ts +61 -14
- package/dist/css-runtime.d.ts +17 -0
- package/dist/debuggers/class-count.d.ts +1 -0
- package/dist/debuggers/index.d.ts +1 -0
- package/dist/debuggers/lifecycle.d.ts +1 -0
- package/dist/debuggers/log.d.ts +17 -0
- package/dist/default-manifest.json +1 -0
- package/dist/generated-rule.d.ts +26 -0
- package/dist/global.min.js +1 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.development.d.ts +1 -0
- package/dist/index.development.mjs +1316 -0
- package/dist/index.mjs +1183 -0
- package/dist/layer.d.ts +16 -0
- package/dist/native-declaration.d.ts +2 -0
- package/dist/register-global.d.ts +6 -0
- package/dist/theme-layer.d.ts +27 -0
- package/dist/types/hydrate-result.d.ts +5 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/utility-layer.d.ts +13 -0
- package/dist/utils/find-native-css-rule-index.d.ts +1 -0
- package/package.json +1 -1
- package/dist/iife.bundle.js +0 -1
- package/dist/index.bundle.js +0 -1
- package/dist/index.bundle.mjs +0 -1
- package/dist/init-css-runtime.d.ts +0 -9
- package/dist/types/global.d.ts +0 -8
- /package/dist/{iife.d.ts → global.min.d.ts} +0 -0
|
@@ -0,0 +1,1316 @@
|
|
|
1
|
+
import { Layer, AnimationRule, VariableRule, compareRulePriority, ThemeLayer, MasterCSS } from '@master/css-engine';
|
|
2
|
+
import { MASTER_CSS_HYDRATION_MANIFEST_ATTR, MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID } from '@master/css-schema/hydration-manifest';
|
|
3
|
+
import { MASTER_CSS_RUNTIME_STYLE_ID } from '@master/css-schema/runtime-style';
|
|
4
|
+
import { installHook } from '@master/css-devtools-hook';
|
|
5
|
+
|
|
6
|
+
const createLogger = (type)=>{
|
|
7
|
+
return (message, ...args)=>{
|
|
8
|
+
if (!type) return console.log(message, ...args);
|
|
9
|
+
if (console[type]) {
|
|
10
|
+
console[type](`%c[Master CSS]%c ${message}`, 'color: gray;', 'color: inherit;', ...args);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
const loggers = {
|
|
15
|
+
info: createLogger('info'),
|
|
16
|
+
debug: createLogger('debug'),
|
|
17
|
+
warn: console.warn,
|
|
18
|
+
error: console.error,
|
|
19
|
+
trace: console.trace,
|
|
20
|
+
table: console.table,
|
|
21
|
+
group: console.group,
|
|
22
|
+
groupCollapsed: console.group,
|
|
23
|
+
groupEnd: console.groupEnd,
|
|
24
|
+
time: console.time,
|
|
25
|
+
timeEnd: console.timeEnd
|
|
26
|
+
};
|
|
27
|
+
const log = createLogger();
|
|
28
|
+
Object.assign(log, loggers);
|
|
29
|
+
|
|
30
|
+
function registerClassCountDebugger() {
|
|
31
|
+
const hook = globalThis.__MASTER_CSS_DEVTOOLS_HOOK__;
|
|
32
|
+
hook.on('runtime:mutated', ({ records, classCounts, cssRuntime })=>{
|
|
33
|
+
const actualClassCounts = {};
|
|
34
|
+
let errored = false;
|
|
35
|
+
const resolveClass = (className)=>{
|
|
36
|
+
if (Object.prototype.hasOwnProperty.call(actualClassCounts, className)) {
|
|
37
|
+
actualClassCounts[className]++;
|
|
38
|
+
} else {
|
|
39
|
+
actualClassCounts[className] = 1;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
(cssRuntime.root.constructor.name === 'HTMLDocument' ? cssRuntime.host : cssRuntime.container).querySelectorAll('[class]').forEach((element)=>{
|
|
43
|
+
element.classList.forEach(resolveClass);
|
|
44
|
+
});
|
|
45
|
+
cssRuntime.host.classList.forEach(resolveClass);
|
|
46
|
+
for(const className in actualClassCounts){
|
|
47
|
+
const eachCount = cssRuntime.classCounts.get(className);
|
|
48
|
+
const eachActualCount = actualClassCounts[className];
|
|
49
|
+
if (eachCount !== eachActualCount) {
|
|
50
|
+
log.error(`Class count mismatch for \`${className}\` (expected ${eachActualCount}) (received ${eachCount})`);
|
|
51
|
+
errored = true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
cssRuntime.classCounts.forEach((eachCount, className)=>{
|
|
55
|
+
if (!Object.prototype.hasOwnProperty.call(actualClassCounts, className)) {
|
|
56
|
+
log.error(`Class count mismatch for \`${className}\` (expected ${0}) (received ${eachCount})`);
|
|
57
|
+
errored = true;
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
if (errored) {
|
|
61
|
+
log.debug('Records:', records);
|
|
62
|
+
log.debug('Serialized Records:', serializeMutations(records));
|
|
63
|
+
log.debug('Counts:', classCounts);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function serializeMutations(mutations) {
|
|
68
|
+
return mutations.map((m)=>{
|
|
69
|
+
const target = m.target;
|
|
70
|
+
return {
|
|
71
|
+
type: m.type,
|
|
72
|
+
attributeName: m.attributeName,
|
|
73
|
+
oldValue: m.oldValue,
|
|
74
|
+
target: {
|
|
75
|
+
tagName: target.tagName,
|
|
76
|
+
id: target.id,
|
|
77
|
+
classList: Array.from(target.classList)
|
|
78
|
+
},
|
|
79
|
+
added: Array.from(m.addedNodes).filter((n)=>n.nodeType === 1).map((el)=>({
|
|
80
|
+
tagName: el.tagName,
|
|
81
|
+
id: el.id,
|
|
82
|
+
classList: Array.from(el.classList)
|
|
83
|
+
})),
|
|
84
|
+
removed: Array.from(m.removedNodes).filter((n)=>n.nodeType === 1).map((el)=>({
|
|
85
|
+
tagName: el.tagName,
|
|
86
|
+
id: el.id,
|
|
87
|
+
classList: Array.from(el.classList)
|
|
88
|
+
}))
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function registerLifecycleDebugger() {
|
|
94
|
+
const hook = globalThis.__MASTER_CSS_DEVTOOLS_HOOK__;
|
|
95
|
+
hook.on('runtime:created', ({ cssRuntime })=>{
|
|
96
|
+
log.debug('created', cssRuntime);
|
|
97
|
+
});
|
|
98
|
+
hook.on('runtime:hydrated', ({ cssRuntime })=>{
|
|
99
|
+
log.debug('hydrated', cssRuntime);
|
|
100
|
+
});
|
|
101
|
+
hook.on('runtime:observed', ({ cssRuntime })=>{
|
|
102
|
+
log.debug('observed', cssRuntime);
|
|
103
|
+
if (cssRuntime.progressive) {
|
|
104
|
+
log.info('Progressive rendering is adopted.');
|
|
105
|
+
log.debug('pre-rendered CSS', cssRuntime.style);
|
|
106
|
+
} else {
|
|
107
|
+
log.info('Runtime rendering is adopted.');
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
hook.on('runtime:refreshed', ({ cssRuntime, manifest })=>{
|
|
111
|
+
log.debug('refreshed', cssRuntime, manifest);
|
|
112
|
+
});
|
|
113
|
+
hook.on('runtime:disconnected', ({ cssRuntime })=>{
|
|
114
|
+
log.debug('disconnected', cssRuntime);
|
|
115
|
+
});
|
|
116
|
+
hook.on('runtime:destroyed', ({ cssRuntime })=>{
|
|
117
|
+
log.debug('destroyed', cssRuntime);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function startDebuggers() {
|
|
122
|
+
log.info('Debug mode is enabled by default in development.');
|
|
123
|
+
registerClassCountDebugger();
|
|
124
|
+
registerLifecycleDebugger();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function registerGlobal(CSSRuntime) {
|
|
128
|
+
if (!globalThis.MasterCSSRuntime) globalThis.MasterCSSRuntime = CSSRuntime;
|
|
129
|
+
installHook();
|
|
130
|
+
{
|
|
131
|
+
startDebuggers();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function findNativeCSSRuleIndex(cssRules, nativeRule) {
|
|
136
|
+
for(let i = 0; i < cssRules.length; i++){
|
|
137
|
+
if (cssRules[i] === nativeRule) {
|
|
138
|
+
return i;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return -1;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function getRuleNodes(rule) {
|
|
145
|
+
const nodes = rule.nodes;
|
|
146
|
+
return Array.isArray(nodes) ? nodes : undefined;
|
|
147
|
+
}
|
|
148
|
+
class RuntimeLayer extends Layer {
|
|
149
|
+
name;
|
|
150
|
+
cssRuntime;
|
|
151
|
+
rules = [];
|
|
152
|
+
tokenCounts = new Map();
|
|
153
|
+
native = null;
|
|
154
|
+
constructor(name, cssRuntime){
|
|
155
|
+
super(name, cssRuntime), this.name = name, this.cssRuntime = cssRuntime;
|
|
156
|
+
}
|
|
157
|
+
attach() {
|
|
158
|
+
super.attach();
|
|
159
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
160
|
+
if (nativeSheet && !this.native?.parentStyleSheet) {
|
|
161
|
+
const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
|
|
162
|
+
this.native = nativeSheet.cssRules.item(insertedIndex);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
insert(rule, index = this.rules.length) {
|
|
166
|
+
const hadNative = !!this.native?.parentStyleSheet;
|
|
167
|
+
const wasEmpty = this.rules.length === 0;
|
|
168
|
+
const insertedIndex = super.insert(rule, index);
|
|
169
|
+
if (insertedIndex === undefined || !this.native) return;
|
|
170
|
+
const nativeInsertIndex = this.getNativeInsertIndex(insertedIndex);
|
|
171
|
+
const wasAttachedWithRule = !hadNative && wasEmpty;
|
|
172
|
+
const insertRuleSafely = (text, position)=>{
|
|
173
|
+
// Checks if the rule is inserted in a native CSS rule with this.attach()
|
|
174
|
+
if (wasAttachedWithRule) {
|
|
175
|
+
return this.native.cssRules.item(position);
|
|
176
|
+
} else {
|
|
177
|
+
try {
|
|
178
|
+
const insertedIndex = this.native.insertRule(text, position);
|
|
179
|
+
return this.native.cssRules.item(insertedIndex);
|
|
180
|
+
} catch (error) {
|
|
181
|
+
console.error(error, rule);
|
|
182
|
+
/**
|
|
183
|
+
* If the rule is invalid, remove it from the rules array.
|
|
184
|
+
* It's important to note that the rule may break the entire CSS runtime,
|
|
185
|
+
*/ this.rules.splice(insertedIndex, 1);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
const nodes = getRuleNodes(rule);
|
|
191
|
+
if (nodes) {
|
|
192
|
+
let currentIndex = nativeInsertIndex;
|
|
193
|
+
nodes.forEach((node)=>{
|
|
194
|
+
node.native = insertRuleSafely(node.text, currentIndex);
|
|
195
|
+
if (node.native) currentIndex++;
|
|
196
|
+
});
|
|
197
|
+
} else {
|
|
198
|
+
rule.native = insertRuleSafely(rule.text, nativeInsertIndex);
|
|
199
|
+
}
|
|
200
|
+
return insertedIndex;
|
|
201
|
+
}
|
|
202
|
+
getNativeInsertIndex(index) {
|
|
203
|
+
let nativeIndex = 0;
|
|
204
|
+
for(let i = 0; i < index; i++){
|
|
205
|
+
const rule = this.rules[i];
|
|
206
|
+
nativeIndex += getRuleNodes(rule)?.length ?? 1;
|
|
207
|
+
}
|
|
208
|
+
return nativeIndex;
|
|
209
|
+
}
|
|
210
|
+
detach() {
|
|
211
|
+
super.detach();
|
|
212
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
213
|
+
if (nativeSheet && this.native?.parentStyleSheet) {
|
|
214
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
215
|
+
if (foundIndex !== -1) {
|
|
216
|
+
nativeSheet.deleteRule(foundIndex);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
delete(key) {
|
|
221
|
+
const deletedRule = super.delete(key);
|
|
222
|
+
if (!deletedRule || !this.native) return;
|
|
223
|
+
const deleteRuleSafely = (rule)=>{
|
|
224
|
+
if (!rule) return;
|
|
225
|
+
const foundIndex = findNativeCSSRuleIndex(this.native.cssRules, rule);
|
|
226
|
+
if (foundIndex !== -1) {
|
|
227
|
+
try {
|
|
228
|
+
this.native.deleteRule(foundIndex);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error(error, rule);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
const nodes = getRuleNodes(deletedRule);
|
|
235
|
+
if (nodes) {
|
|
236
|
+
nodes.forEach((node)=>deleteRuleSafely(node.native));
|
|
237
|
+
} else {
|
|
238
|
+
deleteRuleSafely(deletedRule.native);
|
|
239
|
+
}
|
|
240
|
+
return deletedRule;
|
|
241
|
+
}
|
|
242
|
+
reset() {
|
|
243
|
+
super.reset();
|
|
244
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
245
|
+
if (this.native && nativeSheet) {
|
|
246
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
247
|
+
if (foundIndex !== -1) {
|
|
248
|
+
nativeSheet.deleteRule(foundIndex);
|
|
249
|
+
}
|
|
250
|
+
this.native = null;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function findUtilityInsertIndex(rules, utility) {
|
|
256
|
+
let low = 0;
|
|
257
|
+
let high = rules.length;
|
|
258
|
+
while(low < high){
|
|
259
|
+
const mid = low + high >> 1;
|
|
260
|
+
const rule = rules[mid];
|
|
261
|
+
if (!('priority' in rule)) {
|
|
262
|
+
return findUtilityInsertIndexLinear(rules, utility);
|
|
263
|
+
}
|
|
264
|
+
if (compareRulePriority(utility, rule) < 0) {
|
|
265
|
+
high = mid;
|
|
266
|
+
} else {
|
|
267
|
+
low = mid + 1;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return low;
|
|
271
|
+
}
|
|
272
|
+
function findUtilityInsertIndexLinear(rules, utility) {
|
|
273
|
+
for(let i = 0; i < rules.length; i++){
|
|
274
|
+
const rule = rules[i];
|
|
275
|
+
if (!('priority' in rule)) continue;
|
|
276
|
+
if (compareRulePriority(utility, rule) < 0) {
|
|
277
|
+
return i;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return rules.length;
|
|
281
|
+
}
|
|
282
|
+
class RuntimeUtilityLayer extends RuntimeLayer {
|
|
283
|
+
rules = [];
|
|
284
|
+
insert(utility) {
|
|
285
|
+
if (this.get(utility.key)) return;
|
|
286
|
+
if ('valid' in utility && !utility.valid) return;
|
|
287
|
+
let index = this.rules.length;
|
|
288
|
+
if ('priority' in utility) {
|
|
289
|
+
index = findUtilityInsertIndex(this.rules, utility);
|
|
290
|
+
}
|
|
291
|
+
const insertedIndex = super.insert(utility, index);
|
|
292
|
+
if (insertedIndex === undefined) return;
|
|
293
|
+
this.insertVariables(utility);
|
|
294
|
+
this.insertAnimations(utility);
|
|
295
|
+
return insertedIndex;
|
|
296
|
+
}
|
|
297
|
+
delete(key) {
|
|
298
|
+
const utility = super.delete(key);
|
|
299
|
+
if (!utility) return;
|
|
300
|
+
const deleteLayerToken = (layerToken, layer, visited = new Set())=>{
|
|
301
|
+
if (visited.has(layerToken)) return;
|
|
302
|
+
visited.add(layerToken);
|
|
303
|
+
const count = layer.tokenCounts.get(layerToken) ?? 0;
|
|
304
|
+
const variable = layer === this.css.themeLayer ? this.css.variables.get(layerToken) : undefined;
|
|
305
|
+
if (count <= 1) {
|
|
306
|
+
const deletedRule = layer.delete(layerToken);
|
|
307
|
+
layer.tokenCounts.delete(layerToken);
|
|
308
|
+
variable?.dependencies?.forEach((dependency)=>deleteLayerToken(dependency, layer, visited));
|
|
309
|
+
return deletedRule;
|
|
310
|
+
} else {
|
|
311
|
+
layer.tokenCounts.set(layerToken, count - 1);
|
|
312
|
+
variable?.dependencies?.forEach((dependency)=>deleteLayerToken(dependency, layer, visited));
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
if ('variableNames' in utility) utility.variableNames?.forEach((eachVariableName)=>{
|
|
316
|
+
deleteLayerToken(eachVariableName, this.css.themeLayer);
|
|
317
|
+
});
|
|
318
|
+
if ('animationNames' in utility) utility.animationNames?.forEach((eachAnimationName)=>{
|
|
319
|
+
const deletedAnimationRule = deleteLayerToken(eachAnimationName, this.css.animationsNonLayer);
|
|
320
|
+
const variableNames = deletedAnimationRule && 'variableNames' in deletedAnimationRule ? deletedAnimationRule.variableNames : undefined;
|
|
321
|
+
if (variableNames) {
|
|
322
|
+
variableNames.forEach((eachVariableName)=>{
|
|
323
|
+
deleteLayerToken(eachVariableName, this.css.themeLayer);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
return utility;
|
|
328
|
+
}
|
|
329
|
+
insertVariables(utility) {
|
|
330
|
+
if (!('variableNames' in utility)) return;
|
|
331
|
+
const insertVariable = (eachVariableName, visited = new Set())=>{
|
|
332
|
+
if (visited.has(eachVariableName)) return;
|
|
333
|
+
visited.add(eachVariableName);
|
|
334
|
+
const variable = this.css.variables.get(eachVariableName);
|
|
335
|
+
if (!variable || variable.inline) return;
|
|
336
|
+
if (this.css.themeLayer.rules.find(({ name })=>name === eachVariableName) || this.css.isEmittedGlobalsVariable(eachVariableName)) {
|
|
337
|
+
const count = this.css.themeLayer.tokenCounts.get(eachVariableName) || 0;
|
|
338
|
+
this.css.themeLayer.tokenCounts.set(eachVariableName, count + 1);
|
|
339
|
+
} else {
|
|
340
|
+
const newRule = new VariableRule(eachVariableName, variable, this.css);
|
|
341
|
+
this.css.themeLayer.insert(newRule);
|
|
342
|
+
this.css.themeLayer.tokenCounts.set(eachVariableName, 1);
|
|
343
|
+
}
|
|
344
|
+
variable.dependencies?.forEach((dependency)=>insertVariable(dependency, visited));
|
|
345
|
+
};
|
|
346
|
+
utility.variableNames?.forEach((eachVariableName)=>insertVariable(eachVariableName));
|
|
347
|
+
}
|
|
348
|
+
insertAnimations(utility) {
|
|
349
|
+
if (!('animationNames' in utility)) return;
|
|
350
|
+
utility.animationNames?.forEach((eachAnimationName)=>{
|
|
351
|
+
const animationRule = this.css.animationsNonLayer.rules.find(({ name })=>name === eachAnimationName);
|
|
352
|
+
if (animationRule || this.css.isEmittedGlobalsAnimation(eachAnimationName)) {
|
|
353
|
+
const count = this.css.animationsNonLayer.tokenCounts.get(eachAnimationName) || 0;
|
|
354
|
+
this.css.animationsNonLayer.tokenCounts.set(eachAnimationName, count + 1);
|
|
355
|
+
if (animationRule) this.insertVariables(animationRule);
|
|
356
|
+
} else {
|
|
357
|
+
const keyframes = this.css.animations.get(eachAnimationName);
|
|
358
|
+
if (!keyframes) return;
|
|
359
|
+
const newRule = new AnimationRule(eachAnimationName, keyframes, this.css);
|
|
360
|
+
this.css.animationsNonLayer.insert(newRule);
|
|
361
|
+
this.css.animationsNonLayer.tokenCounts.set(eachAnimationName, 1);
|
|
362
|
+
this.insertVariables(newRule);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
class RuntimeThemeLayer extends ThemeLayer {
|
|
369
|
+
name;
|
|
370
|
+
cssRuntime;
|
|
371
|
+
native = null;
|
|
372
|
+
nativeBuckets = new Map();
|
|
373
|
+
constructor(name, cssRuntime){
|
|
374
|
+
super(name, cssRuntime), this.name = name, this.cssRuntime = cssRuntime;
|
|
375
|
+
}
|
|
376
|
+
attach() {
|
|
377
|
+
super.attach();
|
|
378
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
379
|
+
if (nativeSheet && !this.native?.parentStyleSheet) {
|
|
380
|
+
const insertedIndex = nativeSheet.insertRule(this.text, nativeSheet.cssRules.length);
|
|
381
|
+
this.native = nativeSheet.cssRules.item(insertedIndex);
|
|
382
|
+
this.syncNativeBuckets();
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
getBucketKey(mediaText, selectorText) {
|
|
386
|
+
const normalizedMediaText = mediaText.replace(/\s*:\s*/g, ':').replace(/\s+/g, ' ').trim();
|
|
387
|
+
const normalizedSelectorText = selectorText.replace(/\s*,\s*/g, ',').replace(/\s+/g, ' ').trim();
|
|
388
|
+
return normalizedMediaText + '\n' + normalizedSelectorText;
|
|
389
|
+
}
|
|
390
|
+
getStyleRule(nativeRule) {
|
|
391
|
+
if (nativeRule instanceof CSSStyleRule) return nativeRule;
|
|
392
|
+
if (nativeRule instanceof CSSGroupingRule) {
|
|
393
|
+
return nativeRule.cssRules.item(0);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
syncNativeBuckets() {
|
|
397
|
+
this.nativeBuckets.clear();
|
|
398
|
+
if (!this.native) return;
|
|
399
|
+
for (const nativeRule of this.native.cssRules){
|
|
400
|
+
const styleRule = this.getStyleRule(nativeRule);
|
|
401
|
+
if (!styleRule) continue;
|
|
402
|
+
const mediaText = nativeRule instanceof CSSMediaRule ? nativeRule.conditionText : '';
|
|
403
|
+
const normalizedMediaText = mediaText ? `@media ${mediaText}` : '';
|
|
404
|
+
const key = this.getBucketKey(normalizedMediaText, styleRule.selectorText);
|
|
405
|
+
this.nativeBuckets.set(key, {
|
|
406
|
+
key,
|
|
407
|
+
mediaText: normalizedMediaText,
|
|
408
|
+
selectorText: styleRule.selectorText,
|
|
409
|
+
nativeRule,
|
|
410
|
+
styleRule
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
ensureNativeBucket(mediaText, selectorText) {
|
|
415
|
+
if (!this.native) return;
|
|
416
|
+
const key = this.getBucketKey(mediaText, selectorText);
|
|
417
|
+
let bucket = this.nativeBuckets.get(key);
|
|
418
|
+
if (bucket) return bucket;
|
|
419
|
+
const text = mediaText ? `${mediaText}{${selectorText}{}}` : `${selectorText}{}`;
|
|
420
|
+
const insertedIndex = this.native.insertRule(text, this.getNativeBucketInsertIndex(mediaText, selectorText));
|
|
421
|
+
const nativeRule = this.native.cssRules.item(insertedIndex);
|
|
422
|
+
if (!nativeRule) return;
|
|
423
|
+
const styleRule = this.getStyleRule(nativeRule);
|
|
424
|
+
if (!styleRule) return;
|
|
425
|
+
const newBucket = {
|
|
426
|
+
key,
|
|
427
|
+
mediaText,
|
|
428
|
+
selectorText,
|
|
429
|
+
nativeRule,
|
|
430
|
+
styleRule
|
|
431
|
+
};
|
|
432
|
+
this.nativeBuckets.set(key, newBucket);
|
|
433
|
+
return newBucket;
|
|
434
|
+
}
|
|
435
|
+
getNativeBucketInsertIndex(mediaText, selectorText) {
|
|
436
|
+
if (!this.native) return 0;
|
|
437
|
+
const bucket = {
|
|
438
|
+
mediaText,
|
|
439
|
+
selectorText
|
|
440
|
+
};
|
|
441
|
+
for(let index = 0; index < this.native.cssRules.length; index++){
|
|
442
|
+
const nativeRule = this.native.cssRules.item(index);
|
|
443
|
+
if (!nativeRule) continue;
|
|
444
|
+
const styleRule = this.getStyleRule(nativeRule);
|
|
445
|
+
if (!styleRule) continue;
|
|
446
|
+
const nativeMediaText = nativeRule instanceof CSSMediaRule ? `@media ${nativeRule.conditionText}` : '';
|
|
447
|
+
if (this.shouldInsertBucketBefore(bucket, {
|
|
448
|
+
mediaText: nativeMediaText,
|
|
449
|
+
selectorText: styleRule.selectorText
|
|
450
|
+
})) {
|
|
451
|
+
return index;
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return this.native.cssRules.length;
|
|
455
|
+
}
|
|
456
|
+
insert(rule, index = this.rules.length) {
|
|
457
|
+
const wasEmpty = this.rules.length === 0;
|
|
458
|
+
const insertedIndex = super.insert(rule, index);
|
|
459
|
+
if (insertedIndex === undefined || !this.native || wasEmpty) return insertedIndex;
|
|
460
|
+
for (const node of rule.nodes){
|
|
461
|
+
const bucket = this.ensureNativeBucket(node.mediaText, node.selectorText);
|
|
462
|
+
bucket?.styleRule.style.setProperty(node.declarationName, node.declarationValue);
|
|
463
|
+
node.native = bucket?.styleRule;
|
|
464
|
+
}
|
|
465
|
+
return insertedIndex;
|
|
466
|
+
}
|
|
467
|
+
delete(key) {
|
|
468
|
+
const rule = this.rules.find((rule)=>rule.key === key);
|
|
469
|
+
const deletedRule = super.delete(key);
|
|
470
|
+
if (!rule || !deletedRule || !this.native) return deletedRule;
|
|
471
|
+
for (const node of rule.nodes){
|
|
472
|
+
const bucketKey = this.getBucketKey(node.mediaText, node.selectorText);
|
|
473
|
+
const bucket = this.nativeBuckets.get(bucketKey);
|
|
474
|
+
if (!bucket) continue;
|
|
475
|
+
bucket.styleRule.style.removeProperty(node.declarationName);
|
|
476
|
+
if (bucket.styleRule.style.length === 0) {
|
|
477
|
+
const foundIndex = findNativeCSSRuleIndex(this.native.cssRules, bucket.nativeRule);
|
|
478
|
+
if (foundIndex !== -1) {
|
|
479
|
+
this.native.deleteRule(foundIndex);
|
|
480
|
+
}
|
|
481
|
+
this.nativeBuckets.delete(bucketKey);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return deletedRule;
|
|
485
|
+
}
|
|
486
|
+
detach() {
|
|
487
|
+
super.detach();
|
|
488
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
489
|
+
if (nativeSheet && this.native?.parentStyleSheet) {
|
|
490
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
491
|
+
if (foundIndex !== -1) {
|
|
492
|
+
nativeSheet.deleteRule(foundIndex);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
this.native = null;
|
|
496
|
+
this.nativeBuckets.clear();
|
|
497
|
+
}
|
|
498
|
+
reset() {
|
|
499
|
+
super.reset();
|
|
500
|
+
const nativeSheet = this.cssRuntime.style?.sheet;
|
|
501
|
+
if (this.native && nativeSheet) {
|
|
502
|
+
const foundIndex = findNativeCSSRuleIndex(nativeSheet.cssRules, this.native);
|
|
503
|
+
if (foundIndex !== -1) {
|
|
504
|
+
nativeSheet.deleteRule(foundIndex);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
this.native = null;
|
|
508
|
+
this.nativeBuckets.clear();
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function updateClassDelta(deltaCounts, className, delta) {
|
|
513
|
+
const nextDelta = (deltaCounts.get(className) || 0) + delta;
|
|
514
|
+
if (nextDelta) {
|
|
515
|
+
deltaCounts.set(className, nextDelta);
|
|
516
|
+
} else {
|
|
517
|
+
deltaCounts.delete(className);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
function traverseElementSubtree(root, visited, callback) {
|
|
521
|
+
const stack = [
|
|
522
|
+
root
|
|
523
|
+
];
|
|
524
|
+
while(stack.length){
|
|
525
|
+
const el = stack.pop();
|
|
526
|
+
if (visited.has(el)) continue;
|
|
527
|
+
visited.add(el);
|
|
528
|
+
callback(el);
|
|
529
|
+
for(let child = el.lastElementChild; child; child = child.previousElementSibling){
|
|
530
|
+
stack.push(child);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
class RuntimeClassTracker {
|
|
535
|
+
snapshots = new WeakMap();
|
|
536
|
+
reset() {
|
|
537
|
+
this.snapshots = new WeakMap();
|
|
538
|
+
}
|
|
539
|
+
collectConnected(root, classCounts) {
|
|
540
|
+
this.reset();
|
|
541
|
+
const connectedNames = new Set();
|
|
542
|
+
for (const el of root.querySelectorAll('[class]')){
|
|
543
|
+
const classNames = this.snapshotElement(el);
|
|
544
|
+
if (!classNames) continue;
|
|
545
|
+
for (const className of classNames){
|
|
546
|
+
const count = classCounts.get(className) || 0;
|
|
547
|
+
if (!count) connectedNames.add(className);
|
|
548
|
+
classCounts.set(className, count + 1);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
return connectedNames;
|
|
552
|
+
}
|
|
553
|
+
collectMutations(records) {
|
|
554
|
+
const deltaCounts = new Map();
|
|
555
|
+
const nodeCounts = new Map();
|
|
556
|
+
const attrRecords = new Set();
|
|
557
|
+
const visited = new WeakSet();
|
|
558
|
+
const updateNodeCount = (el, delta)=>{
|
|
559
|
+
const nextCount = (nodeCounts.get(el) || 0) + delta;
|
|
560
|
+
if (nextCount) {
|
|
561
|
+
nodeCounts.set(el, nextCount);
|
|
562
|
+
} else {
|
|
563
|
+
nodeCounts.delete(el);
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
for (const record of records){
|
|
567
|
+
if (record.type === 'childList') {
|
|
568
|
+
for (const node of record.addedNodes){
|
|
569
|
+
if (node instanceof Element && node.isConnected) updateNodeCount(node, 1);
|
|
570
|
+
}
|
|
571
|
+
for (const node of record.removedNodes){
|
|
572
|
+
if (node instanceof Element && !node.isConnected) updateNodeCount(node, -1);
|
|
573
|
+
}
|
|
574
|
+
} else if (record.type === 'attributes' && record.attributeName === 'class') {
|
|
575
|
+
const target = record.target;
|
|
576
|
+
if (target instanceof Element) attrRecords.add(target);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
for (const [node, count] of nodeCounts){
|
|
580
|
+
if (count > 0) {
|
|
581
|
+
traverseElementSubtree(node, visited, (el)=>this.diffElement(el, deltaCounts));
|
|
582
|
+
} else if (count < 0 && !node.isConnected) {
|
|
583
|
+
traverseElementSubtree(node, visited, (el)=>this.removeElement(el, deltaCounts));
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const el of attrRecords){
|
|
587
|
+
if (!visited.has(el)) this.diffElement(el, deltaCounts);
|
|
588
|
+
}
|
|
589
|
+
return deltaCounts;
|
|
590
|
+
}
|
|
591
|
+
snapshotElement(el) {
|
|
592
|
+
const classList = el.classList;
|
|
593
|
+
if (!classList.length) {
|
|
594
|
+
this.snapshots.delete(el);
|
|
595
|
+
return;
|
|
596
|
+
}
|
|
597
|
+
const classNames = new Set();
|
|
598
|
+
for(let i = 0; i < classList.length; i++){
|
|
599
|
+
classNames.add(classList.item(i));
|
|
600
|
+
}
|
|
601
|
+
this.snapshots.set(el, classNames);
|
|
602
|
+
return classNames;
|
|
603
|
+
}
|
|
604
|
+
diffElement(el, deltaCounts) {
|
|
605
|
+
const previousClassNames = this.snapshots.get(el);
|
|
606
|
+
const classList = el.classList;
|
|
607
|
+
let nextClassNames;
|
|
608
|
+
for(let i = 0; i < classList.length; i++){
|
|
609
|
+
const className = classList.item(i);
|
|
610
|
+
if (!previousClassNames?.has(className)) updateClassDelta(deltaCounts, className, 1);
|
|
611
|
+
(nextClassNames ??= new Set()).add(className);
|
|
612
|
+
}
|
|
613
|
+
if (previousClassNames) {
|
|
614
|
+
for (const className of previousClassNames){
|
|
615
|
+
if (!nextClassNames?.has(className)) updateClassDelta(deltaCounts, className, -1);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
if (nextClassNames) {
|
|
619
|
+
this.snapshots.set(el, nextClassNames);
|
|
620
|
+
} else {
|
|
621
|
+
this.snapshots.delete(el);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
removeElement(el, deltaCounts) {
|
|
625
|
+
const previousClassNames = this.snapshots.get(el);
|
|
626
|
+
if (!previousClassNames) return;
|
|
627
|
+
for (const className of previousClassNames){
|
|
628
|
+
updateClassDelta(deltaCounts, className, -1);
|
|
629
|
+
}
|
|
630
|
+
this.snapshots.delete(el);
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
function collectVariableNames(text) {
|
|
635
|
+
const variableNames = new Set();
|
|
636
|
+
for (const match of text.matchAll(/var\(\s*--([_a-zA-Z0-9-]+)/g)){
|
|
637
|
+
variableNames.add(match[1]);
|
|
638
|
+
}
|
|
639
|
+
return variableNames;
|
|
640
|
+
}
|
|
641
|
+
class HydratedGeneratedRuleNode {
|
|
642
|
+
text;
|
|
643
|
+
native;
|
|
644
|
+
constructor(text){
|
|
645
|
+
this.text = text;
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
class HydratedGeneratedRule {
|
|
649
|
+
ir;
|
|
650
|
+
layer;
|
|
651
|
+
native;
|
|
652
|
+
name;
|
|
653
|
+
key;
|
|
654
|
+
layerName;
|
|
655
|
+
type;
|
|
656
|
+
sortTier;
|
|
657
|
+
priority;
|
|
658
|
+
text;
|
|
659
|
+
valid = true;
|
|
660
|
+
fixedClass;
|
|
661
|
+
nodes;
|
|
662
|
+
variableNames;
|
|
663
|
+
animationNames;
|
|
664
|
+
selectorText;
|
|
665
|
+
constructor(ir, layer){
|
|
666
|
+
this.ir = ir;
|
|
667
|
+
this.layer = layer;
|
|
668
|
+
this.name = ir.className;
|
|
669
|
+
this.key = ir.key;
|
|
670
|
+
this.layerName = ir.layer;
|
|
671
|
+
this.type = ir.type;
|
|
672
|
+
this.sortTier = ir.sortTier;
|
|
673
|
+
this.priority = ir.priority;
|
|
674
|
+
this.text = ir.text;
|
|
675
|
+
this.selectorText = ir.selectorText;
|
|
676
|
+
if (ir.nodes?.length) {
|
|
677
|
+
this.nodes = ir.nodes.map((node)=>new HydratedGeneratedRuleNode(node.text));
|
|
678
|
+
}
|
|
679
|
+
const variableNames = collectVariableNames(ir.text);
|
|
680
|
+
ir.nodes?.forEach((node)=>{
|
|
681
|
+
collectVariableNames(node.text).forEach((variableName)=>variableNames.add(variableName));
|
|
682
|
+
});
|
|
683
|
+
ir.variableNames?.forEach((variableName)=>variableNames.add(variableName));
|
|
684
|
+
if (variableNames.size) {
|
|
685
|
+
this.variableNames = variableNames;
|
|
686
|
+
}
|
|
687
|
+
if (ir.animationNames?.length) {
|
|
688
|
+
this.animationNames = new Set(ir.animationNames);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const supportsCache = new Map();
|
|
694
|
+
const browserNativeDeclarationMatcher = ({ property, value })=>{
|
|
695
|
+
if (property.startsWith('--')) return true;
|
|
696
|
+
const cacheKey = property + '\0' + value;
|
|
697
|
+
const cached = supportsCache.get(cacheKey);
|
|
698
|
+
if (cached !== undefined) return cached;
|
|
699
|
+
const supported = Boolean(globalThis.CSS?.supports(property, value));
|
|
700
|
+
supportsCache.set(cacheKey, supported);
|
|
701
|
+
return supported;
|
|
702
|
+
};
|
|
703
|
+
|
|
704
|
+
const MASTER_CSS_RUNTIME_STYLE_SELECTOR = `style#${MASTER_CSS_RUNTIME_STYLE_ID}`;
|
|
705
|
+
function isDocumentRoot(root) {
|
|
706
|
+
const rootConstructorName = root?.constructor.name;
|
|
707
|
+
return rootConstructorName === 'HTMLDocument' || rootConstructorName === 'Document';
|
|
708
|
+
}
|
|
709
|
+
function findElementById(root, id) {
|
|
710
|
+
return isDocumentRoot(root) ? root.getElementById(id) : 'querySelector' in root ? root.querySelector(`#${id}`) : undefined;
|
|
711
|
+
}
|
|
712
|
+
function parseHydrationManifest(source) {
|
|
713
|
+
try {
|
|
714
|
+
const hydrationManifest = JSON.parse(source);
|
|
715
|
+
return hydrationManifest?.version === 1 && Array.isArray(hydrationManifest.rules) ? hydrationManifest : undefined;
|
|
716
|
+
} catch (error) {
|
|
717
|
+
{
|
|
718
|
+
console.debug('Cannot parse Master CSS hydration manifest.', error);
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
function readInlineHydrationManifest(root) {
|
|
723
|
+
const element = findElementById(root, MASTER_CSS_HYDRATION_MANIFEST_SCRIPT_ID);
|
|
724
|
+
const source = element?.textContent?.trim();
|
|
725
|
+
if (!source) return;
|
|
726
|
+
return parseHydrationManifest(source);
|
|
727
|
+
}
|
|
728
|
+
function readExternalHydrationManifestSource(root) {
|
|
729
|
+
const styleElement = findElementById(root, MASTER_CSS_RUNTIME_STYLE_ID);
|
|
730
|
+
const HTMLStyleElementConstructor = globalThis.HTMLStyleElement;
|
|
731
|
+
return HTMLStyleElementConstructor && styleElement instanceof HTMLStyleElementConstructor ? styleElement.getAttribute(MASTER_CSS_HYDRATION_MANIFEST_ATTR) : undefined;
|
|
732
|
+
}
|
|
733
|
+
class CSSRuntime extends MasterCSS {
|
|
734
|
+
root;
|
|
735
|
+
hydrationManifest;
|
|
736
|
+
static instances = new WeakMap();
|
|
737
|
+
host;
|
|
738
|
+
container;
|
|
739
|
+
baseLayer = new RuntimeUtilityLayer('base', this);
|
|
740
|
+
themeLayer = new RuntimeThemeLayer('theme', this);
|
|
741
|
+
defaultsLayer = new RuntimeUtilityLayer('defaults', this);
|
|
742
|
+
componentsLayer = new RuntimeUtilityLayer('components', this);
|
|
743
|
+
utilitiesLayer = new RuntimeUtilityLayer('utilities', this);
|
|
744
|
+
classCounts = new Map();
|
|
745
|
+
classTracker = new RuntimeClassTracker();
|
|
746
|
+
hydrationFailureReason;
|
|
747
|
+
observer;
|
|
748
|
+
progressive = false;
|
|
749
|
+
observing = false;
|
|
750
|
+
static create(options) {
|
|
751
|
+
const { manifest, root = document, emittedGlobals, hydrationManifest } = options;
|
|
752
|
+
const resolvedHydrationManifest = hydrationManifest === undefined ? readInlineHydrationManifest(root) : hydrationManifest;
|
|
753
|
+
const current = globalThis.MasterCSSRuntime.instances.get(root);
|
|
754
|
+
if (current) {
|
|
755
|
+
current.registerEmittedGlobals(emittedGlobals);
|
|
756
|
+
if (resolvedHydrationManifest !== undefined) current.setHydrationManifest(resolvedHydrationManifest);
|
|
757
|
+
return current;
|
|
758
|
+
}
|
|
759
|
+
return new CSSRuntime(root, manifest, emittedGlobals, resolvedHydrationManifest).register();
|
|
760
|
+
}
|
|
761
|
+
constructor(root = document, manifest, emittedGlobals, hydrationManifest){
|
|
762
|
+
super(manifest, emittedGlobals, {
|
|
763
|
+
nativeDeclarationMatcher: browserNativeDeclarationMatcher
|
|
764
|
+
}), this.root = root, this.hydrationManifest = hydrationManifest;
|
|
765
|
+
if (isDocumentRoot(root)) {
|
|
766
|
+
this.container = root.head;
|
|
767
|
+
this.host = root.documentElement;
|
|
768
|
+
} else {
|
|
769
|
+
this.container = this.root;
|
|
770
|
+
this.host = this.root.host;
|
|
771
|
+
}
|
|
772
|
+
this.applyEmittedGlobalsCounts({
|
|
773
|
+
variables: this.emittedGlobals.variables
|
|
774
|
+
});
|
|
775
|
+
}
|
|
776
|
+
register() {
|
|
777
|
+
const registered = globalThis.MasterCSSRuntime.instances.get(this.root) === this;
|
|
778
|
+
globalThis.MasterCSSRuntime.instances.set(this.root, this);
|
|
779
|
+
if (isDocumentRoot(this.root)) {
|
|
780
|
+
this.root.defaultView.globalThis.masterCSSRuntime = this;
|
|
781
|
+
}
|
|
782
|
+
if (!registered) {
|
|
783
|
+
__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:created', {
|
|
784
|
+
cssRuntime: this
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
return this;
|
|
788
|
+
}
|
|
789
|
+
unregister() {
|
|
790
|
+
globalThis.MasterCSSRuntime.instances.delete(this.root);
|
|
791
|
+
if (isDocumentRoot(this.root) && this.root.defaultView.globalThis.masterCSSRuntime === this) {
|
|
792
|
+
this.root.defaultView.globalThis.masterCSSRuntime = undefined;
|
|
793
|
+
}
|
|
794
|
+
return this;
|
|
795
|
+
}
|
|
796
|
+
setHydrationManifest(hydrationManifest) {
|
|
797
|
+
this.hydrationManifest = hydrationManifest;
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
needsHydrationManifest() {
|
|
801
|
+
return this.hydrationManifest === undefined && Boolean(readExternalHydrationManifestSource(this.root));
|
|
802
|
+
}
|
|
803
|
+
async loadHydrationManifest() {
|
|
804
|
+
const inlineHydrationManifest = readInlineHydrationManifest(this.root);
|
|
805
|
+
if (inlineHydrationManifest) {
|
|
806
|
+
this.setHydrationManifest(inlineHydrationManifest);
|
|
807
|
+
return this;
|
|
808
|
+
}
|
|
809
|
+
const source = readExternalHydrationManifestSource(this.root);
|
|
810
|
+
if (!source) return this;
|
|
811
|
+
try {
|
|
812
|
+
const response = await fetch(source, {
|
|
813
|
+
credentials: 'same-origin'
|
|
814
|
+
});
|
|
815
|
+
if (!response.ok) return this;
|
|
816
|
+
this.setHydrationManifest(parseHydrationManifest(await response.text()));
|
|
817
|
+
} catch (error) {
|
|
818
|
+
{
|
|
819
|
+
console.debug('Cannot load Master CSS hydration manifest.', error);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
return this;
|
|
823
|
+
}
|
|
824
|
+
createRuntimeStyle() {
|
|
825
|
+
const ownerDocument = 'createElement' in this.root ? this.root : this.root.ownerDocument;
|
|
826
|
+
this.style = ownerDocument.createElement('style');
|
|
827
|
+
this.style.id = MASTER_CSS_RUNTIME_STYLE_ID;
|
|
828
|
+
this.style.setAttribute('blocking', 'render');
|
|
829
|
+
this.container.append(this.style);
|
|
830
|
+
}
|
|
831
|
+
warnHydrationFallback(reason) {
|
|
832
|
+
console.warn(`Master CSS progressive hydration requires a matching hydration manifest. ${reason} Rebuilding ${MASTER_CSS_RUNTIME_STYLE_SELECTOR} with the runtime.`);
|
|
833
|
+
}
|
|
834
|
+
useRuntimeStyle(connectedNames, reason) {
|
|
835
|
+
if (reason) this.warnHydrationFallback(reason);
|
|
836
|
+
this.style?.remove();
|
|
837
|
+
this.style = null;
|
|
838
|
+
this.progressive = false;
|
|
839
|
+
this.createRuntimeStyle();
|
|
840
|
+
this.insertStaticResources();
|
|
841
|
+
connectedNames.forEach((cls)=>this.add(cls));
|
|
842
|
+
}
|
|
843
|
+
detectRuntimeStyle() {
|
|
844
|
+
if (this.root.styleSheets) {
|
|
845
|
+
for (const sheet of this.root.styleSheets){
|
|
846
|
+
const { ownerNode } = sheet;
|
|
847
|
+
if (ownerNode instanceof HTMLStyleElement && ownerNode.id === MASTER_CSS_RUNTIME_STYLE_ID) {
|
|
848
|
+
this.style = ownerNode;
|
|
849
|
+
this.progressive = true;
|
|
850
|
+
break;
|
|
851
|
+
}
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
collectConnectedClasses() {
|
|
856
|
+
return this.classTracker.collectConnected(this.root, this.classCounts);
|
|
857
|
+
}
|
|
858
|
+
hydrateRuntimeStyle(connectedNames) {
|
|
859
|
+
const hydrateResult = this.style?.sheet && this.hydrate(this.style.sheet.cssRules);
|
|
860
|
+
if (hydrateResult) {
|
|
861
|
+
const hydratedClassNames = new Set(hydrateResult.allUtilities.map(({ fixedClass, name })=>fixedClass || name));
|
|
862
|
+
for (const cls of connectedNames){
|
|
863
|
+
if (!hydratedClassNames.has(cls)) {
|
|
864
|
+
this.add(cls);
|
|
865
|
+
{
|
|
866
|
+
console.debug(`Missing prerendered rule for class \`${cls}\``);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
} else {
|
|
871
|
+
this.useRuntimeStyle(connectedNames, this.hydrationFailureReason || `Cannot read ${MASTER_CSS_RUNTIME_STYLE_SELECTOR} CSS rules.`);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
renderRuntimeStyle(connectedNames) {
|
|
875
|
+
this.createRuntimeStyle();
|
|
876
|
+
this.insertStaticResources();
|
|
877
|
+
connectedNames.forEach((cls)=>this.add(cls));
|
|
878
|
+
}
|
|
879
|
+
handleMutationRecords(records) {
|
|
880
|
+
const deltaCounts = this.classTracker.collectMutations(records);
|
|
881
|
+
const addedClassNames = [];
|
|
882
|
+
const removedClassNames = [];
|
|
883
|
+
for (const [cls, change] of deltaCounts){
|
|
884
|
+
const current = this.classCounts.get(cls) || 0;
|
|
885
|
+
const next = current + change;
|
|
886
|
+
if (next > 0) {
|
|
887
|
+
this.classCounts.set(cls, next);
|
|
888
|
+
if (current === 0) addedClassNames.push(cls);
|
|
889
|
+
} else {
|
|
890
|
+
this.classCounts.delete(cls);
|
|
891
|
+
removedClassNames.push(cls);
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
if (addedClassNames.length) this.add(...addedClassNames);
|
|
895
|
+
if (removedClassNames.length) this.remove(...removedClassNames);
|
|
896
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:mutated', {
|
|
897
|
+
records,
|
|
898
|
+
classCounts: deltaCounts,
|
|
899
|
+
cssRuntime: this
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
startMutationObserver() {
|
|
903
|
+
this.observer = new MutationObserver((records)=>this.handleMutationRecords(records));
|
|
904
|
+
this.observer.observe(this.root, {
|
|
905
|
+
childList: true,
|
|
906
|
+
attributes: true,
|
|
907
|
+
attributeFilter: [
|
|
908
|
+
'class'
|
|
909
|
+
],
|
|
910
|
+
subtree: true
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
revealHostIfNeeded() {
|
|
914
|
+
if (!this.progressive) this.host.removeAttribute('hidden');
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Observe the DOM for changes and update the running stylesheet. (browser only)
|
|
918
|
+
* @param options mutation observer options
|
|
919
|
+
* @returns this
|
|
920
|
+
*/ observe() {
|
|
921
|
+
if (this.observing) return this;
|
|
922
|
+
this.detectRuntimeStyle();
|
|
923
|
+
const connectedNames = this.collectConnectedClasses();
|
|
924
|
+
if (this.progressive) {
|
|
925
|
+
this.hydrateRuntimeStyle(connectedNames);
|
|
926
|
+
} else {
|
|
927
|
+
this.renderRuntimeStyle(connectedNames);
|
|
928
|
+
}
|
|
929
|
+
this.startMutationObserver();
|
|
930
|
+
this.revealHostIfNeeded();
|
|
931
|
+
this.observing = true;
|
|
932
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:observed', {
|
|
933
|
+
cssRuntime: this
|
|
934
|
+
});
|
|
935
|
+
return this;
|
|
936
|
+
}
|
|
937
|
+
failHydration(reason) {
|
|
938
|
+
this.hydrationFailureReason = reason;
|
|
939
|
+
return undefined;
|
|
940
|
+
}
|
|
941
|
+
getHydrationManifestLayerRules() {
|
|
942
|
+
const layers = new Map();
|
|
943
|
+
if (!this.hydrationManifest?.rules?.length) return layers;
|
|
944
|
+
for (const rule of this.hydrationManifest.rules){
|
|
945
|
+
const layerRules = layers.get(rule.layer);
|
|
946
|
+
if (layerRules) {
|
|
947
|
+
layerRules.push(rule);
|
|
948
|
+
} else {
|
|
949
|
+
layers.set(rule.layer, [
|
|
950
|
+
rule
|
|
951
|
+
]);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return layers;
|
|
955
|
+
}
|
|
956
|
+
registerHydratedClassRule(className, rule) {
|
|
957
|
+
const classUtilities = this.classUtilities;
|
|
958
|
+
const rules = classUtilities.get(className);
|
|
959
|
+
if (rules) {
|
|
960
|
+
rules.push(rule);
|
|
961
|
+
} else {
|
|
962
|
+
classUtilities.set(className, [
|
|
963
|
+
rule
|
|
964
|
+
]);
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
getUtilityLayerByName(name) {
|
|
968
|
+
switch(name){
|
|
969
|
+
case 'base':
|
|
970
|
+
return this.baseLayer;
|
|
971
|
+
case 'defaults':
|
|
972
|
+
return this.defaultsLayer;
|
|
973
|
+
case 'components':
|
|
974
|
+
return this.componentsLayer;
|
|
975
|
+
case 'utilities':
|
|
976
|
+
return this.utilitiesLayer;
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
collectHydrationManifestVariableNames() {
|
|
980
|
+
const variableNames = new Set();
|
|
981
|
+
const collectVariableReferences = (text)=>{
|
|
982
|
+
for (const match of text.matchAll(/var\(\s*--([_a-zA-Z0-9-]+)/g)){
|
|
983
|
+
collectVariable(match[1]);
|
|
984
|
+
}
|
|
985
|
+
};
|
|
986
|
+
const collectVariable = (variableName, visited = new Set())=>{
|
|
987
|
+
if (visited.has(variableName)) return;
|
|
988
|
+
visited.add(variableName);
|
|
989
|
+
const variable = this.variables.get(variableName);
|
|
990
|
+
if (!variable) return;
|
|
991
|
+
variableNames.add(variableName);
|
|
992
|
+
variable.dependencies?.forEach((dependency)=>collectVariable(dependency, visited));
|
|
993
|
+
};
|
|
994
|
+
for (const rule of this.hydrationManifest?.rules || []){
|
|
995
|
+
rule.variableNames?.forEach((variableName)=>collectVariable(variableName));
|
|
996
|
+
collectVariableReferences(rule.text);
|
|
997
|
+
rule.nodes?.forEach((node)=>collectVariableReferences(node.text));
|
|
998
|
+
rule.animationNames?.forEach((animationName)=>{
|
|
999
|
+
const keyframes = this.animations.get(animationName);
|
|
1000
|
+
if (!keyframes) return;
|
|
1001
|
+
const animationRule = new AnimationRule(animationName, keyframes, this);
|
|
1002
|
+
animationRule.variableNames?.forEach((variableName)=>collectVariable(variableName));
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
for (const [variableName, variable] of this.variables){
|
|
1006
|
+
if (variable.static) collectVariable(variableName);
|
|
1007
|
+
}
|
|
1008
|
+
return variableNames;
|
|
1009
|
+
}
|
|
1010
|
+
collectHydrationManifestAnimationNames() {
|
|
1011
|
+
const animationNames = new Set();
|
|
1012
|
+
for (const rule of this.hydrationManifest?.rules || []){
|
|
1013
|
+
rule.animationNames?.forEach((animationName)=>animationNames.add(animationName));
|
|
1014
|
+
}
|
|
1015
|
+
for (const animationName of this.animations.keys()){
|
|
1016
|
+
if (this.manifest.animationOptions?.[animationName]?.static) animationNames.add(animationName);
|
|
1017
|
+
}
|
|
1018
|
+
return animationNames;
|
|
1019
|
+
}
|
|
1020
|
+
nativeThemeLayerHasOnlyEmittedGlobalsVariables(nativeThemeLayer) {
|
|
1021
|
+
if (!nativeThemeLayer) return false;
|
|
1022
|
+
let found = false;
|
|
1023
|
+
for (const nativeRule of nativeThemeLayer.cssRules){
|
|
1024
|
+
const styleRule = this.themeLayer.getStyleRule(nativeRule);
|
|
1025
|
+
if (!styleRule) return false;
|
|
1026
|
+
for(let index = 0; index < styleRule.style.length; index++){
|
|
1027
|
+
const propertyName = styleRule.style.item(index);
|
|
1028
|
+
if (!propertyName.startsWith('--')) return false;
|
|
1029
|
+
found = true;
|
|
1030
|
+
if (!this.isEmittedGlobalsVariable(propertyName.slice(2))) return false;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return found;
|
|
1034
|
+
}
|
|
1035
|
+
getVariableRuleBuckets(variableRules) {
|
|
1036
|
+
const buckets = new Map();
|
|
1037
|
+
for (const rule of variableRules){
|
|
1038
|
+
for (const node of rule.nodes){
|
|
1039
|
+
const key = this.themeLayer.getBucketKey(node.mediaText, node.selectorText);
|
|
1040
|
+
let bucket = buckets.get(key);
|
|
1041
|
+
if (!bucket) {
|
|
1042
|
+
bucket = {
|
|
1043
|
+
mediaText: node.mediaText,
|
|
1044
|
+
selectorText: node.selectorText,
|
|
1045
|
+
nodes: []
|
|
1046
|
+
};
|
|
1047
|
+
buckets.set(key, bucket);
|
|
1048
|
+
}
|
|
1049
|
+
bucket.nodes.push(node);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
return Array.from(buckets.values());
|
|
1053
|
+
}
|
|
1054
|
+
getNativeThemeRuleBucketKey(nativeRule) {
|
|
1055
|
+
const styleRule = this.themeLayer.getStyleRule(nativeRule);
|
|
1056
|
+
if (!styleRule) return;
|
|
1057
|
+
const mediaText = nativeRule instanceof CSSMediaRule ? `@media ${nativeRule.conditionText}` : '';
|
|
1058
|
+
return {
|
|
1059
|
+
key: this.themeLayer.getBucketKey(mediaText, styleRule.selectorText),
|
|
1060
|
+
styleRule
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
hydrateHydrationManifestVariables(nativeThemeLayer) {
|
|
1064
|
+
const variableRules = [
|
|
1065
|
+
...this.collectHydrationManifestVariableNames()
|
|
1066
|
+
].filter((variableName)=>!this.isEmittedGlobalsVariable(variableName)).map((variableName)=>{
|
|
1067
|
+
const variable = this.variables.get(variableName);
|
|
1068
|
+
return variable && !variable.inline ? new VariableRule(variableName, variable, this) : undefined;
|
|
1069
|
+
}).filter((rule)=>Boolean(rule));
|
|
1070
|
+
const expectedBuckets = this.getVariableRuleBuckets(variableRules);
|
|
1071
|
+
const nativeRuleCount = nativeThemeLayer?.cssRules.length || 0;
|
|
1072
|
+
if (expectedBuckets.length !== nativeRuleCount) {
|
|
1073
|
+
return !variableRules.length && this.nativeThemeLayerHasOnlyEmittedGlobalsVariables(nativeThemeLayer);
|
|
1074
|
+
}
|
|
1075
|
+
if (!variableRules.length) return true;
|
|
1076
|
+
if (!nativeThemeLayer) return false;
|
|
1077
|
+
this.themeLayer.native = nativeThemeLayer;
|
|
1078
|
+
const nativeBuckets = new Map();
|
|
1079
|
+
for (const nativeRule of nativeThemeLayer.cssRules){
|
|
1080
|
+
const nativeBucket = this.getNativeThemeRuleBucketKey(nativeRule);
|
|
1081
|
+
if (!nativeBucket || nativeBuckets.has(nativeBucket.key)) return false;
|
|
1082
|
+
nativeBuckets.set(nativeBucket.key, nativeBucket.styleRule);
|
|
1083
|
+
}
|
|
1084
|
+
for (const bucket of expectedBuckets){
|
|
1085
|
+
const nativeStyleRule = nativeBuckets.get(this.themeLayer.getBucketKey(bucket.mediaText, bucket.selectorText));
|
|
1086
|
+
if (!nativeStyleRule) return false;
|
|
1087
|
+
for (const node of bucket.nodes){
|
|
1088
|
+
node.native = nativeStyleRule;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
this.themeLayer.rules.push(...variableRules);
|
|
1092
|
+
this.themeLayer.syncNativeBuckets();
|
|
1093
|
+
if (this.themeLayer.rules.length && !this.rules.includes(this.themeLayer)) {
|
|
1094
|
+
this.rules.push(this.themeLayer);
|
|
1095
|
+
}
|
|
1096
|
+
return true;
|
|
1097
|
+
}
|
|
1098
|
+
hydrateHydrationManifestAnimations(nativeKeyframesRules) {
|
|
1099
|
+
const animationNames = this.collectHydrationManifestAnimationNames();
|
|
1100
|
+
const hydratedAnimationNames = new Set();
|
|
1101
|
+
for (const animationName of animationNames){
|
|
1102
|
+
if (this.isEmittedGlobalsAnimation(animationName)) {
|
|
1103
|
+
if (nativeKeyframesRules.has(animationName)) hydratedAnimationNames.add(animationName);
|
|
1104
|
+
continue;
|
|
1105
|
+
}
|
|
1106
|
+
const keyframes = this.animations.get(animationName);
|
|
1107
|
+
if (!keyframes) continue;
|
|
1108
|
+
const nativeRule = nativeKeyframesRules.get(animationName);
|
|
1109
|
+
if (!nativeRule) return false;
|
|
1110
|
+
const animationRule = new AnimationRule(animationName, keyframes, this);
|
|
1111
|
+
animationRule.native = nativeRule;
|
|
1112
|
+
this.animationsNonLayer.rules.push(animationRule);
|
|
1113
|
+
this.rules.push(animationRule);
|
|
1114
|
+
hydratedAnimationNames.add(animationName);
|
|
1115
|
+
}
|
|
1116
|
+
for (const animationName of nativeKeyframesRules.keys()){
|
|
1117
|
+
if (!hydratedAnimationNames.has(animationName)) return false;
|
|
1118
|
+
}
|
|
1119
|
+
return true;
|
|
1120
|
+
}
|
|
1121
|
+
hydrateHydrationManifestLayer(layer, nativeLayerRule, manifestRules, result) {
|
|
1122
|
+
const expectedRuleCount = manifestRules.reduce((count, rule)=>count + (rule.nodes?.length || 1), 0);
|
|
1123
|
+
if (expectedRuleCount !== nativeLayerRule.cssRules.length) return false;
|
|
1124
|
+
layer.native = nativeLayerRule;
|
|
1125
|
+
let nativeIndex = 0;
|
|
1126
|
+
for (const manifestRule of manifestRules){
|
|
1127
|
+
const hydratedRule = new HydratedGeneratedRule(manifestRule, layer);
|
|
1128
|
+
const nodes = hydratedRule.nodes;
|
|
1129
|
+
if (nodes?.length) {
|
|
1130
|
+
for (const node of nodes){
|
|
1131
|
+
node.native = nativeLayerRule.cssRules.item(nativeIndex++) || undefined;
|
|
1132
|
+
}
|
|
1133
|
+
} else {
|
|
1134
|
+
hydratedRule.native = nativeLayerRule.cssRules.item(nativeIndex++) || undefined;
|
|
1135
|
+
}
|
|
1136
|
+
layer.rules.push(hydratedRule);
|
|
1137
|
+
layer.insertVariables(hydratedRule);
|
|
1138
|
+
layer.insertAnimations(hydratedRule);
|
|
1139
|
+
this.registerHydratedClassRule(manifestRule.className, hydratedRule);
|
|
1140
|
+
result.allUtilities.push(hydratedRule);
|
|
1141
|
+
}
|
|
1142
|
+
if (layer.rules.length && !this.rules.includes(layer)) {
|
|
1143
|
+
this.rules.push(layer);
|
|
1144
|
+
}
|
|
1145
|
+
return true;
|
|
1146
|
+
}
|
|
1147
|
+
hydrate(nativeLayerRules) {
|
|
1148
|
+
this.hydrationFailureReason = undefined;
|
|
1149
|
+
if (this.hydrationManifest?.version !== 1 || !Array.isArray(this.hydrationManifest.rules)) {
|
|
1150
|
+
return this.failHydration('Missing or invalid hydration manifest.');
|
|
1151
|
+
}
|
|
1152
|
+
if (!this.hydrationManifest.rules.length) {
|
|
1153
|
+
return this.failHydration(`Hydration manifest has no generated rules for ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1154
|
+
}
|
|
1155
|
+
const result = {
|
|
1156
|
+
allUtilities: []
|
|
1157
|
+
};
|
|
1158
|
+
const manifestLayerRules = this.getHydrationManifestLayerRules();
|
|
1159
|
+
const nativeUtilityLayerRules = new Map();
|
|
1160
|
+
const nativeKeyframesRules = new Map();
|
|
1161
|
+
let nativeThemeLayer;
|
|
1162
|
+
for(let i = 0; i < nativeLayerRules.length; i++){
|
|
1163
|
+
const eachNativeCSSRule = nativeLayerRules[i];
|
|
1164
|
+
if (eachNativeCSSRule.constructor.name === 'CSSLayerBlockRule') {
|
|
1165
|
+
const eachCSSLayerRule = eachNativeCSSRule;
|
|
1166
|
+
if (eachCSSLayerRule.name === 'theme') {
|
|
1167
|
+
if (nativeThemeLayer) return this.failHydration(`Duplicate theme layer in ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1168
|
+
nativeThemeLayer = eachCSSLayerRule;
|
|
1169
|
+
continue;
|
|
1170
|
+
}
|
|
1171
|
+
const layer = this.getUtilityLayerByName(eachCSSLayerRule.name);
|
|
1172
|
+
if (!layer) return this.failHydration(`Unknown layer \`${eachCSSLayerRule.name}\` in ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1173
|
+
if (nativeUtilityLayerRules.has(layer.name)) {
|
|
1174
|
+
return this.failHydration(`Duplicate layer \`${eachCSSLayerRule.name}\` in ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1175
|
+
}
|
|
1176
|
+
nativeUtilityLayerRules.set(layer.name, eachCSSLayerRule);
|
|
1177
|
+
} else if (eachNativeCSSRule.constructor.name === 'CSSKeyframesRule') {
|
|
1178
|
+
const nativeKeyframesRule = eachNativeCSSRule;
|
|
1179
|
+
nativeKeyframesRules.set(nativeKeyframesRule.name, nativeKeyframesRule);
|
|
1180
|
+
} else {
|
|
1181
|
+
return this.failHydration(`Unknown top-level rule \`${eachNativeCSSRule.cssText}\` in ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
for (const [layerName, manifestRules] of manifestLayerRules){
|
|
1185
|
+
const nativeLayerRule = nativeUtilityLayerRules.get(layerName);
|
|
1186
|
+
if (!nativeLayerRule) return this.failHydration(`Missing layer \`${layerName}\` in ${MASTER_CSS_RUNTIME_STYLE_SELECTOR}.`);
|
|
1187
|
+
const expectedRuleCount = manifestRules.reduce((count, rule)=>count + (rule.nodes?.length || 1), 0);
|
|
1188
|
+
if (expectedRuleCount !== nativeLayerRule.cssRules.length) {
|
|
1189
|
+
return this.failHydration(`Layer \`${layerName}\` does not match the hydration manifest.`);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
for (const [layerName, nativeLayerRule] of nativeUtilityLayerRules){
|
|
1193
|
+
if (!manifestLayerRules.has(layerName) && nativeLayerRule.cssRules.length) {
|
|
1194
|
+
return this.failHydration(`Layer \`${layerName}\` has no hydration manifest rules.`);
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
if (!this.hydrateHydrationManifestVariables(nativeThemeLayer)) {
|
|
1198
|
+
return this.failHydration('Theme layer does not match the hydration manifest.');
|
|
1199
|
+
}
|
|
1200
|
+
if (!this.hydrateHydrationManifestAnimations(nativeKeyframesRules)) {
|
|
1201
|
+
return this.failHydration('Keyframes do not match the hydration manifest.');
|
|
1202
|
+
}
|
|
1203
|
+
for (const [layerName, nativeLayerRule] of nativeUtilityLayerRules){
|
|
1204
|
+
const manifestRules = manifestLayerRules.get(layerName);
|
|
1205
|
+
if (!manifestRules?.length) continue;
|
|
1206
|
+
const layer = this.getUtilityLayerByName(layerName);
|
|
1207
|
+
this.hydrateHydrationManifestLayer(layer, nativeLayerRule, manifestRules, result);
|
|
1208
|
+
}
|
|
1209
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:hydrated', {
|
|
1210
|
+
cssRuntime: this,
|
|
1211
|
+
result
|
|
1212
|
+
});
|
|
1213
|
+
return result;
|
|
1214
|
+
}
|
|
1215
|
+
disconnect() {
|
|
1216
|
+
if (!this.observing) return;
|
|
1217
|
+
if (this.observer) {
|
|
1218
|
+
this.observer.disconnect();
|
|
1219
|
+
this.observer = undefined;
|
|
1220
|
+
}
|
|
1221
|
+
// @ts-ignore
|
|
1222
|
+
this.observing = false;
|
|
1223
|
+
this.reset();
|
|
1224
|
+
this.loadManifest(this.manifest);
|
|
1225
|
+
this.classCounts.clear();
|
|
1226
|
+
this.classTracker.reset();
|
|
1227
|
+
if (!this.progressive) {
|
|
1228
|
+
this.style?.remove();
|
|
1229
|
+
this.style = null;
|
|
1230
|
+
}
|
|
1231
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:disconnected', {
|
|
1232
|
+
cssRuntime: this
|
|
1233
|
+
});
|
|
1234
|
+
return this;
|
|
1235
|
+
}
|
|
1236
|
+
refresh(manifest = this.manifest) {
|
|
1237
|
+
if (!this.observing || !this.style.sheet) return this;
|
|
1238
|
+
const cssRules = this.style.sheet.cssRules;
|
|
1239
|
+
for(let i = cssRules.length - 1; i >= 0; i--){
|
|
1240
|
+
this.style.sheet.deleteRule(i);
|
|
1241
|
+
}
|
|
1242
|
+
super.refresh(manifest);
|
|
1243
|
+
/**
|
|
1244
|
+
* Recreate rules from the current class names against the latest manifest.
|
|
1245
|
+
* 所以 refresh 過後 rules 可能會變多也可能會變少
|
|
1246
|
+
*/ this.classCounts.forEach((_, className)=>{
|
|
1247
|
+
this.add(className);
|
|
1248
|
+
});
|
|
1249
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:refreshed', {
|
|
1250
|
+
cssRuntime: this,
|
|
1251
|
+
manifest
|
|
1252
|
+
});
|
|
1253
|
+
return this;
|
|
1254
|
+
}
|
|
1255
|
+
destroy() {
|
|
1256
|
+
this.disconnect();
|
|
1257
|
+
this.unregister();
|
|
1258
|
+
globalThis.__MASTER_CSS_DEVTOOLS_HOOK__?.emit('runtime:destroyed', {
|
|
1259
|
+
cssRuntime: this
|
|
1260
|
+
});
|
|
1261
|
+
return this;
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
(function(CSSRuntime) {
|
|
1265
|
+
registerGlobal(CSSRuntime);
|
|
1266
|
+
})(CSSRuntime);
|
|
1267
|
+
|
|
1268
|
+
function resolveManifest(host, manifest) {
|
|
1269
|
+
return typeof manifest === 'function' ? manifest(host) : manifest;
|
|
1270
|
+
}
|
|
1271
|
+
function resolveRoot(host, root) {
|
|
1272
|
+
return typeof root === 'function' ? root(host) : root || host.shadowRoot;
|
|
1273
|
+
}
|
|
1274
|
+
function resolveEmittedGlobals(host, emittedGlobals) {
|
|
1275
|
+
return typeof emittedGlobals === 'function' ? emittedGlobals(host) : emittedGlobals;
|
|
1276
|
+
}
|
|
1277
|
+
function resolveHydrationManifest(host, hydrationManifest) {
|
|
1278
|
+
return typeof hydrationManifest === 'function' ? hydrationManifest(host) : hydrationManifest;
|
|
1279
|
+
}
|
|
1280
|
+
function cssRuntime(options) {
|
|
1281
|
+
return function(target) {
|
|
1282
|
+
const connectedCallback = target.prototype.connectedCallback;
|
|
1283
|
+
const disconnectedCallback = target.prototype.disconnectedCallback;
|
|
1284
|
+
return class extends target {
|
|
1285
|
+
cssRuntime;
|
|
1286
|
+
connectedCallback() {
|
|
1287
|
+
connectedCallback?.call(this);
|
|
1288
|
+
const root = resolveRoot(this, options.root);
|
|
1289
|
+
if (!root) {
|
|
1290
|
+
throw new Error('`@cssRuntime()` requires a shadow root. Provide `options.root` or create a shadow root before `connectedCallback()` finishes.');
|
|
1291
|
+
}
|
|
1292
|
+
const cssRuntime = CSSRuntime.create({
|
|
1293
|
+
manifest: resolveManifest(this, options.manifest),
|
|
1294
|
+
root,
|
|
1295
|
+
emittedGlobals: resolveEmittedGlobals(this, options.emittedGlobals),
|
|
1296
|
+
hydrationManifest: resolveHydrationManifest(this, options.hydrationManifest)
|
|
1297
|
+
});
|
|
1298
|
+
this.cssRuntime = cssRuntime;
|
|
1299
|
+
if (cssRuntime.needsHydrationManifest()) {
|
|
1300
|
+
void cssRuntime.loadHydrationManifest().then(()=>{
|
|
1301
|
+
if (this.cssRuntime === cssRuntime) cssRuntime.observe();
|
|
1302
|
+
});
|
|
1303
|
+
} else {
|
|
1304
|
+
cssRuntime.observe();
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
disconnectedCallback() {
|
|
1308
|
+
disconnectedCallback?.call(this);
|
|
1309
|
+
this.cssRuntime?.destroy();
|
|
1310
|
+
this.cssRuntime = undefined;
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
export { CSSRuntime, RuntimeUtilityLayer, cssRuntime };
|