Bläddra i källkod

use CSSOM to retrieve stylesheet content only when needed

Gildas 7 år sedan
förälder
incheckning
5405b818be
1 ändrade filer med 9 tillägg och 1 borttagningar
  1. 9 1
      lib/single-file/doc-helper.js

+ 9 - 1
lib/single-file/doc-helper.js

@@ -117,11 +117,19 @@ this.docHelper = this.docHelper || (() => {
 		if (doc) {
 		if (doc) {
 			const contents = [];
 			const contents = [];
 			doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
 			doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
+				let stylesheet;
 				try {
 				try {
-					contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n");
+					const tempStyleElement = doc.createElement("style");
+					tempStyleElement.textContent = styleElement.textContent;
+					doc.body.appendChild(tempStyleElement);
+					stylesheet = tempStyleElement.sheet;
+					tempStyleElement.remove();
 				} catch (error) {
 				} catch (error) {
 					/* ignored */
 					/* ignored */
 				}
 				}
+				if (!stylesheet || stylesheet.cssRules.length != styleElement.sheet.cssRules.length) {
+					contents[styleIndex] = Array.from(styleElement.sheet.cssRules).map(rule => rule.cssText).join("\n");
+				}
 			});
 			});
 			return contents;
 			return contents;
 		}
 		}