-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
83 lines (60 loc) · 2.33 KB
/
index.js
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
var lodash = require('lodash');
var parseAttrs = require('posthtml-attrs-parser');
module.exports = function (options) {
var pxRegex = /(\d*\.?\d+)px/ig;
options = lodash.extend({
rootValue: 16, // root font-size
unitPrecision: 5, // numbers after `.`
minPixelValue: 0 // set it 2 if you want to ignore value like 1px & -1px
}, options);
function createPxReplace(rootValue, unitPrecision, minPixelValue) {
return function (m, $1) {
// ignoring `PX` `Px`
if (m.indexOf('px') === -1) {
return m;
}
if (!$1) {
return m;
}
var pixels = parseFloat($1);
if (pixels < minPixelValue) {
return m;
}
return toFixed((pixels / rootValue), unitPrecision) + 'rem';
};
}
function toFixed(number, precision) {
var multiplier = Math.pow(10, precision + 1),
wholeNumber = Math.floor(number * multiplier);
return Math.round(wholeNumber / 10) * 10 / multiplier;
}
return function px2rem(tree) {
var pxReplace = createPxReplace(options.rootValue, options.unitPrecision, options.minPixelValue);
// inline CSS `<style>` in HTML
tree.match({tag: 'style'}, function (node) {
var replacedContent;
replacedContent = node.content.toString().replace(pxRegex, pxReplace);
node.content = replacedContent;
return node;
});
/* inline CSS `style="xxx"` in DOM*/
tree.match({attrs: {style: true}}, function (node) {
var attrs = parseAttrs(node.attrs);
for (var x in attrs['style']) {
if (attrs['style'].hasOwnProperty(x)) {
var styleValue = attrs['style'][x];
// e.g. style="width=10px; width=20px;"
if (typeof styleValue == 'object')
styleValue = styleValue[styleValue.length - 1];
var newStyleValue;
newStyleValue = styleValue.toString().replace(pxRegex, pxReplace);
attrs['style'][x] = newStyleValue;
}
}
node.attrs = attrs.compose();
return node;
});
return tree;
};
};