css-rules-minifier.js 5.3 KB

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