Files
apes-sidebar/tests/css-block-scope.spec.ts
T
m4ku1337 c0e6851962 fix(menu): close the reduced-motion media query that swallowed the submenu flip (#955)
layout.css opens @media (prefers-reduced-motion: reduce) at line 77 and never
closes it. 36fbc5a deleted the run of 61 lines that started with that brace and
ran to the end of the file (0 insertions, 61 deletions), and b011be1 added the
context-menu submenu rules back below the still-open block (60 insertions). The
file has been one brace short ever since: 13 '{' against 12 '}'.

Everything from line 78 on is therefore a descendant of the media query. The
only rules in that range are the media query's intended child — the center
column's 'transition: none' — and the six rules that consume the
data-dsh-sidebar-submenu attribute menu-flip.ts publishes on <body>: the flip
that keeps a nested submenu inside the viewport, and the visual tether (parent
row highlight, 4px gap, 4px hover corridor). In the default theme none of them
matched, so our "open with" submenu grew past the right edge of a right-docked
workbench; reduce-motion users were the only ones who ever got the flip (#955).

Restoring the brace puts all six back at top level. No rule text changes: the
result is byte-identical to the pre-regression revision 36fbc5a^ (7219 bytes).

tests/css-block-scope.spec.ts pins the property that hid this: menu-flip.spec.tsx
asserts with toContain, which matches selector text wherever it sits, so the
nesting stayed green in every released version from v0.24.1 through v0.26.1, and
on dev (0.27.0) where it still is. The new spec reads block structure instead:
every stylesheet under src/ has to close what it opens, and the six flip/tether
selectors have to open at depth 0. Run against the historical revisions, the spec is red on every
revision that shipped the nesting — v0.24.1 through v0.26.1 and dev — where it
reports "1 unclosed block(s)" and names the flip rules as nested. 36fbc5a is the
commit that lost both the brace and the rules, and it deleted menu-flip.ts with
them, so the spec cannot even load there; with that module stubbed back in, both
assertions fail on its CSS. On 36fbc5a^ and on this branch the spec passes.

Verified on this branch: pnpm exec vitest run tests/css-block-scope.spec.ts
tests/menu-flip.spec.tsx -> 11 passed; pnpm typecheck, pnpm lint clean.
2026-10-17 03:42:23 +08:00

197 lines
7.8 KiB
TypeScript

/**
* Block-scope guard for the client stylesheets.
*
* layout.css shipped from 36fbc5a up to 0.27.0 with an unclosed
* @media (prefers-reduced-motion: reduce) block (issue #955): every rule after
* it — the context-menu submenu flip and the visual tether — became a
* descendant of that media query. The flip then applied only to users who had
* reduce-motion turned on; everyone else lost the flip and the tether, and the
* "open with" submenu grew past the right edge of a right-docked workbench.
*
* tests/menu-flip.spec.tsx could not see it: expect(css).toContain(...) matches
* selector text wherever it sits, and nesting does not change text. These
* assertions read the block structure instead — every client stylesheet has to
* close what it opens, and the rules the flip depends on have to stay at top
* level — so the same mistake fails here.
*/
import { readdirSync, readFileSync } from 'node:fs'
import { join, relative, resolve } from 'node:path'
import { describe, expect, it } from 'vitest'
import { SUBMENU_FLIP_ATTR } from '../src/client/menu-flip.ts'
const ROOT = resolve(import.meta.dirname, '..')
interface Block {
/** The selector (or at-rule prelude) that opened this block, normalised. */
prelude: string
/** The nesting depth the block opens at: 0 = top level. */
depth: number
/** Index of the enclosing block in the same list, or -1 at top level. */
parent: number
}
interface Scan {
blocks: Block[]
/** Blocks still open at end of file. */
unclosed: number
/** Closing braces with nothing to close. */
extraClosers: number
}
/**
* Whitespace collapsed and attribute-value quotes dropped, so a reformatted or
* re-quoted selector (div[role="menu"] vs div[role=menu]) still compares equal
* to the selector a test names — this guard is about block structure, not text.
*/
function normalize(selector: string): string {
return selector
.trim()
.replace(/\s+/g, ' ')
.replace(/\[([-\w]+)\s*([~|^$*]?=)\s*"([^"]*)"\]/g, '[$1$2$3]')
.replace(/\[([-\w]+)\s*([~|^$*]?=)\s*'([^']*)'\]/g, '[$1$2$3]')
}
/**
* Walk a stylesheet and report every block it opens, with the depth it opens at.
* Comment bodies are dropped as they are read and quoted strings are consumed
* whole — including the quotes inside a url() token — so a brace inside
* content: "}" or inside a data URI can neither open nor close a block.
*/
function scanBlocks(css: string): Scan {
const blocks: Block[] = []
const stack: number[] = []
let extraClosers = 0
let prelude = ''
let i = 0
/** The quoted string that starts at `from`. */
function quoted(from: number): string {
const quote = css.charAt(from)
let text = quote
let j = from + 1
while (j < css.length) {
const inner = css.charAt(j)
text += inner
j += 1
if (inner === '\\') { text += css.charAt(j); j += 1; continue }
if (inner === quote) break
}
i = j
return text
}
/** The url(...) token that starts at `from`, unquoted payload included. */
function urlToken(from: number): string {
let text = ''
let j = from
let parens = 0
while (j < css.length) {
const inner = css.charAt(j)
if (inner === '"' || inner === "'") { text += quoted(j); j = i; continue }
text += inner
j += 1
if (inner === '(') parens += 1
else if (inner === ')') { parens -= 1; if (parens === 0) break }
}
i = j
return text
}
while (i < css.length) {
const ch = css.charAt(i)
if (ch === '/' && css.charAt(i + 1) === '*') {
const close = css.indexOf('*/', i + 2)
i = close === -1 ? css.length : close + 2
prelude += ' '
continue
}
if (ch === '"' || ch === "'") { prelude += quoted(i); continue }
if (css.slice(i, i + 4).toLowerCase() === 'url(') { prelude += urlToken(i); continue }
if (ch === '{') {
const parent = stack.length > 0 ? (stack[stack.length - 1] ?? -1) : -1
blocks.push({ prelude: normalize(prelude), depth: stack.length, parent })
stack.push(blocks.length - 1)
prelude = ''
i += 1
continue
}
if (ch === '}') {
if (stack.length === 0) extraClosers += 1
else stack.pop()
prelude = ''
i += 1
continue
}
prelude += ch
i += 1
}
return { blocks, unclosed: stack.length, extraClosers }
}
function cssFilesUnder(dir: string): string[] {
const found: string[] = []
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const path = join(dir, entry.name)
if (entry.isDirectory()) found.push(...cssFilesUnder(path))
else if (entry.name.toLowerCase().endsWith('.css')) found.push(path)
}
return found
}
describe('client stylesheet block scope', () => {
it('closes every block it opens, in every stylesheet under src/', () => {
const files = cssFilesUnder(join(ROOT, 'src'))
// A walk that silently found nothing would make the assertion below vacuous.
expect(files.length).toBeGreaterThan(0)
const unbalanced = files
.map(path => ({ path, ...scanBlocks(readFileSync(path, 'utf8')) }))
.filter(({ unclosed, extraClosers }) => unclosed !== 0 || extraClosers !== 0)
.map(({ path, unclosed, extraClosers }) => {
const faults = [
unclosed > 0 ? unclosed + ' unclosed block(s)' : '',
extraClosers > 0 ? extraClosers + ' extra closing brace(s)' : '',
].filter(Boolean)
return relative(ROOT, path) + ': ' + faults.join(', ')
})
expect(unbalanced).toEqual([])
})
it('keeps the submenu flip and tether rules out of the reduced-motion media query', () => {
const { blocks } = scanBlocks(readFileSync(join(ROOT, 'src/client/layout.css'), 'utf8'))
// menu-flip.ts publishes the attribute on <body>; these six rules are its
// only consumers, and they are the whole mechanism that keeps OUR nested
// menus inside the viewport. Every one of them has to match in the default
// theme, not only for users who asked for reduced motion.
const ATTR = SUBMENU_FLIP_ATTR
const indexOfRule = (selector: string): number => {
const wanted = normalize(selector)
const matched = blocks.findIndex(block => block.prelude === wanted)
expect(matched, 'no rule opened by ' + wanted).toBeGreaterThanOrEqual(0)
expect(blocks.filter(block => block.prelude === wanted), 'more than one rule for ' + wanted).toHaveLength(1)
expect(blocks[matched]?.depth, wanted + ' is nested instead of top level').toBe(0)
return matched
}
const tetherGap = indexOfRule('body[' + ATTR + '] div[role="menu"] div[role="menu"]')
indexOfRule('body[' + ATTR + '] div[role="menu"] button[aria-expanded="true"]')
indexOfRule('body[' + ATTR + '] div[role="menu"] div[role="menu"]::before')
indexOfRule('body[' + ATTR + '~="down"] div[role="menu"] div[role="menu"]')
const flipLeft = indexOfRule('body[' + ATTR + '~="left"] div[role="menu"] div[role="menu"]')
indexOfRule('body[' + ATTR + '~="left"] div[role="menu"] div[role="menu"]::before')
// Same specificity, so the declared order decides: layout.css puts the
// tether BEFORE the flip overrides "so their left/right flips still win".
// Moving the tether below them silently kills the left flip.
expect(tetherGap, 'the tether has to stay declared before the flip overrides').toBeLessThan(flipLeft)
// Only the center-column override belongs inside the reduced-motion block.
// A rule that follows a block missing its closing brace lands here as an
// extra child — which is exactly how #955 shipped, silently and unseen.
const media = blocks.findIndex(block => block.prelude === '@media (prefers-reduced-motion: reduce)')
expect(media, 'the reduced-motion media query is gone').toBeGreaterThanOrEqual(0)
const insideMedia = blocks.filter(block => block.parent === media).map(block => block.prelude)
expect(insideMedia).toEqual(['#root [data-dsh-center-col]'])
})
})