|
|
@@ -31,53 +31,62 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
bold: "700"
|
|
|
};
|
|
|
|
|
|
+ let cssTree, fontPropertyParser, docHelper;
|
|
|
+
|
|
|
return {
|
|
|
- process: (doc, stylesheets, styles, options) => {
|
|
|
- const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
|
|
|
- const fontsInfo = { declared: [], used: [] };
|
|
|
- const workStyleElement = doc.createElement("style");
|
|
|
- let docContent = "";
|
|
|
- doc.body.appendChild(workStyleElement);
|
|
|
- stylesheets.forEach(stylesheetInfo => {
|
|
|
- const cssRules = stylesheetInfo.stylesheet.children;
|
|
|
- if (cssRules) {
|
|
|
- stats.processed += cssRules.getSize();
|
|
|
- stats.discarded += cssRules.getSize();
|
|
|
- getFontsInfo(cssRules, fontsInfo);
|
|
|
- docContent = getRulesTextContent(doc, cssRules, workStyleElement, docContent);
|
|
|
- }
|
|
|
- });
|
|
|
- styles.forEach(declarations => {
|
|
|
- const fontFamilyNames = getFontFamilyNames(declarations);
|
|
|
- if (fontFamilyNames.length) {
|
|
|
- fontsInfo.used.push(fontFamilyNames);
|
|
|
- }
|
|
|
- docContent = getDeclarationsTextContent(declarations.children, workStyleElement, docContent);
|
|
|
- });
|
|
|
- workStyleElement.remove();
|
|
|
- docContent += doc.body.innerText;
|
|
|
- const variableFound = fontsInfo.used.find(fontNames => fontNames.find(fontName => fontName.startsWith("var(--")));
|
|
|
- let unusedFonts, filteredUsedFonts;
|
|
|
- if (variableFound) {
|
|
|
- unusedFonts = [];
|
|
|
- } else {
|
|
|
- filteredUsedFonts = new Map();
|
|
|
- fontsInfo.used.forEach(fontNames => fontNames.forEach(familyName => {
|
|
|
- if (fontsInfo.declared.find(fontInfo => fontInfo.fontFamily == familyName)) {
|
|
|
- const optionalData = options.usedFonts && options.usedFonts.filter(fontInfo => fontInfo[0] == familyName);
|
|
|
- filteredUsedFonts.set(familyName, optionalData);
|
|
|
+ getInstance(cssTreeImpl, fontPropertyParserImpl, docHelperImpl) {
|
|
|
+ cssTree = cssTreeImpl;
|
|
|
+ fontPropertyParser = fontPropertyParserImpl;
|
|
|
+ docHelper = docHelperImpl;
|
|
|
+ return {
|
|
|
+ process: (doc, stylesheets, styles, options) => {
|
|
|
+ const stats = { rules: { processed: 0, discarded: 0 }, fonts: { processed: 0, discarded: 0 } };
|
|
|
+ const fontsInfo = { declared: [], used: [] };
|
|
|
+ const workStyleElement = doc.createElement("style");
|
|
|
+ let docContent = "";
|
|
|
+ doc.body.appendChild(workStyleElement);
|
|
|
+ stylesheets.forEach(stylesheetInfo => {
|
|
|
+ const cssRules = stylesheetInfo.stylesheet.children;
|
|
|
+ if (cssRules) {
|
|
|
+ stats.processed += cssRules.getSize();
|
|
|
+ stats.discarded += cssRules.getSize();
|
|
|
+ getFontsInfo(cssRules, fontsInfo);
|
|
|
+ docContent = getRulesTextContent(doc, cssRules, workStyleElement, docContent);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ styles.forEach(declarations => {
|
|
|
+ const fontFamilyNames = getFontFamilyNames(declarations);
|
|
|
+ if (fontFamilyNames.length) {
|
|
|
+ fontsInfo.used.push(fontFamilyNames);
|
|
|
+ }
|
|
|
+ docContent = getDeclarationsTextContent(declarations.children, workStyleElement, docContent);
|
|
|
+ });
|
|
|
+ workStyleElement.remove();
|
|
|
+ docContent += doc.body.innerText;
|
|
|
+ const variableFound = fontsInfo.used.find(fontNames => fontNames.find(fontName => fontName.startsWith("var(--")));
|
|
|
+ let unusedFonts, filteredUsedFonts;
|
|
|
+ if (variableFound) {
|
|
|
+ unusedFonts = [];
|
|
|
+ } else {
|
|
|
+ filteredUsedFonts = new Map();
|
|
|
+ fontsInfo.used.forEach(fontNames => fontNames.forEach(familyName => {
|
|
|
+ if (fontsInfo.declared.find(fontInfo => fontInfo.fontFamily == familyName)) {
|
|
|
+ const optionalData = options.usedFonts && options.usedFonts.filter(fontInfo => fontInfo[0] == familyName);
|
|
|
+ filteredUsedFonts.set(familyName, optionalData);
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.fontFamily));
|
|
|
}
|
|
|
- }));
|
|
|
- unusedFonts = fontsInfo.declared.filter(fontInfo => !filteredUsedFonts.has(fontInfo.fontFamily));
|
|
|
- }
|
|
|
- stylesheets.forEach(stylesheetInfo => {
|
|
|
- const cssRules = stylesheetInfo.stylesheet.children;
|
|
|
- if (cssRules) {
|
|
|
- filterUnusedFonts(cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
|
|
|
- stats.rules.discarded -= cssRules.getSize();
|
|
|
+ stylesheets.forEach(stylesheetInfo => {
|
|
|
+ const cssRules = stylesheetInfo.stylesheet.children;
|
|
|
+ if (cssRules) {
|
|
|
+ filterUnusedFonts(cssRules, fontsInfo.declared, unusedFonts, filteredUsedFonts, docContent);
|
|
|
+ stats.rules.discarded -= cssRules.getSize();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return stats;
|
|
|
}
|
|
|
- });
|
|
|
- return stats;
|
|
|
+ };
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -152,7 +161,6 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
}
|
|
|
|
|
|
function getDeclarationValue(declarations, propertyName) {
|
|
|
- const cssTree = this.fontsMinifier.cssTree;
|
|
|
let property;
|
|
|
if (declarations) {
|
|
|
property = declarations.filter(declaration => declaration.property == propertyName).tail;
|
|
|
@@ -167,7 +175,6 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
}
|
|
|
|
|
|
function getFontFamilyNames(declarations) {
|
|
|
- const [cssTree, fontPropertyParser] = [this.fontsMinifier.cssTree, this.fontsMinifier.fontPropertyParser];
|
|
|
let fontFamilyName = declarations.children.filter(node => node.property == "font-family").tail;
|
|
|
let fontFamilyNames = [];
|
|
|
if (fontFamilyName) {
|
|
|
@@ -260,7 +267,6 @@ this.fontsMinifier = this.fontsMinifier || (() => {
|
|
|
}
|
|
|
|
|
|
function getDeclarationUnescapedValue(declarations, property, workStylesheet) {
|
|
|
- const docHelper = this.fontsMinifier.docHelper;
|
|
|
const rawValue = docHelper.removeQuotes(getDeclarationValue(declarations, property) || "");
|
|
|
if (rawValue) {
|
|
|
workStylesheet.textContent = "tmp { content:\"" + rawValue + "\"}";
|