css-rules-minifier.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 FILTERED_PSEUDO_CLASSES = [":focus", ":focus-within", ":hover", ":link", ":visited", ":active"];
  23. const FILTERED_PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", "::placeholder", "::-webkit-input-placeholder", "::selection", "::marker", "::cue", "::-webkit-progress-bar", "::-webkit-progress-value", "::-webkit-inner-spin-button", "::-webkit-outer-spin-button", "::-webkit-search-cancel-button", "::-webkit-search-cancel-button"];
  24. const FILTERED_SELECTORS = ["::-webkit-scrollbar", "::-webkit-scrollbar-button", "::-webkit-scrollbar-thumb", "::-webkit-scrollbar-track", "::-webkit-scrollbar-track-piece", "::-webkit-scrollbar-corner", "::-webkit-resizer"];
  25. const IGNORED_SELECTORS = FILTERED_SELECTORS.concat(FILTERED_PSEUDO_CLASSES).concat(FILTERED_PSEUDO_ELEMENTS);
  26. const DEBUG = false;
  27. return {
  28. process: doc => {
  29. const rulesMatcher = RulesMatcher.create(doc);
  30. const mediaAllInfo = rulesMatcher.getAllMatchedRules();
  31. const stats = { processed: 0, discarded: 0 };
  32. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  33. if (styleElement.sheet) {
  34. const cssRules = styleElement.sheet.cssRules;
  35. let mediaInfo;
  36. if (styleElement.media && styleElement.media != "all") {
  37. mediaInfo = mediaAllInfo.medias.get(styleIndex + "-" + styleElement.media);
  38. } else {
  39. mediaInfo = mediaAllInfo;
  40. }
  41. stats.processed += cssRules.length;
  42. stats.discarded += cssRules.length;
  43. styleElement.textContent = processRules(doc, cssRules, mediaInfo);
  44. stats.discarded -= cssRules.length;
  45. }
  46. });
  47. let startTime;
  48. if (DEBUG) {
  49. startTime = Date.now();
  50. log(" -- STARTED processStyleAttribute");
  51. }
  52. doc.querySelectorAll("[style]").forEach(element => {
  53. let textContent = processStyleAttribute(element.style, mediaAllInfo);
  54. if (textContent) {
  55. element.setAttribute("style", textContent);
  56. } else {
  57. element.removeAttribute("style");
  58. }
  59. });
  60. if (DEBUG) {
  61. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  62. }
  63. return stats;
  64. }
  65. };
  66. function processRules(doc, cssRules, mediaInfo) {
  67. let sheetContent = "";
  68. let startTime;
  69. if (DEBUG && cssRules.length > 1) {
  70. startTime = Date.now();
  71. log(" -- STARTED processRules", "rules.length =", cssRules.length);
  72. }
  73. Array.from(cssRules).forEach(cssRule => {
  74. if (cssRule.type == CSSRule.MEDIA_RULE) {
  75. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  76. sheetContent += processRules(doc, cssRule.cssRules, mediaInfo.medias.get(cssRule.media));
  77. sheetContent += "}";
  78. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  79. const ruleInfo = mediaInfo.rules.get(cssRule);
  80. if (ruleInfo || testIgnoredSelector(cssRule.selectorText)) {
  81. sheetContent += processRuleInfo(cssRule, ruleInfo);
  82. }
  83. } else {
  84. sheetContent += cssRule.cssText;
  85. }
  86. });
  87. if (DEBUG && cssRules.length > 1) {
  88. log(" -- ENDED processRules delay =", Date.now() - startTime);
  89. }
  90. return sheetContent;
  91. }
  92. function processRuleInfo(cssRule, ruleInfo) {
  93. let selectorText = "", styleCssText = "";
  94. if (ruleInfo) {
  95. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  96. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  97. const style = stylesInfo[styleIndex];
  98. if (ruleInfo.style.get(style.name)) {
  99. if (styleCssText) {
  100. styleCssText += ";";
  101. }
  102. const priority = cssRule.style.getPropertyPriority(style.name);
  103. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  104. }
  105. }
  106. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  107. for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
  108. const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
  109. if (ruleInfo.matchedSelectors.has(ruleSelectorText) || testIgnoredSelector(ruleSelectorText)) {
  110. if (selectorText) {
  111. selectorText += ",";
  112. }
  113. selectorText += ruleSelectorText;
  114. }
  115. }
  116. }
  117. }
  118. return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
  119. }
  120. function processStyleAttribute(cssStyle, mediaInfo) {
  121. let styleCssText = "";
  122. const styleInfo = mediaInfo.styles.get(cssStyle);
  123. if (styleInfo) {
  124. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  125. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  126. const style = stylesInfo[styleIndex];
  127. if (styleInfo.style.get(style.name)) {
  128. if (styleCssText) {
  129. styleCssText += ";";
  130. }
  131. const priority = cssStyle.getPropertyPriority(style.name);
  132. styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
  133. }
  134. }
  135. }
  136. return (styleCssText || cssStyle.cssText);
  137. }
  138. function testIgnoredSelector(selectorText) {
  139. let indexSelector = 0, found;
  140. selectorText = selectorText.toLowerCase();
  141. while (indexSelector < IGNORED_SELECTORS.length && !found) {
  142. found = selectorText.includes(IGNORED_SELECTORS[indexSelector]);
  143. if (!found) {
  144. indexSelector++;
  145. }
  146. }
  147. return found;
  148. }
  149. function log(...args) {
  150. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  151. }
  152. })();