diff --git a/tests/browser/theme.test.js b/tests/browser/theme.test.js index 14804622f..4a8cfe495 100644 --- a/tests/browser/theme.test.js +++ b/tests/browser/theme.test.js @@ -30,6 +30,7 @@ const sheets = Object.fromEntries( components.map((name) => [name, stripCssComments(read(`web/${name}`))]), ); const theme = stripCssComments(read("web/theme.css")); +const recordingCss = stripCssComments(read("web/recording/recording.css")); /// Every CSS named color (CSS Color 4, 148 names). A system color such as /// Highlight is left out: it is the platform's, and forced-colors rules need it. @@ -59,6 +60,25 @@ const CSS_NAMED_COLORS = ` .trim() .split(/\s+/); +const colorFunction = + /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(|#[0-9a-f]{3,8}\b/i; +const namedColor = new RegExp(`\\b(?:${CSS_NAMED_COLORS.join("|")})\\b`, "i"); + +/// The declarations in a sheet that paint a color literal. Declarations, not +/// lines: a value wrapped onto the lines after its property is still read, and +/// a selector such as `#face-check` is not a hex color. +const paintedColors = (name, css) => + [...css.matchAll(/\{([^{}]*)\}/g)].flatMap(([, body]) => + body.split(";").flatMap((declaration) => { + const [, property, value] = + declaration.match(/^\s*([\w-]+)\s*:([\s\S]*)$/) ?? []; + // A mask paints coverage, not color: its stops are alpha only. + if (!property || /^(?:-webkit-)?mask$/.test(property)) return []; + if (!colorFunction.test(value) && !namedColor.test(value)) return []; + return [`${name}: ${property}:${value.replace(/\s+/g, " ")}`]; + }), + ); + /// The stylesheets a page links, in order, whatever order a writes its /// attributes in. const linkedSheets = (html) => @@ -100,26 +120,43 @@ test("the shipped override file declares nothing", () => { }); test("component stylesheets paint no color a theme cannot override", () => { - const colorFunction = - /\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(|#[0-9a-f]{3,8}\b/i; - const namedColor = new RegExp(`\\b(?:${CSS_NAMED_COLORS.join("|")})\\b`, "i"); - // Declarations, not lines: a value wrapped onto the lines after its property - // is still read, and a selector such as `#face-check` is not a hex color. + for (const [name, css] of Object.entries(sheets)) { + assert.deepEqual(paintedColors(name, css), [], `${name} paints colors`); + } +}); + +test("a declaration does not shadow a token read in the same rule", () => { + // A later declaration must not shadow a token read by an earlier declaration. for (const [name, css] of Object.entries(sheets)) { for (const [, body] of css.matchAll(/\{([^{}]*)\}/g)) { + const properties = new Map(); for (const declaration of body.split(";")) { const [, property, value] = declaration.match(/^\s*([\w-]+)\s*:([\s\S]*)$/) ?? []; - // A mask paints coverage, not color: its stops are alpha only. - if (!property || /^(?:-webkit-)?mask$/.test(property)) continue; - const where = `${name}: ${property}:${value.replace(/\s+/g, " ")}`; - assert.doesNotMatch(value, colorFunction, where); - assert.doesNotMatch(value, namedColor, where); + if (!property) continue; + const hasVar = value.includes("var("); + if (properties.has(property) && (properties.get(property) || hasVar)) { + assert.fail( + `${name} declares ${property} twice in one rule, one of them a token read: ${value.trim()}`, + ); + } + properties.set(property, hasVar); } } } }); +test("recording.css confines color literals to its own :root", () => { + const recordingRest = recordingCss.replace(/:root\s*\{[^{}]*\}/, (block) => + block.replace(/--[\w-]+\s*:[^;]*;/g, ""), + ); + assert.deepEqual( + paintedColors("recording.css", recordingRest), + [], + "recording.css paints colors outside variables", + ); +}); + test("a component sheet neither redeclares a theme role nor reads an undeclared one", () => { const themed = declared(theme); // peer.css sits on top of styles.css, so a component may read a layout diff --git a/web/recording/recording.css b/web/recording/recording.css index e19c7711e..11e64d314 100644 --- a/web/recording/recording.css +++ b/web/recording/recording.css @@ -10,6 +10,10 @@ --edge: #232b34; --pass: #3fb950; --fail: #f85149; + --page: #0d1117; + --paper: #ffffff; + --video-fill: #000; + --avatar-fallback: #33332c; } * { @@ -25,7 +29,7 @@ body { margin: 0; - background: #0d1117; + background: var(--page); color: var(--ink); font: 16px/1.5 ui-monospace, @@ -64,7 +68,7 @@ body { display: block; width: 100%; aspect-ratio: 1600 / 1000; - background: #ffffff; + background: var(--paper); border: 1px solid var(--edge); border-radius: 8px; } @@ -125,7 +129,7 @@ body { .candidate { width: 100%; height: 100%; - background: #000; + background: var(--video-fill); border: 1px solid var(--edge); border-radius: 8px; object-fit: cover; @@ -171,7 +175,7 @@ body { width: 48px; height: 48px; border-radius: 999px; - background: #33332c; + background: var(--avatar-fallback); color: var(--ink); font-weight: 700; }