ShtukbaseStandard
包文件→ .shtukbase/packages/shtukbase-ui-html/lib/tokens.ts

lib/tokens.ts

52 行, 4.0 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // SUI-R02 in code: component CSS takes colours, radii and shadows only from the token contract. A raw colour (hex,
  2. // rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch() without `from var(…)`, color() or a named colour) anywhere in a
  3. // declaration is a violation; so is a border-radius in plain lengths. Allowed: var(--…), color-mix() of vars,
  4. // relative colours from a var, `transparent`, `currentColor`, `inherit`, 0 and percentages for radii.
  5. const NAMED = new Set(('aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood ' +
  6. 'cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen ' +
  7. 'darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray ' +
  8. 'darkslategrey darkturquoise darkviolet deeppink deepskyblue dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia ' +
  9. 'gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender ' +
  10. 'lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink ' +
  11. 'lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon ' +
  12. 'mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise ' +
  13. 'mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid ' +
  14. 'palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red ' +
  15. 'rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow ' +
  16. 'springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow yellowgreen').split(' '));
  17. /** Comments out, strings out (a font name in quotes is not a colour), url(…) out. */
  18. function strip(css: string): string {
  19. return css
  20. .replace(/\/\*[\s\S]*?\*\//g, (m) => m.replace(/[^\n]/g, ' '))
  21. .replace(/url\([^)]*\)/g, (m) => ' '.repeat(m.length))
  22. .replace(/"[^"\n]*"|'[^'\n]*'/g, (m) => ' '.repeat(m.length));
  23. }
  24. /** Problems of one CSS file: "line N: …". */
  25. export function rawValues(css: string): string[] {
  26. const out: string[] = [];
  27. const text = strip(css);
  28. const lines = text.split('\n');
  29. // declarations: property: value; — inside rule blocks (after a `{`), not in selectors
  30. const re = /([{;]\s*)(--[\w-]+|[a-z-]+)\s*:\s*([^;{}]*)/g;
  31. for (let m; (m = re.exec(text)); ) {
  32. const prop = m[2], value = m[3];
  33. const line = text.slice(0, m.index + m[1].length).split('\n').length;
  34. const where = `line ${line} (${prop})`;
  35. 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`);
  36. for (const f of value.matchAll(/\b(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(\s*([^)]*)/gi)) {
  37. if (/^from\s+var\(/i.test(f[2])) continue; // relative colour from a token
  38. out.push(`${where}: a raw colour ${f[1]}(…); use a contract token or color-mix() of tokens`);
  39. }
  40. for (const w of value.toLowerCase().matchAll(/(?<![\w-])([a-z]+)(?![\w(-])/g))
  41. if (NAMED.has(w[1]) && !prop.startsWith('--') && !['font', 'font-family', 'animation', 'animation-name', 'transition', 'transition-property', 'grid-template-areas', 'grid-area', 'content'].includes(prop))
  42. out.push(`${where}: a named colour "${w[1]}"; use a contract token`);
  43. if (/^border(-[a-z]+)*-radius$/.test(prop) || prop === 'border-radius')
  44. for (const part of value.trim().split(/\s+(?![^(]*\))/))
  45. if (!/var\(|^0$|%$|^inherit$|^initial$|^unset$/.test(part)) out.push(`${where}: the radius "${part}" is not a contract token; use calc(var(--radius) ± n)`);
  46. }
  47. void lines;
  48. return out;
  49. }