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. */