css-rules-minifier.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 testIgnoredSelector(selectorText) {
  82. return IGNORED_SELECTORS.find(selector => selectorText.toLowerCase().includes(selector)) || testFilterSelector(selectorText);
  83. }
  84. function processStyle(doc, cssStyle, mediaInfo) {
  85. const styleInfo = mediaInfo.styles.get(cssStyle);
  86. if (styleInfo) {
  87. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  88. const unusedStyles = stylesInfo.filter(style => !styleInfo.style.get(style.name));
  89. if (unusedStyles.length) {
  90. unusedStyles.forEach(style => cssStyle.removeProperty(style.name));
  91. }
  92. }
  93. }
  94. function serializeRules(rules) {
  95. let sheetContent = "";
  96. Array.from(rules).forEach(rule => {
  97. if (rule.media) {
  98. sheetContent += "@media " + Array.from(rule.media).join(",") + "{";
  99. sheetContent += serializeRules(rule.cssRules);
  100. sheetContent += "}";
  101. } else {
  102. sheetContent += rule.cssText;
  103. }
  104. });
  105. return sheetContent;
  106. }
  107. function testFilterSelector(selector) {
  108. return REMOVED_PSEUDO_CLASSES.find(pseudoClass => selector.includes(":" + pseudoClass)) || REMOVED_PSEUDO_ELEMENTS.find(pseudoElement => selector.includes("::" + pseudoElement));
  109. }
  110. })();