Jelajahi Sumber

prevent removing local() functions in src declrations

Gildas 7 tahun lalu
induk
melakukan
d6e28eaa7c
1 mengubah file dengan 13 tambahan dan 21 penghapusan
  1. 13 21
      lib/single-file/css-fonts-minifier.js

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

@@ -38,6 +38,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
 	const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
 	const EMPTY_URL_SOURCE = "url(\"data:base64,\")";
+	const LOCAL_SOURCE = "local(";
 	const PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", ":before", ":after", ":first-line", ":first-letter", "::placeholder", "::selection", "::marker", "::cue", "::slotted", "::spelling-error", "::grammar-error"];
 	const FONT_WEIGHTS = {
 		normal: "400",
@@ -230,36 +231,26 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	}
 
 	function processFontFaceRule(cssRule, fontInfo, stats) {
-		const fontTest = (fontSource, format) => !fontSource.src.startsWith(EMPTY_URL_SOURCE) && fontSource.format == format;
-		let woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2-variations"));
-		if (!woffFontFound) {
-			woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2"));
-		}
-		if (!woffFontFound) {
-			woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff"));
-		}
+		const findSource = fontFormat => fontInfo.find(source => source.src != EMPTY_URL_SOURCE && source.format == fontFormat);
+		const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
 		stats.fonts.processed += fontInfo.length;
 		stats.fonts.discarded += fontInfo.length;
+		let woffFontFound = findSource("woff2-variations") || findSource("woff2") || findSource("woff");
+		let filteredFontInfo = fontInfo;
 		if (woffFontFound) {
-			fontInfo = [woffFontFound];
+			filteredFontInfo = filterSource(woffFontFound);
 		} else {
-			let ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype-variations"));
-			if (!ttfFontFound) {
-				ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype"));
-			}
+			let ttfFontFound = findSource("truetype-variations") || findSource("truetype");
 			if (ttfFontFound) {
-				fontInfo = [ttfFontFound];
+				filteredFontInfo = filterSource(ttfFontFound);
 			} else {
-				let otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "opentype"));
-				if (!otfFontFound) {
-					otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "embedded-opentype"));
-				}
+				let otfFontFound = findSource("opentype") || findSource("embedded-opentype");
 				if (otfFontFound) {
-					fontInfo = [otfFontFound];
+					filteredFontInfo = filterSource(otfFontFound);
 				}
 			}
 		}
-		stats.fonts.discarded -= fontInfo.length;
+		stats.fonts.discarded -= filteredFontInfo.length;
 		const removedNodes = [];
 		for (let node = cssRule.block.children.head; node; node = node.next) {
 			if (node.data.property == "src") {
@@ -270,7 +261,8 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		removedNodes.forEach(node => cssRule.block.children.remove(node));
 		const srcDeclaration = cssRule.block.children.filter(node => node.property == "src").tail;
 		if (srcDeclaration) {
-			srcDeclaration.data.value = cssTree.parse(fontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
+			filteredFontInfo.reverse();
+			srcDeclaration.data.value = cssTree.parse(filteredFontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
 		}
 	}