From 98bc4dc41207657823080909b8252fa73b95b95a Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 6 Dec 2026 04:03:31 +0800 Subject: [PATCH] fix(color-preview): don't read a property name after an earlier declaration as a color In style sheets and style attributes, a word counted as a color value when the text after the last colon looked like a value list. With two declarations on one line (`.one { color: var(--brand); background: var(--alt); }`) the second property name passed that test, and WebKit accepts `background` as a deprecated system color, so the gutter showed an extra swatch for the property itself and it took one of the line's swatch slots. A `;`, `{` or `}` after the colon now ends the declaration. --- src/features/color-preview/colors.test.ts | 5 +++++ src/features/color-preview/colors.ts | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/features/color-preview/colors.test.ts b/src/features/color-preview/colors.test.ts index 1c1cc395f9..2abfbafa93 100644 --- a/src/features/color-preview/colors.test.ts +++ b/src/features/color-preview/colors.test.ts @@ -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('
', "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", () => { diff --git a/src/features/color-preview/colors.ts b/src/features/color-preview/colors.ts index 9b59ebc336..e8bf8a6db5 100644 --- a/src/features/color-preview/colors.ts +++ b/src/features/color-preview/colors.ts @@ -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. */