css-rules-minifier.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2010-2019 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. this.cssRulesMinifier = this.cssRulesMinifier || (() => {
  21. const DEBUG = false;
  22. return {
  23. process: (stylesheets, styles, mediaAllInfo) => {
  24. const stats = { processed: 0, discarded: 0 };
  25. let sheetIndex = 0;
  26. stylesheets.forEach(stylesheetInfo => {
  27. let mediaInfo;
  28. if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != "all") {
  29. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + stylesheetInfo.mediaText);
  30. } else {
  31. mediaInfo = mediaAllInfo;
  32. }
  33. const cssRules = stylesheetInfo.stylesheet.children;
  34. if (cssRules) {
  35. stats.processed += cssRules.getSize();
  36. stats.discarded += cssRules.getSize();
  37. processRules(cssRules, sheetIndex, mediaInfo);
  38. stats.discarded -= cssRules.getSize();
  39. }
  40. sheetIndex++;
  41. });
  42. let startTime;
  43. if (DEBUG) {
  44. startTime = Date.now();
  45. log(" -- STARTED processStyleAttribute");
  46. }
  47. styles.forEach(style => processStyleAttribute(style, mediaAllInfo));
  48. if (DEBUG) {
  49. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  50. }
  51. return stats;
  52. }
  53. };
  54. function processRules(cssRules, sheetIndex, mediaInfo) {
  55. const cssTree = this.cssRulesMinifier.cssTree;
  56. let mediaRuleIndex = 0, startTime;
  57. if (DEBUG && cssRules.getSize() > 1) {
  58. startTime = Date.now();
  59. log(" -- STARTED processRules", "rules.length =", cssRules.getSize());
  60. }
  61. const removedCssRules = [];
  62. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  63. const ruleData = cssRule.data;
  64. if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
  65. if (ruleData.type == "Atrule" && ruleData.name == "media") {
  66. const mediaText = cssTree.generate(ruleData.prelude);
  67. processRules(ruleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + mediaText));
  68. if (!ruleData.prelude.children.getSize() || !ruleData.block.children.getSize()) {
  69. removedCssRules.push(cssRule);
  70. }
  71. mediaRuleIndex++;
  72. } else if (ruleData.type == "Rule") {
  73. const ruleInfo = mediaInfo.rules.get(ruleData);
  74. const pseudoSelectors = mediaInfo.pseudoRules.get(ruleData);
  75. if (!ruleInfo && !pseudoSelectors) {
  76. removedCssRules.push(cssRule);
  77. } else if (ruleInfo) {
  78. processRuleInfo(ruleData, ruleInfo, pseudoSelectors);
  79. if (!ruleData.prelude.children.getSize() || !ruleData.block.children.getSize()) {
  80. removedCssRules.push(cssRule);
  81. }
  82. }
  83. }
  84. } else {
  85. if (!ruleData || ruleData.type == "Raw" || (ruleData.type == "Rule" && (!ruleData.prelude || ruleData.prelude.type == "Raw"))) {
  86. removedCssRules.push(cssRule);
  87. }
  88. }
  89. }
  90. removedCssRules.forEach(cssRule => cssRules.remove(cssRule));
  91. if (DEBUG && cssRules.getSize() > 1) {
  92. log(" -- ENDED processRules delay =", Date.now() - startTime);
  93. }
  94. }
  95. function processRuleInfo(ruleData, ruleInfo, pseudoSelectors) {
  96. const cssTree = this.cssRulesMinifier.cssTree;
  97. const removedDeclarations = [];
  98. const removedSelectors = [];
  99. let pseudoSelectorFound;
  100. for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
  101. const selectorText = cssTree.generate(selector.data);
  102. if (pseudoSelectors && pseudoSelectors.has(selectorText)) {
  103. pseudoSelectorFound = true;
  104. }
  105. if (!ruleInfo.matchedSelectors.has(selectorText) && (!pseudoSelectors || !pseudoSelectors.has(selectorText))) {
  106. removedSelectors.push(selector);
  107. }
  108. }
  109. if (!pseudoSelectorFound) {
  110. for (let declaration = ruleData.block.children.tail; declaration; declaration = declaration.prev) {
  111. if (!ruleInfo.declarations.has(declaration.data)) {
  112. removedDeclarations.push(declaration);
  113. }
  114. }
  115. }
  116. removedDeclarations.forEach(declaration => ruleData.block.children.remove(declaration));
  117. removedSelectors.forEach(selector => ruleData.prelude.children.remove(selector));
  118. }
  119. function processStyleAttribute(styleData, mediaAllInfo) {
  120. const removedDeclarations = [];
  121. const styleInfo = mediaAllInfo.matchedStyles.get(styleData);
  122. if (styleInfo) {
  123. let propertyFound;
  124. for (let declaration = styleData.children.head; declaration && !propertyFound; declaration = declaration.next) {
  125. if (!styleInfo.declarations.has(declaration.data)) {
  126. removedDeclarations.push(declaration);
  127. }
  128. }
  129. removedDeclarations.forEach(declaration => styleData.children.remove(declaration));
  130. }
  131. }
  132. function log(...args) {
  133. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  134. }
  135. })();