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 */
  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. 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. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  92. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  93. const style = stylesInfo[styleIndex];
  94. if (ruleInfo.style.get(style.name)) {
  95. if (styleCssText) {
  96. styleCssText += ";";
  97. }
  98. const priority = cssRule.style.getPropertyPriority(style.name);
  99. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  100. }
  101. }
  102. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  103. const newSelectors = [];
  104. const newSelectorsText = [];
  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. newSelectors.push(ruleInfo.selectors[selectorTextIndex]);
  112. newSelectorsText.push(ruleSelectorText);
  113. selectorText += ruleSelectorText;
  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. })();