Просмотр исходного кода

group images in CSS only when url() call is not nested into another function call

Gildas 7 лет назад
Родитель
Сommit
abcfe2981c
1 измененных файлов с 13 добавлено и 9 удалено
  1. 13 9
      lib/single-file/single-file-core.js

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

@@ -1317,16 +1317,20 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 						if (!Util.testIgnoredPath(resourceURL)) {
 							if (Util.testValidURL(resourceURL, baseURI, options.url)) {
 								let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, true, true);
-								if (duplicate && options.groupDuplicateImages) {
-									const tokens = [];
-									findURLToken(originalResourceURL, declaration.value.children, (token, parent) => {
+								let variableDefined;
+								const tokens = [];
+								findURLToken(originalResourceURL, declaration.value.children, (token, parent, rootFunction) => {
+									if (duplicate && options.groupDuplicateImages && rootFunction) {
 										const value = cssTree.parse("var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")", { context: "value" }).children.head;
 										tokens.push({ parent, token, value });
-									});
+										variableDefined = true;
+									} else {
+										token.data.value.value = content;
+									}
+								});
+								if (variableDefined) {
 									cssVariables.set(indexResource, content);
 									tokens.forEach(({ parent, token, value }) => parent.replace(token, value));
-								} else {
-									findURLToken(originalResourceURL, declaration.value.children, token => token.data.value.value = content);
 								}
 							}
 						}
@@ -1334,13 +1338,13 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				}
 			}));
 
-			function findURLToken(url, children, callback) {
+			function findURLToken(url, children, callback, depth = 0) {
 				for (let token = children.head; token; token = token.next) {
 					if (token.data.children) {
-						findURLToken(url, token.data.children, callback);
+						findURLToken(url, token.data.children, callback, depth + 1);
 					}
 					if (token.data.type == "Url" && DocUtil.removeQuotes(Util.getCSSValue(token.data.value)) == url) {
-						callback(token, children);
+						callback(token, children, depth == 0);
 					}
 				}
 			}