background.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var List = require('css-tree').List;
  2. module.exports = function compressBackground(node) {
  3. function lastType() {
  4. if (buffer.length) {
  5. return buffer[buffer.length - 1].type;
  6. }
  7. }
  8. function flush() {
  9. if (lastType() === 'WhiteSpace') {
  10. buffer.pop();
  11. }
  12. if (!buffer.length) {
  13. buffer.unshift(
  14. {
  15. type: 'Number',
  16. loc: null,
  17. value: '0'
  18. },
  19. {
  20. type: 'WhiteSpace',
  21. value: ' '
  22. },
  23. {
  24. type: 'Number',
  25. loc: null,
  26. value: '0'
  27. }
  28. );
  29. }
  30. newValue.push.apply(newValue, buffer);
  31. buffer = [];
  32. }
  33. var newValue = [];
  34. var buffer = [];
  35. node.children.each(function(node) {
  36. if (node.type === 'Operator' && node.value === ',') {
  37. flush();
  38. newValue.push(node);
  39. return;
  40. }
  41. // remove defaults
  42. if (node.type === 'Identifier') {
  43. if (node.name === 'transparent' ||
  44. node.name === 'none' ||
  45. node.name === 'repeat' ||
  46. node.name === 'scroll') {
  47. return;
  48. }
  49. }
  50. // don't add redundant spaces
  51. if (node.type === 'WhiteSpace' && (!buffer.length || lastType() === 'WhiteSpace')) {
  52. return;
  53. }
  54. buffer.push(node);
  55. });
  56. flush();
  57. node.children = new List().fromArray(newValue);
  58. };