This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
87 lines (72 loc) · 2.31 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
84
85
86
87
import postcss from 'postcss';
export default postcss.plugin('postcss-short-border', opts => {
// options
const prefix = 'prefix' in Object(opts) ? `-${opts.prefix}-` : '';
const skip = 'skip' in Object(opts) ? String(opts.skip) : '*';
// property pattern
const propertyMatch = new RegExp(`^${prefix}(border(?:-(color|style|width))?)$`);
// process a matched declaration
const processMatchedDeclaration = decl => {
// unprefixed property
const property = decl.prop.match(propertyMatch)[1];
// inner-property (color, style, width)
const innerProperty = decl.prop.match(propertyMatch)[2];
// if a prefix is in use
if (prefix) {
// remove it from the property
decl.prop = property;
}
// if a border has an inner-property
if (innerProperty) {
// spaced-separated values (top, right, bottom, left)
const values = postcss.list.space(decl.value);
// if the values contain a skip token
if (values.indexOf(skip) !== -1) {
// conditionally add a right value
if (values.length === 1) {
values.push(values[0]);
}
// conditionally add a bottom value
if (values.length === 2) {
values.push(values[0]);
}
// conditionally add a left value
if (values.length === 3) {
values.push(values[1]);
}
// for each value
values.forEach((value, index) => {
// if the value is not a skip token
if (value !== skip) {
// create a new declaration for the border side inner-property
decl.cloneBefore({
prop: `border-${ sides[index] }-${ innerProperty }`,
value: value
});
}
});
// remove the original declaration
decl.remove();
}
} else if (postcss.list.split(decl.value, '/').length > 1) {
// for each of the border values
postcss.list.split(decl.value, '/').forEach((values, index) => {
// process the new declaration for the border inner-property
processMatchedDeclaration(decl.cloneBefore({
prop: `${prefix}border-${properties[index]}`,
value: values
}));
});
// remove the original declaration
decl.remove();
}
};
return root => {
// walk each matching declaration
root.walkDecls(propertyMatch, processMatchedDeclaration);
};
});
// border properties
const properties = ['width', 'style', 'color'];
// border sides
const sides = ['top', 'right', 'bottom', 'left'];