Przeglądaj źródła

moved getFontWeight into single-file-helper

Former-commit-id: 50a2f24cd5751bf49815d7866669ad657b5cd906
Gildas 5 lat temu
rodzic
commit
71875f7afc

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

@@ -44,10 +44,6 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 	const EMPTY_URL_SOURCE = /^url\(["']?data:[^,]*,?["']?\)/;
 	const EMPTY_URL_SOURCE = /^url\(["']?data:[^,]*,?["']?\)/;
 	const LOCAL_SOURCE = "local(";
 	const LOCAL_SOURCE = "local(";
 	const MEDIA_ALL = "all";
 	const MEDIA_ALL = "all";
-	const FONT_WEIGHTS = {
-		normal: "400",
-		bold: "700"
-	};
 	const FONT_STRETCHES = {
 	const FONT_STRETCHES = {
 		"ultra-condensed": "50%",
 		"ultra-condensed": "50%",
 		"extra-condensed": "62.5%",
 		"extra-condensed": "62.5%",
@@ -284,7 +280,7 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 	function getFontKey(ruleData) {
 	function getFontKey(ruleData) {
 		return JSON.stringify([
 		return JSON.stringify([
 			getFontFamily(getPropertyValue(ruleData, "font-family")),
 			getFontFamily(getPropertyValue(ruleData, "font-family")),
-			getFontWeight(getPropertyValue(ruleData, "font-weight") || "400"),
+			singlefile.lib.helper.getFontWeight(getPropertyValue(ruleData, "font-weight") || "400"),
 			getPropertyValue(ruleData, "font-style") || "normal",
 			getPropertyValue(ruleData, "font-style") || "normal",
 			getPropertyValue(ruleData, "unicode-range"),
 			getPropertyValue(ruleData, "unicode-range"),
 			getFontStretch(getPropertyValue(ruleData, "font-stretch")),
 			getFontStretch(getPropertyValue(ruleData, "font-stretch")),
@@ -304,10 +300,6 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 		return string.trim();
 		return string.trim();
 	}
 	}
 
 
-	function getFontWeight(weight) {
-		return FONT_WEIGHTS[weight] || weight;
-	}
-
 	function getFontStretch(stretch) {
 	function getFontStretch(stretch) {
 		return FONT_STRETCHES[stretch] || stretch;
 		return FONT_STRETCHES[stretch] || stretch;
 	}
 	}

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

@@ -35,13 +35,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 	const REGEXP_STARTS_U_PLUS = /^U\+/i;
 	const REGEXP_STARTS_U_PLUS = /^U\+/i;
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
-	const FONT_WEIGHTS = {
-		regular: "400",
-		normal: "400",
-		bold: "700",
-		bolder: "700",
-		lighter: "100"
-	};
+
 
 
 	return {
 	return {
 		process: (doc, stylesheets, styles, options) => {
 		process: (doc, stylesheets, styles, options) => {
@@ -119,7 +113,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 				if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
 				if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
 					const fontFamily = getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
 					const fontFamily = getFontFamily(getDeclarationValue(ruleData.block.children, "font-family"));
 					if (fontFamily) {
 					if (fontFamily) {
-						const fontWeight = getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
+						const fontWeight = singlefile.lib.helper.getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
 						const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
 						const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
 						const fontVariant = getDeclarationValue(ruleData.block.children, "font-variant") || "normal";
 						const fontVariant = getDeclarationValue(ruleData.block.children, "font-variant") || "normal";
 						fontsInfo.declared.push({ fontFamily, fontWeight, fontStyle, fontVariant });
 						fontsInfo.declared.push({ fontFamily, fontWeight, fontStyle, fontVariant });
@@ -162,7 +156,7 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
 		const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
 		if (optionalUsedFonts && optionalUsedFonts.length) {
 		if (optionalUsedFonts && optionalUsedFonts.length) {
 			const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
 			const fontStyle = getDeclarationValue(ruleData.block.children, "font-style") || "normal";
-			const fontWeight = getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
+			const fontWeight = singlefile.lib.helper.getFontWeight(getDeclarationValue(ruleData.block.children, "font-weight") || "400");
 			const declaredFontsWeights = declaredFonts
 			const declaredFontsWeights = declaredFonts
 				.filter(fontInfo => fontInfo.fontFamily == familyName && fontInfo.fontStyle == fontStyle)
 				.filter(fontInfo => fontInfo.fontFamily == familyName && fontInfo.fontStyle == fontStyle)
 				.map(fontInfo => fontInfo.fontWeight)
 				.map(fontInfo => fontInfo.fontWeight)
@@ -351,8 +345,4 @@ this.singlefile.lib.modules.fontsMinifier = this.singlefile.lib.modules.fontsMin
 		return string.trim().replace(/\\+ +/gi, " ");
 		return string.trim().replace(/\\+ +/gi, " ");
 	}
 	}
 
 
-	function getFontWeight(weight) {
-		return FONT_WEIGHTS[weight.toLowerCase()] || weight;
-	}
-
 })();
 })();

+ 6 - 2
lib/single-file/single-file-helper.js

@@ -50,8 +50,11 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
 	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
 	const FONT_WEIGHTS = {
 	const FONT_WEIGHTS = {
+		regular: "400",
 		normal: "400",
 		normal: "400",
-		bold: "700"
+		bold: "700",
+		bolder: "700",
+		lighter: "100"
 	};
 	};
 
 
 	addEventListener("single-file-user-script-init", () => singlefile.lib.helper.waitForUserScript = async eventPrefixName => {
 	addEventListener("single-file-user-script-init", () => singlefile.lib.helper.waitForUserScript = async eventPrefixName => {
@@ -69,6 +72,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		serialize,
 		serialize,
 		removeQuotes,
 		removeQuotes,
 		flatten,
 		flatten,
+		getFontWeight,
 		ON_BEFORE_CAPTURE_EVENT_NAME,
 		ON_BEFORE_CAPTURE_EVENT_NAME,
 		ON_AFTER_CAPTURE_EVENT_NAME,
 		ON_AFTER_CAPTURE_EVENT_NAME,
 		WIN_ID_ATTRIBUTE_NAME,
 		WIN_ID_ATTRIBUTE_NAME,
@@ -460,7 +464,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 	}
 	}
 
 
 	function getFontWeight(weight) {
 	function getFontWeight(weight) {
-		return FONT_WEIGHTS[weight] || weight;
+		return FONT_WEIGHTS[weight.toLowerCase().trim()] || weight;
 	}
 	}
 
 
 	function flatten(array) {
 	function flatten(array) {