-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (33 loc) · 1.2 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
var postcss = require('postcss');
function getAttrs(axis, name, subname) {
var attrs = {
x: ['left', 'right'],
y: ['top', 'bottom']
};
return attrs[axis].map(function (side) {
var parts = [name, side];
subname && parts.push(subname);
return parts.join('-');
});
}
module.exports = postcss.plugin('postcss-axis', function (opts) {
opts = opts || {};
return function (css) {
var filter = /^(border|margin|padding)-(x|y)(?:-(color|style|width))?$/;
css.walkDecls(filter, function (decl) {
var match = decl.prop.match(filter);
var attrs = getAttrs(match[2], match[1], match[3]);
var values;
if (match[1] === 'border' && !match[3]) {
values = [decl.value];
} else {
values = postcss.list.space(decl.value);
}
if ( values.length === 1 ) values[1] = values[0];
if (opts.trbl && match[2] === 'x') values = [values[1], values[0]];
decl.cloneBefore({ prop: attrs[0], value: values[0] });
decl.cloneBefore({ prop: attrs[1], value: values[1] });
decl.remove();
});
};
});