unit.js
1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var WHOLE_PIXEL_VALUE = /(?:^|\s|\()(-?\d+)px/;
var plugin = {
level1: {
value: function unit(_name, value, options) {
if (!WHOLE_PIXEL_VALUE.test(value)) {
return value;
}
return value.replace(WHOLE_PIXEL_VALUE, function(match, val) {
var newValue;
var intVal = parseInt(val);
if (intVal === 0) {
return match;
}
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.pt
&& intVal * 3 % 4 === 0) {
newValue = intVal * 3 / 4 + 'pt';
}
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.pc
&& intVal % 16 === 0) {
newValue = intVal / 16 + 'pc';
}
if (options.compatibility.properties.shorterLengthUnits
&& options.compatibility.units.in
&& intVal % 96 === 0) {
newValue = intVal / 96 + 'in';
}
if (newValue) {
newValue = match.substring(0, match.indexOf(val)) + newValue;
}
return newValue && newValue.length < match.length ? newValue : match;
});
}
}
};
module.exports = plugin;