包文件→ .shtukbase/packages/shtukbase-ui-html/lib/tokens.ts
lib/tokens.ts
52 行, 4.0 KB.
文件按包中的原样显示:代码及其注释不做翻译。
- // SUI-R02 in code: component CSS takes colours, radii and shadows only from the token contract. A raw colour (hex,
- // rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch() without `from var(…)`, color() or a named colour) anywhere in a
- // declaration is a violation; so is a border-radius in plain lengths. Allowed: var(--…), color-mix() of vars,
- // relative colours from a var, `transparent`, `currentColor`, `inherit`, 0 and percentages for radii.
- const NAMED = new Set(('aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood ' +
- 'cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen ' +
- 'darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray ' +
- 'darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia ' +
- 'gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender ' +
- 'lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink ' +
- 'lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon ' +
- 'mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise ' +
- 'mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid ' +
- 'palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red ' +
- 'rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow ' +
- 'springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen').split(' '));
- /** Comments out, strings out (a font name in quotes is not a colour), url(…) out. */
- function strip(css: string): string {
- return css
- .replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
- .replace(/url\([^)]*\)/g, (m) => ' '.repeat(m.length))
- .replace(/"[^"\n]*"|'[^'\n]*'/g, (m) => ' '.repeat(m.length));
- }
- /** Problems of one CSS file: "line N: …". */
- export function rawValues(css: string): string[] {
- const out: string[] = [];
- const text = strip(css);
- const lines = text.split('\n');
- // declarations: property: value; — inside rule blocks (after a `{`), not in selectors
- const re = /([{;]\s*)(--[\w-]+|[a-z-]+)\s*:\s*([^;{}]*)/g;
- for (let m; (m = re.exec(text)); ) {
- const prop = m[2], value = m[3];
- const line = text.slice(0, m.index + m[1].length).split('\n').length;
- const where = `line ${line} (${prop})`;
- if (/#[0-9a-f]{3,8}\b/i.test(value)) out.push(`${where}: a raw hex colour "${value.match(/#[0-9a-f]{3,8}\b/i)![0]}"; use a contract token`);
- for (const f of value.matchAll(/\b(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(\s*([^)]*)/gi)) {
- if (/^from\s+var\(/i.test(f[2])) continue; // relative colour from a token
- out.push(`${where}: a raw colour ${f[1]}(…); use a contract token or color-mix() of tokens`);
- }
- for (const w of value.toLowerCase().matchAll(/(?<![\w-])([a-z]+)(?![\w(-])/g))
- if (NAMED.has(w[1]) && !prop.startsWith('--') && !['font', 'font-family', 'animation', 'animation-name', 'transition', 'transition-property', 'grid-template-areas', 'grid-area', 'content'].includes(prop))
- out.push(`${where}: a named colour "${w[1]}"; use a contract token`);
- if (/^border(-[a-z]+)*-radius$/.test(prop) || prop === 'border-radius')
- for (const part of value.trim().split(/\s+(?![^(]*\))/))
- if (!/var\(|^0$|%$|^inherit$|^initial$|^unset$/.test(part)) out.push(`${where}: the radius "${part}" is not a contract token; use calc(var(--radius) ± n)`);
- }
- void lines;
- return out;
- }