Atrule.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. var resolveKeyword = require('css-tree').keyword;
  2. var { hasNoChildren } = require('./utils');
  3. module.exports = function cleanAtrule(node, item, list) {
  4. if (node.block) {
  5. // otherwise removed at-rule don't prevent @import for removal
  6. if (this.stylesheet !== null) {
  7. this.stylesheet.firstAtrulesAllowed = false;
  8. }
  9. if (hasNoChildren(node.block)) {
  10. list.remove(item);
  11. return;
  12. }
  13. }
  14. switch (node.name) {
  15. case 'charset':
  16. if (hasNoChildren(node.prelude)) {
  17. list.remove(item);
  18. return;
  19. }
  20. // if there is any rule before @charset -> remove it
  21. if (item.prev) {
  22. list.remove(item);
  23. return;
  24. }
  25. break;
  26. case 'import':
  27. if (this.stylesheet === null || !this.stylesheet.firstAtrulesAllowed) {
  28. list.remove(item);
  29. return;
  30. }
  31. // if there are some rules that not an @import or @charset before @import
  32. // remove it
  33. list.prevUntil(item.prev, function(rule) {
  34. if (rule.type === 'Atrule') {
  35. if (rule.name === 'import' || rule.name === 'charset') {
  36. return;
  37. }
  38. }
  39. this.root.firstAtrulesAllowed = false;
  40. list.remove(item);
  41. return true;
  42. }, this);
  43. break;
  44. default:
  45. var name = resolveKeyword(node.name).basename;
  46. if (name === 'keyframes' ||
  47. name === 'media' ||
  48. name === 'supports') {
  49. // drop at-rule with no prelude
  50. if (hasNoChildren(node.prelude) || hasNoChildren(node.block)) {
  51. list.remove(item);
  52. }
  53. }
  54. }
  55. };