css-rules-minifier.js 5.0 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, 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, styleIndex) => {
  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(styleIndex + "-" + styleElement.media);
  34. } else {
  35. mediaInfo = mediaAllInfo;
  36. }
  37. stats.processed += cssRules.length;
  38. stats.discarded += cssRules.length;
  39. styleElement.textContent = processRules(doc, cssRules, 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, mediaInfo) {
  63. let sheetContent = "";
  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, mediaInfo.medias.get(cssRule.media));
  73. sheetContent += "}";
  74. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  75. const ruleInfo = mediaInfo.rules.get(cssRule);
  76. if (ruleInfo) {
  77. sheetContent += processRuleInfo(cssRule, ruleInfo);
  78. } else if (mediaInfo.pseudoSelectors.has(cssRule.selectorText)) {
  79. sheetContent += cssRule.cssText;
  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. if (ruleInfo) {
  93. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  94. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  95. const style = stylesInfo[styleIndex];
  96. if (ruleInfo.style.get(style.name)) {
  97. if (styleCssText) {
  98. styleCssText += ";";
  99. }
  100. const priority = cssRule.style.getPropertyPriority(style.name);
  101. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  102. }
  103. }
  104. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  105. for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
  106. const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
  107. if (ruleInfo.matchedSelectors.has(ruleSelectorText)) {
  108. if (selectorText) {
  109. selectorText += ",";
  110. }
  111. selectorText += ruleSelectorText;
  112. }
  113. }
  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. })();