rules-minifier.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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, cssWhat */
  21. this.rulesMinifier = this.rulesMinifier || (() => {
  22. const SEPARATOR_TYPES = ["descendant", "child", "sibling", "adjacent"];
  23. const REMOVED_PSEUDO_CLASSES = ["focus", "focus-within", "hover", "link", "visited", "active"];
  24. const REMOVED_PSEUDO_ELEMENTS = ["after", "before", "first-line", "first-letter"];
  25. return {
  26. process: doc => {
  27. const selectorsData = new Set();
  28. const stats = {
  29. processed: 0,
  30. discarded: 0
  31. };
  32. doc.querySelectorAll("style").forEach(style => {
  33. if (style.sheet) {
  34. const processed = style.sheet.cssRules.length;
  35. stats.processed += processed;
  36. style.textContent = processRules(doc, style.sheet.cssRules, selectorsData);
  37. stats.discarded += processed - style.sheet.cssRules.length;
  38. }
  39. });
  40. return stats;
  41. }
  42. };
  43. function processRules(doc, rules, selectorsData) {
  44. let stylesheetContent = "";
  45. if (rules) {
  46. Array.from(rules).forEach(rule => {
  47. if (rule.type == CSSRule.MEDIA_RULE) {
  48. stylesheetContent += "@media " + Array.prototype.join.call(rule.media, ",") + " {";
  49. stylesheetContent += processRules(doc, rule.cssRules, selectorsData);
  50. stylesheetContent += "}";
  51. } else if (rule.type == CSSRule.STYLE_RULE) {
  52. const selector = getFilteredSelector(rule.selectorText);
  53. if (selector) {
  54. try {
  55. if (selectorsData.has(selector) || doc.querySelector(selector)) {
  56. stylesheetContent += rule.cssText;
  57. selectorsData.add(selector);
  58. }
  59. } catch (error) {
  60. stylesheetContent += rule.cssText;
  61. }
  62. }
  63. } else {
  64. stylesheetContent += rule.cssText;
  65. }
  66. });
  67. }
  68. return stylesheetContent;
  69. }
  70. function getFilteredSelector(selector) {
  71. const selectors = cssWhat.parse(selector);
  72. return cssWhat.stringify(selectors.map(selector => filterPseudoClasses(selector)));
  73. function filterPseudoClasses(selector) {
  74. const tokens = selector.filter(token => {
  75. if (token.data) {
  76. if (Array.isArray(token.data)) {
  77. token.data = token.data.map(selector => filterPseudoClasses(selector));
  78. }
  79. }
  80. const test = ((token.type != "pseudo" || !REMOVED_PSEUDO_CLASSES.includes(token.name))
  81. && (token.type != "pseudo-element" || !REMOVED_PSEUDO_ELEMENTS.includes(token.name)));
  82. return test;
  83. });
  84. let insertedTokens = 0;
  85. tokens.forEach((token, index) => {
  86. if (SEPARATOR_TYPES.includes(token.type)) {
  87. if (!tokens[index - 1] || SEPARATOR_TYPES.includes(tokens[index - 1].type)) {
  88. tokens.splice(index + insertedTokens, 0, { type: "universal" });
  89. insertedTokens++;
  90. }
  91. }
  92. });
  93. if (!tokens.length || SEPARATOR_TYPES.includes(tokens[tokens.length - 1].type)) {
  94. tokens.push({ type: "universal" });
  95. }
  96. return tokens;
  97. }
  98. }
  99. })();