fix(color-preview): don't read a property name after an earlier declaration as a color (#8286)

With two declarations on one line, the value check spanned the earlier declaration, so a following property name such as `background` (a deprecated WebKit system color) got a swatch. A `;`, `{` or `}` after the colon now ends the declaration.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-06 04:04:05 +08:00
1 джерело a4394d3ece
коміт f8e1705dff
2 files changed
+13 -4

No files matched your search

+5
Переглянути файл
@@ -22,6 +22,11 @@ describe("findColors", () => {
expect(texts("@media screen { }", "css")).toEqual([]);
expect(texts(" color: $white; background: var(--off-white) off-white;", "scss")).toEqual([]);
expect(texts(" color: #12345;", "css")).toEqual([]);
// A property name after an earlier declaration on the line (WebKit takes `background` as a system color).
expect(texts(".one { color: var(--brand); white: 0; }", "css")).toEqual([]);
expect(texts('<p style="color: var(--a); white: 0">', "html")).toEqual([]);
expect(texts("a:hover { white: 0 }", "css")).toEqual([]);
expect(texts(".one { color: var(--brand); border: 1px solid white; }", "css")).toEqual(["white"]);
});
it("takes only quoted hex values in code, and no names", () => {
+8 -4
Переглянути файл
@@ -63,13 +63,17 @@ const JAVA_INT =
/\bnew\s+(?:(?:java\.awt|javax\.swing\.plaf)\.)?(?:Color|ColorUIResource)\s*\(\s*(0[xX][0-9a-fA-F]{1,8})\s*\)/g;
/**
* Whether `line` before `from` reads `property: value…` (`/:\s*(?:\S+\s+)*$/`). Only the text after
* the last colon is tested: a line of markup has many words, and testing the whole prefix for each
* was quadratic.
* Whether `line` before `from` reads `property: value…` (`/:\s*(?:\S+\s+)*$/`) within one
* declaration: a `;`, `{` or `}` after the colon starts the next one, whose property name is no value
* (`color: var(--a); background: …` must not read `background` as WebKit's system color). Only the
* text after the last colon is tested: a line of markup has many words, and testing the whole prefix
* for each was quadratic.
*/
function afterPropertyColon(line: string, from: number): boolean {
const colon = line.lastIndexOf(":", from - 1);
return colon >= 0 && /^\s*(?:\S+\s+)*$/.test(line.slice(colon + 1, from));
if (colon < 0) return false;
const value = line.slice(colon + 1, from);
return !/[;{}]/.test(value) && /^\s*(?:\S+\s+)*$/.test(value);
}
/** Global keywords and words that are valid colors but say nothing about one. */