|
@@ -100,7 +100,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
const docContent = doc.body.innerText + pseudoElementsContent;
|
|
const docContent = doc.body.innerText + pseudoElementsContent;
|
|
|
doc.querySelectorAll("style").forEach(style => {
|
|
doc.querySelectorAll("style").forEach(style => {
|
|
|
if (style.sheet) {
|
|
if (style.sheet) {
|
|
|
- style.textContent = filterUnusedFonts(doc, style.sheet.cssRules, unusedFonts, filteredUsedFonts, docContent);
|
|
|
|
|
|
|
+ style.textContent = filterUnusedFonts(doc, style.sheet.cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
|
|
|
stats.rules.discarded -= style.sheet.cssRules.length;
|
|
stats.rules.discarded -= style.sheet.cssRules.length;
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
@@ -281,27 +281,43 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
return cssText;
|
|
return cssText;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- function filterUnusedFonts(doc, rules, unusedFonts, filteredUsedFonts, docContent) {
|
|
|
|
|
|
|
+ function filterUnusedFonts(doc, rules, declaredFonts, unusedFonts, filteredUsedFonts, docContent) {
|
|
|
let stylesheetContent = "";
|
|
let stylesheetContent = "";
|
|
|
if (rules) {
|
|
if (rules) {
|
|
|
Array.from(rules).forEach(rule => {
|
|
Array.from(rules).forEach(rule => {
|
|
|
if (rule.media) {
|
|
if (rule.media) {
|
|
|
stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + "{";
|
|
stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + "{";
|
|
|
- stylesheetContent += filterUnusedFonts(doc, rule.cssRules, unusedFonts, filteredUsedFonts, docContent);
|
|
|
|
|
|
|
+ stylesheetContent += filterUnusedFonts(doc, rule.cssRules, declaredFonts, unusedFonts, filteredUsedFonts, docContent);
|
|
|
stylesheetContent += "}";
|
|
stylesheetContent += "}";
|
|
|
} else if (rule.type == CSSRule.FONT_FACE_RULE) {
|
|
} else if (rule.type == CSSRule.FONT_FACE_RULE) {
|
|
|
if (rule.style) {
|
|
if (rule.style) {
|
|
|
const fontFamily = removeQuotes(rule.style.getPropertyValue("font-family"));
|
|
const fontFamily = removeQuotes(rule.style.getPropertyValue("font-family"));
|
|
|
- if (fontFamily && !unusedFonts.includes(fontFamily)) {
|
|
|
|
|
|
|
+ if (fontFamily && !unusedFonts.find(fontInfo => fontInfo.familyName == fontFamily)) {
|
|
|
let optionalTest;
|
|
let optionalTest;
|
|
|
const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(fontFamily);
|
|
const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(fontFamily);
|
|
|
if (optionalUsedFonts && optionalUsedFonts.length) {
|
|
if (optionalUsedFonts && optionalUsedFonts.length) {
|
|
|
const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
|
|
const fontStyle = rule.style.getPropertyValue("font-style") || "normal";
|
|
|
|
|
+ // const fontWeight = getFontWeight(rule.style.getPropertyValue("font-weight")) || "400";
|
|
|
optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontStyle == fontStyle);
|
|
optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontStyle == fontStyle);
|
|
|
if (optionalTest) {
|
|
if (optionalTest) {
|
|
|
const fontVariant = rule.style.getPropertyValue("font-variant");
|
|
const fontVariant = rule.style.getPropertyValue("font-variant");
|
|
|
optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures");
|
|
optionalTest = optionalUsedFonts.find(fontInfo => fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures");
|
|
|
}
|
|
}
|
|
|
|
|
+ /*
|
|
|
|
|
+ if (optionalTest) {
|
|
|
|
|
+ if (isNaN(fontWeight)) {
|
|
|
|
|
+ optionalTest = true;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ const usedFontWeights = optionalUsedFonts.map(fontInfo => fontInfo.fontWeight);
|
|
|
|
|
+ const declaredFontsWeights = declaredFonts
|
|
|
|
|
+ .filter(fontInfo => fontInfo.familyName == fontFamily && fontInfo.fontStyle == fontStyle && (fontInfo.fontVariant == fontVariant || "normal" || fontInfo.fontVariant == fontVariant || "common-ligatures"))
|
|
|
|
|
+ .map(fontInfo => fontInfo.fontWeight)
|
|
|
|
|
+ .sort((w1, w2) => w1 - w2);
|
|
|
|
|
+ const usedComputedFontWeights = usedFontWeights.map(fontWeight => findFontWeight(fontWeight, declaredFontsWeights));
|
|
|
|
|
+ optionalTest = usedComputedFontWeights.includes(fontWeight);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ */
|
|
|
} else {
|
|
} else {
|
|
|
optionalTest = true;
|
|
optionalTest = true;
|
|
|
}
|
|
}
|
|
@@ -318,6 +334,32 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
return stylesheetContent;
|
|
return stylesheetContent;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ function findFontWeight(fontWeight, fontWeights) {
|
|
|
|
|
+ let foundWeight;
|
|
|
|
|
+ if (fontWeight >= 400 && fontWeight <= 500) {
|
|
|
|
|
+ foundWeight = fontWeights.find(weight => weight >= fontWeight && weight <= 500);
|
|
|
|
|
+ if (!foundWeight) {
|
|
|
|
|
+ foundWeight = fontWeights.slice().reverse().find(weight => weight < fontWeight);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!foundWeight) {
|
|
|
|
|
+ foundWeight = fontWeights.find(weight => weight > fontWeight);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fontWeight < 400) {
|
|
|
|
|
+ foundWeight = fontWeights.slice().reverse().find(weight => weight <= fontWeight);
|
|
|
|
|
+ if (!foundWeight) {
|
|
|
|
|
+ foundWeight = fontWeights.find(weight => weight > fontWeight);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fontWeight > 500) {
|
|
|
|
|
+ foundWeight = fontWeights.find(weight => weight >= fontWeight);
|
|
|
|
|
+ if (!foundWeight) {
|
|
|
|
|
+ foundWeight = fontWeights.slice().reverse().find(weight => weight < fontWeight);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return foundWeight;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
function getPseudoElementsContent(doc, rules) {
|
|
function getPseudoElementsContent(doc, rules) {
|
|
|
if (rules) {
|
|
if (rules) {
|
|
|
return Array.from(rules).map(rule => {
|
|
return Array.from(rules).map(rule => {
|