浏览代码

fixed issue with fonts containing no src (fix #512)

Gildas 5 年之前
父节点
当前提交
55f18377ec
共有 1 个文件被更改,包括 52 次插入47 次删除
  1. 52 47
      lib/single-file/modules/css-fonts-alt-minifier.js

+ 52 - 47
lib/single-file/modules/css-fonts-alt-minifier.js

@@ -186,8 +186,10 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 				const key = getFontKey(ruleData);
 				const fontInfo = fontsDetails.fonts.get(key);
 				if (fontInfo) {
-					fontsDetails.fonts.delete(key);
-					await processFontFaceRule(ruleData, fontInfo, fontURLs, stats);
+					const processed = await processFontFaceRule(ruleData, fontInfo, fontURLs, stats);
+					if (processed) {
+						fontsDetails.fonts.delete(key);
+					}
 				} else {
 					removedRules.push(cssRule);
 				}
@@ -208,59 +210,59 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 		removedNodes.pop();
 		removedNodes.forEach(node => ruleData.block.children.remove(node));
 		const srcDeclaration = ruleData.block.children.filter(node => node.property == "src").tail;
-		await Promise.all(fontInfo.map(async (source, sourceIndex) => {
-			if (FontFace) {
-				const fontFace = new FontFace("test-font", source.src);
-				try {
-					await fontFace.load();
-					await fontFace.loaded;
-					source.valid = true;
-				} catch (error) {
-					const declarationFontURLs = fontURLs.get(srcDeclaration.data);
-					if (declarationFontURLs) {
-						const fontURL = declarationFontURLs[declarationFontURLs.length - sourceIndex - 1];
-						if (fontURL) {
-							const fontFace = new FontFace("test-font", "url(" + fontURL + ")");
-							try {
-								await Promise.race(
-									[
-										fontFace.load().then(() => fontFace.loaded).then(() => source.valid = true),
-										new Promise(resolve => setTimeout.call(window, () => { source.valid = true; resolve(); }, FONT_MAX_LOAD_DELAY))
-									]);
-							} catch (error) {
-								// ignored
+		if (srcDeclaration) {
+			await Promise.all(fontInfo.map(async (source, sourceIndex) => {
+				if (FontFace) {
+					const fontFace = new FontFace("test-font", source.src);
+					try {
+						await fontFace.load();
+						await fontFace.loaded;
+						source.valid = true;
+					} catch (error) {
+						const declarationFontURLs = fontURLs.get(srcDeclaration.data);
+						if (declarationFontURLs) {
+							const fontURL = declarationFontURLs[declarationFontURLs.length - sourceIndex - 1];
+							if (fontURL) {
+								const fontFace = new FontFace("test-font", "url(" + fontURL + ")");
+								try {
+									await Promise.race(
+										[
+											fontFace.load().then(() => fontFace.loaded).then(() => source.valid = true),
+											new Promise(resolve => setTimeout.call(window, () => { source.valid = true; resolve(); }, FONT_MAX_LOAD_DELAY))
+										]);
+								} catch (error) {
+									// ignored
+								}
 							}
+						} else {
+							source.valid = true;
 						}
-					} else {
-						source.valid = true;
 					}
+				} else {
+					source.valid = true;
 				}
+			}));
+			const findSource = (fontFormat, testValidity) => fontInfo.find(source => !source.src.match(EMPTY_URL_SOURCE) && source.format == fontFormat && (!testValidity || source.valid));
+			const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
+			stats.fonts.processed += fontInfo.length;
+			stats.fonts.discarded += fontInfo.length;
+			const woffFontFound = findSource("woff2-variations", true) || findSource("woff2", true) || findSource("woff", true);
+			if (woffFontFound) {
+				fontInfo = filterSource(woffFontFound);
 			} else {
-				source.valid = true;
-			}
-		}));
-		const findSource = (fontFormat, testValidity) => fontInfo.find(source => !source.src.match(EMPTY_URL_SOURCE) && source.format == fontFormat && (!testValidity || source.valid));
-		const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
-		stats.fonts.processed += fontInfo.length;
-		stats.fonts.discarded += fontInfo.length;
-		const woffFontFound = findSource("woff2-variations", true) || findSource("woff2", true) || findSource("woff", true);
-		if (woffFontFound) {
-			fontInfo = filterSource(woffFontFound);
-		} else {
-			const ttfFontFound = findSource("truetype-variations", true) || findSource("truetype", true);
-			if (ttfFontFound) {
-				fontInfo = filterSource(ttfFontFound);
-			} else {
-				const otfFontFound = findSource("opentype") || findSource("embedded-opentype");
-				if (otfFontFound) {
-					fontInfo = filterSource(otfFontFound);
+				const ttfFontFound = findSource("truetype-variations", true) || findSource("truetype", true);
+				if (ttfFontFound) {
+					fontInfo = filterSource(ttfFontFound);
 				} else {
-					fontInfo = fontInfo.filter(source => !source.src.match(EMPTY_URL_SOURCE) && (source.valid) || source.src.startsWith(LOCAL_SOURCE));
+					const otfFontFound = findSource("opentype") || findSource("embedded-opentype");
+					if (otfFontFound) {
+						fontInfo = filterSource(otfFontFound);
+					} else {
+						fontInfo = fontInfo.filter(source => !source.src.match(EMPTY_URL_SOURCE) && (source.valid) || source.src.startsWith(LOCAL_SOURCE));
+					}
 				}
 			}
-		}
-		stats.fonts.discarded -= fontInfo.length;
-		if (srcDeclaration) {
+			stats.fonts.discarded -= fontInfo.length;
 			fontInfo.reverse();
 			try {
 				srcDeclaration.data.value = cssTree.parse(fontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
@@ -268,6 +270,9 @@ this.singlefile.lib.modules.fontsAltMinifier = this.singlefile.lib.modules.fonts
 			catch (error) {
 				// ignored
 			}
+			return true;
+		} else {
+			return false;
 		}
 	}