single-file-core 1.3.12 → 1.3.13

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
@@ -256,9 +256,12 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
256
256
  shadowRootInfo.adoptedStyleSheets = getStylesheetsContent(shadowRoot.adoptedStyleSheets);
257
257
  } else if (shadowRoot.adoptedStyleSheets.length === undefined) {
258
258
  const listener = event => shadowRootInfo.adoptedStyleSheets = event.detail.adoptedStyleSheets;
259
- element.addEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
260
- element.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
261
- element.removeEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
259
+ shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
260
+ shadowRoot.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
261
+ if (!shadowRootInfo.adoptedStyleSheets) {
262
+ element.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
263
+ }
264
+ shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, listener);
262
265
  }
263
266
  }
264
267
  } catch (error) {
@@ -269,7 +272,7 @@ function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map
269
272
  shadowRootInfo.mode = shadowRoot.mode;
270
273
  try {
271
274
  if (shadowRoot.adoptedStyleSheets && shadowRoot.adoptedStyleSheets.length === undefined) {
272
- element.dispatchEvent(new CustomEvent(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
275
+ shadowRoot.dispatchEvent(new CustomEvent(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, { bubbles: true }));
273
276
  }
274
277
  } catch (error) {
275
278
  // ignored
package/core/index.js CHANGED
@@ -569,6 +569,7 @@ class Processor {
569
569
  const styleElement = this.doc.createElement("style");
570
570
  styleElement.textContent = "img[src=\"data:,\"],source[src=\"data:,\"]{display:none!important}";
571
571
  this.doc.head.appendChild(styleElement);
572
+ this.doc.head.querySelectorAll("noscript").forEach(element => element.parentElement.appendChild(element));
572
573
  let size;
573
574
  if (this.options.displayStats) {
574
575
  size = util.getContentSize(this.doc.documentElement.outerHTML);
@@ -1221,6 +1222,7 @@ class Processor {
1221
1222
  options.saveFavicon = false;
1222
1223
  options.includeInfobar = false;
1223
1224
  options.saveFilenameTemplateData = false;
1225
+ options.selected = false;
1224
1226
  options.url = frameData.baseURI;
1225
1227
  options.windowId = frameWindowId;
1226
1228
  if (frameData.content) {
@@ -127,8 +127,7 @@ const modules = [
127
127
  removeComments,
128
128
  removeEmptyAttributes,
129
129
  removeRedundantAttributes,
130
- compressJSONLD,
131
- node => mergeElements(node, "style", (node, previousSibling) => node.parentElement && getTagName(node.parentElement) == "HEAD" && node.media == previousSibling.media && node.title == previousSibling.title)
130
+ compressJSONLD
132
131
  ];
133
132
 
134
133
  export {
@@ -168,22 +167,6 @@ function mergeTextNodes(node) {
168
167
  }
169
168
  }
170
169
 
171
- function mergeElements(node, tagName, acceptMerge) {
172
- if (node.nodeType == Node_ELEMENT_NODE && getTagName(node) == tagName.toUpperCase()) {
173
- let previousSibling = node.previousSibling;
174
- const previousSiblings = [];
175
- while (previousSibling && previousSibling.nodeType == Node_TEXT_NODE && !previousSibling.textContent.trim()) {
176
- previousSiblings.push(previousSibling);
177
- previousSibling = previousSibling.previousSibling;
178
- }
179
- if (previousSibling && previousSibling.nodeType == Node_ELEMENT_NODE && previousSibling.tagName == node.tagName && acceptMerge(node, previousSibling)) {
180
- node.textContent = previousSibling.textContent + node.textContent;
181
- previousSiblings.forEach(node => node.remove());
182
- previousSibling.remove();
183
- }
184
- }
185
- }
186
-
187
170
  function collapseWhitespace(node, options) {
188
171
  if (node.nodeType == Node_TEXT_NODE) {
189
172
  let element = node.parentElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "single-file-core",
3
- "version": "1.3.12",
3
+ "version": "1.3.13",
4
4
  "description": "SingleFile Core",
5
5
  "author": "Gildas Lormeau",
6
6
  "license": "AGPL-3.0-or-later",
@@ -28,12 +28,9 @@ export {
28
28
  };
29
29
 
30
30
  async function display(document, docContent, { disableFramePointerEvents } = {}) {
31
- const DISABLED_NOSCRIPT_ATTRIBUTE_NAME = "data-single-file-disabled-noscript";
31
+ docContent = docContent.replace(/<noscript/gi, "<template disabled-noscript");
32
+ docContent = docContent.replaceAll(/<\/noscript/gi, "</template");
32
33
  const doc = (new DOMParser()).parseFromString(docContent, "text/html");
33
- doc.querySelectorAll("noscript:not([" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "])").forEach(element => {
34
- element.setAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME, element.innerHTML);
35
- element.textContent = "";
36
- });
37
34
  if (doc.doctype) {
38
35
  if (document.doctype) {
39
36
  document.replaceChild(doc.doctype, document.doctype);
@@ -51,9 +48,12 @@ async function display(document, docContent, { disableFramePointerEvents } = {})
51
48
  });
52
49
  }
53
50
  document.replaceChild(document.importNode(doc.documentElement, true), document.documentElement);
54
- document.querySelectorAll("[" + DISABLED_NOSCRIPT_ATTRIBUTE_NAME + "]").forEach(element => {
55
- element.textContent = element.getAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
56
- element.removeAttribute(DISABLED_NOSCRIPT_ATTRIBUTE_NAME);
51
+ document.querySelectorAll("template[disabled-noscript]").forEach(element => {
52
+ const noscriptElement = document.createElement("noscript");
53
+ element.removeAttribute("disabled-noscript");
54
+ Array.from(element.attributes).forEach(attribute => noscriptElement.setAttribute(attribute.name, attribute.value));
55
+ noscriptElement.textContent = element.innerHTML;
56
+ element.parentElement.replaceChild(noscriptElement, element);
57
57
  });
58
58
  document.documentElement.setAttribute("data-sfz", "");
59
59
  document.querySelectorAll("link[rel*=icon]").forEach(element => element.parentElement.replaceChild(element, element));
@@ -154,7 +154,7 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
154
154
  }
155
155
  if (!filename.match(REGEXP_MATCH_SCRIPT)) {
156
156
  const resourceFilename = filename;
157
- await Promise.all(resources.map(async innerResource => {
157
+ resources.forEach(innerResource => {
158
158
  const { filename, parentResources, content } = innerResource;
159
159
  if (filename.startsWith(prefixPath) && filename != resourceFilename) {
160
160
  const relativeFilename = filename.substring(prefixPath.length);
@@ -166,7 +166,7 @@ async function extract(content, { password, prompt = () => { }, shadowRootScript
166
166
  }
167
167
  }
168
168
  }
169
- }));
169
+ });
170
170
  resource.textContent = textContent;
171
171
  }
172
172
  if (filename.match(REGEXP_MATCH_INDEX)) {
@@ -382,11 +382,11 @@
382
382
  const shadowRoot = event.target.shadowRoot;
383
383
  event.stopPropagation();
384
384
  if (shadowRoot) {
385
- shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener, {});
385
+ shadowRoot.addEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener, { capture: true });
386
386
  shadowRoot.addEventListener(UNREGISTER_GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, () => shadowRoot.removeEventListener(GET_ADOPTED_STYLESHEETS_REQUEST_EVENT, getAdoptedStylesheetsListener), { once: true });
387
387
  const adoptedStyleSheets = Array.from(shadowRoot.adoptedStyleSheets).map(stylesheet => Array.from(stylesheet.cssRules).map(cssRule => cssRule.cssText).join("\n"));
388
388
  if (adoptedStyleSheets.length) {
389
- event.target.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, { detail: { adoptedStyleSheets } }));
389
+ shadowRoot.dispatchEvent(new CustomEvent(GET_ADOPTED_STYLESHEETS_RESPONSE_EVENT, { detail: { adoptedStyleSheets } }));
390
390
  }
391
391
  }
392
392
  }
@@ -28,6 +28,7 @@ import {
28
28
  COMMENT_HEADER_LEGACY,
29
29
  ON_BEFORE_CAPTURE_EVENT_NAME,
30
30
  ON_AFTER_CAPTURE_EVENT_NAME,
31
+ WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
31
32
  initUserScriptHandler,
32
33
  preProcessDoc,
33
34
  postProcessDoc,
@@ -40,6 +41,7 @@ const helper = {
40
41
  COMMENT_HEADER_LEGACY,
41
42
  ON_BEFORE_CAPTURE_EVENT_NAME,
42
43
  ON_AFTER_CAPTURE_EVENT_NAME,
44
+ WAIT_FOR_USERSCRIPT_PROPERTY_NAME,
43
45
  preProcessDoc,
44
46
  postProcessDoc,
45
47
  serialize(doc, compressHTML) {