Theme engine: a rule is read as it takes effect (a Tailwind opacity utility's nested @supports value, not its solid fallback); unsupported @supports groups are left out; reach() is exported for site adapters

This commit is contained in:
CRTkafa 2026-09-30 14:04:12 +00:00
parent 01763ccdba
commit b3042f9325
3 changed files with 106 additions and 7 deletions

View file

@ -5,7 +5,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { FONT_ROLE_NAMES, MONO_FONT, contrastRatio } from '../src/themeApi.ts';
import { choiceForId, createThemeCatalog, serializeChoice, MAX_USER_THEMES } from '../src/themeChoice.ts';
import { SNAPSHOT_LOCAL_MAX, readSnapshot, resolvedFonts, writeSnapshot, type ThemeSnapshot } from '../src/themeRuntime.ts';
import { SNAPSHOT_LOCAL_MAX, collectRules, readSnapshot, resolvedFonts, writeSnapshot, type ThemeSnapshot } from '../src/themeRuntime.ts';
import {
BASE_EFFECTS, BASE_FONT, BASE_LAYOUT, BASE_SHAPE, FONT_CHOICES, ROLE_LIMITS, applyOverrides, backgroundLayers, ensureContrast, fontChoiceOf, fontStack, isNeutral,
pageBackgroundCss, parseThemeJson, resolveFontRoles, sanitizeSpec, withFamily, withRoles, type ThemeSpec,
@ -197,3 +197,58 @@ test('garbage in storage is no snapshot', () => {
stores.local!.setItem('wb:theme-snapshot', JSON.stringify({ key: 1, css: 'a{}' }));
assert.equal(readSnapshot(stores), null);
});
// collectRules: the site's rules as they take effect
type FakeRule = { selectorText?: string; style?: { getPropertyValue(name: string): string; length: number }; cssRules?: FakeRule[]; conditionText?: string; type?: number };
const decl = (values: Record<string, string>) => ({ getPropertyValue: (name: string) => values[name] ?? '', length: Object.keys(values).length });
const styleRule = (selectorText: string, values: Record<string, string>, cssRules: FakeRule[] = []): FakeRule => ({ selectorText, style: decl(values), cssRules });
const supportsRule = (conditionText: string, cssRules: FakeRule[]): FakeRule => ({ type: 12, conditionText, cssRules });
const mediaRule = (conditionText: string, cssRules: FakeRule[]): FakeRule => ({ type: 4, conditionText, cssRules });
const fakeDoc = (rules: FakeRule[], supports = (_condition: string) => true) => ({ styleSheets: [{ ownerNode: null, cssRules: rules }], defaultView: { CSS: { supports } } }) as unknown as Document;
const MIX = '(color: color-mix(in lab, red, red))';
test('collectRules reads a Tailwind opacity utility as its nested @supports value, not its solid fallback', () => {
const rules = collectRules(fakeDoc([
styleRule('.dark\:bg-white\/15', { 'background-color': 'var(--x-white)', 'border-radius': '9999px' }, [
supportsRule(MIX, [{ style: decl({ 'background-color': 'color-mix(in oklab, var(--x-white) 15%, transparent)' }) }]),
]),
styleRule('.plain', { color: 'rgb(1, 2, 3)' }),
]), null);
assert.equal(rules.length, 2, 'one rule per selector, the nested declarations are not a rule of their own');
assert.equal(rules[0]!.selectorText, '.dark\:bg-white\/15');
assert.equal(rules[0]!.style.getPropertyValue('background-color'), 'color-mix(in oklab, var(--x-white) 15%, transparent)');
assert.equal(rules[0]!.style.getPropertyValue('border-radius'), '9999px', 'what the group does not re-declare stays the rule\'s own');
assert.equal(rules[0]!.style.length, 2);
assert.equal(rules[1]!.style.getPropertyValue('color'), 'rgb(1, 2, 3)');
});
test('collectRules: the `& { }` form of nested declarations counts, a nested rule with its own selector does not', () => {
const [rule] = collectRules(fakeDoc([
styleRule('.border-accent\/50', { 'border-color': 'hsl(var(--accent))' }, [
supportsRule(MIX, [styleRule('&', { 'border-color': 'color-mix(in oklab, hsl(var(--accent)) 50%, transparent)' }), styleRule('&:hover', { 'border-color': 'red' })]),
mediaRule('(hover: hover)', [{ style: decl({ 'border-color': 'blue' }) }]),
]),
]), null);
assert.equal(rule!.style.getPropertyValue('border-color'), 'color-mix(in oklab, hsl(var(--accent)) 50%, transparent)');
});
test('collectRules keeps the fallback where the browser fails the @supports condition, and skips such top-level groups', () => {
const rules = collectRules(fakeDoc([
styleRule('.bg-white\/15', { 'background-color': 'var(--x-white)' }, [supportsRule(MIX, [{ style: decl({ 'background-color': 'color-mix(in oklab, var(--x-white) 15%, transparent)' }) }])]),
supportsRule(MIX, [styleRule('.hover\:bg-white\/10:hover', { 'background-color': 'color-mix(in oklab, var(--x-white) 10%, transparent)' })]),
mediaRule('(hover: hover)', [styleRule('.in-media', { color: 'red' })]),
], () => false), null);
assert.deepEqual(rules.map(rule => rule.selectorText), ['.bg-white\/15', '.in-media']);
assert.equal(rules[0]!.style.getPropertyValue('background-color'), 'var(--x-white)');
});
test('collectRules leaves the engine\'s own sheet out and survives a cross-origin sheet', () => {
const own = { id: 'own' } as unknown as HTMLStyleElement;
const doc = { styleSheets: [
{ ownerNode: own, cssRules: [styleRule('.ours', { color: 'red' })] },
{ ownerNode: null, get cssRules(): FakeRule[] { throw new Error('SecurityError'); } },
{ ownerNode: null, cssRules: [supportsRule(MIX, [mediaRule('(hover: hover)', [styleRule('.deep', { color: 'red' })])])] },
], defaultView: null } as unknown as Document;
assert.deepEqual(collectRules(doc, own).map(rule => rule.selectorText), ['.deep']);
});