single-file-core 1.13.0 → 1.14.0
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/core/index.js +9 -4
- package/modules/css-rules-minifier.js +198 -0
- package/package.json +1 -1
package/core/index.js
CHANGED
|
@@ -495,6 +495,7 @@ class BatchRequest {
|
|
|
495
495
|
// Processor
|
|
496
496
|
// ---------
|
|
497
497
|
const SHADOWROOT_ATTRIBUTE_NAME = "shadowrootmode";
|
|
498
|
+
const STYLESHEET_GROUP_KEY_SEPARATOR = "\u0000";
|
|
498
499
|
const SHADOWROOT_DELEGATES_FOCUS = "shadowrootdelegatesfocus";
|
|
499
500
|
const SHADOWROOT_CLONABLE = "shadowrootclonable";
|
|
500
501
|
const SHADOWROOT_SERIALIZABLE = "shadowrootserializable";
|
|
@@ -1500,14 +1501,18 @@ class Processor {
|
|
|
1500
1501
|
this.doc.querySelectorAll("style").forEach(styleElement => {
|
|
1501
1502
|
const content = styleElement.textContent;
|
|
1502
1503
|
if (content) {
|
|
1503
|
-
|
|
1504
|
-
|
|
1504
|
+
const shadowRootElement = styleElement.closest("[" + SHADOWROOT_ATTRIBUTE_NAME + "]");
|
|
1505
|
+
const hostElement = shadowRootElement && shadowRootElement.parentElement;
|
|
1506
|
+
const groupKey = (hostElement ? hostElement.localName : "") + STYLESHEET_GROUP_KEY_SEPARATOR + content;
|
|
1507
|
+
if (!styleElementGroups.has(groupKey)) {
|
|
1508
|
+
styleElementGroups.set(groupKey, []);
|
|
1505
1509
|
}
|
|
1506
|
-
styleElementGroups.get(
|
|
1510
|
+
styleElementGroups.get(groupKey).push(styleElement);
|
|
1507
1511
|
}
|
|
1508
1512
|
});
|
|
1509
1513
|
let indexContent = 0;
|
|
1510
|
-
styleElementGroups.forEach(
|
|
1514
|
+
styleElementGroups.forEach(styleElements => {
|
|
1515
|
+
const content = styleElements[0].textContent;
|
|
1511
1516
|
const masterIndex = ORDER_DEPENDENT_RULE.test(content) ? 0 : styleElements.length - 1;
|
|
1512
1517
|
this.options.inlineStylesheets.set(indexContent, {
|
|
1513
1518
|
styleElement: styleElements[masterIndex],
|
|
@@ -94,6 +94,29 @@ const UNSCOPED_PROXIMITY = Infinity;
|
|
|
94
94
|
const BLOCK_OPEN = "{";
|
|
95
95
|
const BLOCK_CLOSE = "}";
|
|
96
96
|
const EMPTY_STRING = "";
|
|
97
|
+
const STYLE_TAG_NAME = "style";
|
|
98
|
+
const SHADOW_ROOT_SELECTOR = "template[shadowrootmode]";
|
|
99
|
+
const UNIVERSAL_SELECTOR = "*";
|
|
100
|
+
const NOT_PSEUDO_CLASS_NAME = "not";
|
|
101
|
+
const HAS_PSEUDO_CLASS_NAME = "has";
|
|
102
|
+
const SHADOW_ROOT_CONDITIONAL_AT_RULE_NAMES = new Set([MEDIA_AT_RULE_NAME, SUPPORTS_AT_RULE_NAME, "container", LAYER_NAME]);
|
|
103
|
+
const SHADOW_ROOT_REMOVABLE_AT_RULE_NAMES = new Set([MEDIA_AT_RULE_NAME, SUPPORTS_AT_RULE_NAME, "container"]);
|
|
104
|
+
const SHADOW_HOST_PSEUDO_CLASS_NAMES = new Set(["host", "host-context", SCOPE_PSEUDO_CLASS_NAME]);
|
|
105
|
+
const SHADOW_PSEUDO_ELEMENT_NAMES = new Set(["slotted", "part"]);
|
|
106
|
+
const SIBLING_COMBINATOR_NAMES = new Set(["+", "~"]);
|
|
107
|
+
const SIBLING_DEPENDENT_PSEUDO_CLASS_NAMES = new Set([
|
|
108
|
+
"empty",
|
|
109
|
+
"first-child",
|
|
110
|
+
"last-child",
|
|
111
|
+
"only-child",
|
|
112
|
+
"first-of-type",
|
|
113
|
+
"last-of-type",
|
|
114
|
+
"only-of-type",
|
|
115
|
+
"nth-child",
|
|
116
|
+
"nth-last-child",
|
|
117
|
+
"nth-of-type",
|
|
118
|
+
"nth-last-of-type"
|
|
119
|
+
]);
|
|
97
120
|
const CSS_IMPORTANCE_NOT_IMPORTANT = 0;
|
|
98
121
|
const CSS_IMPORTANCE_IMPORTANT = 1;
|
|
99
122
|
const INVALID_CSS_ESCAPE_TEST = /\\(?![0-9a-fA-F]{1,6}\s|[^0-9a-zA-Z])/;
|
|
@@ -174,6 +197,8 @@ function process(doc, stylesheets, styles) {
|
|
|
174
197
|
nestingParentTexts: new Map(),
|
|
175
198
|
nestingParentNodes: new Map(),
|
|
176
199
|
scopedSelectorTexts: new Map(),
|
|
200
|
+
shadowRootSelectorTexts: new Map(),
|
|
201
|
+
shadowRootMatches: new Map(),
|
|
177
202
|
supportedSelectors: new Map(),
|
|
178
203
|
valueValidities: new Map(),
|
|
179
204
|
baselineValues: new Map(),
|
|
@@ -186,9 +211,182 @@ function process(doc, stylesheets, styles) {
|
|
|
186
211
|
computeCascade(docContext);
|
|
187
212
|
removeUnusedCustomProperties(stylesheets, styles, docContext);
|
|
188
213
|
removeEmptyRules(stylesheets, docContext);
|
|
214
|
+
minifyShadowRootStylesheets(getShadowRootStylesheets(stylesheets), docContext);
|
|
189
215
|
return docContext.stats;
|
|
190
216
|
}
|
|
191
217
|
|
|
218
|
+
function getShadowRootStylesheets(stylesheets) {
|
|
219
|
+
const groups = new Map();
|
|
220
|
+
stylesheets.forEach((stylesheetInfo, key) => {
|
|
221
|
+
const ownerElement = getStylesheetOwnerElement(key);
|
|
222
|
+
if (!key.urlNode && ownerElement && ownerElement.localName === STYLE_TAG_NAME && stylesheetInfo.scoped && stylesheetInfo.scope) {
|
|
223
|
+
const hostElement = stylesheetInfo.scope.parentElement;
|
|
224
|
+
const groupKey = (hostElement ? hostElement.localName : EMPTY_STRING) + LAYER_NAME_KEY_SEPARATOR + ownerElement.textContent;
|
|
225
|
+
let group = groups.get(groupKey);
|
|
226
|
+
if (!group) {
|
|
227
|
+
group = { roots: new Set(), stylesheets: [] };
|
|
228
|
+
groups.set(groupKey, group);
|
|
229
|
+
}
|
|
230
|
+
group.roots.add(stylesheetInfo.scope);
|
|
231
|
+
if (stylesheetInfo.stylesheet) {
|
|
232
|
+
group.stylesheets.push(stylesheetInfo.stylesheet);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
const shadowRootStylesheets = [];
|
|
237
|
+
groups.forEach(group => {
|
|
238
|
+
if (group.stylesheets.length === 1) {
|
|
239
|
+
shadowRootStylesheets.push({ stylesheet: group.stylesheets[0], roots: group.roots });
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
return shadowRootStylesheets;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function minifyShadowRootStylesheets(shadowRootStylesheets, docContext) {
|
|
246
|
+
shadowRootStylesheets.forEach(({ stylesheet, roots }) => {
|
|
247
|
+
if (hasChildNodes(stylesheet) && !hasDefaultNamespace(stylesheet.children)) {
|
|
248
|
+
removeUnmatchedShadowRootRules(stylesheet.children, roots, docContext);
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function removeUnmatchedShadowRootRules(cssRules, roots, docContext) {
|
|
254
|
+
for (let cssRule = cssRules.head; cssRule;) {
|
|
255
|
+
const nextCssRule = cssRule.next;
|
|
256
|
+
const ruleData = cssRule.data;
|
|
257
|
+
let removed = false;
|
|
258
|
+
if (ruleData.type === RULE_TYPE) {
|
|
259
|
+
docContext.stats.processed++;
|
|
260
|
+
removed = isShadowRootRuleUnmatched(ruleData, roots, docContext);
|
|
261
|
+
} else if (ruleData.type === AT_RULE_TYPE && hasChildNodes(ruleData.block)) {
|
|
262
|
+
const atRuleName = decodeName(ruleData.name).toLowerCase();
|
|
263
|
+
if (SHADOW_ROOT_CONDITIONAL_AT_RULE_NAMES.has(atRuleName)) {
|
|
264
|
+
removeUnmatchedShadowRootRules(ruleData.block.children, roots, docContext);
|
|
265
|
+
removed = SHADOW_ROOT_REMOVABLE_AT_RULE_NAMES.has(atRuleName) && !hasChildNodes(ruleData.block);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
if (removed) {
|
|
269
|
+
cssRules.remove(cssRule);
|
|
270
|
+
docContext.stats.discarded++;
|
|
271
|
+
}
|
|
272
|
+
cssRule = nextCssRule;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function isShadowRootRuleUnmatched(ruleData, roots, docContext) {
|
|
277
|
+
if (!hasChildNodes(ruleData.prelude) || !isPreludeSupported(ruleData.prelude, docContext)) {
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
|
|
281
|
+
if (!hasChildNodes(selector.data) || !isShadowRootSelectorQueryable(selector.data)) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
const matchedRoots = getMatchedShadowRoots(getShadowRootSelectorText(sanitizeSelector(selector, docContext), docContext), docContext);
|
|
285
|
+
if (!matchedRoots) {
|
|
286
|
+
return false;
|
|
287
|
+
}
|
|
288
|
+
for (const root of matchedRoots) {
|
|
289
|
+
if (roots.has(root)) {
|
|
290
|
+
return false;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function isShadowRootSelectorQueryable(selector) {
|
|
298
|
+
let queryable = true;
|
|
299
|
+
let functionalPseudoClassDepth = 0;
|
|
300
|
+
let notPseudoClassDepth = 0;
|
|
301
|
+
cssTree.walk(selector, {
|
|
302
|
+
enter(node) {
|
|
303
|
+
if (node.type === PSEUDO_ELEMENT_SELECTOR_TYPE) {
|
|
304
|
+
if (SHADOW_PSEUDO_ELEMENT_NAMES.has(decodeName(node.name).toLowerCase())) {
|
|
305
|
+
queryable = false;
|
|
306
|
+
}
|
|
307
|
+
} else if (node.type === PSEUDO_CLASS_SELECTOR_TYPE) {
|
|
308
|
+
const name = decodeName(node.name).toLowerCase();
|
|
309
|
+
if (SHADOW_HOST_PSEUDO_CLASS_NAMES.has(name) ||
|
|
310
|
+
(functionalPseudoClassDepth && SIBLING_DEPENDENT_PSEUDO_CLASS_NAMES.has(name)) ||
|
|
311
|
+
(notPseudoClassDepth && name === HAS_PSEUDO_CLASS_NAME)) {
|
|
312
|
+
queryable = false;
|
|
313
|
+
}
|
|
314
|
+
if (node.children) {
|
|
315
|
+
functionalPseudoClassDepth++;
|
|
316
|
+
if (name === NOT_PSEUDO_CLASS_NAME) {
|
|
317
|
+
notPseudoClassDepth++;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
} else if (node.type === NESTING_SELECTOR_TYPE) {
|
|
321
|
+
queryable = false;
|
|
322
|
+
} else if (node.type === COMBINATOR_NAME && functionalPseudoClassDepth && SIBLING_COMBINATOR_NAMES.has(node.name)) {
|
|
323
|
+
queryable = false;
|
|
324
|
+
}
|
|
325
|
+
},
|
|
326
|
+
leave(node) {
|
|
327
|
+
if (node.type === PSEUDO_CLASS_SELECTOR_TYPE && node.children) {
|
|
328
|
+
functionalPseudoClassDepth--;
|
|
329
|
+
if (decodeName(node.name).toLowerCase() === NOT_PSEUDO_CLASS_NAME) {
|
|
330
|
+
notPseudoClassDepth--;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
return queryable;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function getShadowRootSelectorText(selectorText, docContext) {
|
|
339
|
+
if (!docContext.shadowRootSelectorTexts.has(selectorText)) {
|
|
340
|
+
const selector = parseCss(selectorText);
|
|
341
|
+
let changed = false;
|
|
342
|
+
if (hasChildNodes(selector)) {
|
|
343
|
+
let lastSiblingCombinator;
|
|
344
|
+
for (let node = selector.children.head; node; node = node.next) {
|
|
345
|
+
if (node.data.type === COMBINATOR_NAME && SIBLING_COMBINATOR_NAMES.has(node.data.name)) {
|
|
346
|
+
lastSiblingCombinator = node;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (lastSiblingCombinator) {
|
|
350
|
+
while (selector.children.head !== lastSiblingCombinator) {
|
|
351
|
+
selector.children.remove(selector.children.head);
|
|
352
|
+
}
|
|
353
|
+
selector.children.remove(lastSiblingCombinator);
|
|
354
|
+
changed = true;
|
|
355
|
+
}
|
|
356
|
+
for (let node = selector.children.head; node;) {
|
|
357
|
+
const nextNode = node.next;
|
|
358
|
+
if (node.data.type === PSEUDO_CLASS_SELECTOR_TYPE && SIBLING_DEPENDENT_PSEUDO_CLASS_NAMES.has(decodeName(node.data.name).toLowerCase())) {
|
|
359
|
+
const startsCompound = !node.prev || node.prev.data.type === COMBINATOR_NAME;
|
|
360
|
+
const endsCompound = !node.next || node.next.data.type === COMBINATOR_NAME;
|
|
361
|
+
if (startsCompound && endsCompound) {
|
|
362
|
+
selector.children.replace(node, parseCss(UNIVERSAL_SELECTOR).children.head);
|
|
363
|
+
} else {
|
|
364
|
+
selector.children.remove(node);
|
|
365
|
+
}
|
|
366
|
+
changed = true;
|
|
367
|
+
}
|
|
368
|
+
node = nextNode;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
docContext.shadowRootSelectorTexts.set(selectorText, changed ? cssTree.generate(selector) : selectorText);
|
|
372
|
+
}
|
|
373
|
+
return docContext.shadowRootSelectorTexts.get(selectorText);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function getMatchedShadowRoots(selectorText, docContext) {
|
|
377
|
+
if (!docContext.shadowRootMatches.has(selectorText)) {
|
|
378
|
+
let matchedRoots;
|
|
379
|
+
try {
|
|
380
|
+
matchedRoots = new Set();
|
|
381
|
+
docContext.doc.querySelectorAll(selectorText).forEach(element => matchedRoots.add(element.closest(SHADOW_ROOT_SELECTOR)));
|
|
382
|
+
} catch {
|
|
383
|
+
matchedRoots = null;
|
|
384
|
+
}
|
|
385
|
+
docContext.shadowRootMatches.set(selectorText, matchedRoots);
|
|
386
|
+
}
|
|
387
|
+
return docContext.shadowRootMatches.get(selectorText);
|
|
388
|
+
}
|
|
389
|
+
|
|
192
390
|
function collectLayerOrder(stylesheets, docContext) {
|
|
193
391
|
stylesheets.forEach((stylesheetInfo, key) => {
|
|
194
392
|
if (!stylesheetInfo.scoped && stylesheetInfo.stylesheet && !key.urlNode) {
|