-
Notifications
You must be signed in to change notification settings - Fork 1
/
postcss-nativescript.js
60 lines (55 loc) · 1.92 KB
/
postcss-nativescript.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
var postcss = require('postcss'),
digitRegExp = /^[\d\s]+$/,
digitReplacementRegExp = /(\d+)+/g,
dashCapitalRegExp = /([A-Z]?[a-z\d])([A-Z])/g,
selectorRegExp = /(^\s?|>\s?|\+\s?|~\s?|,\s?|\s+)([A-Z]\w+?\b)/gm,
dashCase = function (str) {
return str.replace(dashCapitalRegExp, '$1-$2').toLowerCase();
},
sizeProps = {
"font-size": true,
fontSize: true,
width: true,
height: true,
padding: true,
margin: true,
"padding-top": true,
"padding-left": true,
"padding-right": true,
"padding-bottom": true,
"margin-top": true,
"margin-left": true,
"margin-right": true,
"margin-bottom": true,
"border-radius": true,
"min-height": true,
"max-height": true,
"min-width": true,
"max-width": true,
"border-width": true,
border: true
};
module.exports = postcss.plugin('postcss-nativescript', function (opts) {
opts = opts || {};
return function (css) {
css.walk(function (node) {
if (node.type === 'rule') {
node.selector = node.selector.replace(selectorRegExp, (match, g1, g2) => {
return g1 + "ns-" + dashCase(g2);
}).replace(/:highlighted|:pressed/gm, ":active").toLowerCase();
}
});
css.walkDecls(function transformDecl(decl) {
if (decl.prop === "horizontal-align" || decl.prop === "vertical-align") {
decl.prop = 'align-self';
switch (decl.value) {
case "left": decl.value = "flex-start"; break;
case "right": decl.value = "flex-end"; break;
}
}
if (decl.prop in sizeProps && digitRegExp.test(decl.value)) {
decl.value = decl.value.replace(digitReplacementRegExp, "$1px");
}
});
};
});