Ver código fonte

fixed charset issue (cf. #248)

Former-commit-id: a1b817e2cc6c6f32800b3e3e23d6363db1b54ac8
Gildas 6 anos atrás
pai
commit
22669c8ee1
1 arquivos alterados com 16 adições e 4 exclusões
  1. 16 4
      lib/single-file/single-file-core.js

+ 16 - 4
lib/single-file/single-file-core.js

@@ -849,14 +849,18 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						charset: this.charset,
 						compressCSS: this.options.compressCSS
 					};
-					let stylesheetContent;
 					if (element.tagName.toLowerCase() == "link") {
 						if (element.charset) {
 							options.charset = element.charset;
 						}
-						stylesheetContent = await ProcessorHelper.resolveLinkStylesheetURLs(element.href, this.baseURI, options, this.workStyleElement);
-					} else {
-						stylesheetContent = await ProcessorHelper.resolveImportURLs(element.textContent, this.baseURI, options, this.workStyleElement);
+					}
+					let stylesheetContent = await getStylesheetContent(element, this.baseURI, options, this.workStyleElement);
+					const match = stylesheetContent.match(/^@charset\s+"([^"]*)";/i);
+					if (match && match[1] && match[1] != options.charset) {
+						if (match && match[1] && match[1] != options.charset) {
+							options.charset = match[1];
+							stylesheetContent = await getStylesheetContent(element, this.baseURI, options, this.workStyleElement);
+						}
 					}
 					let stylesheet;
 					try {
@@ -873,6 +877,14 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 						this.stylesheets.delete(element);
 					}
 				}));
+
+			async function getStylesheetContent(element, baseURI, options, workStyleElement) {
+				if (element.tagName.toLowerCase() == "link") {
+					return ProcessorHelper.resolveLinkStylesheetURLs(element.href, baseURI, options, workStyleElement);
+				} else {
+					return ProcessorHelper.resolveImportURLs(element.textContent, baseURI, options, workStyleElement);
+				}
+			}
 		}
 
 		async resolveFrameURLs() {