1
0

css-rules-minifier.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 cssRuleData = cssRule.data;
  64. if (cssRuleData.block && cssRuleData.block.children && cssRuleData.prelude && cssRuleData.prelude.children) {
  65. if (cssRuleData.type == "Atrule" && cssRuleData.name == "media") {
  66. const mediaText = cssTree.generate(cssRuleData.prelude);
  67. processRules(cssRuleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + mediaText));
  68. if (!cssRuleData.prelude.children.getSize() || !cssRuleData.block.children.getSize()) {
  69. removedCssRules.push(cssRule);
  70. }
  71. mediaRuleIndex++;
  72. } else if (cssRuleData.type == "Rule") {
  73. const ruleInfo = mediaInfo.rules.get(cssRuleData);
  74. const pseudoSelectors = mediaInfo.pseudoRules.get(cssRuleData);
  75. if (!ruleInfo && !pseudoSelectors) {
  76. removedCssRules.push(cssRule);
  77. } else if (ruleInfo) {
  78. processRuleInfo(cssRuleData, ruleInfo, pseudoSelectors);
  79. if (!cssRuleData.prelude.children.getSize() || !cssRuleData.block.children.getSize()) {
  80. removedCssRules.push(cssRule);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. removedCssRules.forEach(cssRule => cssRules.remove(cssRule));
  87. if (DEBUG && cssRules.getSize() > 1) {
  88. log(" -- ENDED processRules delay =", Date.now() - startTime);
  89. }
  90. }
  91. function processRuleInfo(cssRule, ruleInfo, pseudoSelectors) {
  92. const removedDeclarations = [];
  93. const removedSelectors = [];
  94. for (let declaration = cssRule.block.children.tail; declaration; declaration = declaration.prev) {
  95. if (!ruleInfo.declarations.has(declaration.data)) {
  96. removedDeclarations.push(declaration);
  97. }
  98. }
  99. for (let selector = cssRule.prelude.children.head; selector; selector = selector.next) {
  100. const selectorText = cssTree.generate(selector.data);
  101. if (!ruleInfo.matchedSelectors.has(selectorText) && (!pseudoSelectors || !pseudoSelectors.has(selectorText))) {
  102. removedSelectors.push(selector);
  103. }
  104. }
  105. removedDeclarations.forEach(declaration => cssRule.block.children.remove(declaration));
  106. removedSelectors.forEach(selector => cssRule.prelude.children.remove(selector));
  107. }
  108. function processStyleAttribute(cssStyle, mediaAllInfo) {
  109. const removedDeclarations = [];
  110. const styleInfo = mediaAllInfo.matchedStyles.get(cssStyle);
  111. if (styleInfo) {
  112. let propertyFound;
  113. for (let declaration = cssStyle.children.head; declaration && !propertyFound; declaration = declaration.next) {
  114. if (!styleInfo.declarations.has(declaration.data)) {
  115. removedDeclarations.push(declaration);
  116. }
  117. }
  118. removedDeclarations.forEach(declaration => cssStyle.children.remove(declaration));
  119. }
  120. }
  121. function log(...args) {
  122. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  123. }
  124. })();