Browse Source

call insertShadowRootContents before remove* functions

Gildas 6 years ago
parent
commit
0dc1d3d2e3
1 changed files with 13 additions and 8 deletions
  1. 13 8
      lib/single-file/single-file-core.js

+ 13 - 8
lib/single-file/single-file-core.js

@@ -86,19 +86,19 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		sequential: [
 			{ action: "preProcessPage" },
 			{ action: "replaceStyleContents" },
+			{ action: "resetCharsetMeta" },
+			{ option: "insertFaviconLink", action: "insertFaviconLink" },
+			{ action: "replaceCanvasElements" },
+			{ action: "insertFonts" },
+			{ action: "insertShadowRootContents" },
+			{ action: "setInputValues" },
+			{ option: "removeScripts", action: "removeScripts" },
 			{ option: "selected", action: "removeUnselectedElements" },
 			{ option: "removeVideoSrc", action: "insertVideoPosters" },
 			{ option: "removeFrames", action: "removeFrames" },
-			{ option: "removeScripts", action: "removeScripts" },
 			{ option: "removeVideoSrc", action: "removeVideoSources" },
 			{ option: "removeAudioSrc", action: "removeAudioSources" },
 			{ action: "removeDiscardedResources" },
-			{ action: "resetCharsetMeta" },
-			{ action: "setInputValues" },
-			{ option: "insertFaviconLink", action: "insertFaviconLink" },
-			{ action: "replaceCanvasElements" },
-			{ action: "insertFonts" },
-			{ action: "insertShadowRootContents" },
 			{ option: "removeHiddenElements", action: "removeHiddenElements" },
 			{ action: "resolveHrefs" },
 			{ action: "resolveStyleAttributeURLs" }
@@ -332,6 +332,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 	const NOSCRIPT_TAG_FOUND = /<noscript/gi;
 	const SHADOW_MODE_ATTRIBUTE_NAME = "shadowmode";
 	const SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME = "delegatesfocus";
+	const SCRIPT_TEMPLATE_SHADOW_ROOT = "data-template-shadow-root";
 
 	class Processor {
 		constructor(options, batchRequest) {
@@ -618,7 +619,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					element.removeAttribute("src");
 				}
 			});
-			const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"])");
+			const scriptElements = this.doc.querySelectorAll("script:not([type=\"application/ld+json\"]):not([" + SCRIPT_TEMPLATE_SHADOW_ROOT + "])");
 			this.stats.set("discarded", "scripts", scriptElements.length);
 			this.stats.set("processed", "scripts", scriptElements.length);
 			scriptElements.forEach(element => element.remove());
@@ -910,7 +911,11 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			const options = this.options;
 			if (this.options.shadowRoots && this.options.shadowRoots.length) {
 				processElement(this.doc);
+				if (this.options.removeScripts) {
+					this.doc.querySelectorAll("script[" + SCRIPT_TEMPLATE_SHADOW_ROOT + "]").forEach(element => element.remove());
+				}
 				const scriptElement = doc.createElement("script");
+				scriptElement.setAttribute(SCRIPT_TEMPLATE_SHADOW_ROOT, "");
 				scriptElement.textContent = `(()=>{document.currentScript.remove();processNode(document);function processNode(node){node.querySelectorAll("template[${SHADOW_MODE_ATTRIBUTE_NAME}]").forEach(element=>{if (!element.parentElement.shadowRoot) {const shadowRoot=element.parentElement.attachShadow({mode:element.getAttribute("${SHADOW_MODE_ATTRIBUTE_NAME}"),delegatesFocus:Boolean(element.getAttribute("${SHADOW_DELEGATE_FOCUS_ATTRIBUTE_NAME}"))});shadowRoot.innerHTML=element.innerHTML;element.remove();processNode(shadowRoot)}})}})()`;
 				doc.body.appendChild(scriptElement);
 			}