css-medias-alt-minifier.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.mediasAltMinifier = this.singlefile.lib.modules.mediasAltMinifier || (() => {
  24. const singlefile = this.singlefile;
  25. const MEDIA_ALL = "all";
  26. const MEDIA_SCREEN = "screen";
  27. return {
  28. process
  29. };
  30. function process(stylesheets) {
  31. const stats = { processed: 0, discarded: 0 };
  32. stylesheets.forEach((stylesheetInfo, element) => {
  33. if (matchesMediaType(stylesheetInfo.mediaText || MEDIA_ALL, MEDIA_SCREEN) && stylesheetInfo.stylesheet.children) {
  34. const removedRules = processRules(stylesheetInfo.stylesheet.children, stats);
  35. removedRules.forEach(({ cssRules, cssRule }) => cssRules.remove(cssRule));
  36. } else {
  37. stylesheets.delete(element);
  38. }
  39. });
  40. return stats;
  41. }
  42. function processRules(cssRules, stats, removedRules = []) {
  43. for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
  44. const ruleData = cssRule.data;
  45. if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
  46. stats.processed++;
  47. if (matchesMediaType(singlefile.lib.vendor.cssTree.generate(ruleData.prelude), MEDIA_SCREEN)) {
  48. processRules(ruleData.block.children, stats, removedRules);
  49. } else {
  50. removedRules.push({ cssRules, cssRule });
  51. stats.discarded++;
  52. }
  53. }
  54. }
  55. return removedRules;
  56. }
  57. function matchesMediaType(mediaText, mediaType) {
  58. const foundMediaTypes = singlefile.lib.helper.flatten(singlefile.lib.vendor.mediaQueryParser.parseMediaList(mediaText).map(node => getMediaTypes(node, mediaType)));
  59. return foundMediaTypes.find(mediaTypeInfo => (!mediaTypeInfo.not && (mediaTypeInfo.value == mediaType || mediaTypeInfo.value == MEDIA_ALL))
  60. || (mediaTypeInfo.not && (mediaTypeInfo.value == MEDIA_ALL || mediaTypeInfo.value != mediaType)));
  61. }
  62. function getMediaTypes(parentNode, mediaType, mediaTypes = []) {
  63. parentNode.nodes.map((node, indexNode) => {
  64. if (node.type == "media-query") {
  65. return getMediaTypes(node, mediaType, mediaTypes);
  66. } else {
  67. if (node.type == "media-type") {
  68. const nodeMediaType = { not: Boolean(indexNode && parentNode.nodes[0].type == "keyword" && parentNode.nodes[0].value == "not"), value: node.value };
  69. if (!mediaTypes.find(mediaType => nodeMediaType.not == mediaType.not && nodeMediaType.value == mediaType.value)) {
  70. mediaTypes.push(nodeMediaType);
  71. }
  72. }
  73. }
  74. });
  75. if (!mediaTypes.length) {
  76. mediaTypes.push({ not: false, value: MEDIA_ALL });
  77. }
  78. return mediaTypes;
  79. }
  80. })();