Atrule.js 1.9 KB

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