@unocss/rule-utils 0.58.9 → 0.59.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/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@unocss/rule-utils",
3
- "version": "0.58.9",
3
+ "type": "module",
4
+ "version": "0.59.0",
4
5
  "description": "Utilities for UnoCSS",
5
6
  "author": "Anthony Fu <anthonyfu117@hotmail.com>",
6
7
  "license": "MIT",
@@ -18,12 +19,11 @@
18
19
  "sideEffects": false,
19
20
  "exports": {
20
21
  ".": {
21
- "types": "./dist/index.d.ts",
22
- "import": "./dist/index.mjs",
23
- "require": "./dist/index.cjs"
22
+ "types": "./dist/index.d.mts",
23
+ "default": "./dist/index.mjs"
24
24
  }
25
25
  },
26
- "main": "dist/index.cjs",
26
+ "main": "dist/index.mjs",
27
27
  "module": "dist/index.mjs",
28
28
  "types": "dist/index.d.ts",
29
29
  "files": [
@@ -33,8 +33,8 @@
33
33
  "node": ">=14"
34
34
  },
35
35
  "dependencies": {
36
- "magic-string": "^0.30.8",
37
- "@unocss/core": "^0.58.9"
36
+ "magic-string": "^0.30.9",
37
+ "@unocss/core": "^0.59.0"
38
38
  },
39
39
  "scripts": {
40
40
  "build": "unbuild",
package/dist/index.cjs DELETED
@@ -1,427 +0,0 @@
1
- 'use strict';
2
-
3
- const core = require('@unocss/core');
4
- const MagicString = require('magic-string');
5
-
6
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
7
-
8
- const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
9
-
10
- function getBracket(str, open, close) {
11
- if (str === "")
12
- return;
13
- const l = str.length;
14
- let parenthesis = 0;
15
- let opened = false;
16
- let openAt = 0;
17
- for (let i = 0; i < l; i++) {
18
- switch (str[i]) {
19
- case open:
20
- if (!opened) {
21
- opened = true;
22
- openAt = i;
23
- }
24
- parenthesis++;
25
- break;
26
- case close:
27
- --parenthesis;
28
- if (parenthesis < 0)
29
- return;
30
- if (parenthesis === 0) {
31
- return [
32
- str.slice(openAt, i + 1),
33
- str.slice(i + 1),
34
- str.slice(0, openAt)
35
- ];
36
- }
37
- break;
38
- }
39
- }
40
- }
41
- function getStringComponent(str, open, close, separators) {
42
- if (str === "")
43
- return;
44
- if (core.isString(separators))
45
- separators = [separators];
46
- if (separators.length === 0)
47
- return;
48
- const l = str.length;
49
- let parenthesis = 0;
50
- for (let i = 0; i < l; i++) {
51
- switch (str[i]) {
52
- case open:
53
- parenthesis++;
54
- break;
55
- case close:
56
- if (--parenthesis < 0)
57
- return;
58
- break;
59
- default:
60
- for (const separator of separators) {
61
- const separatorLength = separator.length;
62
- if (separatorLength && separator === str.slice(i, i + separatorLength) && parenthesis === 0) {
63
- if (i === 0 || i === l - separatorLength)
64
- return;
65
- return [
66
- str.slice(0, i),
67
- str.slice(i + separatorLength)
68
- ];
69
- }
70
- }
71
- }
72
- }
73
- return [
74
- str,
75
- ""
76
- ];
77
- }
78
- function getStringComponents(str, separators, limit) {
79
- limit = limit ?? 10;
80
- const components = [];
81
- let i = 0;
82
- while (str !== "") {
83
- if (++i > limit)
84
- return;
85
- const componentPair = getStringComponent(str, "(", ")", separators);
86
- if (!componentPair)
87
- return;
88
- const [component, rest] = componentPair;
89
- components.push(component);
90
- str = rest;
91
- }
92
- if (components.length > 0)
93
- return components;
94
- }
95
-
96
- const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
97
- const alphaPlaceholders = ["%alpha", "<alpha-value>"];
98
- const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => core.escapeRegExp(v)).join("|"));
99
- function hex2rgba(hex = "") {
100
- const color = parseHexColor(hex);
101
- if (color != null) {
102
- const { components, alpha } = color;
103
- if (alpha == null)
104
- return components;
105
- return [...components, alpha];
106
- }
107
- }
108
- function parseCssColor(str = "") {
109
- const color = parseColor(str);
110
- if (color == null || color === false)
111
- return;
112
- const { type: casedType, components, alpha } = color;
113
- const type = casedType.toLowerCase();
114
- if (components.length === 0)
115
- return;
116
- if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
117
- return;
118
- return {
119
- type,
120
- components: components.map((c) => typeof c === "string" ? c.trim() : c),
121
- alpha: typeof alpha === "string" ? alpha.trim() : alpha
122
- };
123
- }
124
- function colorOpacityToString(color) {
125
- const alpha = color.alpha ?? 1;
126
- return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
127
- }
128
- function colorToString(color, alphaOverride) {
129
- if (typeof color === "string")
130
- return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
131
- const { components } = color;
132
- let { alpha, type } = color;
133
- alpha = alphaOverride ?? alpha;
134
- type = type.toLowerCase();
135
- if (["hsla", "rgba"].includes(type))
136
- return `${type}(${components.join(", ")}${alpha == null ? "" : `, ${alpha}`})`;
137
- alpha = alpha == null ? "" : ` / ${alpha}`;
138
- if (cssColorFunctions.includes(type))
139
- return `${type}(${components.join(" ")}${alpha})`;
140
- return `color(${type} ${components.join(" ")}${alpha})`;
141
- }
142
- function parseColor(str) {
143
- if (!str)
144
- return;
145
- let color = parseHexColor(str);
146
- if (color != null)
147
- return color;
148
- color = cssColorKeyword(str);
149
- if (color != null)
150
- return color;
151
- color = parseCssCommaColorFunction(str);
152
- if (color != null)
153
- return color;
154
- color = parseCssSpaceColorFunction(str);
155
- if (color != null)
156
- return color;
157
- color = parseCssColorFunction(str);
158
- if (color != null)
159
- return color;
160
- }
161
- function parseHexColor(str) {
162
- const [, body] = str.match(/^#([\da-f]+)$/i) || [];
163
- if (!body)
164
- return;
165
- switch (body.length) {
166
- case 3:
167
- case 4:
168
- const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
169
- return {
170
- type: "rgb",
171
- components: digits.slice(0, 3),
172
- alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
173
- };
174
- case 6:
175
- case 8:
176
- const value = Number.parseInt(body, 16);
177
- return {
178
- type: "rgb",
179
- components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
180
- alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
181
- };
182
- }
183
- }
184
- function cssColorKeyword(str) {
185
- const color = {
186
- rebeccapurple: [102, 51, 153, 1]
187
- }[str];
188
- if (color != null) {
189
- return {
190
- type: "rgb",
191
- components: color.slice(0, 3),
192
- alpha: color[3]
193
- };
194
- }
195
- }
196
- function parseCssCommaColorFunction(color) {
197
- const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
198
- if (!match)
199
- return;
200
- const [, type, componentString] = match;
201
- const components = getStringComponents(componentString, ",", 5);
202
- if (components) {
203
- if ([3, 4].includes(components.length)) {
204
- return {
205
- type,
206
- components: components.slice(0, 3),
207
- alpha: components[3]
208
- };
209
- } else if (components.length !== 1) {
210
- return false;
211
- }
212
- }
213
- }
214
- const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
215
- function parseCssSpaceColorFunction(color) {
216
- const match = color.match(cssColorFunctionsRe);
217
- if (!match)
218
- return;
219
- const [, fn, componentString] = match;
220
- const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
221
- if (parsed) {
222
- const { alpha, components: [type, ...components] } = parsed;
223
- return {
224
- type,
225
- components,
226
- alpha
227
- };
228
- }
229
- }
230
- function parseCssColorFunction(color) {
231
- const match = color.match(/^color\((.+)\)$/);
232
- if (!match)
233
- return;
234
- const parsed = parseCssSpaceColorValues(match[1]);
235
- if (parsed) {
236
- const { alpha, components: [type, ...components] } = parsed;
237
- return {
238
- type,
239
- components,
240
- alpha
241
- };
242
- }
243
- }
244
- function parseCssSpaceColorValues(componentString) {
245
- const components = getStringComponents(componentString, " ");
246
- if (!components)
247
- return;
248
- let totalComponents = components.length;
249
- if (components[totalComponents - 2] === "/") {
250
- return {
251
- components: components.slice(0, totalComponents - 2),
252
- alpha: components[totalComponents - 1]
253
- };
254
- }
255
- if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
256
- const removed = components.splice(totalComponents - 2);
257
- components.push(removed.join(" "));
258
- --totalComponents;
259
- }
260
- const withAlpha = getStringComponents(components[totalComponents - 1], "/", 2);
261
- if (!withAlpha)
262
- return;
263
- if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
264
- return { components };
265
- const alpha = withAlpha.pop();
266
- components[totalComponents - 1] = withAlpha.join("/");
267
- return {
268
- components,
269
- alpha
270
- };
271
- }
272
-
273
- function createValueHandler(handlers) {
274
- const handler = function(str) {
275
- const s = this.__options?.sequence || [];
276
- this.__options.sequence = [];
277
- for (const n of s) {
278
- const res = handlers[n](str);
279
- if (res != null)
280
- return res;
281
- }
282
- };
283
- function addProcessor(that, name) {
284
- if (!that.__options) {
285
- that.__options = {
286
- sequence: []
287
- };
288
- }
289
- that.__options.sequence.push(name);
290
- return that;
291
- }
292
- for (const name of Object.keys(handlers)) {
293
- Object.defineProperty(handler, name, {
294
- enumerable: true,
295
- get() {
296
- return addProcessor(this, name);
297
- }
298
- });
299
- }
300
- return handler;
301
- }
302
-
303
- function variantMatcher(name, handler) {
304
- let re;
305
- return {
306
- name,
307
- match(input, ctx) {
308
- if (!re)
309
- re = new RegExp(`^${core.escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
310
- const match = input.match(re);
311
- if (match) {
312
- return {
313
- matcher: input.slice(match[0].length),
314
- handle: (input2, next) => next({
315
- ...input2,
316
- ...handler(input2)
317
- })
318
- };
319
- }
320
- },
321
- autocomplete: `${name}:`
322
- };
323
- }
324
- function variantParentMatcher(name, parent) {
325
- let re;
326
- return {
327
- name,
328
- match(input, ctx) {
329
- if (!re)
330
- re = new RegExp(`^${core.escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
331
- const match = input.match(re);
332
- if (match) {
333
- return {
334
- matcher: input.slice(match[0].length),
335
- handle: (input2, next) => next({
336
- ...input2,
337
- parent: `${input2.parent ? `${input2.parent} $$ ` : ""}${parent}`
338
- })
339
- };
340
- }
341
- },
342
- autocomplete: `${name}:`
343
- };
344
- }
345
- function variantGetBracket(prefix, matcher, separators) {
346
- if (matcher.startsWith(`${prefix}[`)) {
347
- const [match, rest] = getBracket(matcher.slice(prefix.length), "[", "]") ?? [];
348
- if (match && rest) {
349
- for (const separator of separators) {
350
- if (rest.startsWith(separator))
351
- return [match, rest.slice(separator.length), separator];
352
- }
353
- return [match, rest, ""];
354
- }
355
- }
356
- }
357
- function variantGetParameter(prefix, matcher, separators) {
358
- if (matcher.startsWith(prefix)) {
359
- const body = variantGetBracket(prefix, matcher, separators);
360
- if (body) {
361
- const [label = "", rest = body[1]] = variantGetParameter("/", body[1], separators) ?? [];
362
- return [body[0], rest, label];
363
- }
364
- for (const separator of separators.filter((x) => x !== "/")) {
365
- const pos = matcher.indexOf(separator, prefix.length);
366
- if (pos !== -1) {
367
- const labelPos = matcher.indexOf("/", prefix.length);
368
- const unlabelled = labelPos === -1 || pos <= labelPos;
369
- return [
370
- matcher.slice(prefix.length, unlabelled ? pos : labelPos),
371
- matcher.slice(pos + separator.length),
372
- unlabelled ? "" : matcher.slice(labelPos + 1, pos)
373
- ];
374
- }
375
- }
376
- }
377
- }
378
-
379
- const themeFnRE = /theme\(\s*['"]?(.*?)['"]?\s*\)/g;
380
- function hasThemeFn(str) {
381
- return str.includes("theme(") && str.includes(")");
382
- }
383
- function transformThemeFn(code, theme, throwOnMissing = true) {
384
- const matches = Array.from(code.toString().matchAll(themeFnRE));
385
- if (!matches.length)
386
- return code;
387
- const s = new MagicString__default(code);
388
- for (const match of matches) {
389
- const rawArg = match[1];
390
- if (!rawArg)
391
- throw new Error("theme() expect exact one argument, but got 0");
392
- const [rawKey, alpha] = rawArg.split("/");
393
- const keys = rawKey.trim().split(".");
394
- let value = keys.reduce((t, k) => t?.[k], theme);
395
- if (typeof value === "string") {
396
- if (alpha) {
397
- const color = parseCssColor(value);
398
- if (color)
399
- value = colorToString(color, alpha);
400
- }
401
- s.overwrite(
402
- match.index,
403
- match.index + match[0].length,
404
- value
405
- );
406
- } else if (throwOnMissing) {
407
- throw new Error(`theme of "${rawArg}" did not found`);
408
- }
409
- }
410
- return s.toString();
411
- }
412
-
413
- exports.colorOpacityToString = colorOpacityToString;
414
- exports.colorToString = colorToString;
415
- exports.createValueHandler = createValueHandler;
416
- exports.getBracket = getBracket;
417
- exports.getStringComponent = getStringComponent;
418
- exports.getStringComponents = getStringComponents;
419
- exports.hasThemeFn = hasThemeFn;
420
- exports.hex2rgba = hex2rgba;
421
- exports.parseCssColor = parseCssColor;
422
- exports.themeFnRE = themeFnRE;
423
- exports.transformThemeFn = transformThemeFn;
424
- exports.variantGetBracket = variantGetBracket;
425
- exports.variantGetParameter = variantGetParameter;
426
- exports.variantMatcher = variantMatcher;
427
- exports.variantParentMatcher = variantParentMatcher;
package/dist/index.d.cts DELETED
@@ -1,32 +0,0 @@
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
- declare const themeFnRE: RegExp;
29
- declare function hasThemeFn(str: string): boolean;
30
- declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
31
-
32
- export { type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasThemeFn, hex2rgba, parseCssColor, themeFnRE, transformThemeFn, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };