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 5
/
Copy pathindex.js
135 lines (112 loc) · 3.05 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import postcss from 'postcss';
export default postcss.plugin('postcss-short-spacing', opts => {
// get the prefix
const prefix = 'prefix' in Object(opts) ? `-${opts.prefix}-` : '';
// get the skip token
const skip = 'skip' in Object(opts) ? String(opts.skip) : '*';
// property pattern
const spacingPropertyRegExp = new RegExp(`^${prefix}(margin|padding)(-(?:block|end|inline|start))?$`, 'i');
return root => {
// walk each matching declaration
root.walkDecls(spacingPropertyRegExp, decl => {
const [, property, logical] = decl.prop.match(spacingPropertyRegExp);
// update the property
if (prefix) {
decl.prop = `${property}${logical || ''}`
}
// get the space-separated values
const values = postcss.list.space(decl.value);
if (values.includes(skip)) {
const isBlockInline = blockInlineRegExp.test(logical);
const isStartEnd = startEndRegExp.test(logical);
const isFourSide = !logical;
if (isBlockInline) {
transformBlockInline(decl, `${property}${logical}-&`, values, skip);
} else if (isStartEnd) {
transformStartEnd(decl, `${property}-&${logical}`, values, skip);
} else if (isFourSide) {
transformFourSide(decl, `${property}-&`, values, skip);
}
decl.remove();
}
});
};
});
const blockInlineRegExp = /^-(block|inline)$/i;
const startEndRegExp = /^-(start|end)$/i;
// conditionally add start/end values
const transformBlockInline = (decl, property, values, skip) => { // eslint-disable-line max-params
const [$1, $2] = values;
if ($1 && $1 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'start'),
value: $1
});
}
if ($2 && $2 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'end'),
value: $2
});
}
};
// conditionally add block/inline values
const transformStartEnd = (decl, property, values, skip) => { // eslint-disable-line max-params
const [$1, $2] = values;
if ($1 && $1 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'block'),
value: $1
});
}
if ($2 && $2 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'inline'),
value: $2
});
}
};
// conditionally add top/right/bottom/left values
const transformFourSide = (decl, property, values, skip) => { // eslint-disable-line max-params
const [$1, $2, $3, $4] = values;
// conditionally add a left value
if ($4 && $4 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'left'),
value: $4
});
}
// conditionally add a bottom value
if ($3 && $3 !== skip) {
decl.cloneBefore({
prop: property.replace('&', 'bottom'),
value: $3
});
}
// conditionally add a right value
if ($2 && $2 !== skip) {
if (!$4) {
decl.cloneBefore({
prop: property.replace('&', 'left'),
value: $2
});
}
decl.cloneBefore({
prop: property.replace('&', 'right'),
value: $2
});
}
// conditionally add a top value
if ($1 && $1 !== skip) {
if (!$3) {
decl.cloneBefore({
prop: property.replace('&', 'bottom'),
value: $1
});
}
decl.cloneBefore({
prop: property.replace('&', 'top'),
value: $1
});
}
};