single-file-core 1.5.69 → 1.5.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/core/helper.js CHANGED
@@ -389,9 +389,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
389
389
  data.shadowRoots.push(shadowRootInfo);
390
390
  try {
391
391
  if (shadowRoot.adoptedStyleSheets) {
392
- if (shadowRoot.adoptedStyleSheets.length) {
393
- shadowRootInfo.adoptedStyleSheets = getStylesheetsContent(shadowRoot.adoptedStyleSheets, adoptedStyleSheetsCache);
394
- } else if (shadowRoot.adoptedStyleSheets.length === undefined) {
392
+
395
393
  const listener = event => shadowRootInfo.adoptedStyleSheets = event.detail.adoptedStyleSheets;
396
394
  shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
397
395
  shadowRoot.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
@@ -399,7 +397,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
399
397
  element.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
400
398
  }
401
399
  shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
402
- }
400
+
403
401
  }
404
402
  // eslint-disable-next-line no-unused-vars
405
403
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.5.69",
3
+ "version": "1.5.71",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -76,11 +76,13 @@
76
76
  const JSON = globalThis.JSON;
77
77
  const MutationObserver = globalThis.MutationObserver;
78
78
  const URL = globalThis.URL;
79
+ const CSSStyleSheet = globalThis.CSSStyleSheet;
79
80
 
80
81
  const observers = new Map();
81
82
  const observedElements = new Map();
82
83
 
83
84
  let dispatchScrollEvent;
85
+ let adoptedStylesheetsData = new WeakMap();
84
86
 
85
87
  init();
86
88
  new MutationObserver(init).observe(document, { childList: true });
@@ -438,13 +440,70 @@
438
440
  globalThis.IntersectionObserver.toString = function () { return "function IntersectionObserver() { [native code] }"; };
439
441
  }
440
442
 
443
+ const originalReplaceSync = CSSStyleSheet.prototype.replaceSync;
444
+ CSSStyleSheet.prototype.replaceSync = function (text) {
445
+ try {
446
+ const result = originalReplaceSync.apply(this, [text]);
447
+ adoptedStylesheetsData.set(this, text);
448
+ return result;
449
+ } catch (error) {
450
+ error.stack = error.message + "\n" + " \n" + error.stack.trim().split("\n").slice(-1).join("\n");
451
+ throw error;
452
+ }
453
+ };
454
+ CSSStyleSheet.prototype.replaceSync.toString = function () { return "function replaceSync() { [native code] }"; };
455
+ const orginalReplace = CSSStyleSheet.prototype.replace;
456
+ CSSStyleSheet.prototype.replace = async function (text) {
457
+ try {
458
+ const result = await orginalReplace.apply(this, [text]);
459
+ adoptedStylesheetsData.set(this, text);
460
+ return result;
461
+ } catch (error) {
462
+ error.stack = error.message + "\n" + " \n" + error.stack.trim().split("\n").slice(-1).join("\n");
463
+ throw error;
464
+ }
465
+ };
466
+ CSSStyleSheet.prototype.replace.toString = function () { return "function replace() { [native code] }"; };
467
+ const originalInsertRule = CSSStyleSheet.prototype.insertRule;
468
+ CSSStyleSheet.prototype.insertRule = function (rule, index) {
469
+ try {
470
+ const result = originalInsertRule.apply(this, [rule, index]);
471
+ adoptedStylesheetsData.delete(this);
472
+ return result;
473
+ } catch (error) {
474
+ error.stack = error.message + "\n" + " \n" + error.stack.trim().split("\n").slice(-1).join("\n");
475
+ throw error;
476
+ }
477
+ };
478
+ CSSStyleSheet.prototype.insertRule.toString = function () { return "function insertRule() { [native code] }"; };
479
+ const originalDeleteRule = CSSStyleSheet.prototype.deleteRule;
480
+ CSSStyleSheet.prototype.deleteRule = function (index) {
481
+ try {
482
+ const result = originalDeleteRule.apply(this, [index]);
483
+ adoptedStylesheetsData.delete(this);
484
+ return result;
485
+ } catch (error) {
486
+ error.stack = error.message + "\n" + " \n" + error.stack.trim().split("\n").slice(-1).join("\n");
487
+ throw error;
488
+ }
489
+ };
490
+ CSSStyleSheet.prototype.deleteRule.toString = function () { return "function deleteRule() { [native code] }"; };
491
+
441
492
  function getAdoptedStylesheetsListener(event) {
442
493
  const shadowRoot = event.target.shadowRoot;
443
494
  event.stopPropagation();
444
495
  if (shadowRoot) {
445
496
  shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener, { capture: true });
446
- shadowRoot.addEventListener(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, () => shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener), { once: true });
447
- const adoptedStyleSheets = Array.from(shadowRoot.adoptedStyleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n"));
497
+ shadowRoot.addEventListener(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, () => {
498
+ shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener);
499
+ }, { once: true });
500
+ const adoptedStyleSheets = Array.from(shadowRoot.adoptedStyleSheets).map(stylesheet => {
501
+ if (adoptedStylesheetsData.has(stylesheet)) {
502
+ return adoptedStylesheetsData.get(stylesheet);
503
+ } else {
504
+ return Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
505
+ }
506
+ });
448
507
  if (adoptedStyleSheets.length) {
449
508
  shadowRoot.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, { detail: { adoptedStyleSheets } }));
450
509
  }