Dimension.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var packNumber = require('./Number').pack;
  2. var MATH_FUNCTIONS = {
  3. 'calc': true,
  4. 'min': true,
  5. 'max': true,
  6. 'clamp': true
  7. };
  8. var LENGTH_UNIT = {
  9. // absolute length units
  10. 'px': true,
  11. 'mm': true,
  12. 'cm': true,
  13. 'in': true,
  14. 'pt': true,
  15. 'pc': true,
  16. // relative length units
  17. 'em': true,
  18. 'ex': true,
  19. 'ch': true,
  20. 'rem': true,
  21. // viewport-percentage lengths
  22. 'vh': true,
  23. 'vw': true,
  24. 'vmin': true,
  25. 'vmax': true,
  26. 'vm': true
  27. };
  28. module.exports = function compressDimension(node, item) {
  29. var value = packNumber(node.value, item);
  30. node.value = value;
  31. if (value === '0' && this.declaration !== null && this.atrulePrelude === null) {
  32. var unit = node.unit.toLowerCase();
  33. // only length values can be compressed
  34. if (!LENGTH_UNIT.hasOwnProperty(unit)) {
  35. return;
  36. }
  37. // issue #362: shouldn't remove unit in -ms-flex since it breaks flex in IE10/11
  38. // issue #200: shouldn't remove unit in flex since it breaks flex in IE10/11
  39. if (this.declaration.property === '-ms-flex' ||
  40. this.declaration.property === 'flex') {
  41. return;
  42. }
  43. // issue #222: don't remove units inside calc
  44. if (this.function && MATH_FUNCTIONS.hasOwnProperty(this.function.name)) {
  45. return;
  46. }
  47. item.data = {
  48. type: 'Number',
  49. loc: node.loc,
  50. value: value
  51. };
  52. }
  53. };