Tim van der Lippe | 706ec96 | 2021-06-04 13:24:42 +0100 | [diff] [blame^] | 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 | |
| 17 | // relative length units |
| 18 | 'em': true, |
| 19 | 'ex': true, |
| 20 | 'ch': true, |
| 21 | 'rem': true, |
| 22 | |
| 23 | // viewport-percentage lengths |
| 24 | 'vh': true, |
| 25 | 'vw': true, |
| 26 | 'vmin': true, |
| 27 | 'vmax': true, |
| 28 | 'vm': true |
| 29 | }; |
| 30 | |
| 31 | module.exports = function compressDimension(node, item) { |
| 32 | var value = packNumber(node.value, item); |
| 33 | |
| 34 | node.value = value; |
| 35 | |
| 36 | if (value === '0' && this.declaration !== null && this.atrulePrelude === null) { |
| 37 | var unit = node.unit.toLowerCase(); |
| 38 | |
| 39 | // only length values can be compressed |
| 40 | if (!LENGTH_UNIT.hasOwnProperty(unit)) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // issue #362: shouldn't remove unit in -ms-flex since it breaks flex in IE10/11 |
| 45 | // issue #200: shouldn't remove unit in flex since it breaks flex in IE10/11 |
| 46 | if (this.declaration.property === '-ms-flex' || |
| 47 | this.declaration.property === 'flex') { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | // issue #222: don't remove units inside calc |
| 52 | if (this.function && MATH_FUNCTIONS.hasOwnProperty(this.function.name)) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | item.data = { |
| 57 | type: 'Number', |
| 58 | loc: node.loc, |
| 59 | value: value |
| 60 | }; |
| 61 | } |
| 62 | }; |