Quellcode durchsuchen

defined global regexps

Gildas vor 7 Jahren
Ursprung
Commit
0e7fe4c748
1 geänderte Dateien mit 26 neuen und 14 gelöschten Zeilen
  1. 26 14
      lib/single-file/css-fonts-minifier.js

+ 26 - 14
lib/single-file/css-fonts-minifier.js

@@ -25,6 +25,18 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	const REGEXP_URL_SIMPLE_QUOTES_FN = /url\s*\(\s*'(.*?)'\s*\)/i;
 	const REGEXP_URL_DOUBLE_QUOTES_FN = /url\s*\(\s*"(.*?)"\s*\)/i;
 	const REGEXP_URL_NO_QUOTES_FN = /url\s*\(\s*(.*?)\s*\)/i;
+	const REGEXP_URL_FUNCTION = /url\(.*?\)\s*(,|$)/g;
+	const REGEXP_COMMA = /\s*,\s*/;
+	const REGEXP_DASH = /-/;
+	const REGEXP_QUESTION_MARK = /\?/g;
+	const REGEXP_STARTS_U_PLUS = /^U\+/i;
+	const REGEXP_SIMPLE_QUOTES_STRING = /^'(.*?)'$/;
+	const REGEXP_DOUBLE_QUOTES_STRING = /^"(.*?)"$/;
+	const REGEXP_URL_FUNCTION_WOFF = /^url\(\s*["']?data:font\/(woff2?)/;
+	const REGEXP_URL_FUNCTION_WOFF_ALT = /^url\(\s*["']?data:application\/x-font-(woff)/;
+	const REGEXP_FONT_FORMAT = /\.([^.?#]+)((\?|#).*?)?$/;
+	const REGEXP_FONT_FORMAT_VALUE = /format\((.*?)\)\s*,?$/;
+	const REGEXP_FONT_SRC = /(.*?)\s*,?$/;
 
 	return {
 		process: (doc, secondPass) => {
@@ -90,7 +102,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 						}
 						const src = rule.style.getPropertyValue("src");
 						if (src) {
-							const fontSources = src.match(/url\(.*?\)\s*(,|$)/g);
+							const fontSources = src.match(REGEXP_URL_FUNCTION);
 							if (fontSources) {
 								if (secondPass || testUnicodeRange(doc, rule)) {
 									cssText = processFontFaceRule(rule, fontSources, stats);
@@ -111,9 +123,9 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		const unicodeRange = rule.style.getPropertyValue("unicode-range");
 		const docContent = doc.body.outerText;
 		if (unicodeRange) {
-			const unicodeRanges = unicodeRange.split(/\s*,\s*/);
+			const unicodeRanges = unicodeRange.split(REGEXP_COMMA);
 			const result = unicodeRanges.filter(rangeValue => {
-				const range = rangeValue.split(/-/);
+				const range = rangeValue.split(REGEXP_DASH);
 				if (range.length == 2) {
 					range[0] = transformRange(range[0]);
 					const regExpString = "[" + range[0] + "-" + transformRange("U+" + range[1]) + "]";
@@ -123,7 +135,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 					if (range[0].includes("?")) {
 						const firstRange = transformRange(range[0]);
 						const secondRange = firstRange;
-						const regExpString = "[" + firstRange.replace(/\?/g, "0") + "-" + secondRange.replace(/\?/g, "F") + "]";
+						const regExpString = "[" + firstRange.replace(REGEXP_QUESTION_MARK, "0") + "-" + secondRange.replace(REGEXP_QUESTION_MARK, "F") + "]";
 						return (new RegExp(regExpString, "u")).test(docContent);
 
 					} else {
@@ -139,7 +151,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	}
 
 	function transformRange(range) {
-		range = range.replace(/^U\+/i, "");
+		range = range.replace(REGEXP_STARTS_U_PLUS, "");
 		while (range.length < 6) {
 			range = "0" + range;
 		}
@@ -148,17 +160,17 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 
 	function processFontFaceRule(rule, fontSources, stats) {
 		fontSources = fontSources.map(fontSrc => {
-			const fontFormatMatch = fontSrc.match(/format\((.*?)\)\s*,?$/);
+			const fontFormatMatch = fontSrc.match(REGEXP_FONT_FORMAT_VALUE);
 			let fontFormat;
 			if (fontFormatMatch && fontFormatMatch[1]) {
-				fontFormat = fontFormatMatch[1].replace(/^'(.*?)'$/, "$1").replace(/^"(.*?)"$/, "$1").toLowerCase();
+				fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
 			}
 			if (!fontFormat) {
-				const fontFormatMatch = fontSrc.match(/^url\(\s*["']?data:font\/(woff2?)/);
+				const fontFormatMatch = fontSrc.match(REGEXP_URL_FUNCTION_WOFF);
 				if (fontFormatMatch && fontFormatMatch[1]) {
 					fontFormat = fontFormatMatch[1];
 				} else {
-					const fontFormatMatch = fontSrc.match(/^url\(\s*["']?data:application\/x-font-(woff)/);
+					const fontFormatMatch = fontSrc.match(REGEXP_URL_FUNCTION_WOFF_ALT);
 					if (fontFormatMatch && fontFormatMatch[1]) {
 						fontFormat = fontFormatMatch[1];
 					}
@@ -170,13 +182,13 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 					fontSrc.match(REGEXP_URL_NO_QUOTES_FN);
 				const fontUrl = urlMatch && urlMatch[1];
 				if (fontUrl) {
-					const fontFormatMatch = fontUrl.match(/\.([^.?#]+)((\?|#).*?)?$/);
+					const fontFormatMatch = fontUrl.match(REGEXP_FONT_FORMAT);
 					if (fontFormatMatch && fontFormatMatch[1]) {
 						fontFormat = fontFormatMatch[1];
 					}
 				}
 			}
-			return { src: fontSrc.match(/(.*?)\s*,?$/)[1], format: fontFormat };
+			return { src: fontSrc.match(REGEXP_FONT_SRC)[1], format: fontFormat };
 		});
 		const fontTest = (fontSource, format) => fontSource.format == format;
 		const woff2FontFound = fontSources.find(fontSource => fontTest(fontSource, "woff2"));
@@ -222,10 +234,10 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 
 	function getFontFamilyName(fontFamilyName) {
 		fontFamilyName = fontFamilyName.toLowerCase().trim();
-		if (fontFamilyName.match(/^'(.*)'$/)) {
-			fontFamilyName = fontFamilyName.replace(/^'(.*)'$/, "$1");
+		if (fontFamilyName.match(REGEXP_SIMPLE_QUOTES_STRING)) {
+			fontFamilyName = fontFamilyName.replace(REGEXP_SIMPLE_QUOTES_STRING, "$1");
 		} else {
-			fontFamilyName = fontFamilyName.replace(/^"(.*)"$/, "$1");
+			fontFamilyName = fontFamilyName.replace(REGEXP_DOUBLE_QUOTES_STRING, "$1");
 		}
 		return fontFamilyName.trim();
 	}