diff --git a/src/features/color-preview/colors.ts b/src/features/color-preview/colors.ts index 54d742929..10a39f6fb 100644 --- a/src/features/color-preview/colors.ts +++ b/src/features/color-preview/colors.ts @@ -21,7 +21,7 @@ export interface ColorMatch { /** Style sheet languages: named colors and unquoted hex values are colors there. */ const STYLE_SHEETS = new Set(["css", "scss", "sass", "less", "stylus", "postcss"]); -/** Markup that embeds style sheets (`", + "", + '
', + "

red: blue

", + ].join("\n"), + "html", + ), + ).toEqual(["white", "#f0f8ff", "red", "#abc"]); + expect( + swatchTitles( + '\n', + "vue", + ), + ).toEqual(["#1b766c", "red"]); +}); + it("updates a CSS literal from alpha and hex fields and ignores invalid input", async () => { view = new EditorView({ state: EditorState.create({ diff --git a/src/features/color-preview/gutter.ts b/src/features/color-preview/gutter.ts index ad8396e8e..477af84a8 100644 --- a/src/features/color-preview/gutter.ts +++ b/src/features/color-preview/gutter.ts @@ -78,10 +78,16 @@ export function lineColor(state: EditorState, pos: number, language: string): Co const matches = findColors(line.text, language, isColorName); if (!isMarkupLanguage(language)) return matches[0] ?? null; const tree = syntaxTree(state); + let script: ColorMatch[] | undefined; return ( matches.find((color) => { let node: ReturnType | null = tree.resolveInner(line.from + color.from + 1); - for (; node; node = node.parent) if (node.name === "StyleSheet" || node.name === "Styles") return true; + for (; node; node = node.parent) { + if (node.name === "StyleSheet" || node.name === "Styles") return true; + // Embedded scripts keep the rules of a script file: quoted hex values and color functions only. + if (node.name === "Script") + return (script ??= findColors(line.text, "javascript", isColorName)).some((c) => c.from === color.from); + } return false; }) ?? null );