From 0df25e4892b9a3e1fdec8e9aa76642fb2aa239e9 Mon Sep 17 00:00:00 2001 From: OddTurtleHead <355182507+OddTurtleHead@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:04:25 +0800 Subject: [PATCH] fix: close review gaps in host-control fonts, on-accent contrast and protected filter values - Host-owned controls refuse font tokens naming the compiler's scoped skin font prefix (ws-), so a skin @font-face gated behind a non-matching @media (absent from document.fonts) cannot blank labels after a resize; the theme binding also re-reads tokens on viewport resize for @media-scoped tokens. - A token palette is accepted only if black or white text reaches 4.5:1 on its accent; mid-tone accents (~4.23:1) now fall back as a group. - Protected app-DOM controls reject var()/attr() for filter, clip and content-visibility, which could smuggle opacity(0)/rect(0,0,0,0)/hidden past the literal hide checks; literal filters and other parts are unchanged. - Docs and tests updated (unit + headless-Chrome fixture). --- docs/SKIN_DEVELOPMENT.md | 12 +++++----- src/shared/skin-css.test.ts | 14 +++++++++++ src/shared/skin-css.ts | 20 ++++++++++++---- web/src/services/host-control-tokens.test.ts | 16 +++++++++++++ web/src/services/host-control-tokens.ts | 25 ++++++++++++++++---- web/test/fixtures/host-control-theme.html | 16 +++++++++++++ web/test/host-chrome-recovery.test.mjs | 4 ++++ 7 files changed, 92 insertions(+), 15 deletions(-) diff --git a/docs/SKIN_DEVELOPMENT.md b/docs/SKIN_DEVELOPMENT.md index 62e3b26..10289e5 100644 --- a/docs/SKIN_DEVELOPMENT.md +++ b/docs/SKIN_DEVELOPMENT.md @@ -514,7 +514,7 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动 28 个受保护部件里,`host.recovery`、`app.skin-load-recovery` 和授权对话框由宿主在闭合 Shadow DOM 中绘制,`host.permission` 目前没有渲染点;其余 25 个都渲染在 `.ws-skin-root` 内(服务器/频道密码弹窗也在根内,不是 `Teleport`)。开放皮肤(schemaVersion 3/4)可以直接用 `skin.css` 给它们换肤;宿主只保留「不能隐藏 / 不能移出视口 / 不能禁用 / 使用内置皮肤的路径不能被切断」四条底线。 -**应用 DOM 内的受保护控件。** 颜色、`var()` 颜色、纯色/渐变/包内位图背景、边框、圆角、`font-family`/`font-size`、内边距、宽高、`box-shadow`、`text-shadow`、`filter`、`outline` 等纯视觉属性都可以写: +**应用 DOM 内的受保护控件。** 颜色、`var()` 颜色、纯色/渐变/包内位图背景、边框、圆角、`font-family`/`font-size`、内边距、宽高、`box-shadow`、`text-shadow`、`filter`(字面值)、`outline` 等纯视觉属性都可以写: ```css [data-ws-part="voice.audio-dock.microphone"], @@ -541,7 +541,7 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动 | 控件 | 每部件前缀 | 说明 | | --- | --- | --- | | `host.recovery` | `--skin-recovery-*` | 兼容原有令牌;写到锚点、锚点祖先或皮肤根上都有效 | -| `app.skin-load-recovery` | `--skin-load-recovery-*` | 皮肤加载失败时出现的顶部横幅;只换肤,不参与停靠 | +| `app.skin-load-recovery` | `--skin-load-recovery-*` | 皮肤加载失败时出现的顶部横幅;只换肤,不参与停靠。令牌取自横幅出现时的**活动**皮肤:加载失败后宿主会退回内置皮肤(内置皮肤不声明令牌),所以失败皮肤自己的值通常不会作用在自己的横幅上,横幅保持默认琥珀色 | | 授权对话框(`host.permission` 概念) | `--skin-permission-*` | `host.permission` 没有 DOM 渲染点;令牌作用于宿主自己的授权对话框 | 每个每部件令牌都有全局回退:`--skin-host-accent`、`--skin-host-background`、`--skin-host-foreground`、`--skin-host-border`、`--skin-host-radius`、`--skin-host-font`、`--skin-host-size`。每部件值优先于全局值。例如: @@ -561,15 +561,15 @@ Wasm 每轮最多读取一次输入并提交一个 UI 输出;首版宿主动 } ``` -宿主从计算样式读取这些变量,通过影子根内的探针解析颜色(`oklch()`、`color-mix()` 等现代写法会先换算成 sRGB 再检查对比度),再写入控件自己的 `--ws-control-*` 变量;皮肤样式晚于皮肤 ID 加载、运行时插件追加样式时,宿主会自动重新读取,不需要刷新页面。皮肤不能写入 Shadow DOM 内部,也不能替换结构令牌。安全底线与其他受保护控件一致: +宿主从计算样式读取这些变量,通过影子根内的探针解析颜色(`oklch()`、`color-mix()` 等现代写法会先换算成 sRGB 再检查对比度),再写入控件自己的 `--ws-control-*` 变量;皮肤样式晚于皮肤 ID 加载、运行时插件追加样式、窗口尺寸变化让 `@media` 里的令牌生效时,宿主会自动重新读取,不需要刷新页面。皮肤不能写入 Shadow DOM 内部,也不能替换结构令牌。安全底线与其他受保护控件一致: - 背景强制不透明(alpha 视为 1)。 - 文字与背景对比度必须 ≥ 4.5,强调色与背景 ≥ 3;任一不达标时整组颜色回退到宿主默认,而不是只改一项。 - 边框与背景对比度 ≥ 3,否则使用宿主默认边框。 - 圆角只接受 px 并限制上限(恢复胶囊 999px,横幅和授权对话框 64px)。 -- 字体族只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符;只认系统已安装字体和通用族名(`sans-serif` 等)。列表里只要有一个族名来自文档里的 `@font-face`(也就是皮肤自带字体),整项令牌就被忽略,因为自带字体可以把字形做成空白,让宿主控件的文字消失。应用 DOM 内的受保护控件不受此限制,照常可以在 `skin.css` 里用皮肤字体。 +- 字体族只允许字母、数字、空格、逗号、引号和连字符,最长 120 字符;只认系统已安装字体和通用族名(`sans-serif` 等)。列表里只要有一个族名来自文档里的 `@font-face`,或带有编译器给皮肤字体加的 `ws-` 作用域前缀(包括放在暂未生效的 `@media` 里的字体),整项令牌就被忽略,因为自带字体可以把字形做成空白,让宿主控件的文字消失。应用 DOM 内的受保护控件不受此限制,照常可以在 `skin.css` 里用皮肤字体。 - `size` 只接受 `small`、`medium`、`large`,并且只能把命中区域/字号调大;恢复胶囊为 28/32/36px 命中区域、11/12/13px 字号,横幅和授权对话框以自身更大的内置尺寸为下限。 -- 授权对话框的主按钮文字色由宿主根据强调色自动选择黑/白,保证可读。 +- 授权对话框的主按钮文字色由宿主根据强调色自动选择黑/白;如果黑白两种文字在强调色上都达不到 4.5,这组颜色同样整体回退到宿主默认。 - 恢复胶囊停靠后如果锚点消失、被遮挡或压到其他受保护控件,仍会回退到默认位置;横幅固定在顶部,授权对话框始终是顶层模态框,二者不参与停靠,因此不会因为皮肤布局而找不到。 `Ctrl+Alt+Shift+R`(macOS 为 `⌘+⌥+⇧+R`)和「使用内置皮肤」的重置路径完全不受令牌影响。旧皮肤继续写 `--skin-recovery-*` 也能工作;新皮肤建议优先使用全局 `--skin-host-*`,只在需要区分具体控件时写每部件覆盖。 @@ -731,7 +731,7 @@ v1/v2 校验器会阻止隐藏必要操作控件和其容器。v3 可改变显 上面必须使用单独的精确属性选择器;不能通过父容器、组合选择器或子元素间接隐藏/重置内容。`host.watermark`、`host.hint`、`host.voice-status-decoration` 默认不渲染,`chrome.json#visibleParts` 才是它们的开启开关。 -关键帧与普通声明遵循同一套受保护控件检查,但只检查动画能够**停留**的帧:`to` / `100%` 始终检查;`from` / `0%` 仅在引用它的声明(含同一主体上另一条规则的方向 / 填充 / 播放状态长属性)使用 `reverse`、`alternate`、`alternate-reverse` 方向、`backwards` / `both` 填充模式或 `paused` 播放状态时检查;转义的 `to` / `from` 与 `1e2%` 这类写法按停留帧识别;`50%` 等中间帧不会因为过渡状态被拒绝,缺少 `to` / `100%` 时结束状态回到元素自身样式(`paused` 配合负延迟、或超长动画停在中间帧属于已知残留,由闭合 Shadow DOM 的恢复胶囊兜底)。受保护控件上被拒绝的写法包括 `display: none`、`visibility: hidden|collapse`、`opacity` ≤ 0.01(含 `0`、`0%`、`0e0`、`calc(0)` 等)、`pointer-events: none`、`filter` / `-webkit-filter` 的 `opacity(≤0.01)` / `brightness(0)`、`transform` / `scale` 的零缩放、`content-visibility: hidden`、`clip-path` 的 `inset(...)`(任一边对合计 ≥ 100%)、`circle(0)` / `ellipse(0 …)`、`clip: rect(0,0,0,0)`,以及 `mask` / `mask-image` 的 `alpha(0)` / `luminance(0)` 或全透明渐变色标;`polygon()` 等装饰性裁切不受限制。受保护主体的判定会展开 `:is()` / `:where()` 参数、双重否定 `:not(:not(X))` 与 `:nth-child(An+B of S)` / `:nth-last-child(...)` 的 `S`,`[data-ws-part="…" i]` 按不区分大小写匹配。把控件移出视口只在明显越界时拒绝:负向 ≤ -1000px 或 ≤ -100%,正向 ≥ 2000px 或 ≥ 200%(`margin` / `inset` / `left` / `right` / `top` / `bottom` / `translate` / `transform`,含简单 `calc()` 与 `matrix()` 平移);`scale`、`rotate` 和小幅平移不受影响。`host.plugins.overlay` 的布局锁在关键帧中复用普通声明的属性锁:停留帧里的定位、变换等锁定属性只能取中性或单位变换(`none`、`auto`、`static`、`0`、`scale(1)`、`translateY(0)`、`rotate(0deg)` 或 CSS 全局关键字),尺寸属性仅在达到视口尺寸时拒绝(含 `calc(50vw * 2)`);入场动画停在这些值时通过。受保护控件和 `host.plugins.overlay` 上的 `animation` / `animation-name` 也不能用 `var()` / `attr()` 间接指定动画名。只被可选/装饰部件引用的关键帧仍可隐藏它们,v3 对非受保护部件的隐藏只给出警告。校验器是恢复路径的最后一道安全网,不是布局或视觉质量检查;自定义字体、复杂背景和高对比特效仍需人工检查文字可读性与控件辨识度。 +关键帧与普通声明遵循同一套受保护控件检查,但只检查动画能够**停留**的帧:`to` / `100%` 始终检查;`from` / `0%` 仅在引用它的声明(含同一主体上另一条规则的方向 / 填充 / 播放状态长属性)使用 `reverse`、`alternate`、`alternate-reverse` 方向、`backwards` / `both` 填充模式或 `paused` 播放状态时检查;转义的 `to` / `from` 与 `1e2%` 这类写法按停留帧识别;`50%` 等中间帧不会因为过渡状态被拒绝,缺少 `to` / `100%` 时结束状态回到元素自身样式(`paused` 配合负延迟、或超长动画停在中间帧属于已知残留,由闭合 Shadow DOM 的恢复胶囊兜底)。受保护控件上被拒绝的写法包括 `display: none`、`visibility: hidden|collapse`、`opacity` ≤ 0.01(含 `0`、`0%`、`0e0`、`calc(0)` 等)、`pointer-events: none`、`filter` / `-webkit-filter` 的 `opacity(≤0.01)` / `brightness(0)`、`transform` / `scale` 的零缩放、`content-visibility: hidden`、`clip-path` 的 `inset(...)`(任一边对合计 ≥ 100%)、`circle(0)` / `ellipse(0 …)`、`clip: rect(0,0,0,0)`,以及 `mask` / `mask-image` 的 `alpha(0)` / `luminance(0)` 或全透明渐变色标;`polygon()` 等装饰性裁切不受限制。受保护主体的判定会展开 `:is()` / `:where()` 参数、双重否定 `:not(:not(X))` 与 `:nth-child(An+B of S)` / `:nth-last-child(...)` 的 `S`,`[data-ws-part="…" i]` 按不区分大小写匹配。把控件移出视口只在明显越界时拒绝:负向 ≤ -1000px 或 ≤ -100%,正向 ≥ 2000px 或 ≥ 200%(`margin` / `inset` / `left` / `right` / `top` / `bottom` / `translate` / `transform`,含简单 `calc()` 与 `matrix()` 平移);`scale`、`rotate` 和小幅平移不受影响。`host.plugins.overlay` 的布局锁在关键帧中复用普通声明的属性锁:停留帧里的定位、变换等锁定属性只能取中性或单位变换(`none`、`auto`、`static`、`0`、`scale(1)`、`translateY(0)`、`rotate(0deg)` 或 CSS 全局关键字),尺寸属性仅在达到视口尺寸时拒绝(含 `calc(50vw * 2)`);入场动画停在这些值时通过。受保护控件和 `host.plugins.overlay` 上的 `animation` / `animation-name` 也不能用 `var()` / `attr()` 间接指定动画名;受保护控件上的 `filter` / `-webkit-filter`、`clip` 与 `content-visibility` 同样只接受字面值,不能用 `var()` / `attr()`(否则变量可以绕过上面的隐藏检查),其他部件不受影响。只被可选/装饰部件引用的关键帧仍可隐藏它们,v3 对非受保护部件的隐藏只给出警告。校验器是恢复路径的最后一道安全网,不是布局或视觉质量检查;自定义字体、复杂背景和高对比特效仍需人工检查文字可读性与控件辨识度。 皮肤可以重画控制外观。v1/v2 不能重新布置必要操作;v3 可以,因此须人工实测加入/退出、麦克风状态、设置、菜单、屏幕共享退出、音量、全屏等操作的可达性。v3 也不能用大的负 `margin*`、`inset`/`translate` 等把受保护控件移出视口,或把 `host.plugins.overlay` 及其后代改成定位全屏、放大到覆盖视口(`zoom` 等);这些写法会在导入时被拒绝。装饰性宿主外壳(`host.watermark`、`host.hint`、`host.voice-status-decoration`、`host.plugins.launcher`)和普通页面部件不受布局锁限制,可以自由使用 flex/grid、重定位、自定义尺寸、大背景和动画;固定定位或极端 `z-index` 只会给出提示。开放皮肤(v3/v4)的布局与视觉质量由官方库的人工审核保证,编译器不会阻止一般性的布局回归。校验成功不代表设计质量合格;提交前要逐页实测窄屏和桌面尺寸、所有界面语言、键盘焦点与减少动效设置。 diff --git a/src/shared/skin-css.test.ts b/src/shared/skin-css.test.ts index 0c5d251..6943b80 100644 --- a/src/shared/skin-css.test.ts +++ b/src/shared/skin-css.test.ts @@ -386,11 +386,25 @@ test("protected controls accept every purely visual property", () => { '[data-ws-part="voice.audio-dock.microphone"] { clip-path: inset(100%); }', '[data-ws-part="voice.audio-dock.microphone"] { transform: translateY(-9999px); }', ':root { --skin-pull: -9999px; } [data-ws-part="voice.audio-dock.microphone"] { margin-left: var(--skin-pull); }', + // Literal filter/clip/content-visibility hiding is rejected, so a variable + // must not smuggle the same value past the check. + ':root { --skin-x: opacity(0); } [data-ws-part="voice.audio-dock.microphone"] { filter: var(--skin-x); }', + ':root { --skin-x: opacity(0); } [data-ws-part="voice.audio-dock.microphone"] { -webkit-filter: var(--skin-x); }', + ':root { --skin-x: rect(0, 0, 0, 0); } [data-ws-part="voice.audio-dock.microphone"] { clip: var(--skin-x); }', + ':root { --skin-x: hidden; } [data-ws-part="voice.audio-dock.microphone"] { content-visibility: var(--skin-x); }', ]) { assert.throws(() => compile(css), /must remain available|must remain on screen|cannot use indirect values/, css); } }); +test("indirect filter values stay free outside protected controls", () => { + for (const version of [2, 3] as const) { + assert.doesNotThrow(() => compileSkinCss(':root { --skin-glow: drop-shadow(0 0 4px #0ff); } [data-ws-part="voice.header"] { filter: var(--skin-glow); }', "demo-skin", new Set(), version)); + } + // Literal visual filters remain allowed on protected controls. + assert.doesNotThrow(() => compile('[data-ws-part="voice.audio-dock.microphone"] { filter: drop-shadow(0 0 4px #0ff) saturate(1.2); }')); +}); + test("host chrome cannot be restyled into a viewport-sized overlay", () => { assert.throws( () => compile('[data-ws-part="host.plugins.overlay"] { position: fixed; inset: 0; z-index: 2147483647; }'), diff --git a/src/shared/skin-css.ts b/src/shared/skin-css.ts index 363fc9e..384a49d 100644 --- a/src/shared/skin-css.ts +++ b/src/shared/skin-css.ts @@ -84,6 +84,14 @@ const INDIRECT_VALUE_PROPERTIES = new Set([ "margin-block", "margin-block-start", "margin-block-end", "margin-inline", "margin-inline-start", "margin-inline-end", ]); +/** + * Visual properties whose literal hiding values (`filter: opacity(0)`, + * `clip: rect(0 0 0 0)`, `content-visibility: hidden`) are rejected on + * protected controls. They stay free on every other part, but a protected + * control may not take them from `var()`/`attr()`, which would hide the + * same value from the static check. + */ +const PROTECTED_INDIRECT_HIDE_PROPERTIES = new Set(["filter", "clip", "content-visibility"]); /** Attributes a skin author must never use to reach outside the public hooks. */ const FORBIDDEN_SCOPE_ATTRIBUTES = new Set([ HOST_SKIN_ATTRIBUTE, @@ -319,11 +327,13 @@ export function compileSkinCss(source: string, id: string, assets: SkinCssAssets if (["behavior", "-moz-binding"].includes(property) || /expression\s*\(|javascript\s*:|vbscript\s*:/i.test(decodedValue)) { throw declaration.error("Executable CSS values are not allowed."); } - if (INDIRECT_VALUE_PROPERTIES.has(unprefixedProperty) && restingFrame && /\b(?:var|attr)\s*\(/i.test(decodedValue)) { - if (protectedTarget) { - throw declaration.error(`The required control "${protectedTarget}" cannot use indirect values for visibility or positioning.`); - } - if (!canHideTarget && schemaVersion < 3) throw declaration.error("Visibility and interaction styles can only use indirect values on explicitly optional visual parts."); + const indirectValue = restingFrame && /\b(?:var|attr)\s*\(/i.test(decodedValue); + if (indirectValue && protectedTarget + && (INDIRECT_VALUE_PROPERTIES.has(unprefixedProperty) || PROTECTED_INDIRECT_HIDE_PROPERTIES.has(unprefixedProperty))) { + throw declaration.error(`The required control "${protectedTarget}" cannot use indirect values for visibility or positioning.`); + } + if (indirectValue && !protectedTarget && INDIRECT_VALUE_PROPERTIES.has(unprefixedProperty) && !canHideTarget && schemaVersion < 3) { + throw declaration.error("Visibility and interaction styles can only use indirect values on explicitly optional visual parts."); } // An indirect animation name is checked at the declaration site. Locked // host chrome (the plugin overlay) uses the same rule, otherwise a diff --git a/web/src/services/host-control-tokens.test.ts b/web/src/services/host-control-tokens.test.ts index 83cbe67..aa4b3f8 100644 --- a/web/src/services/host-control-tokens.test.ts +++ b/web/src/services/host-control-tokens.test.ts @@ -15,6 +15,7 @@ import { applyHostControlTokens, contrastRatio, fontFamilyNames, + formatRgb, hasHostControlTokens, hostControlTokenKey, hostControlTokenProperty, @@ -194,6 +195,17 @@ test("global tokens theme the banner and the authorization dialog", () => { assert.ok(contrastRatio(parseCssColor(dialog.onAccent)!, parseCssColor(dialog.accent)!) >= 4.5); assert.equal(dialog.hitTarget, 46); + // A mid-grey accent passes 3:1 on white, but neither black nor white text + // reaches 4.5:1 on it (best is ~4.23), so the palette falls back as a group. + const midTone = resolveHostControlTokens( + style(prefixed("host", { background: "#ffffff", foreground: "#000000", accent: "#7b7b7b" })), + "host.permission", + raw => raw, + PERMISSION_CONTROL_DEFAULTS, + ); + assert.equal(midTone.accent, formatRgb(PERMISSION_CONTROL_DEFAULTS.accent)); + assert.ok(contrastRatio(parseCssColor(midTone.onAccent)!, parseCssColor(midTone.accent)!) >= 4.5); + const override = resolveHostControlTokens( style({ ...prefixed("host", { background: "#102030", foreground: "#ffffff", accent: "#47dcc4" }), ...prefixed("load-recovery", { background: "#ffffff", foreground: "#111111", accent: "#1d4ed8" }) }), "app.skin-load-recovery", @@ -264,4 +276,8 @@ test("host controls ignore fonts registered by a skin @font-face", () => { assert.equal(resolve('"Blank Glyphs", sans-serif'), SKIN_LOAD_RECOVERY_CONTROL_DEFAULTS.font); assert.equal(resolve("Tahoma, 'blank glyphs'"), SKIN_LOAD_RECOVERY_CONTROL_DEFAULTS.font); assert.equal(resolve('Tahoma, "Segoe UI", sans-serif'), 'Tahoma, "Segoe UI", sans-serif'); + // Compiled skin faces are always `ws--`; a face gated behind a + // non-matching @media is not in document.fonts yet but must still be refused. + assert.equal(resolve('"ws-community-evil-blank", sans-serif'), SKIN_LOAD_RECOVERY_CONTROL_DEFAULTS.font); + assert.equal(resolve("Tahoma, ws-community-evil-blank"), SKIN_LOAD_RECOVERY_CONTROL_DEFAULTS.font); }); diff --git a/web/src/services/host-control-tokens.ts b/web/src/services/host-control-tokens.ts index 71b9fbf..f4297ca 100644 --- a/web/src/services/host-control-tokens.ts +++ b/web/src/services/host-control-tokens.ts @@ -147,6 +147,14 @@ const ON_ACCENT_DARK: RgbColor = { r: 17, g: 24, b: 39 }; const MAX_RADIUS = 999; const MAX_FONT_LENGTH = 120; const FONT_FAMILY_PATTERN = /^[A-Za-z0-9 ,'"-]{1,120}$/; +/** + * The skin compiler renames every skin/plugin `@font-face` family to + * `ws-[-]-` (see `compileSkinCss` in + * src/shared/skin-css.ts), so that prefix identifies a skin font even when the + * face is not in `document.fonts` yet (for example behind a non-matching + * `@media` that activates after the token was accepted). + */ +const SCOPED_SKIN_FONT_PREFIX = "ws-"; /** Defaults for the recovery capsule: white surface, teal text, amber accent. */ export const RECOVERY_CONTROL_DEFAULTS: HostControlDefaults = { @@ -281,7 +289,7 @@ function sanitizeFont(value: string | undefined, fallback: string, isWebFont: We } // A skin-supplied @font-face can ship glyph-less outlines and blank the // control's label, so host-owned controls only accept installed/generic fonts. - if (fontFamilyNames(font).some(isWebFont)) return fallback; + if (fontFamilyNames(font).some((name) => name.startsWith(SCOPED_SKIN_FONT_PREFIX) || isWebFont(name))) return fallback; return font; } @@ -334,7 +342,10 @@ function resolvePalette( function paletteReadable(palette: { accent: RgbColor; background: RgbColor; foreground: RgbColor }): boolean { return contrastRatio(palette.foreground, palette.background) >= HOST_CONTROL_MIN_FOREGROUND_CONTRAST - && contrastRatio(palette.accent, palette.background) >= HOST_CONTROL_MIN_ACCENT_CONTRAST; + && contrastRatio(palette.accent, palette.background) >= HOST_CONTROL_MIN_ACCENT_CONTRAST + // Primary actions put host-chosen text on the accent; mid-tone accents + // where neither black nor white reaches the text floor are rejected. + && contrastRatio(readableOnAccent(palette.accent), palette.accent) >= HOST_CONTROL_MIN_FOREGROUND_CONTRAST; } function resolveBorder( @@ -373,7 +384,8 @@ export function hasHostControlTokens( * unreadable, the resolver retries the global layer and finally the built-in * palette, so a skin can never make a protected control unreadable. The * border is optional and must stay visible against the resolved background. - * Fonts registered by the document (skin `@font-face`) are ignored. + * Fonts registered by the document (skin `@font-face`) and names carrying the + * compiler's scoped skin-font prefix (`ws-`) are ignored. */ export function resolveHostControlTokens( style: Pick | null, @@ -463,7 +475,8 @@ export interface HostControlThemeBinding { * side. Callers re-apply it when the active skin or the control changes; the * binding also re-applies itself when stylesheets in `` or the document * root attributes change, because a stored skin id is known before its CSS - * (and therefore its tokens) has been injected. + * (and therefore its tokens) has been injected, and on viewport resize, because + * tokens may be declared inside `@media`. */ export function bindHostControlTheme(options: HostControlThemeBindingOptions): HostControlThemeBinding { let probe: HostControlProbe | null = null; @@ -478,6 +491,9 @@ export function bindHostControlTheme(options: HostControlThemeBindingOptions): H observer.observe(doc.head, { childList: true, subtree: true, characterData: true }); observer.observe(doc.documentElement, { attributes: true }); } + // Tokens declared inside `@media` change with the viewport, not the DOM. + const onResize = (): void => binding.apply(); + win?.addEventListener("resize", onResize, { passive: true }); const binding: HostControlThemeBinding = { apply() { @@ -505,6 +521,7 @@ export function bindHostControlTheme(options: HostControlThemeBindingOptions): H dispose() { observer?.disconnect(); observer = null; + win?.removeEventListener("resize", onResize); probe?.dispose(); probe = null; }, diff --git a/web/test/fixtures/host-control-theme.html b/web/test/fixtures/host-control-theme.html index 3d8e992..1e500f4 100644 --- a/web/test/fixtures/host-control-theme.html +++ b/web/test/fixtures/host-control-theme.html @@ -61,6 +61,22 @@ const addSkinStyle = (css) => { await frames(); results.push(snap("system-font")); + // A compiled skin face behind a non-matching @media is absent from + // document.fonts; its scoped `ws-` name must still be refused. + addSkinStyle(`@media (max-width: 1px) { @font-face { font-family: "ws-community-theme-fixture-blank"; src: url(data:font/woff2;base64,AAAA); } } + .ws-skin-root { --skin-host-font: "ws-community-theme-fixture-blank", sans-serif; }`); + await frames(); + results.push(snap("media-gated-skin-font")); + + // Tokens declared inside @media follow the viewport without a DOM change. + const viewportStyle = addSkinStyle(`@media (min-width: 1px) { .ws-skin-root { --skin-host-background: #102030; } }`); + await frames(); + const beforeResize = control.style.getPropertyValue("--ws-control-background"); + viewportStyle.sheet.cssRules[0].media.mediaText = "(max-width: 1px)"; + window.dispatchEvent(new Event("resize")); + await frames(); + results.push({ ...snap("media-token-resize"), beforeResize }); + theme.dispose(); addSkinStyle(`.ws-skin-root { --skin-host-background: #ffffff; --skin-host-foreground: #111111; }`); await frames(); diff --git a/web/test/host-chrome-recovery.test.mjs b/web/test/host-chrome-recovery.test.mjs index 2add711..f1ac1ee 100644 --- a/web/test/host-chrome-recovery.test.mjs +++ b/web/test/host-chrome-recovery.test.mjs @@ -331,5 +331,9 @@ test("host control tokens follow late skin CSS, resolve modern colours and refus 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["media-gated-skin-font"].inlineFont, "ui-sans-serif, system-ui, sans-serif", "a media-gated skin @font-face reached a host-owned control"); + const resized = byName["media-token-resize"]; + assert.equal(resized.beforeResize, "rgb(16, 32, 48)", "an @media token was not applied"); + assert.notEqual(resized.inlineBackground, resized.beforeResize, "a viewport change did not refresh @media tokens"); assert.equal(byName["after-dispose"].inlineBackground, late.inlineBackground, "a disposed binding kept observing the document"); });