|
|
@@ -125,6 +125,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
getFontsDetails(doc, style.sheet.cssRules, fontsDetails);
|
|
|
}
|
|
|
});
|
|
|
+ processFontDetails(fontsDetails);
|
|
|
doc.querySelectorAll("style").forEach(style => {
|
|
|
if (style.sheet) {
|
|
|
style.textContent = processFontFaceRules(style.sheet.cssRules, fontsDetails, "all", stats);
|
|
|
@@ -135,6 +136,40 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ function processFontDetails(fontsDetails) {
|
|
|
+ fontsDetails.forEach((fontInfo, fontKey) => {
|
|
|
+ fontsDetails.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];
|
|
|
+ if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
+ fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
|
|
|
+ }
|
|
|
+ if (!fontFormat) {
|
|
|
+ const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF);
|
|
|
+ if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
+ fontFormat = fontFormatMatch[1];
|
|
|
+ } else {
|
|
|
+ const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF_ALT);
|
|
|
+ if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
+ fontFormat = fontFormatMatch[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!fontFormat && fontUrl) {
|
|
|
+ const fontFormatMatch = fontUrl.match(REGEXP_FONT_FORMAT);
|
|
|
+ if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
+ fontFormat = fontFormatMatch[1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return { src: fontSource.match(REGEXP_FONT_SRC)[1], fontUrl, format: fontFormat };
|
|
|
+ }));
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
function getFontsInfo(doc, rules, fontsInfo) {
|
|
|
if (rules) {
|
|
|
Array.from(rules).forEach(rule => {
|
|
|
@@ -206,73 +241,42 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
return stylesheetContent;
|
|
|
}
|
|
|
|
|
|
- function processFontFaceRule(rule, fontInfo, stats) {
|
|
|
- let fontSources = fontInfo.map(fontSource => {
|
|
|
- const fontFormatMatch = fontSource.match(REGEXP_FONT_FORMAT_VALUE);
|
|
|
- let fontFormat;
|
|
|
- if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
- fontFormat = fontFormatMatch[1].replace(REGEXP_SIMPLE_QUOTES_STRING, "$1").replace(REGEXP_DOUBLE_QUOTES_STRING, "$1").toLowerCase();
|
|
|
- }
|
|
|
- if (!fontFormat) {
|
|
|
- const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF);
|
|
|
- if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
- fontFormat = fontFormatMatch[1];
|
|
|
- } else {
|
|
|
- const fontFormatMatch = fontSource.match(REGEXP_URL_FUNCTION_WOFF_ALT);
|
|
|
- if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
- fontFormat = fontFormatMatch[1];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- if (!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];
|
|
|
- if (fontUrl) {
|
|
|
- const fontFormatMatch = fontUrl.match(REGEXP_FONT_FORMAT);
|
|
|
- if (fontFormatMatch && fontFormatMatch[1]) {
|
|
|
- fontFormat = fontFormatMatch[1];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return { src: fontSource.match(REGEXP_FONT_SRC)[1], format: fontFormat };
|
|
|
- });
|
|
|
+ function processFontFaceRule(rule, fontInfo, stats) {
|
|
|
const fontTest = (fontSource, format) => !fontSource.src.startsWith(EMPTY_URL_SOURCE) && fontSource.format == format;
|
|
|
- let woffFontFound = fontSources.find(fontSource => fontTest(fontSource, "woff2-variations"));
|
|
|
+ let woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2-variations"));
|
|
|
if (!woffFontFound) {
|
|
|
- woffFontFound = fontSources.find(fontSource => fontTest(fontSource, "woff2"));
|
|
|
+ woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff2"));
|
|
|
}
|
|
|
if (!woffFontFound) {
|
|
|
- woffFontFound = fontSources.find(fontSource => fontTest(fontSource, "woff"));
|
|
|
+ woffFontFound = fontInfo.find(fontSource => fontTest(fontSource, "woff"));
|
|
|
}
|
|
|
- stats.fonts.processed += fontSources.length;
|
|
|
- stats.fonts.discarded += fontSources.length;
|
|
|
+ stats.fonts.processed += fontInfo.length;
|
|
|
+ stats.fonts.discarded += fontInfo.length;
|
|
|
if (woffFontFound) {
|
|
|
- fontSources = [woffFontFound];
|
|
|
+ fontInfo = [woffFontFound];
|
|
|
} else {
|
|
|
- let ttfFontFound = fontSources.find(fontSource => fontTest(fontSource, "truetype-variations"));
|
|
|
+ let ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype-variations"));
|
|
|
if (!ttfFontFound) {
|
|
|
- ttfFontFound = fontSources.find(fontSource => fontTest(fontSource, "truetype"));
|
|
|
+ ttfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "truetype"));
|
|
|
}
|
|
|
if (ttfFontFound) {
|
|
|
- fontSources = [ttfFontFound];
|
|
|
+ fontInfo = [ttfFontFound];
|
|
|
} else {
|
|
|
- let otfFontFound = fontSources.find(fontSource => fontTest(fontSource, "opentype"));
|
|
|
+ let otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "opentype"));
|
|
|
if (!otfFontFound) {
|
|
|
- otfFontFound = fontSources.find(fontSource => fontTest(fontSource, "embedded-opentype"));
|
|
|
+ otfFontFound = fontInfo.find(fontSource => fontTest(fontSource, "embedded-opentype"));
|
|
|
}
|
|
|
if (otfFontFound) {
|
|
|
- fontSources = [otfFontFound];
|
|
|
+ fontInfo = [otfFontFound];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- stats.fonts.discarded -= fontSources.length;
|
|
|
+ stats.fonts.discarded -= fontInfo.length;
|
|
|
let cssText = "";
|
|
|
Array.from(rule.style).forEach(propertyName => {
|
|
|
cssText += propertyName + ":";
|
|
|
if (propertyName == "src") {
|
|
|
- cssText += fontSources.map(fontSource => fontSource.src).join(",");
|
|
|
+ cssText += fontInfo.map(fontSource => fontSource.src).join(",");
|
|
|
} else {
|
|
|
cssText += rule.style.getPropertyValue(propertyName);
|
|
|
}
|