дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-28 05:22:23 +02:00
fix(editor): show color swatches for HTML style attributes and embedded scripts (#6412)
Named colors only counted in style sheet languages, and in markup only matches inside style nodes were kept, so embedded script code was dropped. Named colors now count in markup (still limited to <style> and style attributes by the syntax tree), and a match inside a Script node is kept when a standalone script file would accept it: quoted hex and color functions, never names. The property-colon check now looks only after the last colon, so long markup lines no longer cost quadratic time. Refs #5529. Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
807c447792
коміт
581c8cf025
3 files changed
+64
-4
No files matched your search
@@ -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 (`<style>`, `style="…"`): unquoted hex values too. */
|
||||
/** Markup that embeds style sheets (`<style>`, `style="…"`): unquoted hex values and names too. */
|
||||
const MARKUP = new Set(["html", "vue", "svelte", "astro", "xml", "xhtml"]);
|
||||
export const isMarkupLanguage = (language: string): boolean => MARKUP.has(language);
|
||||
/** Languages whose hex colors put the alpha first: `#AARRGGBB` / `#ARGB` (XAML). */
|
||||
@@ -45,6 +45,16 @@ const COLOR_RE = new RegExp(`${FUNCTION}|${HEX}|[a-zA-Z]{3,20}(?![\\w(-])`, "g")
|
||||
/** Before a name, these make it part of a selector, variable or longer word (`.red`, `$white`, `off-white`). */
|
||||
const NAME_PREFIX = /[\w.#$@-]/;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/** Global keywords and words that are valid colors but say nothing about one. */
|
||||
const NOT_COLORS = new Set(["inherit", "initial", "unset", "revert", "currentcolor", "transparent"]);
|
||||
|
||||
@@ -70,10 +80,10 @@ export function findColors(line: string, language: string, isColorName: (word: s
|
||||
if (format === "name") {
|
||||
// A property value (`color: red`), not a property, selector or at-rule.
|
||||
if (
|
||||
!sheet ||
|
||||
!looseHex ||
|
||||
(from > 0 && NAME_PREFIX.test(line[from - 1]!)) ||
|
||||
NOT_COLORS.has(text.toLowerCase()) ||
|
||||
!/:\s*(?:\S+\s+)*$/.test(line.slice(0, from))
|
||||
!afterPropertyColon(line, from)
|
||||
)
|
||||
continue;
|
||||
if (!isColorName(text)) continue;
|
||||
|
||||
@@ -153,6 +153,50 @@ it("ignores color-like HTML text while previewing inline styles", () => {
|
||||
expect(swatches.map((swatch) => swatch.getAttribute("title"))).toEqual(["#abc"]);
|
||||
});
|
||||
|
||||
it("previews named colors in HTML styles and quoted colors in embedded scripts by code rules", () => {
|
||||
const swatchTitles = (doc: string, language: "html" | "vue") => {
|
||||
view?.destroy();
|
||||
view = new EditorView({
|
||||
state: EditorState.create({
|
||||
doc,
|
||||
extensions: [
|
||||
language === "html" ? html() : vue(),
|
||||
colorPreviewGutter({ doc: 1, path: `/p/a.${language}`, language, uiMode: null, readOnly: false }),
|
||||
],
|
||||
}),
|
||||
parent: document.body,
|
||||
});
|
||||
return [...view.dom.querySelectorAll(".cm-openidea-gutter-colors .cm-gutterElement span[title]")].map((swatch) =>
|
||||
swatch.getAttribute("title"),
|
||||
);
|
||||
};
|
||||
expect(
|
||||
swatchTitles(
|
||||
[
|
||||
"<style>",
|
||||
"h1 { color: white; }",
|
||||
"</style>",
|
||||
"<script>",
|
||||
' const data = [{ "name": "aliceblue", "code": "#f0f8ff" }];',
|
||||
" // see #123 and #add",
|
||||
" class A { #fab = 1; f() { return this.#fab; } }",
|
||||
" const c = cond ? a : red;",
|
||||
" const d = { color: red };",
|
||||
"</script>",
|
||||
'<div style="background: red;color:blue;"></div>',
|
||||
"<p onclick=\"paint('#abc')\">red: blue</p>",
|
||||
].join("\n"),
|
||||
"html",
|
||||
),
|
||||
).toEqual(["white", "#f0f8ff", "red", "#abc"]);
|
||||
expect(
|
||||
swatchTitles(
|
||||
'<script setup>\nconst accent = "#1b766c";\n</script>\n<template><b style="color: red">x</b></template>',
|
||||
"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({
|
||||
|
||||
@@ -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<typeof tree.resolveInner> | 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
|
||||
);
|
||||
|
||||
Reference in new issue
Заблокувати користувача