feature: vor und nachteile

This commit is contained in:
Hikyu
2026-05-31 13:54:31 +02:00
parent 552a947a1c
commit e3dad17446
23270 changed files with 3024794 additions and 200 deletions
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021-PRESENT Anthony Fu <https://github.com/antfu>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+7
View File
@@ -0,0 +1,7 @@
# @unocss/rule-utils
The utilities for creating rules/presets for [UnoCSS](https://github.com/unocss/unocss).
## License
MIT License &copy; 2021-PRESENT [Anthony Fu](https://github.com/antfu)
+68
View File
@@ -0,0 +1,68 @@
import { VariantHandlerContext, VariantObject } from '@unocss/core';
interface CSSColorValue {
type: string;
components: (string | number)[];
alpha: string | number | undefined;
}
type RGBAColorValue = [number, number, number, number] | [number, number, number];
interface ParsedColorValue {
/**
* Parsed color value.
*/
color?: string;
/**
* Parsed opacity value.
*/
opacity: string;
/**
* Color name.
*/
name: string;
/**
* Color scale, preferably 000 - 999.
*/
no: string;
/**
* {@link CSSColorValue}
*/
cssColor: CSSColorValue | undefined;
/**
* Parsed alpha value from opacity
*/
alpha: string | number | undefined;
}
declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
declare function parseCssColor(str?: string): CSSColorValue | undefined;
declare function colorOpacityToString(color: CSSColorValue): string | number;
declare function colorToString(color: CSSColorValue | string, alphaOverride?: string | number): string;
type ValueHandlerCallback = (str: string) => string | number | undefined;
type ValueHandler<K extends string> = {
[S in K]: ValueHandler<K>;
} & {
(str: string): string | undefined;
__options: {
sequence: K[];
};
};
declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
declare const iconFnRE: RegExp;
declare function hasIconFn(str: string): boolean;
declare const themeFnRE: RegExp;
declare function hasThemeFn(str: string): boolean;
declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
declare function transformThemeString(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string | undefined;
declare function getBracket(str: string, open: string, close: string): string[] | undefined;
declare function getStringComponent(str: string, open: string, close: string, separators: string | string[]): string[] | undefined;
declare function getStringComponents(str: string, separators: string | string[], limit?: number): string[] | undefined;
declare function variantMatcher(name: string, handler: (input: VariantHandlerContext) => Record<string, any>): VariantObject;
declare function variantParentMatcher(name: string, parent: string): VariantObject;
declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
export { type CSSColorValue, type ParsedColorValue, type RGBAColorValue, type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasIconFn, hasThemeFn, hex2rgba, iconFnRE, parseCssColor, themeFnRE, transformThemeFn, transformThemeString, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
+68
View File
@@ -0,0 +1,68 @@
import { VariantHandlerContext, VariantObject } from '@unocss/core';
interface CSSColorValue {
type: string;
components: (string | number)[];
alpha: string | number | undefined;
}
type RGBAColorValue = [number, number, number, number] | [number, number, number];
interface ParsedColorValue {
/**
* Parsed color value.
*/
color?: string;
/**
* Parsed opacity value.
*/
opacity: string;
/**
* Color name.
*/
name: string;
/**
* Color scale, preferably 000 - 999.
*/
no: string;
/**
* {@link CSSColorValue}
*/
cssColor: CSSColorValue | undefined;
/**
* Parsed alpha value from opacity
*/
alpha: string | number | undefined;
}
declare function hex2rgba(hex?: string): RGBAColorValue | undefined;
declare function parseCssColor(str?: string): CSSColorValue | undefined;
declare function colorOpacityToString(color: CSSColorValue): string | number;
declare function colorToString(color: CSSColorValue | string, alphaOverride?: string | number): string;
type ValueHandlerCallback = (str: string) => string | number | undefined;
type ValueHandler<K extends string> = {
[S in K]: ValueHandler<K>;
} & {
(str: string): string | undefined;
__options: {
sequence: K[];
};
};
declare function createValueHandler<K extends string>(handlers: Record<K, ValueHandlerCallback>): ValueHandler<K>;
declare const iconFnRE: RegExp;
declare function hasIconFn(str: string): boolean;
declare const themeFnRE: RegExp;
declare function hasThemeFn(str: string): boolean;
declare function transformThemeFn(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string;
declare function transformThemeString(code: string, theme: Record<string, any>, throwOnMissing?: boolean): string | undefined;
declare function getBracket(str: string, open: string, close: string): string[] | undefined;
declare function getStringComponent(str: string, open: string, close: string, separators: string | string[]): string[] | undefined;
declare function getStringComponents(str: string, separators: string | string[], limit?: number): string[] | undefined;
declare function variantMatcher(name: string, handler: (input: VariantHandlerContext) => Record<string, any>): VariantObject;
declare function variantParentMatcher(name: string, parent: string): VariantObject;
declare function variantGetBracket(prefix: string, matcher: string, separators: string[]): string[] | undefined;
declare function variantGetParameter(prefix: string, matcher: string, separators: string[]): string[] | undefined;
export { type CSSColorValue, type ParsedColorValue, type RGBAColorValue, type ValueHandler, type ValueHandlerCallback, colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasIconFn, hasThemeFn, hex2rgba, iconFnRE, parseCssColor, themeFnRE, transformThemeFn, transformThemeString, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
+422
View File
@@ -0,0 +1,422 @@
import { isString, escapeRegExp } from '@unocss/core';
import MagicString from 'magic-string';
function getBracket(str, open, close) {
if (str === "")
return;
const l = str.length;
let parenthesis = 0;
let opened = false;
let openAt = 0;
for (let i = 0; i < l; i++) {
switch (str[i]) {
case open:
if (!opened) {
opened = true;
openAt = i;
}
parenthesis++;
break;
case close:
--parenthesis;
if (parenthesis < 0)
return;
if (parenthesis === 0) {
return [
str.slice(openAt, i + 1),
str.slice(i + 1),
str.slice(0, openAt)
];
}
break;
}
}
}
function getStringComponent(str, open, close, separators) {
if (str === "")
return;
if (isString(separators))
separators = [separators];
if (separators.length === 0)
return;
const l = str.length;
let parenthesis = 0;
for (let i = 0; i < l; i++) {
switch (str[i]) {
case open:
parenthesis++;
break;
case close:
if (--parenthesis < 0)
return;
break;
default:
for (const separator of separators) {
const separatorLength = separator.length;
if (separatorLength && separator === str.slice(i, i + separatorLength) && parenthesis === 0) {
if (i === 0 || i === l - separatorLength)
return;
return [
str.slice(0, i),
str.slice(i + separatorLength)
];
}
}
}
}
return [
str,
""
];
}
function getStringComponents(str, separators, limit) {
limit = limit ?? 10;
const components = [];
let i = 0;
while (str !== "") {
if (++i > limit)
return;
const componentPair = getStringComponent(str, "(", ")", separators);
if (!componentPair)
return;
const [component, rest] = componentPair;
components.push(component);
str = rest;
}
if (components.length > 0)
return components;
}
const cssColorFunctions = ["hsl", "hsla", "hwb", "lab", "lch", "oklab", "oklch", "rgb", "rgba"];
const alphaPlaceholders = ["%alpha", "<alpha-value>"];
const alphaPlaceholdersRE = new RegExp(alphaPlaceholders.map((v) => escapeRegExp(v)).join("|"));
function hex2rgba(hex = "") {
const color = parseHexColor(hex);
if (color != null) {
const { components, alpha } = color;
if (alpha == null)
return components;
return [...components, alpha];
}
}
function parseCssColor(str = "") {
const color = parseColor(str);
if (color == null || color === false)
return;
const { type: casedType, components, alpha } = color;
const type = casedType.toLowerCase();
if (components.length === 0)
return;
if (cssColorFunctions.includes(type) && ![1, 3].includes(components.length))
return;
return {
type,
components: components.map((c) => typeof c === "string" ? c.trim() : c),
alpha: typeof alpha === "string" ? alpha.trim() : alpha
};
}
function colorOpacityToString(color) {
const alpha = color.alpha ?? 1;
return typeof alpha === "string" && alphaPlaceholders.includes(alpha) ? 1 : alpha;
}
function colorToString(color, alphaOverride) {
if (typeof color === "string")
return color.replace(alphaPlaceholdersRE, `${alphaOverride ?? 1}`);
const { components } = color;
let { alpha, type } = color;
alpha = alphaOverride ?? alpha;
type = type.toLowerCase();
if (["hsla", "rgba"].includes(type))
return `${type}(${components.join(", ")}${alpha == null ? "" : `, ${alpha}`})`;
alpha = alpha == null ? "" : ` / ${alpha}`;
if (cssColorFunctions.includes(type))
return `${type}(${components.join(" ")}${alpha})`;
return `color(${type} ${components.join(" ")}${alpha})`;
}
function parseColor(str) {
if (!str)
return;
let color = parseHexColor(str);
if (color != null)
return color;
color = cssColorKeyword(str);
if (color != null)
return color;
color = parseCssCommaColorFunction(str);
if (color != null)
return color;
color = parseCssSpaceColorFunction(str);
if (color != null)
return color;
color = parseCssColorFunction(str);
if (color != null)
return color;
}
function parseHexColor(str) {
const [, body] = str.match(/^#([\da-f]+)$/i) || [];
if (!body)
return;
switch (body.length) {
case 3:
case 4:
const digits = Array.from(body, (s) => Number.parseInt(s, 16)).map((n) => n << 4 | n);
return {
type: "rgb",
components: digits.slice(0, 3),
alpha: body.length === 3 ? void 0 : Math.round(digits[3] / 255 * 100) / 100
};
case 6:
case 8:
const value = Number.parseInt(body, 16);
return {
type: "rgb",
components: body.length === 6 ? [value >> 16 & 255, value >> 8 & 255, value & 255] : [value >> 24 & 255, value >> 16 & 255, value >> 8 & 255],
alpha: body.length === 6 ? void 0 : Math.round((value & 255) / 255 * 100) / 100
};
}
}
function cssColorKeyword(str) {
const color = {
rebeccapurple: [102, 51, 153, 1]
}[str];
if (color != null) {
return {
type: "rgb",
components: color.slice(0, 3),
alpha: color[3]
};
}
}
function parseCssCommaColorFunction(color) {
const match = color.match(/^(rgb|rgba|hsl|hsla)\((.+)\)$/i);
if (!match)
return;
const [, type, componentString] = match;
const components = getStringComponents(componentString, ",", 5);
if (components) {
if ([3, 4].includes(components.length)) {
return {
type,
components: components.slice(0, 3),
alpha: components[3]
};
} else if (components.length !== 1) {
return false;
}
}
}
const cssColorFunctionsRe = new RegExp(`^(${cssColorFunctions.join("|")})\\((.+)\\)$`, "i");
function parseCssSpaceColorFunction(color) {
const match = color.match(cssColorFunctionsRe);
if (!match)
return;
const [, fn, componentString] = match;
const parsed = parseCssSpaceColorValues(`${fn} ${componentString}`);
if (parsed) {
const { alpha, components: [type, ...components] } = parsed;
return {
type,
components,
alpha
};
}
}
function parseCssColorFunction(color) {
const match = color.match(/^color\((.+)\)$/);
if (!match)
return;
const parsed = parseCssSpaceColorValues(match[1]);
if (parsed) {
const { alpha, components: [type, ...components] } = parsed;
return {
type,
components,
alpha
};
}
}
function parseCssSpaceColorValues(componentString) {
const components = getStringComponents(componentString, " ");
if (!components)
return;
let totalComponents = components.length;
if (components[totalComponents - 2] === "/") {
return {
components: components.slice(0, totalComponents - 2),
alpha: components[totalComponents - 1]
};
}
if (components[totalComponents - 2] != null && (components[totalComponents - 2].endsWith("/") || components[totalComponents - 1].startsWith("/"))) {
const removed = components.splice(totalComponents - 2);
components.push(removed.join(" "));
--totalComponents;
}
const withAlpha = getStringComponents(components[totalComponents - 1], "/", 2);
if (!withAlpha)
return;
if (withAlpha.length === 1 || withAlpha[withAlpha.length - 1] === "")
return { components };
const alpha = withAlpha.pop();
components[totalComponents - 1] = withAlpha.join("/");
return {
components,
alpha
};
}
function createValueHandler(handlers) {
const handler = function(str) {
const s = this.__options?.sequence || [];
this.__options.sequence = [];
for (const n of s) {
const res = handlers[n](str);
if (res != null)
return res;
}
};
function addProcessor(that, name) {
if (!that.__options) {
that.__options = {
sequence: []
};
}
that.__options.sequence.push(name);
return that;
}
for (const name of Object.keys(handlers)) {
Object.defineProperty(handler, name, {
enumerable: true,
configurable: true,
get() {
return addProcessor(this, name);
}
});
}
return handler;
}
const iconFnRE = /icon\(\s*(['"])?(.*?)\1?\s*\)/g;
function hasIconFn(str) {
return str.includes("icon(") && str.includes(")");
}
const themeFnRE = /theme\(\s*(['"])?(.*?)\1?\s*\)/g;
function hasThemeFn(str) {
return str.includes("theme(") && str.includes(")");
}
function transformThemeFn(code, theme, throwOnMissing = true) {
const matches = Array.from(code.toString().matchAll(themeFnRE));
if (!matches.length)
return code;
const s = new MagicString(code);
for (const match of matches) {
const rawArg = match[2];
if (!rawArg)
throw new Error("theme() expect exact one argument, but got 0");
const value = transformThemeString(rawArg, theme, throwOnMissing);
if (value) {
s.overwrite(
match.index,
match.index + match[0].length,
value
);
}
}
return s.toString();
}
function transformThemeString(code, theme, throwOnMissing = true) {
const [rawKey, alpha] = code.split("/");
const keys = rawKey.trim().split(".");
let value = keys.reduce((t, k) => t?.[k], theme);
if (typeof value === "object") {
value = value.DEFAULT;
}
if (typeof value === "string") {
if (alpha) {
const color = parseCssColor(value);
if (color)
value = colorToString(color, alpha);
}
return value;
} else if (throwOnMissing) {
throw new Error(`theme of "${code}" did not found`);
}
}
function variantMatcher(name, handler) {
let re;
return {
name,
match(input, ctx) {
if (!re)
re = new RegExp(`^${escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
const match = input.match(re);
if (match) {
return {
matcher: input.slice(match[0].length),
handle: (input2, next) => next({
...input2,
...handler(input2)
})
};
}
},
autocomplete: `${name}:`
};
}
function variantParentMatcher(name, parent) {
let re;
return {
name,
match(input, ctx) {
if (!re)
re = new RegExp(`^${escapeRegExp(name)}(?:${ctx.generator.config.separators.join("|")})`);
const match = input.match(re);
if (match) {
return {
matcher: input.slice(match[0].length),
handle: (input2, next) => next({
...input2,
parent: `${input2.parent ? `${input2.parent} $$ ` : ""}${parent}`
})
};
}
},
autocomplete: `${name}:`
};
}
function variantGetBracket(prefix, matcher, separators) {
if (matcher.startsWith(`${prefix}[`)) {
const [match, rest] = getBracket(matcher.slice(prefix.length), "[", "]") ?? [];
if (match && rest) {
for (const separator of separators) {
if (rest.startsWith(separator))
return [match, rest.slice(separator.length), separator];
}
return [match, rest, ""];
}
}
}
function variantGetParameter(prefix, matcher, separators) {
if (matcher.startsWith(prefix)) {
const body = variantGetBracket(prefix, matcher, separators);
if (body) {
const [label = "", rest = body[1]] = variantGetParameter("/", body[1], separators) ?? [];
return [body[0], rest, label];
}
for (const separator of separators.filter((x) => x !== "/")) {
const pos = matcher.indexOf(separator, prefix.length);
if (pos !== -1) {
const labelPos = matcher.indexOf("/", prefix.length);
const unlabelled = labelPos === -1 || pos <= labelPos;
return [
matcher.slice(prefix.length, unlabelled ? pos : labelPos),
matcher.slice(pos + separator.length),
unlabelled ? "" : matcher.slice(labelPos + 1, pos)
];
}
}
}
}
export { colorOpacityToString, colorToString, createValueHandler, getBracket, getStringComponent, getStringComponents, hasIconFn, hasThemeFn, hex2rgba, iconFnRE, parseCssColor, themeFnRE, transformThemeFn, transformThemeString, variantGetBracket, variantGetParameter, variantMatcher, variantParentMatcher };
+44
View File
@@ -0,0 +1,44 @@
{
"name": "@unocss/rule-utils",
"type": "module",
"version": "0.65.4",
"description": "Utilities for UnoCSS",
"author": "Anthony Fu <anthonyfu117@hotmail.com>",
"license": "MIT",
"funding": "https://github.com/sponsors/antfu",
"homepage": "https://github.com/unocss/unocss/tree/main/packages/rule-utils#readme",
"repository": {
"type": "git",
"url": "https://github.com/unocss/unocss",
"directory": "packages/rule-utils"
},
"bugs": {
"url": "https://github.com/unocss/unocss/issues"
},
"keywords": [],
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
}
},
"main": "dist/index.mjs",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"engines": {
"node": ">=14"
},
"dependencies": {
"magic-string": "^0.30.17",
"@unocss/core": "^0.65.4"
},
"scripts": {
"build": "unbuild",
"stub": "unbuild --stub",
"test:attw": "attw --pack --config-path ../../.attw-esm-only.json"
}
}