Browse Source

avoid embedding images multiple times in stylesheets

Gildas 7 years ago
parent
commit
a366d0e8f1
1 changed files with 12 additions and 10 deletions
  1. 12 10
      lib/single-file/single-file-core.js

+ 12 - 10
lib/single-file/single-file-core.js

@@ -773,6 +773,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 	const REGEXP_START_TAG = /</g;
 	const REGEXP_END_TAG = />/g;
 	const REGEXP_URL_HASH = /(#.+?)$/;
+	const PREFIX_DATA_URI_IMAGE = "data:image/";
+	const PREFIX_DATA_URI_IMAGE_SVG = "data:image/svg+xml";
 
 	class DomProcessorHelper {
 		static setFrameEmptySrc(frameElement) {
@@ -884,19 +886,19 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				const resourceURL = DomUtil.normalizeURL(originalResourceURL);
 				if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL) && stylesheetContent.includes(urlFunction)) {
 					const dataURI = await batchRequest.addURL(resourceURL);
-					const functions = stylesheetContent.match(DomUtil.getRegExp(urlFunction));
-					/*
-					if (!inline && options.compressCSS && functions.length > 1) {
-						const variableName = "--single-file-" + indexStyle + "-" + indexVariable;
-						stylesheetContent = variableName + ":url(\"" + dataURI + "\")" + (indexVariable ? ";" : "}") + stylesheetContent;
-						stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), "var(" + variableName + ")");
-						indexVariable++;
+					if (!inline && options.compressCSS && dataURI.startsWith(PREFIX_DATA_URI_IMAGE) && !dataURI.startsWith(PREFIX_DATA_URI_IMAGE_SVG)) {
+						const functions = stylesheetContent.match(DomUtil.getRegExp(urlFunction));
+						if (functions.length > 1) {
+							const variableName = "--single-file-" + indexStyle + "-" + indexVariable;
+							stylesheetContent = variableName + ":url(\"" + dataURI + "\")" + (indexVariable ? ";" : "}") + stylesheetContent;
+							stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), "var(" + variableName + ")");
+							indexVariable++;
+						} else {
+							stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, dataURI));
+						}
 					} else {
-						*/
 						stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, dataURI));
-						/*
 					}
-					*/
 				}
 			}));
 			if (indexVariable) {