Преглед изворни кода

use charset of the document if present when dowloading CSS

Gildas пре 7 година
родитељ
комит
e5bddd9b59
2 измењених фајлова са 15 додато и 5 уклоњено
  1. 2 2
      lib/single-file/single-file-browser.js
  2. 13 3
      lib/single-file/single-file-core.js

+ 2 - 2
lib/single-file/single-file-browser.js

@@ -103,8 +103,8 @@ this.SingleFile = this.SingleFile || (() => {
 			} else {
 				const matchCharset = contentType && contentType.match(/\s*;\s*charset\s*=\s*"?([^";]*)"?(;|$)/i);
 				let charSet;
-				if (matchCharset && matchCharset[1]) {
-					charSet = matchCharset[1].toLowerCase();
+				if (matchCharset && matchCharset[1] || options.charSet) {
+					charSet = (matchCharset && matchCharset[1].toLowerCase()) || options.charSet;
 				}
 				if (!charSet) {
 					charSet = "utf-8";

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

@@ -451,7 +451,16 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		}
 
 		resetCharsetMeta() {
-			this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => element.remove());
+			this.doc.querySelectorAll("meta[charset], meta[http-equiv=\"content-type\"]").forEach(element => {
+				const charSetDeclaration = element.content.split(";")[1];
+				if (charSetDeclaration) {
+					const charSet = charSetDeclaration.split("=")[1]
+					if (charSet) {
+						this.charSet = charSet.trim().toLowerCase();
+					}
+				}
+				element.remove();
+			});
 			const metaElement = this.doc.createElement("meta");
 			metaElement.setAttribute("charset", "utf-8");
 			this.doc.head.insertBefore(metaElement, this.doc.head.firstElementChild);
@@ -839,7 +848,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 
 		async resolveLinkedStylesheetURLs() {
 			await Promise.all(Array.from(this.doc.querySelectorAll("link[rel*=stylesheet]")).map(async linkElement => {
-				const stylesheetContent = await DomProcessorHelper.resolveLinkStylesheetURLs(linkElement.href, this.baseURI, linkElement.media, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
+				const options = { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled, charSet: this.charSet };
+				const stylesheetContent = await DomProcessorHelper.resolveLinkStylesheetURLs(linkElement.href, this.baseURI, linkElement.media, options);
 				const styleElement = this.doc.createElement("style");
 				styleElement.textContent = stylesheetContent;
 				linkElement.parentElement.replaceChild(styleElement, linkElement);
@@ -1007,7 +1017,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		static async resolveLinkStylesheetURLs(resourceURL, baseURI, media, options) {
 			resourceURL = DomUtil.normalizeURL(resourceURL);
 			if (resourceURL && resourceURL != baseURI && resourceURL != ABOUT_BLANK_URI) {
-				let stylesheetContent = await Download.getContent(resourceURL, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled });
+				let stylesheetContent = await Download.getContent(resourceURL, { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled, charSet: options.charSet });
 				stylesheetContent = await DomProcessorHelper.resolveImportURLs(stylesheetContent, resourceURL, options);
 				stylesheetContent = DomUtil.wrapMediaQuery(stylesheetContent, media);
 				return stylesheetContent;