Dimension.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var packNumber = require('./Number.js').pack;
  2. var LENGTH_UNIT = {
  3. // absolute length units
  4. 'px': true,
  5. 'mm': true,
  6. 'cm': true,
  7. 'in': true,
  8. 'pt': true,
  9. 'pc': true,
  10. // relative length units
  11. 'em': true,
  12. 'ex': true,
  13. 'ch': true,
  14. 'rem': true,
  15. // viewport-percentage lengths
  16. 'vh': true,
  17. 'vw': true,
  18. 'vmin': true,
  19. 'vmax': true,
  20. 'vm': true
  21. };
  22. module.exports = function compressDimension(node, item) {
  23. var value = packNumber(node.value);
  24. node.value = value;
  25. if (value === '0' && this.declaration) {
  26. var unit = node.unit.toLowerCase();
  27. // only length values can be compressed
  28. if (!LENGTH_UNIT.hasOwnProperty(unit)) {
  29. return;
  30. }
  31. // issue #200: don't remove units in flex property as it could change value meaning
  32. if (this.declaration.property.name === 'flex') {
  33. return;
  34. }
  35. // issue #222: don't remove units inside calc
  36. if (this['function'] && this['function'].name === 'calc') {
  37. return;
  38. }
  39. item.data = {
  40. type: 'Number',
  41. info: node.info,
  42. value: value
  43. };
  44. }
  45. };