You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB

4 years ago
  1. var packNumber = require('./Number').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, item);
  24. node.value = value;
  25. if (value === '0' && this.declaration !== null && this.atrulePrelude === null) {
  26. var unit = node.unit.toLowerCase();
  27. // only length values can be compressed
  28. if (!LENGTH_UNIT.hasOwnProperty(unit)) {
  29. return;
  30. }
  31. // issue #362: shouldn't remove unit in -ms-flex since it breaks flex in IE10/11
  32. // issue #200: shouldn't remove unit in flex since it breaks flex in IE10/11
  33. if (this.declaration.property === '-ms-flex' ||
  34. this.declaration.property === 'flex') {
  35. return;
  36. }
  37. // issue #222: don't remove units inside calc
  38. if (this.function && this.function.name === 'calc') {
  39. return;
  40. }
  41. item.data = {
  42. type: 'Number',
  43. loc: node.loc,
  44. value: value
  45. };
  46. }
  47. };