1
0

css-rules-minifier.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file 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 GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. this.singlefile.lib.modules.cssRulesMinifier = this.singlefile.lib.modules.cssRulesMinifier || (() => {
  24. const singlefile = this.singlefile;
  25. const DEBUG = false;
  26. return {
  27. process: (stylesheets, styles, mediaAllInfo) => {
  28. const stats = { processed: 0, discarded: 0 };
  29. let sheetIndex = 0;
  30. stylesheets.forEach(stylesheetInfo => {
  31. if (!stylesheetInfo.scoped) {
  32. const cssRules = stylesheetInfo.stylesheet.children;
  33. if (cssRules) {
  34. stats.processed += cssRules.getSize();
  35. stats.discarded += cssRules.getSize();
  36. let mediaInfo;
  37. if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != "all") {
  38. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + stylesheetInfo.mediaText);
  39. } else {
  40. mediaInfo = mediaAllInfo;
  41. }
  42. processRules(cssRules, sheetIndex, mediaInfo);
  43. stats.discarded -= cssRules.getSize();
  44. }
  45. }
  46. sheetIndex++;
  47. });
  48. let startTime;
  49. if (DEBUG) {
  50. startTime = Date.now();
  51. log(" -- STARTED processStyleAttribute");
  52. }
  53. styles.forEach(style => processStyleAttribute(style, mediaAllInfo));
  54. if (DEBUG) {
  55. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  56. }
  57. return stats;
  58. }
  59. };
  60. function processRules(cssRules, sheetIndex, mediaInfo) {
  61. const cssTree = singlefile.lib.vendor.cssTree;
  62. let mediaRuleIndex = 0, startTime;
  63. if (DEBUG && cssRules.getSize() > 1) {
  64. startTime = Date.now();
  65. log(" -- STARTED processRules", "rules.length =", cssRules.getSize());
  66. }
  67. const removedCssRules = [];
  68. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  69. const ruleData = cssRule.data;
  70. if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
  71. if (ruleData.type == "Atrule" && ruleData.name == "media") {
  72. const mediaText = cssTree.generate(ruleData.prelude);
  73. processRules(ruleData.block.children, sheetIndex, mediaInfo.medias.get("rule-" + sheetIndex + "-" + mediaRuleIndex + "-" + mediaText));
  74. if (!ruleData.prelude.children.getSize() || !ruleData.block.children.getSize()) {
  75. removedCssRules.push(cssRule);
  76. }
  77. mediaRuleIndex++;
  78. } else if (ruleData.type == "Rule") {
  79. const ruleInfo = mediaInfo.rules.get(ruleData);
  80. const pseudoSelectors = mediaInfo.pseudoRules.get(ruleData);
  81. if (!ruleInfo && !pseudoSelectors) {
  82. removedCssRules.push(cssRule);
  83. } else if (ruleInfo) {
  84. processRuleInfo(ruleData, ruleInfo, pseudoSelectors);
  85. if (!ruleData.prelude.children.getSize() || !ruleData.block.children.getSize()) {
  86. removedCssRules.push(cssRule);
  87. }
  88. }
  89. }
  90. } else {
  91. if (!ruleData || ruleData.type == "Raw" || (ruleData.type == "Rule" && (!ruleData.prelude || ruleData.prelude.type == "Raw"))) {
  92. removedCssRules.push(cssRule);
  93. }
  94. }
  95. }
  96. removedCssRules.forEach(cssRule => cssRules.remove(cssRule));
  97. if (DEBUG && cssRules.getSize() > 1) {
  98. log(" -- ENDED processRules delay =", Date.now() - startTime);
  99. }
  100. }
  101. function processRuleInfo(ruleData, ruleInfo, pseudoSelectors) {
  102. const cssTree = singlefile.lib.vendor.cssTree;
  103. const removedDeclarations = [];
  104. const removedSelectors = [];
  105. let pseudoSelectorFound;
  106. for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
  107. const selectorText = cssTree.generate(selector.data);
  108. if (pseudoSelectors && pseudoSelectors.has(selectorText)) {
  109. pseudoSelectorFound = true;
  110. }
  111. if (!ruleInfo.matchedSelectors.has(selectorText) && (!pseudoSelectors || !pseudoSelectors.has(selectorText))) {
  112. removedSelectors.push(selector);
  113. }
  114. }
  115. if (!pseudoSelectorFound) {
  116. for (let declaration = ruleData.block.children.tail; declaration; declaration = declaration.prev) {
  117. if (!ruleInfo.declarations.has(declaration.data)) {
  118. removedDeclarations.push(declaration);
  119. }
  120. }
  121. }
  122. removedDeclarations.forEach(declaration => ruleData.block.children.remove(declaration));
  123. removedSelectors.forEach(selector => ruleData.prelude.children.remove(selector));
  124. }
  125. function processStyleAttribute(styleData, mediaAllInfo) {
  126. const removedDeclarations = [];
  127. const styleInfo = mediaAllInfo.matchedStyles.get(styleData);
  128. if (styleInfo) {
  129. let propertyFound;
  130. for (let declaration = styleData.children.head; declaration && !propertyFound; declaration = declaration.next) {
  131. if (!styleInfo.declarations.has(declaration.data)) {
  132. removedDeclarations.push(declaration);
  133. }
  134. }
  135. removedDeclarations.forEach(declaration => styleData.children.remove(declaration));
  136. }
  137. }
  138. function log(...args) {
  139. console.log("S-File <css-min>", ...args); // eslint-disable-line no-console
  140. }
  141. })();