css-rules-minifier.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.mediaText);
  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. let textContent = processStyleAttribute(element.style, mediaAllInfo);
  48. if (textContent) {
  49. element.setAttribute("style", textContent);
  50. } else {
  51. element.removeAttribute("style");
  52. }
  53. });
  54. if (DEBUG) {
  55. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  56. }
  57. return stats;
  58. }
  59. };
  60. function processRules(doc, cssRules, sheetIndex, mediaInfo) {
  61. let sheetContent = "", mediaRuleIndex = 0;
  62. let startTime;
  63. if (DEBUG && cssRules.length > 1) {
  64. startTime = Date.now();
  65. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  66. }
  67. Array.from(cssRules).forEach(cssRule => {
  68. if (cssRule.type == CSSRule.MEDIA_RULE) {
  69. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  70. sheetContent += processRules(doc, cssRule.cssRules, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + cssRule.media.mediaText));
  71. mediaRuleIndex++;
  72. sheetContent += "}";
  73. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  74. const ruleInfo = mediaInfo.rules.get(cssRule);
  75. if (mediaInfo.pseudoSelectors.has(cssRule.selectorText)) {
  76. sheetContent += cssRule.cssText;
  77. } else if (ruleInfo) {
  78. sheetContent += processRuleInfo(cssRule, ruleInfo);
  79. }
  80. } else {
  81. sheetContent += cssRule.cssText;
  82. }
  83. });
  84. if (DEBUG && cssRules.length > 1) {
  85. log(" -- ENDED processRules delay =", Date.now() - startTime);
  86. }
  87. return sheetContent;
  88. }
  89. function processRuleInfo(cssRule, ruleInfo) {
  90. let selectorText = "", styleCssText = "";
  91. if (ruleInfo) {
  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. for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
  105. const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
  106. if (ruleInfo.matchedSelectors.has(ruleSelectorText)) {
  107. if (selectorText) {
  108. selectorText += ", ";
  109. }
  110. selectorText += ruleSelectorText;
  111. }
  112. }
  113. cssRule.selectorText = selectorText;
  114. }
  115. }
  116. return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
  117. }
  118. function processStyleAttribute(cssStyle, mediaInfo) {
  119. let styleCssText = "";
  120. const styleInfo = mediaInfo.styles.get(cssStyle);
  121. if (styleInfo) {
  122. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  123. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  124. const style = stylesInfo[styleIndex];
  125. if (styleInfo.style.get(style.name)) {
  126. if (styleCssText) {
  127. styleCssText += ";";
  128. }
  129. const priority = cssStyle.getPropertyPriority(style.name);
  130. styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
  131. }
  132. }
  133. }
  134. return (styleCssText || cssStyle.cssText);
  135. }
  136. function log(...args) {
  137. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  138. }
  139. })();