Procházet zdrojové kódy

use "data-single-file-stylesheet" attribute to mark dynamic stylesheets

Gildas před 6 roky
rodič
revize
b4632b15c2

+ 6 - 2
lib/single-file/single-file-core.js

@@ -527,8 +527,12 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		replaceStyleContents() {
 		replaceStyleContents() {
 			if (this.options.stylesheets) {
 			if (this.options.stylesheets) {
 				this.doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
 				this.doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
-					if (this.options.stylesheets[styleIndex]) {
-						styleElement.textContent = this.options.stylesheets[styleIndex];
+					const attributeValue = styleElement.getAttribute(util.STYLESHEET_ATTRIBUTE_NAME);
+					if (attributeValue) {
+						const stylesheetContent = this.options.stylesheets[Number(styleIndex)];
+						if (stylesheetContent) {
+							styleElement.textContent = stylesheetContent;
+						}
 					}
 					}
 				});
 				});
 			}
 			}

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

@@ -35,6 +35,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	const HTML_IMPORT_ATTRIBUTE_NAME = "data-single-file-import";
 	const HTML_IMPORT_ATTRIBUTE_NAME = "data-single-file-import";
 	const INPUT_VALUE_ATTRIBUTE_NAME = "data-single-file-input-value";
 	const INPUT_VALUE_ATTRIBUTE_NAME = "data-single-file-input-value";
 	const LAZY_SRC_ATTRIBUTE_NAME = "data-single-file-lazy-loaded-src";
 	const LAZY_SRC_ATTRIBUTE_NAME = "data-single-file-lazy-loaded-src";
+	const STYLESHEET_ATTRIBUTE_NAME = "data-single-file-stylesheet";
 	const IGNORED_REMOVED_TAG_NAMES = ["NOSCRIPT", "DISABLED-NOSCRIPT", "META", "LINK", "STYLE", "TITLE", "TEMPLATE", "SOURCE", "OBJECT", "SCRIPT"];
 	const IGNORED_REMOVED_TAG_NAMES = ["NOSCRIPT", "DISABLED-NOSCRIPT", "META", "LINK", "STYLE", "TITLE", "TEMPLATE", "SOURCE", "OBJECT", "SCRIPT"];
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
@@ -58,7 +59,8 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		INPUT_VALUE_ATTRIBUTE_NAME,
 		INPUT_VALUE_ATTRIBUTE_NAME,
 		SHADOW_ROOT_ATTRIBUTE_NAME,
 		SHADOW_ROOT_ATTRIBUTE_NAME,
 		HTML_IMPORT_ATTRIBUTE_NAME,
 		HTML_IMPORT_ATTRIBUTE_NAME,
-		LAZY_SRC_ATTRIBUTE_NAME
+		LAZY_SRC_ATTRIBUTE_NAME,
+		STYLESHEET_ATTRIBUTE_NAME
 	};
 	};
 
 
 	function initDoc(doc) {
 	function initDoc(doc) {
@@ -287,6 +289,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 					const stylesheet = tempStyleElement.sheet;
 					const stylesheet = tempStyleElement.sheet;
 					tempStyleElement.remove();
 					tempStyleElement.remove();
 					if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
 					if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
+						styleElement.setAttribute(STYLESHEET_ATTRIBUTE_NAME, styleIndex);
 						contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
 						contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(cssRule => cssRule.cssText).join("\n");
 					}
 					}
 				} catch (error) {
 				} catch (error) {

+ 2 - 1
lib/single-file/single-file-util.js

@@ -157,7 +157,8 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 				HTML_IMPORT_ATTRIBUTE_NAME: modules.helper.HTML_IMPORT_ATTRIBUTE_NAME,
 				HTML_IMPORT_ATTRIBUTE_NAME: modules.helper.HTML_IMPORT_ATTRIBUTE_NAME,
 				INPUT_VALUE_ATTRIBUTE_NAME: modules.helper.INPUT_VALUE_ATTRIBUTE_NAME,
 				INPUT_VALUE_ATTRIBUTE_NAME: modules.helper.INPUT_VALUE_ATTRIBUTE_NAME,
 				SHADOW_ROOT_ATTRIBUTE_NAME: modules.helper.SHADOW_ROOT_ATTRIBUTE_NAME,
 				SHADOW_ROOT_ATTRIBUTE_NAME: modules.helper.SHADOW_ROOT_ATTRIBUTE_NAME,
-				PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME: modules.helper.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME
+				PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME: modules.helper.PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME,
+				STYLESHEET_ATTRIBUTE_NAME: modules.helper.STYLESHEET_ATTRIBUTE_NAME
 			};
 			};
 
 
 			async function getContent(resourceURL, options) {
 			async function getContent(resourceURL, options) {