css-rules-minifier.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global CSSRule, parseCss */
  21. this.cssMinifier = this.cssMinifier || (() => {
  22. const DEBUG = false;
  23. return {
  24. process: (doc, mediaAllInfo) => {
  25. const stats = { processed: 0, discarded: 0 };
  26. doc.querySelectorAll("style").forEach((styleElement, sheetIndex) => {
  27. if (styleElement.sheet) {
  28. const cssRules = styleElement.sheet.cssRules;
  29. let mediaInfo;
  30. if (styleElement.media && styleElement.media != "all") {
  31. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + styleElement.media);
  32. } else {
  33. mediaInfo = mediaAllInfo;
  34. }
  35. stats.processed += cssRules.length;
  36. stats.discarded += cssRules.length;
  37. styleElement.textContent = processRules(doc, cssRules, sheetIndex, mediaInfo);
  38. stats.discarded -= cssRules.length;
  39. }
  40. });
  41. let startTime;
  42. if (DEBUG) {
  43. startTime = Date.now();
  44. log(" -- STARTED processStyleAttribute");
  45. }
  46. doc.querySelectorAll("[style]").forEach(element => {
  47. if (element.style) {
  48. const textContent = processStyleAttribute(element.style, mediaAllInfo);
  49. if (textContent) {
  50. element.setAttribute("style", textContent);
  51. } else {
  52. element.removeAttribute("style");
  53. }
  54. }
  55. });
  56. if (DEBUG) {
  57. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  58. }
  59. return stats;
  60. }
  61. };
  62. function processRules(doc, cssRules, sheetIndex, mediaInfo) {
  63. let sheetContent = "", mediaRuleIndex = 0, startTime;
  64. if (DEBUG && cssRules.length > 1) {
  65. startTime = Date.now();
  66. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  67. }
  68. Array.from(cssRules).forEach(cssRule => {
  69. if (cssRule.type == CSSRule.MEDIA_RULE) {
  70. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  71. sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText));
  72. mediaRuleIndex++;
  73. sheetContent += "}";
  74. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  75. const ruleInfo = mediaInfo.rules.get(cssRule);
  76. if (mediaInfo.pseudoSelectors.has(cssRule.selectorText)) {
  77. sheetContent += cssRule.cssText;
  78. } else if (ruleInfo) {
  79. sheetContent += processRuleInfo(cssRule, ruleInfo);
  80. }
  81. } else {
  82. sheetContent += cssRule.cssText;
  83. }
  84. });
  85. if (DEBUG && cssRules.length > 1) {
  86. log(" -- ENDED processRules delay =", Date.now() - startTime);
  87. }
  88. return sheetContent;
  89. }
  90. function processRuleInfo(cssRule, ruleInfo) {
  91. let selectorText = "", styleCssText = "";
  92. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  93. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  94. const style = stylesInfo[styleIndex];
  95. if (ruleInfo.style.get(style.name)) {
  96. if (styleCssText) {
  97. styleCssText += ";";
  98. }
  99. const priority = cssRule.style.getPropertyPriority(style.name);
  100. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  101. }
  102. }
  103. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  104. const newSelectors = [];
  105. const newSelectorsText = [];
  106. for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
  107. const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
  108. if (ruleInfo.matchedSelectors.has(ruleSelectorText)) {
  109. if (selectorText) {
  110. selectorText += ",";
  111. }
  112. newSelectors.push(ruleInfo.selectors[selectorTextIndex]);
  113. newSelectorsText.push(ruleSelectorText);
  114. selectorText += ruleSelectorText;
  115. }
  116. }
  117. }
  118. return (selectorText || cssRule.selectorText) + "{" + styleCssText + "}";
  119. }
  120. function processStyleAttribute(cssStyle, mediaAllInfo) {
  121. let styleCssText = "";
  122. const styleInfos = mediaAllInfo.matchedStyles.get(cssStyle);
  123. if (styleInfos) {
  124. styleInfos.style.forEach((styleValue, styleName) => {
  125. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  126. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  127. const style = stylesInfo[styleIndex];
  128. if (styleName == style.name) {
  129. if (styleCssText) {
  130. styleCssText += ";";
  131. }
  132. const priority = cssStyle.getPropertyPriority(style.name);
  133. styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
  134. }
  135. }
  136. });
  137. }
  138. return (styleCssText || cssStyle.cssText);
  139. }
  140. function log(...args) {
  141. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  142. }
  143. })();