css-rules-minifier.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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, RulesMatcher */
  21. this.cssMinifier = this.cssMinifier || (() => {
  22. const DEBUG = false;
  23. return {
  24. process: doc => {
  25. const rulesMatcher = RulesMatcher.create(doc);
  26. const mediaAllInfo = rulesMatcher.getAllMatchedRules();
  27. const stats = { processed: 0, discarded: 0 };
  28. doc.querySelectorAll("style").forEach((styleElement, sheetIndex) => {
  29. if (styleElement.sheet) {
  30. const cssRules = styleElement.sheet.cssRules;
  31. let mediaInfo;
  32. if (styleElement.media && styleElement.media != "all") {
  33. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + styleElement.media.mediaText);
  34. } else {
  35. mediaInfo = mediaAllInfo;
  36. }
  37. stats.processed += cssRules.length;
  38. stats.discarded += cssRules.length;
  39. styleElement.textContent = processRules(doc, cssRules, sheetIndex, mediaInfo);
  40. stats.discarded -= cssRules.length;
  41. }
  42. });
  43. let startTime;
  44. if (DEBUG) {
  45. startTime = Date.now();
  46. log(" -- STARTED processStyleAttribute");
  47. }
  48. doc.querySelectorAll("[style]").forEach(element => {
  49. let textContent = processStyleAttribute(element.style, mediaAllInfo);
  50. if (textContent) {
  51. element.setAttribute("style", textContent);
  52. } else {
  53. element.removeAttribute("style");
  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;
  64. let startTime;
  65. if (DEBUG && cssRules.length > 1) {
  66. startTime = Date.now();
  67. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  68. }
  69. Array.from(cssRules).forEach(cssRule => {
  70. if (cssRule.type == CSSRule.MEDIA_RULE) {
  71. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  72. sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText));
  73. mediaRuleIndex++;
  74. sheetContent += "}";
  75. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  76. const ruleInfo = mediaInfo.rules.get(cssRule);
  77. if (mediaInfo.pseudoSelectors.has(cssRule.selectorText)) {
  78. sheetContent += cssRule.cssText;
  79. } else if (ruleInfo) {
  80. sheetContent += processRuleInfo(cssRule, ruleInfo);
  81. }
  82. } else {
  83. sheetContent += cssRule.cssText;
  84. }
  85. });
  86. if (DEBUG && cssRules.length > 1) {
  87. log(" -- ENDED processRules delay =", Date.now() - startTime);
  88. }
  89. return sheetContent;
  90. }
  91. function processRuleInfo(cssRule, ruleInfo) {
  92. let selectorText = "", styleCssText = "";
  93. if (ruleInfo) {
  94. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  95. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  96. const style = stylesInfo[styleIndex];
  97. if (ruleInfo.style.get(style.name)) {
  98. if (styleCssText) {
  99. styleCssText += ";";
  100. }
  101. const priority = cssRule.style.getPropertyPriority(style.name);
  102. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  103. }
  104. }
  105. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  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. selectorText += ruleSelectorText;
  113. }
  114. }
  115. }
  116. }
  117. return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
  118. }
  119. function processStyleAttribute(cssStyle, mediaInfo) {
  120. let styleCssText = "";
  121. const styleInfo = mediaInfo.styles.get(cssStyle);
  122. if (styleInfo) {
  123. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  124. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  125. const style = stylesInfo[styleIndex];
  126. if (styleInfo.style.get(style.name)) {
  127. if (styleCssText) {
  128. styleCssText += ";";
  129. }
  130. const priority = cssStyle.getPropertyPriority(style.name);
  131. styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
  132. }
  133. }
  134. }
  135. return (styleCssText || cssStyle.cssText);
  136. }
  137. function log(...args) {
  138. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  139. }
  140. })();