Bläddra i källkod

replaced helper.getFontFamily with helper.normalizeFontFamily

Former-commit-id: 9903ca4f178dd75cd93b5b5c9674fbd034c337e5
Gildas 5 år sedan
förälder
incheckning
aa7410e69f

+ 1 - 1
lib/single-file/modules/css-fonts-alt-minifier.js

@@ -286,7 +286,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 
 	function getFontKey(ruleData) {
 		return JSON.stringify([
-			singlefile.lib.helper.getFontFamily(getPropertyValue(ruleData, "font-family")),
+			singlefile.lib.helper.normalizeFontFamily(getPropertyValue(ruleData, "font-family")),
 			singlefile.lib.helper.getFontWeight(getPropertyValue(ruleData, "font-weight") || "400"),
 			getPropertyValue(ruleData, "font-style") || "normal",
 			getPropertyValue(ruleData, "unicode-range"),

+ 6 - 6
lib/single-file/modules/css-fonts-minifier.js

@@ -64,7 +64,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 					const matchedVar = familyName.match(/^var\((--.*)\)$/);
 					if (matchedVar && matchedVar[1]) {
 						const computedFamilyName = getComputedStyle.call(window, options.doc.body).getPropertyValue(matchedVar[1]);
-						return (computedFamilyName && computedFamilyName.split(",").map(name => singlefile.lib.helper.getFontFamily(name))) || familyName;
+						return (computedFamilyName && computedFamilyName.split(",").map(name => singlefile.lib.helper.normalizeFontFamily(name))) || familyName;
 					}
 					return familyName;
 				}));
@@ -108,7 +108,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 				}
 			} else {
 				if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
-					const fontFamily = singlefile.lib.helper.getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
+					const fontFamily = singlefile.lib.helper.normalizeFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
 					if (fontFamily) {
 						const fontWeight = getDeclarationValue(ruleData.block.children, "font-weight") || "400";
 						const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
@@ -128,7 +128,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 			if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children) {
 				filterUnusedFonts(ruleData.block.children, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
 			} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
-				const fontFamily = singlefile.lib.helper.getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
+				const fontFamily = singlefile.lib.helper.normalizeFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
 				if (fontFamily) {
 					const unicodeRange = getDeclarationValue(ruleData.block.children, "unicode-range");
 					if (unusedFonts.find(fontInfo => fontInfo.fontFamily == fontFamily) || !testUnicodeRange(docContent, unicodeRange) || !testUsedFont(ruleData, fontFamily, declaredFonts, filteredUsedFonts)) {
@@ -202,7 +202,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 			if (fontFamilyName.data.value.children) {
 				fontFamilyName.data.value.children.forEach(node => {
 					if (node.type == "Operator" && node.value == "," && familyName) {
-						fontFamilyNames.push(singlefile.lib.helper.getFontFamily(familyName));
+						fontFamilyNames.push(singlefile.lib.helper.normalizeFontFamily(familyName));
 						familyName = "";
 					} else {
 						familyName += cssTree.generate(node);
@@ -212,14 +212,14 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 				fontFamilyName = cssTree.generate(fontFamilyName.data.value);
 			}
 			if (familyName) {
-				fontFamilyNames.push(singlefile.lib.helper.getFontFamily(familyName));
+				fontFamilyNames.push(singlefile.lib.helper.normalizeFontFamily(familyName));
 			}
 		}
 		const font = declarations.children.filter(node => node.property == "font").tail;
 		if (font && font.data && font.data.value) {
 			try {
 				const parsedFont = singlefile.lib.vendor.fontPropertyParser.parse(cssTree.generate(font.data.value));
-				parsedFont.family.forEach(familyName => fontFamilyNames.push(singlefile.lib.helper.getFontFamily(familyName)));
+				parsedFont.family.forEach(familyName => fontFamilyNames.push(singlefile.lib.helper.normalizeFontFamily(familyName)));
 			} catch (error) {
 				// ignored				
 			}

+ 1 - 11
lib/single-file/single-file-helper.js

@@ -75,7 +75,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		removeQuotes,
 		flatten,
 		getFontWeight,
-		getFontFamily,
+		normalizeFontFamily,
 		ON_BEFORE_CAPTURE_EVENT_NAME,
 		ON_AFTER_CAPTURE_EVENT_NAME,
 		WIN_ID_ATTRIBUTE_NAME,
@@ -472,16 +472,6 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		return FONT_WEIGHTS[weight.toLowerCase().trim()] || weight;
 	}
 
-	function getFontFamily(string = "") {
-		string = string.toLowerCase().trim();
-		if (string.match(REGEXP_SIMPLE_QUOTES_STRING)) {
-			string = string.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
-		} else {
-			string = string.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
-		}
-		return string.trim().replace(/\\+ +/gi, " ");
-	}
-
 	function flatten(array) {
 		return array.flat ? array.flat() : array.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
 	}