소스 검색

added DomUtil.getCSSValue to retrieve CSS values

Gildas 7 년 전
부모
커밋
53fe8dbd46
1개의 변경된 파일15개의 추가작업 그리고 18개의 파일을 삭제
  1. 15 18
      lib/single-file/single-file-core.js

+ 15 - 18
lib/single-file/single-file-core.js

@@ -1204,13 +1204,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			async function processDeclaration(cssRule) {
 				await Promise.all(cssRule.block.children.toArray().map(async declaration => {
 					if (declaration.type == "Declaration") {
-						let declarationValue = "";
-						try {
-							declarationValue = cssTree.generate(declaration.value);
-						} catch (error) {
-							// ignored
-						}
-						const urlFunctions = DomUtil.getUrlFunctions(declarationValue); // TODO: use OM
+						const urlFunctions = DomUtil.getUrlFunctions(DomUtil.getCSSValue(declaration.value));
 						await Promise.all(urlFunctions.map(async urlFunction => {
 							const originalResourceURL = DomUtil.matchURL(urlFunction);
 							const resourceURL = DomUtil.normalizeURL(originalResourceURL);
@@ -1225,13 +1219,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 										content = EMPTY_DATA_URI;
 									}
 									declaration.value.children.forEach(token => {
-										let tokenValue = "";
-										try {
-											tokenValue = cssTree.generate(token.value);
-										} catch (error) {
-											// ignored
-										}
-										if (token.type == "Url" && DOM.removeQuotes(tokenValue) == originalResourceURL) {
+										if (token.type == "Url" && DOM.removeQuotes(DomUtil.getCSSValue(token.value)) == originalResourceURL) {
 											token.value.value = content;
 										}
 									});
@@ -1246,8 +1234,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		static async processStyle(declarations, baseURI, options, cssVariables, batchRequest) {
 			await Promise.all(declarations.map(async declaration => {
 				if (declaration.type == "Declaration") {
-					const declarationValue = cssTree.generate(declaration.value);
-					const urlFunctions = DomUtil.getUrlFunctions(declarationValue);
+					const urlFunctions = DomUtil.getUrlFunctions(DomUtil.getCSSValue(declaration.value));
 					await Promise.all(urlFunctions.map(async urlFunction => {
 						const originalResourceURL = DomUtil.matchURL(urlFunction);
 						const resourceURL = DomUtil.normalizeURL(originalResourceURL);
@@ -1257,7 +1244,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 								if (duplicate && options.groupDuplicateImages) {
 									const tokens = [];
 									for (let token = declaration.value.children.head; token; token = token.next) {
-										if (token.data.type == "Url" && DOM.removeQuotes(cssTree.generate(token.data.value)) == originalResourceURL) {
+										if (token.data.type == "Url" && DOM.removeQuotes(DomUtil.getCSSValue(token.data.value)) == originalResourceURL) {
 											const value = cssTree.parse("var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")", { context: "value" }).children.head;
 											tokens.push({ token, value });
 										}
@@ -1266,7 +1253,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 									cssVariables.set(indexResource, content);
 								} else {
 									declaration.value.children.forEach(token => {
-										if (token.type == "Url" && DOM.removeQuotes(cssTree.generate(token.value)) == originalResourceURL) {
+										if (token.type == "Url" && DOM.removeQuotes(DomUtil.getCSSValue(token.value)) == originalResourceURL) {
 											token.value.value = content;
 										}
 									});
@@ -1411,6 +1398,16 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			}
 		}
 
+		static getCSSValue(value) {
+			let result = "";
+			try {
+				result = cssTree.generate(value);
+			} catch (error) {
+				// ignored
+			}
+			return result;
+		}
+
 		static async evalTemplateVariable(template, variableName, valueGetter, dontReplaceSlash) {
 			const replaceRegExp = new RegExp("{\\s*" + variableName + "\\s*}", "g");
 			if (template.match(replaceRegExp)) {