| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- /*
- * Copyright 2018 Gildas Lormeau
- * contact : gildas.lormeau <at> gmail.com
- *
- * This file is part of SingleFile.
- *
- * SingleFile is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * SingleFile is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
- */
- /* global CSSRule */
- this.rulesMinifier = this.rulesMinifier || (() => {
- const REGEXP_PSEUDO_CLASSES = /::after|::before|::first-line|::first-letter|:focus|:focus-within|:hover|:link|:visited|:active/gi;
- return {
- process: doc => {
- const rulesData = {
- selectors: new Set(),
- fonts: {
- declared: new Set(),
- used: new Set()
- }
- };
- const stats = {
- processed: 0,
- discarded: 0
- };
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- const processed = style.sheet.cssRules.length;
- stats.processed += processed;
- style.textContent = processRules(doc, style.sheet.cssRules, rulesData);
- stats.discarded += processed - style.sheet.cssRules.length;
- }
- });
- doc.querySelectorAll("[style]").forEach(element => {
- if (element.style.fontFamily) {
- element.style.fontFamily.split(",").forEach(fontFamily => rulesData.fonts.used.add(getFontFamily(fontFamily)));
- }
- });
- const unusedFonts = Array.from(rulesData.fonts.declared).filter(fontFamily => !rulesData.fonts.used.has(fontFamily));
- doc.querySelectorAll("style").forEach(style => {
- if (style.sheet) {
- const processed = style.sheet.cssRules.length;
- style.textContent = deleteUnusedFonts(doc, style.sheet.cssRules, unusedFonts);
- stats.discarded += processed - style.sheet.cssRules.length;
- }
- });
- return stats;
- }
- };
- function processRules(doc, rules, rulesData) {
- let stylesheetContent = "";
- if (rules) {
- Array.from(rules).forEach(rule => {
- if (rule.media) {
- stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + " {";
- stylesheetContent += processRules(doc, rule.cssRules, rulesData);
- stylesheetContent += "}";
- } else if (rule.selectorText) {
- const selector = getFilteredSelector(rule.selectorText);
- if (selector) {
- try {
- if (rulesData.selectors.has(selector) || doc.querySelector(selector)) {
- stylesheetContent += rule.cssText;
- rulesData.selectors.add(selector);
- if (rule.style && rule.style.fontFamily) {
- rule.style.fontFamily.split(",").forEach(fontFamily => rulesData.fonts.used.add(getFontFamily(fontFamily)));
- }
- }
- } catch (error) {
- stylesheetContent += rule.cssText;
- }
- }
- } else {
- if (rule.type == CSSRule.FONT_FACE_RULE && rule.style && rule.style.fontFamily) {
- rulesData.fonts.declared.add(getFontFamily(rule.style.fontFamily));
- }
- stylesheetContent += rule.cssText;
- }
- });
- }
- return stylesheetContent;
- }
- function deleteUnusedFonts(doc, rules, unusedFonts) {
- let stylesheetContent = "";
- if (rules) {
- Array.from(rules).forEach(rule => {
- if (rule.media) {
- stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + " {";
- stylesheetContent += deleteUnusedFonts(doc, rule.cssRules, unusedFonts);
- stylesheetContent += "}";
- } else if (rule.type != CSSRule.FONT_FACE_RULE || (rule.type == CSSRule.FONT_FACE_RULE && rule.style && rule.style.fontFamily && !unusedFonts.includes(getFontFamily(rule.style.fontFamily)))) {
- stylesheetContent += rule.cssText;
- }
- });
- }
- return stylesheetContent;
- }
- function getFilteredSelector(selector) {
- if (selector.match(REGEXP_PSEUDO_CLASSES)) {
- let selectors = selector.split(/\s*,\s*/g);
- selector = selectors.map(selector => {
- const simpleSelectors = selector.split(/\s*[ >~+]\s*/g);
- const separators = selector.match(/\s*[ >~+]\s*/g);
- return simpleSelectors.map((selector, selectorIndex) => {
- while (selector.match(REGEXP_PSEUDO_CLASSES)) {
- selector = selector.replace(REGEXP_PSEUDO_CLASSES, "").trim();
- }
- selector = selector.replace(/:?:[^(]+\(\)/g, "");
- if (selector == "") {
- selector = "*";
- }
- return selector + (separators && separators[selectorIndex] ? separators[selectorIndex] : "");
- }).join("");
- }).join(",");
- }
- return selector;
- }
- function getFontFamily(fontFamily) {
- fontFamily = fontFamily.toLowerCase().trim();
- if (fontFamily.match(/^'(.*)'$/)) {
- fontFamily = fontFamily.replace(/^'(.*)'$/, "$1");
- } else {
- fontFamily = fontFamily.replace(/^"(.*)"$/, "$1");
- }
- return fontFamily.trim();
- }
- })();
|