css-rules-minifier.js 5.6 KB

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