Sfoglia il codice sorgente

support @charset in stylesheets retrieved with @import

Gildas 5 anni fa
parent
commit
fee517b715
1 ha cambiato i file con 22 aggiunte e 3 eliminazioni
  1. 22 3
      lib/single-file/single-file-core.js

+ 22 - 3
lib/single-file/single-file-core.js

@@ -942,9 +942,8 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			async function processElement(element, stylesheetInfo, stylesheets, baseURI, options, workStyleElement) {
 				stylesheets.set(element, stylesheetInfo);
 				let stylesheetContent = await getStylesheetContent(element, baseURI, options, workStyleElement);
-				const match = stylesheetContent.match(/^@charset\s+"([^"]*)";/i);
-				if (match && match[1] && match[1] != options.charset) {
-					options.charset = match[1];
+				if (!matchCharsetEquals(stylesheetContent, options.charset)) {
+					options.charset = getCharset(stylesheetContent);
 					stylesheetContent = await getStylesheetContent(element, baseURI, options, workStyleElement);
 				}
 				let stylesheet;
@@ -1653,6 +1652,10 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 					resourceReferrer: options.resourceReferrer,
 					validateTextContentType: true
 				});
+				if (!matchCharsetEquals(content.data, content.charset || options.charset)) {
+					options.charset = getCharset(content.data);
+					return ProcessorHelper.resolveLinkStylesheetURLs(resourceURL, baseURI, options, workStylesheet);
+				}
 				resourceURL = content.resourceURL;
 				content.data = getUpdatedResourceContent(content.resourceURL, content, options);
 				let stylesheetContent = removeCssComments(content.data);
@@ -1985,6 +1988,22 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		return result;
 	}
 
+	function matchCharsetEquals(stylesheetContent, charset = "utf-8") {
+		const stylesheetCharset = getCharset(stylesheetContent);
+		if (stylesheetCharset) {
+			return stylesheetCharset == charset.toLowerCase();
+		} else {
+			return true;
+		}
+	}
+
+	function getCharset(stylesheetContent) {
+		const match = stylesheetContent.match(/^@charset\s+"([^"]*)";/i);
+		if (match && match[1]) {
+			return match[1].toLowerCase().trim();
+		}
+	}
+
 	function getOnEventAttributeNames(doc) {
 		const element = doc.createElement("div");
 		const attributeNames = [];