css-rules-minifier.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.media && stylesheetInfo.media != "all") {
  30. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + stylesheetInfo.media);
  31. } else {
  32. mediaInfo = mediaAllInfo;
  33. }
  34. const cssRules = stylesheetInfo.stylesheet.children;
  35. stats.processed += cssRules.getSize();
  36. stats.discarded += cssRules.getSize();
  37. processRules(cssRules, sheetIndex, mediaInfo);
  38. sheetIndex++;
  39. stats.discarded -= stylesheetInfo.stylesheet.children.getSize();
  40. });
  41. let startTime;
  42. if (DEBUG) {
  43. startTime = Date.now();
  44. log(" -- STARTED processStyleAttribute");
  45. }
  46. styles.forEach(style => processStyleAttribute(style, mediaAllInfo));
  47. if (DEBUG) {
  48. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  49. }
  50. return stats;
  51. }
  52. };
  53. function processRules(cssRules, sheetIndex, mediaInfo) {
  54. let mediaRuleIndex = 0, startTime;
  55. if (DEBUG && cssRules.length > 1) {
  56. startTime = Date.now();
  57. log(" -- STARTED processRules", "rules.length =", cssRules.children.toArray().length);
  58. }
  59. const removedCssRules = [];
  60. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  61. const cssRuleData = cssRule.data;
  62. if (cssRuleData.type == "Atrule" && cssRuleData.name == "media") {
  63. const mediaText = cssTree.generate(cssRuleData.prelude);
  64. processRules(cssRuleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + mediaText));
  65. mediaRuleIndex++;
  66. } else if (cssRuleData.type == "Rule") {
  67. const ruleInfo = mediaInfo.rules.get(cssRuleData);
  68. if (!ruleInfo && !mediaInfo.pseudoSelectors.has(cssRuleData)) {
  69. removedCssRules.push(cssRule);
  70. } else if (ruleInfo) {
  71. processRuleInfo(cssRuleData, ruleInfo);
  72. if (!cssRuleData.prelude.children.getSize() || !cssRuleData.block.children.getSize()) {
  73. removedCssRules.push(cssRule);
  74. }
  75. }
  76. }
  77. }
  78. removedCssRules.forEach(cssRule => cssRules.remove(cssRule));
  79. if (DEBUG && cssRules.length > 1) {
  80. log(" -- ENDED processRules delay =", Date.now() - startTime);
  81. }
  82. }
  83. function processRuleInfo(cssRule, ruleInfo) {
  84. const removedDeclarations = [];
  85. const removedSelectors = [];
  86. for (let declaration = cssRule.block.children.head; declaration; declaration = declaration.next) {
  87. if (!ruleInfo.style.get(declaration.data.property)) {
  88. removedDeclarations.push(declaration);
  89. }
  90. }
  91. for (let selector = cssRule.prelude.children.head; selector; selector = selector.next) {
  92. if (!ruleInfo.selectorsText.includes(cssTree.generate(selector.data))) {
  93. removedSelectors.push(selector);
  94. }
  95. }
  96. removedDeclarations.forEach(declaration => cssRule.block.children.remove(declaration));
  97. removedSelectors.forEach(selector => cssRule.prelude.children.remove(selector));
  98. }
  99. function processStyleAttribute(cssStyle, mediaAllInfo) {
  100. const removedDeclarations = [];
  101. const styleInfos = mediaAllInfo.matchedStyles.get(cssStyle);
  102. if (styleInfos) {
  103. let propertyFound;
  104. for (let declaration = cssStyle.children.head; declaration && !propertyFound; declaration = declaration.next) {
  105. if (!styleInfos.style.get(declaration.data.property)) {
  106. removedDeclarations.push(declaration);
  107. }
  108. }
  109. removedDeclarations.forEach(declaration => cssStyle.children.remove(declaration));
  110. }
  111. }
  112. function log(...args) {
  113. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  114. }
  115. })();