single-file-core 1.5.25 → 1.5.26

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.
Files changed (2) hide show
  1. package/core/helper.js +25 -6
  2. package/package.json +1 -1
package/core/helper.js CHANGED
@@ -233,7 +233,7 @@ function preProcessDoc(doc, win, options) {
233
233
  };
234
234
  }
235
235
 
236
- function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], videos: [], shadowRoots: [], markedElements: [] }, ascendantHidden) {
236
+ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], videos: [], shadowRoots: [], markedElements: [] }, adoptedStyleSheetsCache = new Map(), ascendantHidden) {
237
237
  if (element.childNodes) {
238
238
  const elements = Array.from(element.childNodes).filter(node => (node instanceof win.HTMLElement) || (node instanceof win.SVGElement) || (node instanceof globalThis.HTMLElement) || (node instanceof globalThis.SVGElement));
239
239
  elements.forEach(element => {
@@ -278,7 +278,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
278
278
  try {
279
279
  if (shadowRoot.adoptedStyleSheets) {
280
280
  if (shadowRoot.adoptedStyleSheets.length) {
281
- shadowRootInfo.adoptedStyleSheets = getStylesheetsContent(shadowRoot.adoptedStyleSheets);
281
+ shadowRootInfo.adoptedStyleSheets = getStylesheetsContent(shadowRoot.adoptedStyleSheets, adoptedStyleSheetsCache);
282
282
  } else if (shadowRoot.adoptedStyleSheets.length === undefined) {
283
283
  const listener = event => shadowRootInfo.adoptedStyleSheets = event.detail.adoptedStyleSheets;
284
284
  shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
@@ -292,7 +292,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
292
292
  } catch (error) {
293
293
  // ignored
294
294
  }
295
- getElementsInfo(win, doc, shadowRoot, options, data, elementHidden);
295
+ getElementsInfo(win, doc, shadowRoot, options, data, adoptedStyleSheetsCache, elementHidden);
296
296
  shadowRootInfo.content = shadowRoot.innerHTML;
297
297
  shadowRootInfo.mode = shadowRoot.mode;
298
298
  shadowRootInfo.delegateFocus = shadowRoot.delegatesFocus;
@@ -306,7 +306,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
306
306
  // ignored
307
307
  }
308
308
  }
309
- getElementsInfo(win, doc, element, options, data, elementHidden);
309
+ getElementsInfo(win, doc, element, options, data, adoptedStyleSheetsCache, elementHidden);
310
310
  if (!options.autoSaveExternalSave && options.removeHiddenElements && ascendantHidden) {
311
311
  if (elementKept || element.getAttribute(KEPT_CONTENT_ATTRIBUTE_NAME) == "") {
312
312
  if (element.parentElement) {
@@ -323,8 +323,27 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
323
323
  return data;
324
324
  }
325
325
 
326
- function getStylesheetsContent(styleSheets) {
327
- return styleSheets ? Array.from(styleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n")) : [];
326
+ function getStylesheetsContent(styleSheets, adoptedStyleSheetsCache = new Map()) {
327
+ if (styleSheets) {
328
+ const result = [];
329
+ for (const styleSheet of styleSheets) {
330
+ if (adoptedStyleSheetsCache.has(styleSheet)) {
331
+ result.push(adoptedStyleSheetsCache.get(styleSheet));
332
+ } else {
333
+ let cssText = "";
334
+ if (styleSheet && styleSheet.cssRules) {
335
+ for (const cssRule of styleSheet.cssRules) {
336
+ cssText += cssRule.cssText + "\n";
337
+ }
338
+ }
339
+ adoptedStyleSheetsCache.set(styleSheet, cssText);
340
+ result.push(cssText);
341
+ }
342
+ }
343
+ return result;
344
+ } else {
345
+ return [];
346
+ }
328
347
  }
329
348
 
330
349
  function getResourcesInfo(win, doc, element, options, data, elementHidden, computedStyle) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.25",
3
+ "version": "1.5.26",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",