-
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathindex.ts
More file actions
194 lines (160 loc) · 5.46 KB
/
index.ts
File metadata and controls
194 lines (160 loc) · 5.46 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import type { ComponentValue} from '@csstools/css-parser-algorithms';
import { replaceComponentValues, stringify, TokenNode } from '@csstools/css-parser-algorithms';
import type { PluginCreator } from 'postcss';
import { FunctionNode, isCommentNode, isTokenNode, WhitespaceNode, isFunctionNode, parseCommaSeparatedListOfComponentValues } from '@csstools/css-parser-algorithms';
import { calcFromComponentValues } from '@csstools/css-calc';
import { isTokenNumeric, isTokenDelim, NumberType } from '@csstools/css-tokenizer';
import { tokenize, TokenType } from '@csstools/css-tokenizer';
/** postcss-gradient-stop-increments plugin options */
export type pluginOptions = {
/** Preserve the original notation. default: false */
preserve?: boolean,
};
const GRADIENT_FUNCTION_REGEX = /(?:repeating-)?(?:linear|radial|conic)-gradient\(/i;
const GRADIENT_NAME_REGEX = /^(?:repeating-)?(?:linear|radial|conic)-gradient$/i;
const MATH_FUNCTION_NAME_REGEX = /^(?:abs|acos|asin|atan|atan2|calc|clamp|cos|exp|hypot|log|max|min|mod|pow|rem|round|sign|sin|sqrt|tan)$/i;
const creator: PluginCreator<pluginOptions> = (opts?: pluginOptions) => {
const options: pluginOptions = Object.assign(
// Default options
{
preserve: false,
},
// Provided options
opts,
);
return {
postcssPlugin: 'postcss-gradient-stop-increments',
Declaration(decl): void {
if (!GRADIENT_FUNCTION_REGEX.test(decl.value)) {
return;
}
const tokens = tokenize({
css: decl.value,
});
if (!tokens.find((token) => isTokenDelim(token) && token[4].value === '+')) {
return;
}
const modified = stringify(replaceComponentValues(
parseCommaSeparatedListOfComponentValues(tokens),
(componentValue) => {
if (!isFunctionNode(componentValue)) {
return;
}
const functionName = componentValue.getName();
if (!GRADIENT_NAME_REGEX.test(functionName)) {
return;
}
let lastLengthNode: ComponentValue | null = null;
for (let i = 0; i < componentValue.value.length; i++) {
const node = componentValue.value[i];
if (isTokenNode(node) && isTokenDelim(node.value) && node.value[4].value === '+') {
const operatorNode = node;
const operatorIndex = i;
while (isCommentNode(componentValue.value[i + 1])) {
i++;
}
i++;
if (isZeroOrNegative(componentValue.value[i])) {
const zeroNode = new TokenNode([TokenType.Number, '0', -1, -1, { value: 0, type: NumberType.Integer }]);
componentValue.value.splice(operatorIndex, (i - operatorIndex + 1), zeroNode);
i = componentValue.value.indexOf(zeroNode);
continue;
}
const nextNode = incrementLengthNode(lastLengthNode, operatorNode, componentValue.value[i]);
componentValue.value.splice(operatorIndex, (i - operatorIndex + 1), nextNode);
lastLengthNode = nextNode;
i = componentValue.value.indexOf(nextNode);
continue;
}
if (isNumericLargerThanZero(node)) {
lastLengthNode = maxOfLastAndCurrentLengthNode(lastLengthNode, node);
continue;
}
if (isFunctionNode(node) && MATH_FUNCTION_NAME_REGEX.test(node.getName())) {
lastLengthNode = maxOfLastAndCurrentLengthNode(lastLengthNode, node);
continue;
}
}
return;
},
));
if (modified === decl.value) {
return;
}
decl.cloneBefore({
value: modified,
});
if (!options?.preserve) {
decl.remove();
}
},
};
};
creator.postcss = true;
export default creator;
export { creator as 'module.exports' };
function isNumericLargerThanZero(node: ComponentValue): boolean {
if (
isTokenNode(node) &&
isTokenNumeric(node.value) &&
/*
Values can only increase.
For zero specifically we know that it will be smaller and normalized by browsers.
For all other values we wrap with `max(a, b)`.
*/
node.value[4].value > 0
) {
return true;
}
return false;
}
function isZeroOrNegative(node: ComponentValue): boolean {
if (
isTokenNode(node) &&
isTokenNumeric(node.value) &&
/*
Values can only increase.
For zero specifically we know that it will be smaller and normalized by browsers.
For all other values we wrap with `max(a, b)`.
*/
node.value[4].value <= 0
) {
return true;
}
return false;
}
function incrementLengthNode(lastLengthNode: ComponentValue | null, operatorNode: ComponentValue, nextNode: ComponentValue): ComponentValue {
if (!lastLengthNode) {
return nextNode;
}
const maxLengthNode = new FunctionNode(
[TokenType.Function, 'calc(', -1, -1, { value: 'calc' }],
[TokenType.CloseParen, ')', -1, -1, undefined],
[
lastLengthNode,
new WhitespaceNode([[TokenType.Whitespace, ' ', -1, -1, undefined]]),
operatorNode,
new WhitespaceNode([[TokenType.Whitespace, ' ', -1, -1, undefined]]),
nextNode,
],
);
const [[solvedLengthNode]] = calcFromComponentValues([[maxLengthNode]]);
return solvedLengthNode;
}
function maxOfLastAndCurrentLengthNode(lastLengthNode: ComponentValue | null, newLengthNode: ComponentValue): ComponentValue {
if (!lastLengthNode) {
return newLengthNode;
}
const maxLengthNode = new FunctionNode(
[TokenType.Function, 'max(', -1, -1, { value: 'max' }],
[TokenType.CloseParen, ')', -1, -1, undefined],
[
lastLengthNode,
new TokenNode([TokenType.Comma, ',', -1, -1, undefined]),
new WhitespaceNode([[TokenType.Whitespace, ' ', -1, -1, undefined]]),
newLengthNode,
],
);
const [[solvedLengthNode]] = calcFromComponentValues([[maxLengthNode]]);
return solvedLengthNode;
}