single-file-core 1.12.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 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
- if (!styleElementGroups.has(content)) {
1504
- styleElementGroups.set(content, []);
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(content).push(styleElement);
1510
+ styleElementGroups.get(groupKey).push(styleElement);
1507
1511
  }
1508
1512
  });
1509
1513
  let indexContent = 0;
1510
- styleElementGroups.forEach((styleElements, content) => {
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],
@@ -40,7 +40,6 @@ const HTTP_URI_PREFIX = /^https?:\/\//;
40
40
  const FILE_URI_PREFIX = /^file:\/\//;
41
41
  const EMPTY_URL = /^https?:\/\/+\s*$/;
42
42
  const NOT_EMPTY_URL = /^(https?:\/\/|file:\/\/|blob:).+/;
43
- const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
44
43
  const REFERRER_POLICY_ELEMENTS = ["IMG", "LINK", "SCRIPT"];
45
44
  const UTF8_CHARSET = "utf-8";
46
45
  const REGEXP_URL_FUNCTION = /(url|local|-sf-url-original)\(.*?\)\s*(,|$)/g;
@@ -66,6 +65,7 @@ const OffscreenCanvas = globalThis.OffscreenCanvas;
66
65
  const DEFAULT_IMAGE_QUALITY = 0.8;
67
66
  const SVG_NAMESPACE = "http://www.w3.org/2000/svg";
68
67
  const XMLNS_NAMESPACE = "http://www.w3.org/2000/xmlns/";
68
+ const SVG_ROOT_ID_PREFIX = "sf-svg-";
69
69
 
70
70
  let util, cssTree;
71
71
 
@@ -166,13 +166,19 @@ class ProcessorHelperCommon {
166
166
  const response = await batchRequest.addURL(resourceURL, { expectedType: "image" });
167
167
  const svgDoc = util.parseSVGContent(response.content);
168
168
  if (hashMatch && hashMatch[0]) {
169
- const symbolElement = getFragmentElement(svgDoc, hashMatch[0]);
170
- if (symbolElement) {
169
+ const fragmentElement = getFragmentElement(svgDoc, hashMatch[0]);
170
+ if (fragmentElement) {
171
171
  resourceElement.setAttribute(attributeName, hashMatch[0]);
172
- resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
172
+ insertReferencedElement(doc, resourceElement, fragmentElement);
173
173
  }
174
174
  } else {
175
- resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + response.content);
175
+ const rootElement = svgDoc.documentElement;
176
+ if (rootElement && rootElement.localName == "svg") {
177
+ const id = getUnusedId(doc, svgDoc);
178
+ rootElement.setAttribute("id", id);
179
+ resourceElement.setAttribute(attributeName, "#" + id);
180
+ insertReferencedElement(doc, resourceElement, rootElement);
181
+ }
176
182
  }
177
183
  }
178
184
  }
@@ -935,6 +941,24 @@ function getURLHash(url) {
935
941
  return hashMatch ? hashMatch[0] : "";
936
942
  }
937
943
 
944
+ function insertReferencedElement(doc, resourceElement, element) {
945
+ const parentElement = resourceElement.parentElement;
946
+ if (element.localName != "symbol") {
947
+ const defsElement = doc.createElementNS(SVG_NAMESPACE, "defs");
948
+ defsElement.appendChild(element);
949
+ element = defsElement;
950
+ }
951
+ parentElement.insertBefore(element, parentElement.firstChild);
952
+ }
953
+
954
+ function getUnusedId(doc, svgDoc) {
955
+ let index = 0;
956
+ while (doc.getElementById(SVG_ROOT_ID_PREFIX + index) || svgDoc.getElementById(SVG_ROOT_ID_PREFIX + index)) {
957
+ index++;
958
+ }
959
+ return SVG_ROOT_ID_PREFIX + index;
960
+ }
961
+
938
962
  function getFragmentElement(svgDoc, hash) {
939
963
  const fragmentId = hash.substring(1);
940
964
  let element = svgDoc.getElementById(fragmentId);
@@ -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) {
@@ -99,7 +99,7 @@ function normalizeSelectorNode(selector) {
99
99
  } else if (childNode.type === "PseudoClassSelector") {
100
100
  if (matchUnqueryablePseudoClass(childNode)) {
101
101
  removeNode(selector.children, current);
102
- } else if (childNode.children && MATCHING_FUNCTIONAL_PSEUDO_CLASSES.includes(childNode.name.toLowerCase())) {
102
+ } else if (childNode.children && childNode.children.head && MATCHING_FUNCTIONAL_PSEUDO_CLASSES.includes(childNode.name.toLowerCase())) {
103
103
  normalizeSelectorNode(childNode.children.head.data);
104
104
  }
105
105
  } else if (childNode.type === "AttributeSelector") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.12.0",
3
+ "version": "1.14.0",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -35,6 +35,8 @@ const MAX_IDLE_TIMEOUT_CALLS = 10;
35
35
  const ATTRIBUTES_MUTATION_TYPE = "attributes";
36
36
  const CHILD_LIST_MUTATION_TYPE = "childList";
37
37
  const STYLESHEET_TAG_NAMES = ["STYLE", "LINK"];
38
+ const SCROLLABLE_OVERFLOW_VALUES = ["auto", "scroll", "overlay"];
39
+ const SCROLLABLE_ELEMENT_MIN_SIZE_RATIO = 1 / 3;
38
40
 
39
41
  const browser = globalThis.browser;
40
42
  const document = globalThis.document;
@@ -76,10 +78,38 @@ async function process(options) {
76
78
  if (globalThis.scrollY < maxScrollY || globalThis.scrollX < maxScrollX) {
77
79
  return triggerLazyLoading(options);
78
80
  }
81
+ } else if (findScrollableElement()) {
82
+ return triggerLazyLoading(options);
79
83
  }
80
84
  }
81
85
  }
82
86
 
87
+ function findScrollableElement() {
88
+ const minHeight = globalThis.innerHeight * SCROLLABLE_ELEMENT_MIN_SIZE_RATIO;
89
+ const minWidth = globalThis.innerWidth * SCROLLABLE_ELEMENT_MIN_SIZE_RATIO;
90
+ let elements = [document.documentElement];
91
+ while (elements.length) {
92
+ const descendants = [];
93
+ for (const element of elements) {
94
+ for (const child of element.children) {
95
+ if (child.clientHeight >= minHeight && child.clientWidth >= minWidth) {
96
+ if (isScrollable(child)) {
97
+ return child;
98
+ }
99
+ descendants.push(child);
100
+ }
101
+ }
102
+ }
103
+ elements = descendants;
104
+ }
105
+ }
106
+
107
+ function isScrollable(element) {
108
+ const style = globalThis.getComputedStyle(element);
109
+ return (SCROLLABLE_OVERFLOW_VALUES.includes(style.overflowY) && element.scrollHeight > element.clientHeight) ||
110
+ (SCROLLABLE_OVERFLOW_VALUES.includes(style.overflowX) && element.scrollWidth > element.clientWidth);
111
+ }
112
+
83
113
  function resetZoomLevel(options) {
84
114
  hooksFrames.loadDeferredContentResetZoomLevel(options);
85
115
  }