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

moved getFontFamily into single-file-helper

Former-commit-id: eab19fdfd0c8ca4cd04cea8959ebf6740aa093de
Gildas пре 5 година
родитељ
комит
ec370c9c1a

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

@@ -279,7 +279,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 
 	function getFontKey(ruleData) {
 		return JSON.stringify([
-			getFontFamily(getPropertyValue(ruleData, "font-family")),
+			singlefile.lib.helper.getFontFamily(getPropertyValue(ruleData, "font-family")),
 			singlefile.lib.helper.getFontWeight(getPropertyValue(ruleData, "font-weight") || "400"),
 			getPropertyValue(ruleData, "font-style") || "normal",
 			getPropertyValue(ruleData, "unicode-range"),
@@ -290,16 +290,6 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 		]);
 	}
 
-	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();
-	}
-
 	function getFontStretch(stretch) {
 		return FONT_STRETCHES[stretch] || stretch;
 	}

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

@@ -33,9 +33,6 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 	const REGEXP_DASH = /-/;
 	const REGEXP_QUESTION_MARK = /\?/g;
 	const REGEXP_STARTS_U_PLUS = /^U\+/i;
-	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
-	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
-
 
 	return {
 		process: (doc, stylesheets, styles, options) => {
@@ -67,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 => getFontFamily(name))) || familyName;
+						return (computedFamilyName && computedFamilyName.split(",").map(name => singlefile.lib.helper.getFontFamily(name))) || familyName;
 					}
 					return familyName;
 				}));
@@ -111,7 +108,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 				}
 			} else {
 				if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
-					const fontFamily = getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
+					const fontFamily = singlefile.lib.helper.getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
 					if (fontFamily) {
 						const fontWeight = singlefile.lib.helper.getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
 						const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
@@ -130,7 +127,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 = getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
+				const fontFamily = singlefile.lib.helper.getFontFamily(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)) {
@@ -192,7 +189,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(getFontFamily(familyName));
+						fontFamilyNames.push(singlefile.lib.helper.getFontFamily(familyName));
 						familyName = "";
 					} else {
 						familyName += cssTree.generate(node);
@@ -202,14 +199,14 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 				fontFamilyName = cssTree.generate(fontFamilyName.data.value);
 			}
 			if (familyName) {
-				fontFamilyNames.push(getFontFamily(familyName));
+				fontFamilyNames.push(singlefile.lib.helper.getFontFamily(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(getFontFamily(familyName)));
+				parsedFont.family.forEach(familyName => fontFamilyNames.push(singlefile.lib.helper.getFontFamily(familyName)));
 			} catch (error) {
 				// ignored				
 			}
@@ -335,14 +332,4 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		return "\\u{" + range + "}";
 	}
 
-	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, " ");
-	}
-
 })();

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

@@ -73,6 +73,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		removeQuotes,
 		flatten,
 		getFontWeight,
+		getFontFamily,
 		ON_BEFORE_CAPTURE_EVENT_NAME,
 		ON_AFTER_CAPTURE_EVENT_NAME,
 		WIN_ID_ATTRIBUTE_NAME,
@@ -467,6 +468,16 @@ 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), []);
 	}