From a353c328927cc3cefefbd940c942527a0776a524 Mon Sep 17 00:00:00 2001 From: OddTurtleHead <355182507+OddTurtleHead@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:14:42 +0800 Subject: [PATCH] fix: refresh host-control tokens on late skin CSS, resolve modern colours and refuse skin fonts - bindHostControlTheme re-applies when
stylesheets or root attributes change, so a stored skin id themes the floating recovery capsule, skin-load banner and authorization dialog once its CSS actually arrives; the recovery root pass stands down while a slot binding owns the capsule tokens. - The shadow-root probe converts oklch()/color-mix() results to sRGB via a 1x1 canvas instead of silently dropping them to the default palette. - Host-owned controls ignore font tokens naming a document @font-face, which could ship blank glyphs and erase the recovery/consent labels. - Authorization body text uses the full sanitized foreground (the 76% mix could fall under the 4.5 contrast floor); theme refreshes before showModal. - Browser fixture covers late CSS, oklch and font refusal; docs updated. --- docs/SKIN_DEVELOPMENT.md | 6 +- web/src/components/HostChrome.vue | 13 ++- web/src/components/InstanceAuthorization.vue | 3 +- web/src/services/host-control-tokens.test.ts | 17 ++++ web/src/services/host-control-tokens.ts | 101 ++++++++++++++++--- web/src/services/host-slot-placement.ts | 14 +-- web/test/fixtures/host-control-theme.html | 75 ++++++++++++++ web/test/host-chrome-recovery.test.mjs | 34 ++++++- 8 files changed, 232 insertions(+), 31 deletions(-) create mode 100644 web/test/fixtures/host-control-theme.html diff --git a/docs/SKIN_DEVELOPMENT.md b/docs/SKIN_DEVELOPMENT.md index 4a3d937..62e3b26 100644 --- a/docs/SKIN_DEVELOPMENT.md +++ b/docs/SKIN_DEVELOPMENT.md @@ -501,7 +501,7 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动 | `--skin-recovery-foreground` | 文字色。与背景的对比度必须 ≥ 4.5。 | | `--skin-recovery-border` | 胶囊和普通按钮的边框色。与背景的对比度必须 ≥ 3,否则使用宿主默认边框;不影响固定浅色底的「使用内置皮肤」按钮。 | | `--skin-recovery-radius` | 圆角,只接受 px 长度并限制在 0–999;其他值使用宿主默认 999px。 | -| `--skin-recovery-font` | 字体族列表,只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符;其他值使用宿主默认字体。 | +| `--skin-recovery-font` | 字体族列表,只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符;列表中只要出现皮肤用 `@font-face` 注册的字体就整项忽略(只认系统已安装字体和通用族名);其他值使用宿主默认字体。 | | `--skin-recovery-size` | `small`(默认)、`medium` 或 `large`。宿主映射为固定指标:按钮最小命中高度 28 / 32 / 36 px,最小字号 11 / 12 / 13 px;皮肤不能把这些值降到宿主下限以下。 | 颜色建议使用 `#rgb`、`#rrggbb`、`rgb()` 或 `rgba()`;宿主通过影子根内的探针元素让浏览器解析颜色,再按 WCAG 相对亮度检查对比度。无效、缺失或无法解析的值使用宿主默认颜色(强调色 `#b45309`、背景 `#ffffff`、文字 `#31524f`)。 @@ -561,13 +561,13 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动 } ``` -宿主从计算样式读取这些变量,通过影子根内的探针解析颜色,再写入控件自己的 `--ws-control-*` 变量;皮肤不能写入 Shadow DOM 内部,也不能替换结构令牌。安全底线与其他受保护控件一致: +宿主从计算样式读取这些变量,通过影子根内的探针解析颜色(`oklch()`、`color-mix()` 等现代写法会先换算成 sRGB 再检查对比度),再写入控件自己的 `--ws-control-*` 变量;皮肤样式晚于皮肤 ID 加载、运行时插件追加样式时,宿主会自动重新读取,不需要刷新页面。皮肤不能写入 Shadow DOM 内部,也不能替换结构令牌。安全底线与其他受保护控件一致: - 背景强制不透明(alpha 视为 1)。 - 文字与背景对比度必须 ≥ 4.5,强调色与背景 ≥ 3;任一不达标时整组颜色回退到宿主默认,而不是只改一项。 - 边框与背景对比度 ≥ 3,否则使用宿主默认边框。 - 圆角只接受 px 并限制上限(恢复胶囊 999px,横幅和授权对话框 64px)。 -- 字体族只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符。 +- 字体族只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符;只认系统已安装字体和通用族名(`sans-serif` 等)。列表里只要有一个族名来自文档里的 `@font-face`(也就是皮肤自带字体),整项令牌就被忽略,因为自带字体可以把字形做成空白,让宿主控件的文字消失。应用 DOM 内的受保护控件不受此限制,照常可以在 `skin.css` 里用皮肤字体。 - `size` 只接受 `small`、`medium`、`large`,并且只能把命中区域/字号调大;恢复胶囊为 28/32/36px 命中区域、11/12/13px 字号,横幅和授权对话框以自身更大的内置尺寸为下限。 - 授权对话框的主按钮文字色由宿主根据强调色自动选择黑/白,保证可读。 - 恢复胶囊停靠后如果锚点消失、被遮挡或压到其他受保护控件,仍会回退到默认位置;横幅固定在顶部,授权对话框始终是顶层模态框,二者不参与停靠,因此不会因为皮肤布局而找不到。 diff --git a/web/src/components/HostChrome.vue b/web/src/components/HostChrome.vue index bf4ed35..189683b 100644 --- a/web/src/components/HostChrome.vue +++ b/web/src/components/HostChrome.vue @@ -178,11 +178,13 @@ const showRecovery = computed(() => shouldShowRecovery({ const shortcutOpen = ref(false); const recoveryPopover = refpending+ + + diff --git a/web/test/host-chrome-recovery.test.mjs b/web/test/host-chrome-recovery.test.mjs index e3e1b5a..2add711 100644 --- a/web/test/host-chrome-recovery.test.mjs +++ b/web/test/host-chrome-recovery.test.mjs @@ -169,7 +169,7 @@ test("the public-skin composable opts in only through chrome.json#visibleParts", assert.doesNotMatch(usePublicSkin, /\.test\(skin\.css\)/); }); -async function dumpDom(url) { +async function dumpDom(url, extraArgs = []) { return await new Promise((resolve, reject) => { const child = spawn(CHROME, [ "--headless=new", @@ -177,6 +177,7 @@ async function dumpDom(url) { "--disable-gpu", "--disable-dev-shm-usage", "--hide-scrollbars", + ...extraArgs, "--dump-dom", url, ], { stdio: ["ignore", "pipe", "pipe"] }); @@ -301,3 +302,34 @@ test("a declared slot docks the capsule and falls back at runtime", { skip: !CHR assert.equal(byName.disposed.atDefaultCorner, true); assert.equal(byName.disposed.leftoverAccent, ""); }); + +test("host control tokens follow late skin CSS, resolve modern colours and refuse skin fonts", { skip: !CHROME && "google-chrome is not installed" }, async (t) => { + const server = await createServer({ + root: webRoot, + configFile: path.join(webRoot, "vite.config.ts"), + logLevel: "silent", + server: { host: "127.0.0.1", port: 0, strictPort: false, hmr: false, ws: false }, + }); + t.after(() => server.close()); + await server.listen(); + const address = server.httpServer?.address(); + const port = address && typeof address === "object" ? address.port : 0; + assert.ok(port, "Vite did not expose a listening port"); + + // The fixture awaits animation frames, so give headless Chrome virtual time. + const dom = await dumpDom(`http://127.0.0.1:${port}/test/fixtures/host-control-theme.html`, ["--virtual-time-budget=10000"]); + const match = dom.match(/
]*>([\s\S]*?)<\/pre>/); + assert.ok(match, "the fixture did not emit a result block"); + const payload = JSON.parse(match[1].replace(/"/g, '"').replace(/&/g, "&")); + assert.equal(payload.error, undefined, payload.error); + const byName = Object.fromEntries(payload.results.map((result) => [result.name, result])); + + assert.equal(byName["before-skin-css"].inlineBackground, "", "tokens were applied before the skin declared any"); + const late = byName["late-skin-css"]; + assert.match(late.inlineBackground, /^rgb\(\d+, \d+, \d+\)$/, "late skin CSS did not re-theme the control"); + assert.notEqual(late.inlineBackground, "rgb(255, 251, 235)", "the oklch() background fell back to the default palette"); + assert.equal(late.background, late.inlineBackground); + assert.equal(late.inlineFont, "ui-sans-serif, system-ui, sans-serif", "a skin @font-face reached a host-owned control"); + assert.equal(byName["system-font"].inlineFont, 'Tahoma, "Segoe UI", sans-serif'); + assert.equal(byName["after-dispose"].inlineBackground, late.inlineBackground, "a disposed binding kept observing the document"); +});