css-medias-alt-minifier.js 3.4 KB

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