-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
38 lines (32 loc) · 972 Bytes
/
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
/**
* rework-rem-fallback
*
* Copyright (c) 2013 Chris Talkington, contributors.
* Licensed under the MIT license.
* https://github.com/ctalkington/rework-rem-fallback/blob/master/LICENSE-MIT
*/
var visit = require('rework-visit');
function remToPx(val, base) {
base = typeof base === 'number' ? base : 16;
return val.replace(/([+-]?\d*\.?\d+)\s*rem/g, function(a, b, c) {
var rem = parseFloat(b);
var px = Math.round(rem * base);
return px + 'px';
});
}
module.exports = function(base) {
return function(style, rework) {
visit(style, function(declarations, node) {
for (var i = 0; i < declarations.length; ++i) {
var decl = declarations[i];
if ('comment' === decl.type) continue;
if (!~decl.value.indexOf('rem')) continue;
declarations.splice(i++, 0, {
type: 'declaration',
property: decl.property,
value: remToPx(decl.value, base)
});
}
});
};
};