@unocss/rule-utils 0.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @unocss/rule-utils
2
+
3
+ The utilities for creating rules/presets for [UnoCSS](https://github.com/unocss/unocss).
4
+
5
+ ## License
6
+
7
+ MIT License &copy; 2021-PRESENT [Anthony Fu](https://github.com/antfu)
package/dist/index.cjs ADDED
@@ -0,0 +1,387 @@
1
+ 'use strict';
2
+
3
+ const core = require('@unocss/core');
4
+
5
+ function getBracket(str, open, close) {
6
+ if (str === "")
7
+ return;
8
+ const l = str.length;
9
+ let parenthesis = 0;
10
+ let opened = false;
11
+ let openAt = 0;
12
+ for (let i = 0; i < l; i++) {
13
+ switch (str[i]) {
14
+ case open:
15
+ if (!opened) {
16
+ opened = true;
17
+ openAt = i;
18
+ }
19
+ parenthesis++;
20
+ break;
21
+ case close:
22
+ --parenthesis;
23
+ if (parenthesis < 0)
24
+ return;
25
+ if (parenthesis === 0) {
26
+ return [
27
+ str.slice(openAt, i + 1),
28
+ str.slice(i + 1),
29
+ str.slice(0, openAt)
30
+ ];
31
+ }
32
+ break;
33
+ }
34
+ }
35
+ }
36
+ function getStringComponent(str, open, close, separators) {
37
+ if (str === "")
38
+ return;
39
+ if (core.isString(separators))
40
+ separators = [separators];
41
+ if (separators.length === 0)
42
+ return;
43
+ const l = str.length;
44
+ let parenthesis = 0;
45
+ for (let i = 0; i < l; i++) {
46
+ switch (str[i]) {
47
+ case open:
48
+ parenthesis++;
49
+ break;
50
+ case close:
51
+ if (--parenthesis < 0)
52
+ return;
53
+ break;
54
+ default:
55
+ for (const separator of separators) {
56
+ const separatorLength = separator.length;
57
+ if (separatorLength && separator === str.slice(i, i + separatorLength) && parenthesis === 0) {
58
+ if (i === 0 || i === l - separatorLength)
59
+ return;
60
+ return [
61
+ str.slice(0, i),
62
+ str.slice(i + separatorLength)
63
+ ];
64
+ }
65
+ }
66
+ }
67
+ }
68
+ return [
69
+ str,
70
+ ""
71
+ ];
72
+ }
73
+ function getStringComponents(str, separators, limit) {
74
+ limit = limit ?? 10;
75
+ const components = [];
76
+ let i = 0;
77
+ while (str !== "") {
78
+ if (++i > limit)
79
+ return;
80
+ const componentPair = getStringComponent(str, "(", ")", separators);
81
+ if (!componentPair)
82
+ return;
83
+ const [component, rest] = componentPair;
84
+ components.push(component);
85
+ str = rest;
86
+ }
87
+ if (components.length > 0)
88
+ return components;
89
+ }
90
+
91
+ const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
92
+ const alphaPlaceholders = ["%alpha", "<alpha-value>"];
93
+ const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => core.escapeRegExp(v)).join("|"));
94
+ function hex2rgba(hex = "") {
95
+ const color = parseHexColor(hex);
96
+ if (color != null) {
97
+ const { components, alpha } = color;
98
+ if (alpha == null)
99
+ return components;
100
+ return [...components, alpha];
101
+ }
102
+ }
103
+ function parseCssColor(str = "") {
104
+ const color = parseColor(str);
105
+ if (color == null || color === false)
106
+ return;
107
+ const { type: casedType, components, alpha } = color;
108
+ const type = casedType.toLowerCase();
109
+ if (components.length === 0)
110
+ return;
111
+ if (["rgba", "hsla"].includes(type) && alpha == null)
112
+ return;
113
+ if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
114
+ return;
115
+ return {
116
+ type,
117
+ components: components.map((c) => typeof c === "string" ? c.trim() : c),
118
+ alpha: typeof alpha === "string" ? alpha.trim() : alpha
119
+ };
120
+ }
121
+ function colorOpacityToString(color) {
122
+ const alpha = color.alpha ?? 1;
123
+ return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
124
+ }
125
+ function colorToString(color, alphaOverride) {
126
+ if (typeof color === "string")
127
+ return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
128
+ const { components } = color;
129
+ let { alpha, type } = color;
130
+ alpha = alphaOverride ?? alpha;
131
+ type = type.toLowerCase();
132
+ if (["hsla", "hsl", "rgba", "rgb"].includes(type))
133
+ return `${type.replace("a", "")}a(${components.join(",")}${alpha == null ? "" : `,${alpha}`})`;
134
+ alpha = alpha == null ? "" : ` / ${alpha}`;
135
+ if (cssColorFunctions.includes(type))
136
+ return `${type}(${components.join(" ")}${alpha})`;
137
+ return `color(${type} ${components.join(" ")}${alpha})`;
138
+ }
139
+ function parseColor(str) {
140
+ if (!str)
141
+ return;
142
+ let color = parseHexColor(str);
143
+ if (color != null)
144
+ return color;
145
+ color = cssColorKeyword(str);
146
+ if (color != null)
147
+ return color;
148
+ color = parseCssCommaColorFunction(str);
149
+ if (color != null)
150
+ return color;
151
+ color = parseCssSpaceColorFunction(str);
152
+ if (color != null)
153
+ return color;
154
+ color = parseCssColorFunction(str);
155
+ if (color != null)
156
+ return color;
157
+ }
158
+ function parseHexColor(str) {
159
+ const [, body] = str.match(/^#([\da-f]+)$/i) || [];
160
+ if (!body)
161
+ return;
162
+ switch (body.length) {
163
+ case 3:
164
+ case 4:
165
+ const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
166
+ return {
167
+ type: "rgb",
168
+ components: digits.slice(0, 3),
169
+ alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
170
+ };
171
+ case 6:
172
+ case 8:
173
+ const value = Number.parseInt(body, 16);
174
+ return {
175
+ type: "rgb",
176
+ components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
177
+ alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
178
+ };
179
+ }
180
+ }
181
+ function cssColorKeyword(str) {
182
+ const color = {
183
+ rebeccapurple: [102, 51, 153, 1]
184
+ }[str];
185
+ if (color != null) {
186
+ return {
187
+ type: "rgb",
188
+ components: color.slice(0, 3),
189
+ alpha: color[3]
190
+ };
191
+ }
192
+ }
193
+ function parseCssCommaColorFunction(color) {
194
+ const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
195
+ if (!match)
196
+ return;
197
+ const [, type, componentString] = match;
198
+ const components = getStringComponents(componentString, ",", 5);
199
+ if (components) {
200
+ if ([3, 4].includes(components.length)) {
201
+ return {
202
+ type,
203
+ components: components.slice(0, 3),
204
+ alpha: components[3]
205
+ };
206
+ } else if (components.length !== 1) {
207
+ return false;
208
+ }
209
+ }
210
+ }
211
+ const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
212
+ function parseCssSpaceColorFunction(color) {
213
+ const match = color.match(cssColorFunctionsRe);
214
+ if (!match)
215
+ return;
216
+ const [, fn, componentString] = match;
217
+ const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
218
+ if (parsed) {
219
+ const { alpha, components: [type, ...components] } = parsed;
220
+ return {
221
+ type,
222
+ components,
223
+ alpha
224
+ };
225
+ }
226
+ }
227
+ function parseCssColorFunction(color) {
228
+ const match = color.match(/^color\((.+)\)$/);
229
+ if (!match)
230
+ return;
231
+ const parsed = parseCssSpaceColorValues(match[1]);
232
+ if (parsed) {
233
+ const { alpha, components: [type, ...components] } = parsed;
234
+ return {
235
+ type,
236
+ components,
237
+ alpha
238
+ };
239
+ }
240
+ }
241
+ function parseCssSpaceColorValues(componentString) {
242
+ const components = getStringComponents(componentString, " ");
243
+ if (!components)
244
+ return;
245
+ let totalComponents = components.length;
246
+ if (components[totalComponents - 2] === "/") {
247
+ return {
248
+ components: components.slice(0, totalComponents - 2),
249
+ alpha: components[totalComponents - 1]
250
+ };
251
+ }
252
+ if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
253
+ const removed = components.splice(totalComponents - 2);
254
+ components.push(removed.join(" "));
255
+ --totalComponents;
256
+ }
257
+ const withAlpha = getStringComponents(components[totalComponents - 1], "/", 2);
258
+ if (!withAlpha)
259
+ return;
260
+ if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
261
+ return { components };
262
+ const alpha = withAlpha.pop();
263
+ components[totalComponents - 1] = withAlpha.join("/");
264
+ return {
265
+ components,
266
+ alpha
267
+ };
268
+ }
269
+
270
+ function createValueHandler(handlers) {
271
+ const handler = function(str) {
272
+ const s = this.__options?.sequence || [];
273
+ this.__options.sequence = [];
274
+ for (const n of s) {
275
+ const res = handlers[n](str);
276
+ if (res != null)
277
+ return res;
278
+ }
279
+ };
280
+ function addProcessor(that, name) {
281
+ if (!that.__options) {
282
+ that.__options = {
283
+ sequence: []
284
+ };
285
+ }
286
+ that.__options.sequence.push(name);
287
+ return that;
288
+ }
289
+ for (const name of Object.keys(handlers)) {
290
+ Object.defineProperty(handler, name, {
291
+ enumerable: true,
292
+ get() {
293
+ return addProcessor(this, name);
294
+ }
295
+ });
296
+ }
297
+ return handler;
298
+ }
299
+
300
+ function variantMatcher(name, handler) {
301
+ let re;
302
+ return {
303
+ name,
304
+ match(input, ctx) {
305
+ if (!re)
306
+ re = new RegExp(`^${core.escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
307
+ const match = input.match(re);
308
+ if (match) {
309
+ return {
310
+ matcher: input.slice(match[0].length),
311
+ handle: (input2, next) => next({
312
+ ...input2,
313
+ ...handler(input2)
314
+ })
315
+ };
316
+ }
317
+ },
318
+ autocomplete: `${name}:`
319
+ };
320
+ }
321
+ function variantParentMatcher(name, parent) {
322
+ let re;
323
+ return {
324
+ name,
325
+ match(input, ctx) {
326
+ if (!re)
327
+ re = new RegExp(`^${core.escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
328
+ const match = input.match(re);
329
+ if (match) {
330
+ return {
331
+ matcher: input.slice(match[0].length),
332
+ handle: (input2, next) => next({
333
+ ...input2,
334
+ parent: `${input2.parent ? `${input2.parent} $$ ` : ""}${parent}`
335
+ })
336
+ };
337
+ }
338
+ },
339
+ autocomplete: `${name}:`
340
+ };
341
+ }
342
+ function variantGetBracket(prefix, matcher, separators) {
343
+ if (matcher.startsWith(`${prefix}[`)) {
344
+ const [match, rest] = getBracket(matcher.slice(prefix.length), "[", "]") ?? [];
345
+ if (match && rest) {
346
+ for (const separator of separators) {
347
+ if (rest.startsWith(separator))
348
+ return [match, rest.slice(separator.length), separator];
349
+ }
350
+ return [match, rest, ""];
351
+ }
352
+ }
353
+ }
354
+ function variantGetParameter(prefix, matcher, separators) {
355
+ if (matcher.startsWith(prefix)) {
356
+ const body = variantGetBracket(prefix, matcher, separators);
357
+ if (body) {
358
+ const [label = "", rest = body[1]] = variantGetParameter("/", body[1], separators) ?? [];
359
+ return [body[0], rest, label];
360
+ }
361
+ for (const separator of separators.filter((x) => x !== "/")) {
362
+ const pos = matcher.indexOf(separator, prefix.length);
363
+ if (pos !== -1) {
364
+ const labelPos = matcher.indexOf("/", prefix.length);
365
+ const unlabelled = labelPos === -1 || pos <= labelPos;
366
+ return [
367
+ matcher.slice(prefix.length, unlabelled ? pos : labelPos),
368
+ matcher.slice(pos + separator.length),
369
+ unlabelled ? "" : matcher.slice(labelPos + 1, pos)
370
+ ];
371
+ }
372
+ }
373
+ }
374
+ }
375
+
376
+ exports.colorOpacityToString = colorOpacityToString;
377
+ exports.colorToString = colorToString;
378
+ exports.createValueHandler = createValueHandler;
379
+ exports.getBracket = getBracket;
380
+ exports.getStringComponent = getStringComponent;
381
+ exports.getStringComponents = getStringComponents;
382
+ exports.hex2rgba = hex2rgba;
383
+ exports.parseCssColor = parseCssColor;
384
+ exports.variantGetBracket = variantGetBracket;
385
+ exports.variantGetParameter = variantGetParameter;
386
+ exports.variantMatcher = variantMatcher;
387
+ exports.variantParentMatcher = variantParentMatcher;
@@ -0,0 +1,28 @@
1
+ import { RGBAColorValue, CSSColorValue, VariantHandlerContext, VariantObject } from '@unocss/core';
2
+
3
+ declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
4
+ declare function parseCssColor(str?: string): CSSColorValue | undefined;
5
+ declare function colorOpacityToString(color: CSSColorValue): string | number;
6
+ declare function colorToString(color: CSSColorValue | string, alphaOverride?: string | number): string;
7
+
8
+ declare function getBracket(str: string, open: string, close: string): string[] | undefined;
9
+ declare function getStringComponent(str: string, open: string, close: string, separators: string | string[]): string[] | undefined;
10
+ declare function getStringComponents(str: string, separators: string | string[], limit?: number): string[] | undefined;
11
+
12
+ type ValueHandlerCallback = (str: string) => string | number | undefined;
13
+ type ValueHandler<K extends string> = {
14
+ [S in K]: ValueHandler<K>;
15
+ } & {
16
+ (str: string): string | undefined;
17
+ __options: {
18
+ sequence: K[];
19
+ };
20
+ };
21
+ declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
22
+
23
+ declare function variantMatcher(name: string, handler: (input: VariantHandlerContext) => Record<string, any>): VariantObject;
24
+ declare function variantParentMatcher(name: string, parent: string): VariantObject;
25
+ declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
26
+ declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
27
+
28
+ export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hex2rgba, parseCssColor, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
@@ -0,0 +1,28 @@
1
+ import { RGBAColorValue, CSSColorValue, VariantHandlerContext, VariantObject } from '@unocss/core';
2
+
3
+ declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
4
+ declare function parseCssColor(str?: string): CSSColorValue | undefined;
5
+ declare function colorOpacityToString(color: CSSColorValue): string | number;
6
+ declare function colorToString(color: CSSColorValue | string, alphaOverride?: string | number): string;
7
+
8
+ declare function getBracket(str: string, open: string, close: string): string[] | undefined;
9
+ declare function getStringComponent(str: string, open: string, close: string, separators: string | string[]): string[] | undefined;
10
+ declare function getStringComponents(str: string, separators: string | string[], limit?: number): string[] | undefined;
11
+
12
+ type ValueHandlerCallback = (str: string) => string | number | undefined;
13
+ type ValueHandler<K extends string> = {
14
+ [S in K]: ValueHandler<K>;
15
+ } & {
16
+ (str: string): string | undefined;
17
+ __options: {
18
+ sequence: K[];
19
+ };
20
+ };
21
+ declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
22
+
23
+ declare function variantMatcher(name: string, handler: (input: VariantHandlerContext) => Record<string, any>): VariantObject;
24
+ declare function variantParentMatcher(name: string, parent: string): VariantObject;
25
+ declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
26
+ declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
27
+
28
+ export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hex2rgba, parseCssColor, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
@@ -0,0 +1,28 @@
1
+ import { RGBAColorValue, CSSColorValue, VariantHandlerContext, VariantObject } from '@unocss/core';
2
+
3
+ declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
4
+ declare function parseCssColor(str?: string): CSSColorValue | undefined;
5
+ declare function colorOpacityToString(color: CSSColorValue): string | number;
6
+ declare function colorToString(color: CSSColorValue | string, alphaOverride?: string | number): string;
7
+
8
+ declare function getBracket(str: string, open: string, close: string): string[] | undefined;
9
+ declare function getStringComponent(str: string, open: string, close: string, separators: string | string[]): string[] | undefined;
10
+ declare function getStringComponents(str: string, separators: string | string[], limit?: number): string[] | undefined;
11
+
12
+ type ValueHandlerCallback = (str: string) => string | number | undefined;
13
+ type ValueHandler<K extends string> = {
14
+ [S in K]: ValueHandler<K>;
15
+ } & {
16
+ (str: string): string | undefined;
17
+ __options: {
18
+ sequence: K[];
19
+ };
20
+ };
21
+ declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
22
+
23
+ declare function variantMatcher(name: string, handler: (input: VariantHandlerContext) => Record<string, any>): VariantObject;
24
+ declare function variantParentMatcher(name: string, parent: string): VariantObject;
25
+ declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
26
+ declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
27
+
28
+ export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hex2rgba, parseCssColor, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
package/dist/index.mjs ADDED
@@ -0,0 +1,374 @@
1
+ import { isString, escapeRegExp } from '@unocss/core';
2
+
3
+ function getBracket(str, open, close) {
4
+ if (str === "")
5
+ return;
6
+ const l = str.length;
7
+ let parenthesis = 0;
8
+ let opened = false;
9
+ let openAt = 0;
10
+ for (let i = 0; i < l; i++) {
11
+ switch (str[i]) {
12
+ case open:
13
+ if (!opened) {
14
+ opened = true;
15
+ openAt = i;
16
+ }
17
+ parenthesis++;
18
+ break;
19
+ case close:
20
+ --parenthesis;
21
+ if (parenthesis < 0)
22
+ return;
23
+ if (parenthesis === 0) {
24
+ return [
25
+ str.slice(openAt, i + 1),
26
+ str.slice(i + 1),
27
+ str.slice(0, openAt)
28
+ ];
29
+ }
30
+ break;
31
+ }
32
+ }
33
+ }
34
+ function getStringComponent(str, open, close, separators) {
35
+ if (str === "")
36
+ return;
37
+ if (isString(separators))
38
+ separators = [separators];
39
+ if (separators.length === 0)
40
+ return;
41
+ const l = str.length;
42
+ let parenthesis = 0;
43
+ for (let i = 0; i < l; i++) {
44
+ switch (str[i]) {
45
+ case open:
46
+ parenthesis++;
47
+ break;
48
+ case close:
49
+ if (--parenthesis < 0)
50
+ return;
51
+ break;
52
+ default:
53
+ for (const separator of separators) {
54
+ const separatorLength = separator.length;
55
+ if (separatorLength && separator === str.slice(i, i + separatorLength) && parenthesis === 0) {
56
+ if (i === 0 || i === l - separatorLength)
57
+ return;
58
+ return [
59
+ str.slice(0, i),
60
+ str.slice(i + separatorLength)
61
+ ];
62
+ }
63
+ }
64
+ }
65
+ }
66
+ return [
67
+ str,
68
+ ""
69
+ ];
70
+ }
71
+ function getStringComponents(str, separators, limit) {
72
+ limit = limit ?? 10;
73
+ const components = [];
74
+ let i = 0;
75
+ while (str !== "") {
76
+ if (++i > limit)
77
+ return;
78
+ const componentPair = getStringComponent(str, "(", ")", separators);
79
+ if (!componentPair)
80
+ return;
81
+ const [component, rest] = componentPair;
82
+ components.push(component);
83
+ str = rest;
84
+ }
85
+ if (components.length > 0)
86
+ return components;
87
+ }
88
+
89
+ const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
90
+ const alphaPlaceholders = ["%alpha", "<alpha-value>"];
91
+ const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => escapeRegExp(v)).join("|"));
92
+ function hex2rgba(hex = "") {
93
+ const color = parseHexColor(hex);
94
+ if (color != null) {
95
+ const { components, alpha } = color;
96
+ if (alpha == null)
97
+ return components;
98
+ return [...components, alpha];
99
+ }
100
+ }
101
+ function parseCssColor(str = "") {
102
+ const color = parseColor(str);
103
+ if (color == null || color === false)
104
+ return;
105
+ const { type: casedType, components, alpha } = color;
106
+ const type = casedType.toLowerCase();
107
+ if (components.length === 0)
108
+ return;
109
+ if (["rgba", "hsla"].includes(type) && alpha == null)
110
+ return;
111
+ if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
112
+ return;
113
+ return {
114
+ type,
115
+ components: components.map((c) => typeof c === "string" ? c.trim() : c),
116
+ alpha: typeof alpha === "string" ? alpha.trim() : alpha
117
+ };
118
+ }
119
+ function colorOpacityToString(color) {
120
+ const alpha = color.alpha ?? 1;
121
+ return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
122
+ }
123
+ function colorToString(color, alphaOverride) {
124
+ if (typeof color === "string")
125
+ return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
126
+ const { components } = color;
127
+ let { alpha, type } = color;
128
+ alpha = alphaOverride ?? alpha;
129
+ type = type.toLowerCase();
130
+ if (["hsla", "hsl", "rgba", "rgb"].includes(type))
131
+ return `${type.replace("a", "")}a(${components.join(",")}${alpha == null ? "" : `,${alpha}`})`;
132
+ alpha = alpha == null ? "" : ` / ${alpha}`;
133
+ if (cssColorFunctions.includes(type))
134
+ return `${type}(${components.join(" ")}${alpha})`;
135
+ return `color(${type} ${components.join(" ")}${alpha})`;
136
+ }
137
+ function parseColor(str) {
138
+ if (!str)
139
+ return;
140
+ let color = parseHexColor(str);
141
+ if (color != null)
142
+ return color;
143
+ color = cssColorKeyword(str);
144
+ if (color != null)
145
+ return color;
146
+ color = parseCssCommaColorFunction(str);
147
+ if (color != null)
148
+ return color;
149
+ color = parseCssSpaceColorFunction(str);
150
+ if (color != null)
151
+ return color;
152
+ color = parseCssColorFunction(str);
153
+ if (color != null)
154
+ return color;
155
+ }
156
+ function parseHexColor(str) {
157
+ const [, body] = str.match(/^#([\da-f]+)$/i) || [];
158
+ if (!body)
159
+ return;
160
+ switch (body.length) {
161
+ case 3:
162
+ case 4:
163
+ const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
164
+ return {
165
+ type: "rgb",
166
+ components: digits.slice(0, 3),
167
+ alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
168
+ };
169
+ case 6:
170
+ case 8:
171
+ const value = Number.parseInt(body, 16);
172
+ return {
173
+ type: "rgb",
174
+ components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
175
+ alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
176
+ };
177
+ }
178
+ }
179
+ function cssColorKeyword(str) {
180
+ const color = {
181
+ rebeccapurple: [102, 51, 153, 1]
182
+ }[str];
183
+ if (color != null) {
184
+ return {
185
+ type: "rgb",
186
+ components: color.slice(0, 3),
187
+ alpha: color[3]
188
+ };
189
+ }
190
+ }
191
+ function parseCssCommaColorFunction(color) {
192
+ const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
193
+ if (!match)
194
+ return;
195
+ const [, type, componentString] = match;
196
+ const components = getStringComponents(componentString, ",", 5);
197
+ if (components) {
198
+ if ([3, 4].includes(components.length)) {
199
+ return {
200
+ type,
201
+ components: components.slice(0, 3),
202
+ alpha: components[3]
203
+ };
204
+ } else if (components.length !== 1) {
205
+ return false;
206
+ }
207
+ }
208
+ }
209
+ const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
210
+ function parseCssSpaceColorFunction(color) {
211
+ const match = color.match(cssColorFunctionsRe);
212
+ if (!match)
213
+ return;
214
+ const [, fn, componentString] = match;
215
+ const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
216
+ if (parsed) {
217
+ const { alpha, components: [type, ...components] } = parsed;
218
+ return {
219
+ type,
220
+ components,
221
+ alpha
222
+ };
223
+ }
224
+ }
225
+ function parseCssColorFunction(color) {
226
+ const match = color.match(/^color\((.+)\)$/);
227
+ if (!match)
228
+ return;
229
+ const parsed = parseCssSpaceColorValues(match[1]);
230
+ if (parsed) {
231
+ const { alpha, components: [type, ...components] } = parsed;
232
+ return {
233
+ type,
234
+ components,
235
+ alpha
236
+ };
237
+ }
238
+ }
239
+ function parseCssSpaceColorValues(componentString) {
240
+ const components = getStringComponents(componentString, " ");
241
+ if (!components)
242
+ return;
243
+ let totalComponents = components.length;
244
+ if (components[totalComponents - 2] === "/") {
245
+ return {
246
+ components: components.slice(0, totalComponents - 2),
247
+ alpha: components[totalComponents - 1]
248
+ };
249
+ }
250
+ if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
251
+ const removed = components.splice(totalComponents - 2);
252
+ components.push(removed.join(" "));
253
+ --totalComponents;
254
+ }
255
+ const withAlpha = getStringComponents(components[totalComponents - 1], "/", 2);
256
+ if (!withAlpha)
257
+ return;
258
+ if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
259
+ return { components };
260
+ const alpha = withAlpha.pop();
261
+ components[totalComponents - 1] = withAlpha.join("/");
262
+ return {
263
+ components,
264
+ alpha
265
+ };
266
+ }
267
+
268
+ function createValueHandler(handlers) {
269
+ const handler = function(str) {
270
+ const s = this.__options?.sequence || [];
271
+ this.__options.sequence = [];
272
+ for (const n of s) {
273
+ const res = handlers[n](str);
274
+ if (res != null)
275
+ return res;
276
+ }
277
+ };
278
+ function addProcessor(that, name) {
279
+ if (!that.__options) {
280
+ that.__options = {
281
+ sequence: []
282
+ };
283
+ }
284
+ that.__options.sequence.push(name);
285
+ return that;
286
+ }
287
+ for (const name of Object.keys(handlers)) {
288
+ Object.defineProperty(handler, name, {
289
+ enumerable: true,
290
+ get() {
291
+ return addProcessor(this, name);
292
+ }
293
+ });
294
+ }
295
+ return handler;
296
+ }
297
+
298
+ function variantMatcher(name, handler) {
299
+ let re;
300
+ return {
301
+ name,
302
+ match(input, ctx) {
303
+ if (!re)
304
+ re = new RegExp(`^${escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
305
+ const match = input.match(re);
306
+ if (match) {
307
+ return {
308
+ matcher: input.slice(match[0].length),
309
+ handle: (input2, next) => next({
310
+ ...input2,
311
+ ...handler(input2)
312
+ })
313
+ };
314
+ }
315
+ },
316
+ autocomplete: `${name}:`
317
+ };
318
+ }
319
+ function variantParentMatcher(name, parent) {
320
+ let re;
321
+ return {
322
+ name,
323
+ match(input, ctx) {
324
+ if (!re)
325
+ re = new RegExp(`^${escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
326
+ const match = input.match(re);
327
+ if (match) {
328
+ return {
329
+ matcher: input.slice(match[0].length),
330
+ handle: (input2, next) => next({
331
+ ...input2,
332
+ parent: `${input2.parent ? `${input2.parent} $$ ` : ""}${parent}`
333
+ })
334
+ };
335
+ }
336
+ },
337
+ autocomplete: `${name}:`
338
+ };
339
+ }
340
+ function variantGetBracket(prefix, matcher, separators) {
341
+ if (matcher.startsWith(`${prefix}[`)) {
342
+ const [match, rest] = getBracket(matcher.slice(prefix.length), "[", "]") ?? [];
343
+ if (match && rest) {
344
+ for (const separator of separators) {
345
+ if (rest.startsWith(separator))
346
+ return [match, rest.slice(separator.length), separator];
347
+ }
348
+ return [match, rest, ""];
349
+ }
350
+ }
351
+ }
352
+ function variantGetParameter(prefix, matcher, separators) {
353
+ if (matcher.startsWith(prefix)) {
354
+ const body = variantGetBracket(prefix, matcher, separators);
355
+ if (body) {
356
+ const [label = "", rest = body[1]] = variantGetParameter("/", body[1], separators) ?? [];
357
+ return [body[0], rest, label];
358
+ }
359
+ for (const separator of separators.filter((x) => x !== "/")) {
360
+ const pos = matcher.indexOf(separator, prefix.length);
361
+ if (pos !== -1) {
362
+ const labelPos = matcher.indexOf("/", prefix.length);
363
+ const unlabelled = labelPos === -1 || pos <= labelPos;
364
+ return [
365
+ matcher.slice(prefix.length, unlabelled ? pos : labelPos),
366
+ matcher.slice(pos + separator.length),
367
+ unlabelled ? "" : matcher.slice(labelPos + 1, pos)
368
+ ];
369
+ }
370
+ }
371
+ }
372
+ }
373
+
374
+ export { colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hex2rgba, parseCssColor, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@unocss/rule-utils",
3
+ "version": "0.56.0",
4
+ "description": "Utilities for UnoCSS",
5
+ "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
+ "license": "MIT",
7
+ "funding": "https://github.com/sponsors/antfu",
8
+ "homepage": "https://github.com/unocss/unocss/tree/main/packages/rule-utils#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/unocss/unocss",
12
+ "directory": "packages/rule-utils"
13
+ },
14
+ "bugs": {
15
+ "url": "https://github.com/unocss/unocss/issues"
16
+ },
17
+ "keywords": [],
18
+ "sideEffects": false,
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.mjs",
23
+ "require": "./dist/index.cjs"
24
+ }
25
+ },
26
+ "main": "dist/index.cjs",
27
+ "module": "dist/index.mjs",
28
+ "types": "dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "engines": {
33
+ "node": ">=14"
34
+ },
35
+ "dependencies": {
36
+ "@unocss/core": "^0.56.0"
37
+ },
38
+ "scripts": {
39
+ "build": "unbuild",
40
+ "stub": "unbuild --stub"
41
+ }
42
+ }