| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * 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, parseCss */
- this.cssMinifier = this.cssMinifier || (() => {
- const DEBUG = false;
- return {
- process: (doc, mediaAllInfo) => {
- const stats = { processed: 0, discarded: 0 };
- doc.querySelectorAll("style").forEach((styleElement, sheetIndex) => {
- if (styleElement.sheet) {
- const cssRules = styleElement.sheet.cssRules;
- let mediaInfo;
- if (styleElement.media && styleElement.media != "all") {
- mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + styleElement.media.mediaText);
- } else {
- mediaInfo = mediaAllInfo;
- }
- stats.processed += cssRules.length;
- stats.discarded += cssRules.length;
- styleElement.textContent = processRules(doc, cssRules, sheetIndex, mediaInfo);
- stats.discarded -= cssRules.length;
- }
- });
- let startTime;
- if (DEBUG) {
- startTime = Date.now();
- log(" -- STARTED processStyleAttribute");
- }
- doc.querySelectorAll("[style]").forEach(element => {
- let textContent = processStyleAttribute(element.style, mediaAllInfo);
- if (textContent) {
- element.setAttribute("style", textContent);
- } else {
- element.removeAttribute("style");
- }
- });
- if (DEBUG) {
- log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
- }
- return stats;
- }
- };
- function processRules(doc, cssRules, sheetIndex, mediaInfo) {
- let sheetContent = "", mediaRuleIndex = 0;
- let startTime;
- if (DEBUG && cssRules.length > 1) {
- startTime = Date.now();
- log(" -- STARTED processRules", "rules.length =", cssRules.length);
- }
- Array.from(cssRules).forEach(cssRule => {
- if (cssRule.type == CSSRule.MEDIA_RULE) {
- sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
- sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText));
- mediaRuleIndex++;
- sheetContent += "}";
- } else if (cssRule.type == CSSRule.STYLE_RULE) {
- const ruleInfo = mediaInfo.rules.get(cssRule);
- if (mediaInfo.pseudoSelectors.has(cssRule.selectorText)) {
- sheetContent += cssRule.cssText;
- } else if (ruleInfo) {
- sheetContent += processRuleInfo(cssRule, ruleInfo);
- }
- } else {
- sheetContent += cssRule.cssText;
- }
- });
- if (DEBUG && cssRules.length > 1) {
- log(" -- ENDED processRules delay =", Date.now() - startTime);
- }
- return sheetContent;
- }
- function processRuleInfo(cssRule, ruleInfo) {
- let selectorText = "", styleCssText = "";
- if (ruleInfo) {
- const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
- for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
- const style = stylesInfo[styleIndex];
- if (ruleInfo.style.get(style.name)) {
- if (styleCssText) {
- styleCssText += ";";
- }
- const priority = cssRule.style.getPropertyPriority(style.name);
- styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
- }
- }
- if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
- for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
- const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
- if (ruleInfo.matchedSelectors.has(ruleSelectorText)) {
- if (selectorText) {
- selectorText += ", ";
- }
- selectorText += ruleSelectorText;
- }
- }
- cssRule.selectorText = selectorText;
- }
- }
- return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
- }
- function processStyleAttribute(cssStyle, mediaInfo) {
- let styleCssText = "";
- const styleInfo = mediaInfo.styles.get(cssStyle);
- if (styleInfo) {
- const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
- for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
- const style = stylesInfo[styleIndex];
- if (styleInfo.style.get(style.name)) {
- if (styleCssText) {
- styleCssText += ";";
- }
- const priority = cssStyle.getPropertyPriority(style.name);
- styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
- }
- }
- }
- return (styleCssText || cssStyle.cssText);
- }
- function log(...args) {
- console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
- }
- })();
|