Explorar el Código

always insert stylesheets coming from HTML imports into the doc

Gildas hace 6 años
padre
commit
42e27328cf
Se han modificado 2 ficheros con 12 adiciones y 14 borrados
  1. 11 14
      lib/single-file/single-file-core.js
  2. 1 0
      lib/single-file/single-file-helper.js

+ 11 - 14
lib/single-file/single-file-core.js

@@ -89,7 +89,6 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			{ option: "selected", action: "removeUnselectedElements" },
 			{ option: "removeVideoSrc", action: "insertVideoPosters" },
 			{ option: "removeFrames", action: "removeFrames" },
-			{ option: "removeImports", action: "removeImports" },
 			{ option: "removeScripts", action: "removeScripts" },
 			{ option: "removeVideoSrc", action: "removeVideoSources" },
 			{ option: "removeAudioSrc", action: "removeAudioSources" },
@@ -107,7 +106,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		parallel: [
 			{ action: "resolveStylesheetURLs" },
 			{ option: "!removeFrames", action: "resolveFrameURLs" },
-			{ option: "!removeImports", action: "resolveHtmlImportURLs" }
+			{ action: "resolveHtmlImportURLs" }
 		]
 	}, {
 		sequential: [
@@ -963,9 +962,17 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						const processor = new Runner(options);
 						this.relImportProcessors.set(linkElement, processor);
 						await processor.loadPage();
-						return processor.initialize();
+						await processor.initialize();
+						processor.processor.stylesheets.forEach(stylesheet => {
+							const importedStyleElement = this.doc.createElement("style");
+							linkElement.insertAdjacentElement("afterEnd", importedStyleElement);
+							this.stylesheets.set(importedStyleElement, stylesheet);
+						});
 					}
 				}
+				if (options.removeImports) {
+					linkElement.remove();
+				}
 			}));
 		}
 
@@ -1100,18 +1107,8 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 				if (processor) {
 					this.stats.add("processed", "HTML imports", 1);
 					this.relImportProcessors.delete(linkElement);
+					await processor.run();
 					const pageData = await processor.getPageData();
-					processor.getDocument().querySelectorAll("style").forEach(styleElement => {
-						const stylesheet = processor.processor.stylesheets.get(styleElement);
-						if (stylesheet) {
-							const importedStyleElement = this.doc.createElement("style");
-							importedStyleElement.media = styleElement.media;
-							importedStyleElement.textContent = styleElement.textContent;
-							linkElement.parentElement.insertBefore(importedStyleElement, linkElement);
-							const stylesheet = processor.processor.stylesheets.get(styleElement);
-							this.stylesheets.set(importedStyleElement, stylesheet);
-						}
-					});
 					linkElement.setAttribute("href", "data:text/html," + pageData.content);
 					this.stats.addAll(pageData);
 				} else {

+ 1 - 0
lib/single-file/single-file-helper.js

@@ -269,6 +269,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		doc.querySelectorAll("[" + CANVAS_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(CANVAS_ATTRIBUTE_NAME));
 		doc.querySelectorAll("[" + INPUT_VALUE_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME));
 		doc.querySelectorAll("[" + SHADOW_ROOT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME));
+		doc.querySelectorAll("[" + HTML_IMPORT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(HTML_IMPORT_ATTRIBUTE_NAME));
 	}
 
 	function getStylesheetsData(doc) {