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.
Цей коміт міститься в:
l0ng-ai committed 2026-12-06 04:03:31 +08:00
1 джерело a4394d3ece
коміт 98bc4dc412
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. */