Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions tests/browser/theme.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const sheets = Object.fromEntries(
components.map((name) => [name, stripCssComments(read(`web/${name}`))]),
);
const theme = stripCssComments(read("web/theme.css"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the redeclaration test removed, nothing in this file reads theme any more, so the eslint gate in scripts/test.sh fails here with no-unused-vars ('theme' is assigned a value but never used). Nothing at this head checks theme.css for unused tokens either, although the reply on this PR says the test now does. Restoring the test that compares each component sheet's declared(css) against declared(theme) reads it again; if that test stays out, delete this line.

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.
Expand Down Expand Up @@ -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 <link> writes its
/// attributes in.
const linkedSheets = (html) =>
Expand Down Expand Up @@ -100,26 +120,43 @@ test("the shipped override file declares nothing", () => {
});

test("component stylesheets paint no color a theme cannot override", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replacement removes the existing checks for component-level theme-role redeclarations and undeclared token references. With the helper scope repaired, .card { color: var(--missing-role); } and .card { --ink: var(--muted); color: var(--ink); } both pass the remaining tests in this file, while the original test rejects them. Restore the original declaration/reference test alongside the new checks.

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`);
}
});
Comment on lines +122 to 126

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a second test with the same name as the one just above it, and it runs the same check on the same sheets: the first test walks every declaration with assert.doesNotMatch against colorFunction and namedColor, and paintedColors repeats that loop. Once the helper is at module scope, have the existing test assert paintedColors(name, css) is empty and delete this copy, so the rule lives in one place.

Comment on lines 122 to +126

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing at this head defines paintedColors, colorFunction or namedColor, so this test throws ReferenceError: paintedColors is not defined, and styles.css, task.css and peer.css lose the color check they pass on main; there is no helper left to move to module scope, so it has to be written back. The version below keeps the mask exemption, which the #000 stop in the -webkit-mask and mask declarations of styles.css needs, along with the two comments that explained the parsing. With it this test and the recording.css one pass, and the recording.css test still fails on main's four hex backgrounds.

Suggested change
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`);
}
});
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, " ")}`];
}),
);
test("component stylesheets paint no color a theme cannot override", () => {
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()}`,
);
}
Comment on lines +138 to +142

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition also fires when the earlier declaration is a literal and the later one reads a token (color: #fff; color: var(--ink)), and then the message says the later declaration "shadows a token read", which is the reverse of what happened. Name what the rule actually enforces, so a failure points at the right declaration:

Suggested change
if (properties.has(property) && (properties.get(property) || hasVar)) {
assert.fail(
`${name} shadows a token read for ${property}: ${value.trim()}`,
);
}
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", () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff deletes task dialogs keep their backdrop in both system themes, a test unrelated to this change, and the description does not mention it. Please restore it.

const recordingRest = recordingCss.replace(/:root\s*\{[^{}]*\}/, (block) =>
block.replace(/--[\w-]+\s*:[^;]*;/g, ""),
);
assert.deepEqual(
paintedColors("recording.css", recordingRest),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paintedColors is a const inside the callback of the first component stylesheets paint no color a theme cannot override test, so this test and the second test of that name throw ReferenceError: paintedColors is not defined, and the recording.css rule never checks anything. Move colorFunction, namedColor and paintedColors to module scope next to CSS_NAMED_COLORS, so every test that calls the helper can reach it.

[],
"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
Expand Down
12 changes: 8 additions & 4 deletions web/recording/recording.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
--edge: #232b34;
--pass: #3fb950;
--fail: #f85149;
--page: #0d1117;
--paper: #ffffff;
--video-fill: #000;
--avatar-fallback: #33332c;
}

* {
Expand All @@ -25,7 +29,7 @@

body {
margin: 0;
background: #0d1117;
background: var(--page);
color: var(--ink);
font:
16px/1.5 ui-monospace,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -171,7 +175,7 @@ body {
width: 48px;
height: 48px;
border-radius: 999px;
background: #33332c;
background: var(--avatar-fallback);
color: var(--ink);
font-weight: 700;
}
Expand Down
Loading