single-file-core 1.12.0 → 1.13.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.
@@ -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);
@@ -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.13.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
  }