css-rules-minifier.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. return {
  27. process: doc => {
  28. const rulesMatcher = RulesMatcher.create(doc);
  29. const mediaAllInfo = rulesMatcher.getAllMatchedRules();
  30. const stats = { processed: 0, discarded: 0 };
  31. doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
  32. if (styleElement.sheet) {
  33. let mediaInfo;
  34. if (styleElement.media && styleElement.media != "all") {
  35. mediaInfo = mediaAllInfo.medias.get(styleIndex + "-" + styleElement.media);
  36. } else {
  37. mediaInfo = mediaAllInfo;
  38. }
  39. const cssRules = styleElement.sheet.cssRules;
  40. stats.processed += cssRules.length;
  41. stats.discarded += cssRules.length;
  42. styleElement.textContent = processRules(doc, cssRules, mediaInfo);
  43. stats.discarded -= cssRules.length;
  44. }
  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. return stats;
  55. }
  56. };
  57. function processRules(doc, cssRules, mediaInfo) {
  58. let sheetContent = "";
  59. Array.from(cssRules).forEach(cssRule => {
  60. if (cssRule.type == CSSRule.MEDIA_RULE) {
  61. sheetContent += "@media " + Array.from(cssRule.media).join(",") + "{";
  62. sheetContent += processRules(doc, cssRule.cssRules, mediaInfo.medias.get(cssRule.media));
  63. sheetContent += "}";
  64. } else if (cssRule.type == CSSRule.STYLE_RULE) {
  65. const ruleInfo = mediaInfo.rules.get(cssRule);
  66. if (ruleInfo || testIgnoredSelector(cssRule.selectorText)) {
  67. sheetContent += processRuleInfo(cssRule, ruleInfo);
  68. }
  69. } else {
  70. sheetContent += cssRule.cssText;
  71. }
  72. });
  73. return sheetContent;
  74. }
  75. function processRuleInfo(cssRule, ruleInfo) {
  76. let selectorText = "", styleCssText = "";
  77. if (ruleInfo) {
  78. const stylesInfo = parseCss.parseAListOfDeclarations(cssRule.style.cssText);
  79. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  80. const style = stylesInfo[styleIndex];
  81. if (ruleInfo.style.get(style.name)) {
  82. if (styleCssText) {
  83. styleCssText += ";";
  84. }
  85. const priority = cssRule.style.getPropertyPriority(style.name);
  86. styleCssText += style.name + ":" + cssRule.style.getPropertyValue(style.name) + (priority && ("!" + priority));
  87. }
  88. }
  89. if (ruleInfo.matchedSelectors.size < ruleInfo.selectorsText.length) {
  90. for (let selectorTextIndex = 0; selectorTextIndex < ruleInfo.selectorsText.length; selectorTextIndex++) {
  91. const ruleSelectorText = ruleInfo.selectorsText[selectorTextIndex];
  92. if (ruleInfo.matchedSelectors.has(ruleSelectorText) || testIgnoredSelector(ruleSelectorText)) {
  93. if (selectorText) {
  94. selectorText += ",";
  95. }
  96. selectorText += ruleSelectorText;
  97. }
  98. }
  99. }
  100. }
  101. return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
  102. }
  103. function processStyleAttribute(cssStyle, mediaInfo) {
  104. let styleCssText = "";
  105. const styleInfo = mediaInfo.styles.get(cssStyle);
  106. if (styleInfo) {
  107. const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
  108. for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
  109. const style = stylesInfo[styleIndex];
  110. if (styleInfo.style.get(style.name)) {
  111. if (styleCssText) {
  112. styleCssText += ";";
  113. }
  114. const priority = cssStyle.getPropertyPriority(style.name);
  115. styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
  116. }
  117. }
  118. }
  119. return (styleCssText || cssStyle.cssText);
  120. }
  121. function testIgnoredSelector(selectorText) {
  122. let indexSelector = 0, found;
  123. selectorText = selectorText.toLowerCase();
  124. while (indexSelector < IGNORED_SELECTORS.length && !found) {
  125. found = selectorText.includes(IGNORED_SELECTORS[indexSelector]);
  126. if (!found) {
  127. indexSelector++;
  128. }
  129. }
  130. return found;
  131. }
  132. })();