Selaa lähdekoodia

rename property

Gildas 3 vuotta sitten
vanhempi
sitoutus
7a28a570da

+ 28 - 21
src/single-file/modules/css-fonts-alt-minifier.js

@@ -67,7 +67,7 @@ export {
 	process
 };
 
-async function process(doc, stylesheets, fontURLs, fontTests) {
+async function process(doc, stylesheets, fontDeclarations, fontTests) {
 	const fontsDetails = {
 		fonts: new Map(),
 		medias: new Map(),
@@ -96,9 +96,9 @@ async function process(doc, stylesheets, fontURLs, fontTests) {
 		const media = stylesheetInfo.mediaText;
 		if (cssRules) {
 			if (media && media != MEDIA_ALL) {
-				await processFontFaceRules(cssRules, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + media), fontURLs, fontTests, stats);
+				await processFontFaceRules(cssRules, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + media), fontDeclarations, fontTests, stats);
 			} else {
-				await processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontURLs, fontTests, stats);
+				await processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontDeclarations, fontTests, stats);
 			}
 			stats.rules.discarded -= cssRules.getSize();
 		}
@@ -144,10 +144,7 @@ function processFontDetails(fontsDetails) {
 		fontsDetails.fonts.set(fontKey, fontInfo.map(fontSource => {
 			const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
 			let fontFormat;
-			const urlMatch = fontSource.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
-				fontSource.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
-				fontSource.match(REGEXP_URL_NO_QUOTES_FN);
-			const fontUrl = urlMatch && urlMatch[1];
+			const fontUrl = getURL(fontSource);
 			if (fontFormatMatch && fontFormatMatch[1]) {
 				fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
 			}
@@ -175,24 +172,24 @@ function processFontDetails(fontsDetails) {
 	fontsDetails.supports.forEach(supportsFontsDetails => processFontDetails(supportsFontsDetails));
 }
 
-async function processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontURLs, fontTests, stats) {
+async function processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontDeclarations, fontTests, stats) {
 	const removedRules = [];
 	let mediaIndex = 0, supportsIndex = 0;
 	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 && ruleData.prelude) {
 			const mediaText = cssTree.generate(ruleData.prelude);
-			await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText), fontURLs, fontTests, stats);
+			await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.medias.get("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText), fontDeclarations, fontTests, stats);
 			mediaIndex++;
 		} else if (ruleData.type == "Atrule" && ruleData.name == "supports" && ruleData.block && ruleData.block.children && ruleData.prelude) {
 			const supportsText = cssTree.generate(ruleData.prelude);
-			await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.supports.get("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText), fontURLs, fontTests, stats);
+			await processFontFaceRules(ruleData.block.children, sheetIndex, fontsDetails.supports.get("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText), fontDeclarations, fontTests, stats);
 			supportsIndex++;
 		} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
 			const key = getFontKey(ruleData);
 			const fontInfo = fontsDetails.fonts.get(key);
 			if (fontInfo) {
-				const processed = await processFontFaceRule(ruleData, fontInfo, fontURLs, fontTests, stats);
+				const processed = await processFontFaceRule(ruleData, fontInfo, fontDeclarations, fontTests, stats);
 				if (processed) {
 					fontsDetails.fonts.delete(key);
 				}
@@ -204,7 +201,7 @@ async function processFontFaceRules(cssRules, sheetIndex, fontsDetails, fontURLs
 	removedRules.forEach(cssRule => cssRules.remove(cssRule));
 }
 
-async function processFontFaceRule(ruleData, fontInfo, fontURLs, fontTests, stats) {
+async function processFontFaceRule(ruleData, fontInfo, fontDeclarations, fontTests, stats) {
 	const removedNodes = [];
 	for (let node = ruleData.block.children.head; node; node = node.next) {
 		if (node.data.property == "src") {
@@ -228,7 +225,7 @@ async function processFontFaceRule(ruleData, fontInfo, fontURLs, fontTests, stat
 							new Promise(resolve => timeout = globalThis.setTimeout(() => { source.valid = true; resolve(); }, FONT_MAX_LOAD_DELAY))
 						]);
 					} catch (error) {
-						const declarationFontURLs = fontURLs.get(srcDeclaration.data);
+						const declarationFontURLs = fontDeclarations.get(srcDeclaration.data);
 						if (declarationFontURLs) {
 							const fontURL = declarationFontURLs[declarationFontURLs.length - sourceIndex - 1];
 							if (fontURL) {
@@ -253,21 +250,24 @@ async function processFontFaceRule(ruleData, fontInfo, fontURLs, fontTests, stat
 				fontTests.set(source.src, source.valid);
 			}
 		}));
-		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));
+		const findSourceByFormat = (fontFormat, testValidity) => fontInfo.find(source => !source.src.match(EMPTY_URL_SOURCE) && source.format == fontFormat && (!testValidity || source.valid));
+		const filterSources = 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);
+		const woffFontFound =
+			findSourceByFormat("woff2-variations", true) || findSourceByFormat("woff2", true) || findSourceByFormat("woff", true);
 		if (woffFontFound) {
-			fontInfo = filterSource(woffFontFound);
+			fontInfo = filterSources(woffFontFound);
 		} else {
-			const ttfFontFound = findSource("truetype-variations", true) || findSource("truetype", true);
+			const ttfFontFound =
+				findSourceByFormat("truetype-variations", true) || findSourceByFormat("truetype", true);
 			if (ttfFontFound) {
-				fontInfo = filterSource(ttfFontFound);
+				fontInfo = filterSources(ttfFontFound);
 			} else {
-				const otfFontFound = findSource("opentype") || findSource("embedded-opentype");
+				const otfFontFound =
+					findSourceByFormat("opentype") || findSourceByFormat("embedded-opentype");
 				if (otfFontFound) {
-					fontInfo = filterSource(otfFontFound);
+					fontInfo = filterSources(otfFontFound);
 				} else {
 					fontInfo = fontInfo.filter(source => !source.src.match(EMPTY_URL_SOURCE) && (source.valid) || source.src.startsWith(LOCAL_SOURCE));
 				}
@@ -330,4 +330,11 @@ function createFontsDetailsInfo() {
 		medias: new Map(),
 		supports: new Map()
 	};
+}
+
+function getURL(urlFunction) {
+	const urlMatch = urlFunction.match(REGEXP_URL_SIMPLE_QUOTES_FN) ||
+		urlFunction.match(REGEXP_URL_DOUBLE_QUOTES_FN) ||
+		urlFunction.match(REGEXP_URL_NO_QUOTES_FN);
+	return urlMatch && urlMatch[1];
 }

+ 6 - 6
src/single-file/single-file-core.js

@@ -1249,7 +1249,7 @@ class Processor {
 	}
 
 	async processStylesheets() {
-		this.options.fontURLs = new Map();
+		this.options.fontDeclarations = new Map();
 		await Promise.all([...this.stylesheets].map(([, stylesheetInfo]) =>
 			ProcessorHelper.processStylesheet(stylesheetInfo.stylesheet.children, this.baseURI, this.options, this.cssVariables, this.batchRequest)
 		));
@@ -1354,7 +1354,7 @@ class Processor {
 	}
 
 	async removeAlternativeFonts() {
-		await util.removeAlternativeFonts(this.doc, this.stylesheets, this.options.fontURLs, this.options.fontTests);
+		await util.removeAlternativeFonts(this.doc, this.stylesheets, this.options.fontDeclarations, this.options.fontTests);
 	}
 
 	async processFrames() {
@@ -1877,14 +1877,14 @@ class ProcessorHelper {
 				} else if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports")) {
 					promises.push(this.processStylesheet(ruleData.block.children, baseURI, options, cssVariables, batchRequest));
 				} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
-					promises.push(processFontFaceRule(ruleData, options.fontURLs));
+					promises.push(processFontFaceRule(ruleData));
 				}
 			}
 		}
 		removedRules.forEach(cssRule => cssRules.remove(cssRule));
 		await Promise.all(promises);
 
-		async function processFontFaceRule(ruleData, fontURLs) {
+		async function processFontFaceRule(ruleData) {
 			await Promise.all(ruleData.block.children.toArray().map(async declaration => {
 				if (declaration.type == "Declaration" && declaration.value.children) {
 					const urlFunctions = getUrlFunctions(getCSSValue(declaration.value), true);
@@ -1895,10 +1895,10 @@ class ProcessorHelper {
 							if (testValidURL(resourceURL)) {
 								let { content } = await batchRequest.addURL(resourceURL,
 									{ asBinary: true, expectedType: "font", baseURI, blockMixedContent: options.blockMixedContent });
-								let resourceURLs = fontURLs.get(declaration);
+								let resourceURLs = options.fontDeclarations.get(declaration);
 								if (!resourceURLs) {
 									resourceURLs = [];
-									fontURLs.set(declaration, resourceURLs);
+									options.fontDeclarations.set(declaration, resourceURLs);
 								}
 								resourceURLs.push(resourceURL);
 								replaceURLs(declaration, originalResourceURL, content);

+ 2 - 2
src/single-file/single-file-util.js

@@ -136,8 +136,8 @@ function getInstance(utilOptions) {
 		removeUnusedFonts(doc, stylesheets, styles, options) {
 			return modules.fontsMinifier.process(doc, stylesheets, styles, options);
 		},
-		removeAlternativeFonts(doc, stylesheets, fontURLs, fontTests) {
-			return modules.fontsAltMinifier.process(doc, stylesheets, fontURLs, fontTests);
+		removeAlternativeFonts(doc, stylesheets, fontDeclarations, fontTests) {
+			return modules.fontsAltMinifier.process(doc, stylesheets, fontDeclarations, fontTests);
 		},
 		getMediaAllInfo(doc, stylesheets, styles) {
 			return modules.matchedRules.getMediaAllInfo(doc, stylesheets, styles);