Ver Fonte

extracted code from filterUnusedFonts into testUsedFont

Gildas há 7 anos atrás
pai
commit
c3d334f9f4
1 ficheiros alterados com 31 adições e 27 exclusões
  1. 31 27
      lib/single-file/css-fonts-minifier.js

+ 31 - 27
lib/single-file/css-fonts-minifier.js

@@ -293,33 +293,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 					if (rule.style) {
 						const familyName = removeQuotes(rule.style.getPropertyValue("font-family"));
 						if (familyName && !unusedFonts.find(fontInfo => fontInfo.familyName == familyName)) {
-							let optionalTest;
-							const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
-							if (optionalUsedFonts && optionalUsedFonts.length) {
-								const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
-								const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
-								const fontVariant = rule.style.getPropertyValue("font-variant");
-								optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontStyle == fontStyle);
-								if (optionalTest) {
-									optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures");
-								}
-								if (optionalTest) {
-									if (isNaN(fontWeight)) {
-										optionalTest = true;
-									} else {
-										const usedFontWeights = optionalUsedFonts.map(fontInfo => fontInfo.fontWeight);
-										const declaredFontsWeights = declaredFonts
-											.filter(fontInfo => fontInfo.familyName == familyName && fontInfo.fontStyle == fontStyle && (fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures"))
-											.map(fontInfo => fontInfo.fontWeight)
-											.sort((w1, w2) => w1 - w2);
-										const usedComputedFontWeights = usedFontWeights.map(fontWeight => findFontWeight(fontWeight, declaredFontsWeights));
-										optionalTest = usedComputedFontWeights.includes(fontWeight);
-									}
-								}
-							} else {
-								optionalTest = true;
-							}
-							if (testUnicodeRange(docContent, rule.style.getPropertyValue("unicode-range")) && optionalTest) {
+							if (testUnicodeRange(docContent, rule.style.getPropertyValue("unicode-range")) && testUsedFont(rule, familyName, declaredFonts, filteredUsedFonts)) {
 								stylesheetContent += rule.cssText;
 							}
 						}
@@ -332,6 +306,36 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		return stylesheetContent;
 	}
 
+	function testUsedFont(rule, familyName, declaredFonts, filteredUsedFonts) {
+		let test;
+		const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
+		if (optionalUsedFonts && optionalUsedFonts.length) {
+			const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
+			const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
+			const fontVariant = rule.style.getPropertyValue("font-variant");
+			test = optionalUsedFonts.find(fontInfo => fontInfo.fontStyle == fontStyle);
+			if (test) {
+				test = optionalUsedFonts.find(fontInfo => fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures");
+			}
+			if (test) {
+				if (isNaN(fontWeight)) {
+					test = true;
+				} else {
+					const usedFontWeights = optionalUsedFonts.map(fontInfo => fontInfo.fontWeight);
+					const declaredFontsWeights = declaredFonts
+						.filter(fontInfo => fontInfo.familyName == familyName && fontInfo.fontStyle == fontStyle && (fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures"))
+						.map(fontInfo => fontInfo.fontWeight)
+						.sort((w1, w2) => w1 - w2);
+					const usedComputedFontWeights = usedFontWeights.map(fontWeight => findFontWeight(fontWeight, declaredFontsWeights));
+					test = usedComputedFontWeights.includes(fontWeight);
+				}
+			}
+		} else {
+			test = true;
+		}
+		return test;
+	}
+
 	function findFontWeight(fontWeight, fontWeights) {
 		let foundWeight;
 		if (fontWeight >= 400 && fontWeight <= 500) {