Bladeren bron

validate imported CSS content before inserting it into the stylesheet

Gildas 7 jaren geleden
bovenliggende
commit
238d4bb98b
1 gewijzigde bestanden met toevoegingen van 15 en 7 verwijderingen
  1. 15 7
      lib/single-file/single-file-core.js

+ 15 - 7
lib/single-file/single-file-core.js

@@ -362,10 +362,13 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 					return this.loadPage(pageContent, charset);
 					return this.loadPage(pageContent, charset);
 				}
 				}
 			}
 			}
+			this.workStyleElement = this.doc.createElement("style");
+			this.doc.body.appendChild(this.workStyleElement);
 			this.onEventAttributeNames = Util.getOnEventAttributeNames(this.doc);
 			this.onEventAttributeNames = Util.getOnEventAttributeNames(this.doc);
 		}
 		}
 
 
 		async finalize() {
 		async finalize() {
+			this.workStyleElement.remove();
 			const metaCharset = this.doc.head.querySelector("meta[charset]");
 			const metaCharset = this.doc.head.querySelector("meta[charset]");
 			if (metaCharset) {
 			if (metaCharset) {
 				this.doc.head.insertBefore(metaCharset, this.doc.head.firstChild);
 				this.doc.head.insertBefore(metaCharset, this.doc.head.firstChild);
@@ -746,9 +749,9 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 						if (element.charset) {
 						if (element.charset) {
 							options.charset = element.charset;
 							options.charset = element.charset;
 						}
 						}
-						stylesheetContent = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, this.baseURI, options);
+						stylesheetContent = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, this.baseURI, options, this.workStyleElement);
 					} else {
 					} else {
-						stylesheetContent = await ProcessorHelper.resolveImportURLs(element.textContent, this.baseURI, options);
+						stylesheetContent = await ProcessorHelper.resolveImportURLs(element.textContent, this.baseURI, options, this.workStyleElement);
 					}
 					}
 					let stylesheet;
 					let stylesheet;
 					try {
 					try {
@@ -1192,7 +1195,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			removedRules.forEach(cssRule => stylesheet.children.remove(cssRule));
 			removedRules.forEach(cssRule => stylesheet.children.remove(cssRule));
 		}
 		}
 
 
-		static async resolveImportURLs(stylesheetContent, baseURI, options) {
+		static async resolveImportURLs(stylesheetContent, baseURI, options, workStylesheet) {
 			stylesheetContent = ProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI, options);
 			stylesheetContent = ProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI, options);
 			const imports = Util.getImportFunctions(stylesheetContent);
 			const imports = Util.getImportFunctions(stylesheetContent);
 			await Promise.all(imports.map(async cssImport => {
 			await Promise.all(imports.map(async cssImport => {
@@ -1215,8 +1218,13 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 							}
 							}
 							importedStylesheetContent = Util.wrapMediaQuery(importedStylesheetContent, match.media);
 							importedStylesheetContent = Util.wrapMediaQuery(importedStylesheetContent, match.media);
 							if (stylesheetContent.includes(cssImport)) {
 							if (stylesheetContent.includes(cssImport)) {
-								importedStylesheetContent = await ProcessorHelper.resolveImportURLs(importedStylesheetContent, resourceURL, options);
-								stylesheetContent = stylesheetContent.replace(Util.getRegExp(cssImport), importedStylesheetContent);
+								importedStylesheetContent = await ProcessorHelper.resolveImportURLs(importedStylesheetContent, resourceURL, options, workStylesheet);
+								workStylesheet.textContent = importedStylesheetContent;
+								if (workStylesheet.sheet.cssRules.length) {
+									stylesheetContent = stylesheetContent.replace(Util.getRegExp(cssImport), importedStylesheetContent);
+								} else {
+									stylesheetContent = stylesheetContent.replace(Util.getRegExp(cssImport), "");
+								}
 							}
 							}
 						}
 						}
 					}
 					}
@@ -1262,7 +1270,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			return stylesheetContent;
 			return stylesheetContent;
 		}
 		}
 
 
-		static async resolveLinkStylesheetURLs(resourceURL, baseURI, options) {
+		static async resolveLinkStylesheetURLs(resourceURL, baseURI, options, workStylesheet) {
 			resourceURL = Util.normalizeURL(resourceURL);
 			resourceURL = Util.normalizeURL(resourceURL);
 			if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
 			if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
 				const downloadOptions = { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled, charset: options.charset };
 				const downloadOptions = { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled, charset: options.charset };
@@ -1272,7 +1280,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				if (options.compressCSS) {
 				if (options.compressCSS) {
 					stylesheetContent = DocUtil.compressCSS(stylesheetContent);
 					stylesheetContent = DocUtil.compressCSS(stylesheetContent);
 				}
 				}
-				stylesheetContent = await ProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL, options);
+				stylesheetContent = await ProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL, options, workStylesheet);
 				return stylesheetContent;
 				return stylesheetContent;
 			}
 			}
 		}
 		}