css-rules-minifier.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 cssTree */
  21. this.cssRulesMinifier = this.cssRulesMinifier || (() => {
  22. const DEBUG = false;
  23. return {
  24. process: (stylesheets, styles, mediaAllInfo) => {
  25. const stats = { processed: 0, discarded: 0 };
  26. let sheetIndex = 0;
  27. stylesheets.forEach(stylesheetInfo => {
  28. let mediaInfo;
  29. if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != "all") {
  30. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + stylesheetInfo.mediaText);
  31. } else {
  32. mediaInfo = mediaAllInfo;
  33. }
  34. const cssRules = stylesheetInfo.stylesheet.children;
  35. if (cssRules) {
  36. stats.processed += cssRules.getSize();
  37. stats.discarded += cssRules.getSize();
  38. processRules(cssRules, sheetIndex, mediaInfo);
  39. stats.discarded -= cssRules.getSize();
  40. }
  41. sheetIndex++;
  42. });
  43. let startTime;
  44. if (DEBUG) {
  45. startTime = Date.now();
  46. log(" -- STARTED processStyleAttribute");
  47. }
  48. styles.forEach(style => processStyleAttribute(style, mediaAllInfo));
  49. if (DEBUG) {
  50. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  51. }
  52. return stats;
  53. }
  54. };
  55. function processRules(cssRules, sheetIndex, mediaInfo) {
  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 removedDeclarations = [];
  97. const removedSelectors = [];
  98. let pseudoSelectorFound;
  99. for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
  100. const selectorText = cssTree.generate(selector.data);
  101. if (pseudoSelectors && pseudoSelectors.has(selectorText)) {
  102. pseudoSelectorFound = true;
  103. }
  104. if (!ruleInfo.matchedSelectors.has(selectorText) && (!pseudoSelectors || !pseudoSelectors.has(selectorText))) {
  105. removedSelectors.push(selector);
  106. }
  107. }
  108. if (!pseudoSelectorFound) {
  109. for (let declaration = ruleData.block.children.tail; declaration; declaration = declaration.prev) {
  110. if (!ruleInfo.declarations.has(declaration.data)) {
  111. removedDeclarations.push(declaration);
  112. }
  113. }
  114. }
  115. removedDeclarations.forEach(declaration => ruleData.block.children.remove(declaration));
  116. removedSelectors.forEach(selector => ruleData.prelude.children.remove(selector));
  117. }
  118. function processStyleAttribute(styleData, mediaAllInfo) {
  119. const removedDeclarations = [];
  120. const styleInfo = mediaAllInfo.matchedStyles.get(styleData);
  121. if (styleInfo) {
  122. let propertyFound;
  123. for (let declaration = styleData.children.head; declaration && !propertyFound; declaration = declaration.next) {
  124. if (!styleInfo.declarations.has(declaration.data)) {
  125. removedDeclarations.push(declaration);
  126. }
  127. }
  128. removedDeclarations.forEach(declaration => styleData.children.remove(declaration));
  129. }
  130. }
  131. function log(...args) {
  132. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  133. }
  134. })();