|
|
@@ -18,163 +18,48 @@
|
|
|
* along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
|
|
|
*/
|
|
|
|
|
|
-/* global CSSRule, cssWhat, parseCss */
|
|
|
+/* global CSSRule, cssWhat, parseCss, RulesMatcher */
|
|
|
|
|
|
this.cssMinifier = this.stylesMinifier || (() => {
|
|
|
|
|
|
const SEPARATOR_TYPES = ["descendant", "child", "sibling", "adjacent"];
|
|
|
const REMOVED_PSEUDO_CLASSES = ["focus", "focus-within", "hover", "link", "visited", "active"];
|
|
|
const REMOVED_PSEUDO_ELEMENTS = ["after", "before", "first-line", "first-letter"];
|
|
|
- const MEDIA_ALL = "all";
|
|
|
- const PRIORITY_IMPORTANT = "important";
|
|
|
|
|
|
return {
|
|
|
process: doc => {
|
|
|
- const mediaAllInfo = createMediaInfo(MEDIA_ALL);
|
|
|
+ const rulesMatcher = new RulesMatcher(doc);
|
|
|
+ const mediaAllInfo = rulesMatcher.getAllMatchedRules();
|
|
|
const stats = { processed: 0, discarded: 0 };
|
|
|
- doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
|
|
|
- if (styleElement.sheet) {
|
|
|
- stats.processed += styleElement.sheet.cssRules.length;
|
|
|
- stats.discarded += styleElement.sheet.cssRules.length;
|
|
|
- if (styleElement.media && styleElement.media != MEDIA_ALL) {
|
|
|
- const mediaInfo = createMediaInfo(styleElement.media);
|
|
|
- mediaAllInfo.medias.set(styleElement.media, mediaInfo);
|
|
|
- getMatchedElements(doc, styleElement.sheet.cssRules, mediaInfo, styleIndex);
|
|
|
- } else {
|
|
|
- getMatchedElements(doc, styleElement.sheet.cssRules, mediaAllInfo, styleIndex);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- sortRules(mediaAllInfo);
|
|
|
- computeCascade(mediaAllInfo);
|
|
|
doc.querySelectorAll("style").forEach(styleElement => {
|
|
|
if (styleElement.sheet) {
|
|
|
- replaceRules(doc, styleElement.sheet.cssRules, mediaAllInfo);
|
|
|
+ processRules(doc, styleElement.sheet.cssRules, mediaAllInfo);
|
|
|
styleElement.textContent = serializeRules(styleElement.sheet.cssRules);
|
|
|
stats.discarded -= styleElement.sheet.cssRules.length;
|
|
|
}
|
|
|
});
|
|
|
doc.querySelectorAll("[style]").forEach(element => {
|
|
|
- replaceStyle(doc, element.style, mediaAllInfo);
|
|
|
+ processStyle(doc, element.style, mediaAllInfo);
|
|
|
});
|
|
|
return stats;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- function getMatchedElements(doc, cssRules, mediaInfo, sheetIndex) {
|
|
|
- Array.from(cssRules).forEach((cssRule, ruleIndex) => {
|
|
|
- if (cssRule.type == CSSRule.MEDIA_RULE) {
|
|
|
- const ruleMediaInfo = createMediaInfo(cssRule.media);
|
|
|
- mediaInfo.medias.set(cssRule.media, ruleMediaInfo);
|
|
|
- getMatchedElements(doc, cssRule.cssRules, ruleMediaInfo, sheetIndex);
|
|
|
- } else if (cssRule.type == CSSRule.STYLE_RULE) {
|
|
|
- if (cssRule.selectorText) {
|
|
|
- let selectors = cssWhat.parse(cssRule.selectorText);
|
|
|
- let removedSelectors;
|
|
|
- selectors.forEach((selector, selectorIndex) => {
|
|
|
- const matchedElements = doc.querySelectorAll(cssWhat.stringify([selector]));
|
|
|
- if (matchedElements.length) {
|
|
|
- matchedElements.forEach(element => {
|
|
|
- let elementInfo;
|
|
|
- if (mediaInfo.elements.has(element)) {
|
|
|
- elementInfo = mediaInfo.elements.get(element);
|
|
|
- } else {
|
|
|
- elementInfo = [];
|
|
|
- const elementStyle = element.getAttribute("style");
|
|
|
- if (elementStyle) {
|
|
|
- elementInfo.push({ cssStyle: element.style });
|
|
|
- }
|
|
|
- mediaInfo.elements.set(element, elementInfo);
|
|
|
- }
|
|
|
- elementInfo.push({ cssRule, specificity: computeSpecificity(selector), selectorIndex, ruleIndex, sheetIndex });
|
|
|
- });
|
|
|
- } else {
|
|
|
- selectors = selectors.filter(s => s != selector);
|
|
|
- removedSelectors = true;
|
|
|
- }
|
|
|
- });
|
|
|
- if (selectors.length && removedSelectors) {
|
|
|
- cssRule.selectorText = cssWhat.stringify(selectors);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- function createMediaInfo(media) {
|
|
|
- const mediaInfo = { media: media, elements: new Map(), medias: new Map(), rules: new Map() };
|
|
|
- if (media == MEDIA_ALL) {
|
|
|
- mediaInfo.styles = new Map();
|
|
|
- }
|
|
|
- return mediaInfo;
|
|
|
- }
|
|
|
-
|
|
|
- function sortRules(media) {
|
|
|
- media.elements.forEach(elementRules => elementRules.sort(compareRules));
|
|
|
- media.medias.forEach(sortRules);
|
|
|
- }
|
|
|
-
|
|
|
- function computeCascade(mediaInfo, parentMediaInfos = []) {
|
|
|
- mediaInfo.elements.forEach((elementInfo) => {
|
|
|
- const elementStylesInfo = new Map();
|
|
|
- elementInfo.forEach(ruleInfo => {
|
|
|
- if (ruleInfo.cssStyle) {
|
|
|
- const cssStyle = ruleInfo.cssStyle;
|
|
|
- const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
|
|
|
- stylesInfo.forEach(styleInfo => {
|
|
|
- const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
|
|
|
- const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? " !" + PRIORITY_IMPORTANT : "");
|
|
|
- elementStylesInfo.set(styleInfo.name, { styleValue, cssStyle: ruleInfo.cssStyle, important });
|
|
|
- });
|
|
|
- } else {
|
|
|
- const cssStyle = ruleInfo.cssRule.style;
|
|
|
- const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
|
|
|
- stylesInfo.forEach(styleInfo => {
|
|
|
- const important = cssStyle.getPropertyPriority(styleInfo.name) == PRIORITY_IMPORTANT;
|
|
|
- const styleValue = cssStyle.getPropertyValue(styleInfo.name) + (important ? " !" + PRIORITY_IMPORTANT : "");
|
|
|
- let elementStyleInfo = elementStylesInfo.get(styleInfo.name);
|
|
|
- if (!elementStyleInfo || (important && !elementStyleInfo.important)) {
|
|
|
- elementStylesInfo.set(styleInfo.name, { styleValue, cssRule: ruleInfo.cssRule, important });
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- elementStylesInfo.forEach((styleInfo, styleName) => {
|
|
|
- let ruleInfo, ascendantMedia, allMedia;
|
|
|
- if (styleInfo.cssRule) {
|
|
|
- ascendantMedia = [mediaInfo, ...parentMediaInfos].find(media => media.rules.get(styleInfo.cssRule)) || mediaInfo;
|
|
|
- ruleInfo = ascendantMedia.rules.get(styleInfo.cssRule);
|
|
|
- }
|
|
|
- if (styleInfo.cssStyle) {
|
|
|
- allMedia = parentMediaInfos[parentMediaInfos.length - 1] || mediaInfo;
|
|
|
- ruleInfo = allMedia.styles.get(styleInfo.cssStyle);
|
|
|
- }
|
|
|
- if (!ruleInfo) {
|
|
|
- ruleInfo = new Map();
|
|
|
- if (styleInfo.cssRule) {
|
|
|
- ascendantMedia.rules.set(styleInfo.cssRule, ruleInfo);
|
|
|
- } else {
|
|
|
- allMedia.styles.set(styleInfo.cssStyle, ruleInfo);
|
|
|
- }
|
|
|
- }
|
|
|
- ruleInfo.set(styleName, styleInfo.styleValue);
|
|
|
- });
|
|
|
- });
|
|
|
- mediaInfo.medias.forEach(childMediaInfo => computeCascade(childMediaInfo, [mediaInfo, ...parentMediaInfos]));
|
|
|
- }
|
|
|
-
|
|
|
- function replaceRules(doc, cssRules, ruleMedia) {
|
|
|
+ function processRules(doc, cssRules, mediaInfo) {
|
|
|
Array.from(cssRules).forEach(cssRule => {
|
|
|
if (cssRule.type == CSSRule.MEDIA_RULE) {
|
|
|
- replaceRules(doc, cssRule.cssRules, ruleMedia.medias.get(cssRule.media));
|
|
|
+ processRules(doc, cssRule.cssRules, mediaInfo.medias.get(cssRule.media));
|
|
|
} else if (cssRule.type == CSSRule.STYLE_RULE) {
|
|
|
- const ruleInfo = ruleMedia.rules.get(cssRule);
|
|
|
+ const ruleInfo = mediaInfo.rules.get(cssRule);
|
|
|
if (ruleInfo) {
|
|
|
const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
|
|
|
- const unusedStyles = stylesInfo.filter(style => !ruleInfo.get(style.name));
|
|
|
+ const unusedStyles = stylesInfo.filter(style => !ruleInfo.style.get(style.name));
|
|
|
if (unusedStyles.length) {
|
|
|
unusedStyles.forEach(style => cssRule.style.removeProperty(style.name));
|
|
|
}
|
|
|
+ if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
|
|
|
+ cssRule.selectorText = ruleInfo.selectorsText.filter(selector => ruleInfo.matchedSelectors.has(selector)).join(",");
|
|
|
+ }
|
|
|
} else {
|
|
|
if (!testFilterSelector(cssRule.selectorText) || !doc.querySelector(getFilteredSelector(cssRule.selectorText))) {
|
|
|
const parent = cssRule.parentRule || cssRule.parentStyleSheet;
|
|
|
@@ -191,11 +76,11 @@ this.cssMinifier = this.stylesMinifier || (() => {
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- function replaceStyle(doc, cssStyle, ruleMedia) {
|
|
|
- const styleInfo = ruleMedia.styles.get(cssStyle);
|
|
|
+ function processStyle(doc, cssStyle, mediaInfo) {
|
|
|
+ const styleInfo = mediaInfo.styles.get(cssStyle);
|
|
|
if (styleInfo) {
|
|
|
const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
|
|
|
- const unusedStyles = stylesInfo.filter(style => !styleInfo.get(style.name));
|
|
|
+ const unusedStyles = stylesInfo.filter(style => !styleInfo.style.get(style.name));
|
|
|
if (unusedStyles.length) {
|
|
|
unusedStyles.forEach(style => cssStyle.removeProperty(style.name));
|
|
|
}
|
|
|
@@ -216,62 +101,6 @@ this.cssMinifier = this.stylesMinifier || (() => {
|
|
|
return sheetContent;
|
|
|
}
|
|
|
|
|
|
- function computeSpecificity(selector, specificity = { a: 0, b: 0, c: 0 }) {
|
|
|
- selector.forEach(token => {
|
|
|
- if (token.expandedSelector && token.type == "attribute" && token.name === "id" && token.action === "equals") {
|
|
|
- specificity.a++;
|
|
|
- }
|
|
|
- if ((!token.expandedSelector && token.type == "attribute") ||
|
|
|
- (token.expandedSelector && token.type == "attribute" && token.name === "class" && token.action === "element") ||
|
|
|
- (token.type == "pseudo" && token.name != "not")) {
|
|
|
- specificity.b++;
|
|
|
- }
|
|
|
- if ((token.type == "tag" && token.value != "*") || (token.type == "pseudo-element")) {
|
|
|
- specificity.c++;
|
|
|
- }
|
|
|
- if (token.data) {
|
|
|
- if (Array.isArray(token.data)) {
|
|
|
- token.data.forEach(selector => computeSpecificity(selector, specificity));
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
- return specificity;
|
|
|
- }
|
|
|
-
|
|
|
- function compareRules(ruleInfo1, ruleInfo2) {
|
|
|
- if (ruleInfo1.cssStyle && !ruleInfo2.cssStyle) {
|
|
|
- return -1;
|
|
|
- } else if (!ruleInfo1.cssStyle && ruleInfo2.cssStyle) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.specificity.a > ruleInfo2.specificity.a) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.specificity.a < ruleInfo2.specificity.a) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.specificity.b > ruleInfo2.specificity.b) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.specificity.b < ruleInfo2.specificity.b) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.specificity.c > ruleInfo2.specificity.c) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.specificity.c < ruleInfo2.specificity.c) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.sheetIndex > ruleInfo2.sheetIndex) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.sheetIndex < ruleInfo2.sheetIndex) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.ruleIndex > ruleInfo2.ruleIndex) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.ruleIndex < ruleInfo2.ruleIndex) {
|
|
|
- return 1;
|
|
|
- } else if (ruleInfo1.selectorIndex > ruleInfo2.selectorIndex) {
|
|
|
- return -1;
|
|
|
- } else if (ruleInfo1.selectorIndex < ruleInfo2.selectorIndex) {
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
function testFilterSelector(selector) {
|
|
|
return REMOVED_PSEUDO_CLASSES.find(pseudoClass => selector.includes(":" + pseudoClass)) || REMOVED_PSEUDO_ELEMENTS.find(pseudoElement => selector.includes("::" + pseudoElement));
|
|
|
}
|