소스 검색

tolerate unique invalid font weight values

Gildas 5 년 전
부모
커밋
2c249de49c
1개의 변경된 파일25개의 추가작업 그리고 20개의 파일을 삭제
  1. 25 20
      lib/single-file/modules/css-fonts-minifier.js

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

@@ -99,7 +99,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 
 	function getFontsInfo(cssRules, fontsInfo) {
 		cssRules.forEach(ruleData => {
-			if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children) {
+			if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports") && ruleData.block && ruleData.block.children) {
 				getFontsInfo(ruleData.block.children, fontsInfo);
 			} else if (ruleData.type == "Rule") {
 				const fontFamilyNames = getFontFamilyNames(ruleData.block);
@@ -125,7 +125,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		const removedRules = [];
 		for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
 			const ruleData = cssRule.data;
-			if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children) {
+			if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports") && 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.normalizeFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
@@ -150,6 +150,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 	}
 
 	function testUsedFont(ruleData, familyName, declaredFonts, filteredUsedFonts) {
+		debugger;
 		let test;
 		const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
 		if (optionalUsedFonts && optionalUsedFonts.length) {
@@ -231,26 +232,30 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		let foundWeight;
 		if (fontInfo[2] == fontStyle) {
 			let fontWeight = Number(fontInfo[1]);
-			if (fontWeight >= 400 && fontWeight <= 500) {
-				foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
-				if (!foundWeight) {
-					foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
-				}
-				if (!foundWeight) {
-					foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
+			if (fontWeights.length > 1) {
+				if (fontWeight >= 400 && fontWeight <= 500) {
+					foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
+					if (!foundWeight) {
+						foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
+					}
+					if (!foundWeight) {
+						foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
+					}
 				}
-			}
-			if (fontWeight < 400) {
-				foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
-				if (!foundWeight) {
-					foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
+				if (fontWeight < 400) {
+					foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
+					if (!foundWeight) {
+						foundWeight = findAscendingFontWeight(fontWeight, fontWeights);
+					}
 				}
-			}
-			if (fontWeight > 500) {
-				foundWeight = fontWeights.find(weight => weight >= fontWeight);
-				if (!foundWeight) {
-					foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
+				if (fontWeight > 500) {
+					foundWeight = fontWeights.find(weight => weight >= fontWeight);
+					if (!foundWeight) {
+						foundWeight = findDescendingFontWeight(fontWeight, fontWeights);
+					}
 				}
+			} else {
+				foundWeight = fontWeights[0];
 			}
 		}
 		return foundWeight;
@@ -267,7 +272,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 	function getRulesTextContent(doc, cssRules, workStylesheet, content) {
 		cssRules.forEach(ruleData => {
 			if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
-				if (ruleData.type == "Atrule" && ruleData.name == "media") {
+				if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports")) {
 					content = getRulesTextContent(doc, ruleData.block.children, workStylesheet, content);
 				} else if (ruleData.type == "Rule") {
 					content = getDeclarationsTextContent(ruleData.block.children, workStylesheet, content);