-
Notifications
You must be signed in to change notification settings - Fork 194
[diffs/edit] fix marker popover text contrast #1061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+93
−2
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import { readFileSync } from 'fs'; | ||
| import { resolve } from 'path'; | ||
|
|
||
| // CSS files that ship with the package. The tests below check every | ||
| // `contrast-color()` call in them. | ||
| const STYLESHEETS = ['../src/style.css', '../src/editor/editor.css']; | ||
|
|
||
| // Splits a `contrast-color()` argument list into its top-level arguments. A | ||
| // comma inside `var()`, `light-dark()`, or `color-mix()` does not split. | ||
| function splitTopLevelArgs(args: string): string[] { | ||
| const parts: string[] = []; | ||
| let depth = 0; | ||
| let current = ''; | ||
| for (const char of args) { | ||
| if (char === '(') { | ||
| depth += 1; | ||
| } else if (char === ')') { | ||
| depth -= 1; | ||
| } else if (char === ',' && depth === 0) { | ||
| parts.push(current.trim()); | ||
| current = ''; | ||
| continue; | ||
| } | ||
| current += char; | ||
| } | ||
| parts.push(current.trim()); | ||
| return parts; | ||
| } | ||
|
|
||
| // Returns the argument list of every `contrast-color()` call in `css`. It counts | ||
| // nested parens to find the closing paren of each call. | ||
| function findContrastColorArgs(css: string): string[] { | ||
| const calls: string[] = []; | ||
| const marker = 'contrast-color('; | ||
| let index = css.indexOf(marker); | ||
| while (index !== -1) { | ||
| let depth = 1; | ||
| let cursor = index + marker.length; | ||
| while (cursor < css.length && depth > 0) { | ||
| if (css[cursor] === '(') { | ||
| depth += 1; | ||
| } else if (css[cursor] === ')') { | ||
| depth -= 1; | ||
| } | ||
| cursor += 1; | ||
| } | ||
| calls.push(css.slice(index + marker.length, cursor - 1)); | ||
| index = css.indexOf(marker, cursor); | ||
| } | ||
| return calls; | ||
| } | ||
|
|
||
| // Regression test for white text on the yellow warning popover. | ||
| // `contrast-color()` takes one argument. A call with two arguments still parses, | ||
| // because `var()` defers the grammar check to substitution. The declaration is | ||
| // then invalid at computed-value time, so `color` inherits its value and ignores | ||
| // the `--diffs-marker-contrast` declaration. No browser test catches this: | ||
| // Playwright bundles an old Chromium without `contrast-color()`, so it runs the | ||
| // fallback branch only. | ||
| describe('contrast-color() arity in shipped CSS', () => { | ||
| for (const stylesheet of STYLESHEETS) { | ||
| test(`${stylesheet} passes one argument per call`, () => { | ||
| const css = readFileSync(resolve(__dirname, stylesheet), 'utf-8'); | ||
| for (const args of findContrastColorArgs(css)) { | ||
| expect( | ||
| splitTopLevelArgs(args), | ||
| `contrast-color(${args}) must take exactly one argument` | ||
| ).toHaveLength(1); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| test('detects a two-argument call', () => { | ||
| expect( | ||
| splitTopLevelArgs( | ||
| findContrastColorArgs( | ||
| 'a { color: contrast-color(var(--a), light-dark(#000, #fff)); }' | ||
| )[0] | ||
| ) | ||
| ).toHaveLength(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep the intent here, this was meant to be a fallback. Can probably update the code comment to remove mention of two colors.