1
0

css-rules-minifier.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. import * as cssTree from "./../vendor/css-tree.js";
  24. const DEBUG = false;
  25. export {
  26. process
  27. };
  28. function process(stylesheets, styles, mediaAllInfo) {
  29. const stats = { processed: 0, discarded: 0 };
  30. let sheetIndex = 0;
  31. stylesheets.forEach(stylesheetInfo => {
  32. if (!stylesheetInfo.scoped) {
  33. const cssRules = stylesheetInfo.stylesheet.children;
  34. if (cssRules) {
  35. stats.processed += cssRules.getSize();
  36. stats.discarded += cssRules.getSize();
  37. let mediaInfo;
  38. if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != "all") {
  39. mediaInfo = mediaAllInfo.medias.get("style-" + sheetIndex + "-" + stylesheetInfo.mediaText);
  40. } else {
  41. mediaInfo = mediaAllInfo;
  42. }
  43. processRules(cssRules, sheetIndex, mediaInfo);
  44. stats.discarded -= cssRules.getSize();
  45. }
  46. }
  47. sheetIndex++;
  48. });
  49. let startTime;
  50. if (DEBUG) {
  51. startTime = Date.now();
  52. log(" -- STARTED processStyleAttribute");
  53. }
  54. styles.forEach(style => processStyleAttribute(style, mediaAllInfo));
  55. if (DEBUG) {
  56. log(" -- ENDED processStyleAttribute delay =", Date.now() - startTime);
  57. }
  58. return stats;
  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. }