1
0

css-rules-minifier.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 REMOVED_PSEUDO_CLASSES = [":focus", ":focus-within", ":hover", ":link", ":visited", ":active"];
  23. const REMOVED_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 IGNORED_SELECTORS = ["::-webkit-scrollbar", "::-webkit-scrollbar-button", "::-webkit-scrollbar-thumb", "::-webkit-scrollbar-track", "::-webkit-scrollbar-track-piece", "::-webkit-scrollbar-corner", "::-webkit-resizer"];
  25. return {
  26. process: doc => {
  27. const rulesMatcher = RulesMatcher.create(doc);
  28. const mediaAllInfo = rulesMatcher.getAllMatchedRules();
  29. const stats = { processed: 0, discarded: 0 };
  30. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  31. if (styleElement.sheet) {
  32. let mediaInfo;
  33. if (styleElement.media && styleElement.media != "all") {
  34. mediaInfo = mediaAllInfo.medias.get(styleIndex + "-" + styleElement.media);
  35. } else {
  36. mediaInfo = mediaAllInfo;
  37. }
  38. processRules(doc, styleElement.sheet.cssRules, mediaInfo, stats);
  39. styleElement.textContent = serializeRules(styleElement.sheet.cssRules);
  40. }
  41. });
  42. doc.querySelectorAll("[style]").forEach(element => {
  43. processStyle(doc, element.style, mediaAllInfo);
  44. });
  45. return stats;
  46. }
  47. };
  48. function processRules(doc, cssRules, mediaInfo, stats) {
  49. stats.processed += cssRules.length;
  50. stats.discarded += cssRules.length;
  51. Array.from(cssRules).forEach(cssRule => {
  52. if (cssRule.type == CSSRule.MEDIA_RULE) {
  53. processRules(doc, cssRule.cssRules, mediaInfo.medias.get(cssRule.media), stats);
  54. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  55. const ruleInfo = mediaInfo.rules.get(cssRule);
  56. if (ruleInfo) {
  57. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  58. const unusedStyles = stylesInfo.filter(style => !ruleInfo.style.get(style.name));
  59. if (unusedStyles.length) {
  60. unusedStyles.forEach(style => cssRule.style.removeProperty(style.name));
  61. }
  62. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  63. cssRule.selectorText = ruleInfo.selectorsText.filter(selectorText => ruleInfo.matchedSelectors.has(selectorText) || testIgnoredSelector(selectorText)).join(",");
  64. }
  65. } else {
  66. if (!testIgnoredSelector(cssRule.selectorText)) {
  67. const parent = cssRule.parentRule || cssRule.parentStyleSheet;
  68. let indexRule = 0;
  69. while (cssRule != parent.cssRules[indexRule] && indexRule < parent.cssRules.length) {
  70. indexRule++;
  71. }
  72. if (cssRule == parent.cssRules[indexRule]) {
  73. parent.deleteRule(indexRule);
  74. }
  75. }
  76. }
  77. }
  78. });
  79. stats.discarded -= cssRules.length;
  80. }
  81. function processStyle(doc, cssStyle, mediaInfo) {
  82. const styleInfo = mediaInfo.styles.get(cssStyle);
  83. if (styleInfo) {
  84. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  85. const unusedStyles = stylesInfo.filter(style => !styleInfo.style.get(style.name));
  86. if (unusedStyles.length) {
  87. unusedStyles.forEach(style => cssStyle.removeProperty(style.name));
  88. }
  89. }
  90. }
  91. function serializeRules(rules) {
  92. let sheetContent = "";
  93. Array.from(rules).forEach(rule => {
  94. if (rule.media) {
  95. sheetContent += "@media " + Array.from(rule.media).join(",") + "{";
  96. sheetContent += serializeRules(rule.cssRules);
  97. sheetContent += "}";
  98. } else {
  99. sheetContent += rule.cssText;
  100. }
  101. });
  102. return sheetContent;
  103. }
  104. function testIgnoredSelector(selectorText) {
  105. let indexSelector = 0, found;
  106. selectorText = selectorText.toLowerCase();
  107. while (indexSelector < IGNORED_SELECTORS.length && !found) {
  108. found = selectorText.includes(IGNORED_SELECTORS[indexSelector]);
  109. if (!found) {
  110. indexSelector++;
  111. }
  112. }
  113. if (!found) {
  114. indexSelector = 0;
  115. while (indexSelector < IGNORED_SELECTORS.length && !found) {
  116. found = testFilterSelector(selectorText);
  117. if (!found) {
  118. indexSelector++;
  119. }
  120. }
  121. }
  122. return found;
  123. }
  124. function testFilterSelector(selector) {
  125. let indexPseudoClass = 0, found;
  126. while (indexPseudoClass < REMOVED_PSEUDO_CLASSES.length && !found) {
  127. found = selector.includes(REMOVED_PSEUDO_CLASSES[indexPseudoClass]);
  128. if (!found) {
  129. indexPseudoClass++;
  130. }
  131. }
  132. if (!found) {
  133. let indexPseudoElement = 0;
  134. while (indexPseudoElement < REMOVED_PSEUDO_ELEMENTS.length && !found) {
  135. found = selector.includes(REMOVED_PSEUDO_ELEMENTS[indexPseudoElement]);
  136. if (!found) {
  137. indexPseudoElement++;
  138. }
  139. }
  140. }
  141. return found;
  142. }
  143. })();