Atrule.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. module.exports = function cleanAtrule(node, item, list) {
  2. if (node.block) {
  3. // otherwise removed at-rule don't prevent @import for removal
  4. this.root.firstAtrulesAllowed = false;
  5. if (node.block.type === 'Block' && node.block.declarations.isEmpty()) {
  6. list.remove(item);
  7. return;
  8. }
  9. if (node.block.type === 'StyleSheet' && node.block.rules.isEmpty()) {
  10. list.remove(item);
  11. return;
  12. }
  13. }
  14. switch (node.name) {
  15. case 'charset':
  16. if (node.expression.sequence.isEmpty()) {
  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.root.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. }
  45. };