mirror of
https://github.com/webstudio-is/webstudio.git
synced 2026-12-01 21:05:42 +09:00
The Builder was stalling when selecting instances because Tag selector resolution repeatedly scanned and parsed project styles for every displayed property. This PR indexes that data when styles change, reuses style source suggestions, and avoids an unnecessary stylesheet rebuild on multi-selection. It also restores the documented `:hover .button` and `:hover > .icon` selectors, fixes their editing preview, and adds sharp light and dark Tag styles screenshots. Ref #5657 Closes #6678 ### Plan and tools - [x] Reproduce the selector rejection in shared validator and runtime tests. - [x] Restore one shared validation path for new and saved selectors. Keep CSS parsing through css-tree. - [x] Update Tag styles and States and selectors docs, navigation emoji, and paired Storybook screenshots. - [x] Measure the instance selection slowdown and remove repeated full scans while preserving source order. - [x] Audit the full selection path and reuse existing style indexes in state matching and style controls. - [x] Avoid redundant Tag CSS rebuild on multi-selection and test that real preview styles still clear. - [x] Fix descendant state previews for Token, Local, and Tag styles; verify live Tag editing. - [x] Run targeted tests, typechecks, lint, formatting, and inspect both screenshot themes. - [ ] Monitor PR checks and address failures. - [ ] Approve the new Storybook baseline after user review. Verification: CSS data 62 tests; project build styles 167 tests; Builder style panel/model 113 targeted tests; canvas selection and styles 89 targeted tests; CSS data/project build/Builder typechecks; lint; formatting. A local benchmark with 5,100 styles, 5,001 instance selections, and 100 property reads fell from 99 ms to 6 ms for reads after the second fix, plus 6 ms to build indexes when data changed. The original repeated style scan measured 716 ms for 100 reads with no instance selection map. These measure the affected calculation, not total Builder selection time. Two older selection-time style scans remain and cost about 0.1 ms each per 5,100 declarations in a synthetic Node scan; their output ordering needs a separate check before changing them. Previewing an unrelated Token or Local edit still rebuilds the Tag stylesheet, an editing-time cost outside the reported selection path. No fixture inputs or generated fixture bundles changed. Agent evaluations were not run because they require separate approval.
454 lines
13 KiB
TypeScript
454 lines
13 KiB
TypeScript
import { describe, test, expect } from "vitest";
|
|
import { validateSelector } from "./selector-validation";
|
|
|
|
describe("validateSelector", () => {
|
|
test("reads the condition from one nested tag", () => {
|
|
expect(validateSelector(' h2[data-state="open"]:hover')).toMatchObject({
|
|
success: true,
|
|
type: "tag",
|
|
tag: "h2",
|
|
condition: '[data-state="open"]:hover',
|
|
});
|
|
expect(validateSelector(" h2[ data-state = open ]:hover")).toMatchObject({
|
|
success: true,
|
|
type: "tag",
|
|
tag: "h2",
|
|
condition: "[ data-state = open ]:hover",
|
|
});
|
|
expect(validateSelector(" h2:before")).toMatchObject({
|
|
success: true,
|
|
type: "pseudo-element",
|
|
});
|
|
});
|
|
test.each([
|
|
":hover, body",
|
|
"body:hover",
|
|
".class",
|
|
"#identifier",
|
|
":hover + .tooltip",
|
|
":hover ~ .tooltip",
|
|
])(
|
|
"rejects selector syntax that can escape element scope: %s",
|
|
(selector) => {
|
|
expect(validateSelector(selector)).toMatchObject({ success: false });
|
|
}
|
|
);
|
|
|
|
test.each([
|
|
":hover:focus-visible",
|
|
"[data-state=open]:hover",
|
|
":not(.disabled, [aria-disabled=true])",
|
|
])("accepts compound state suffixes: %s", (selector) => {
|
|
expect(validateSelector(selector).success).toBe(true);
|
|
});
|
|
|
|
test.each([
|
|
":hover .button",
|
|
":focus-within input",
|
|
"[data-state=open] [data-part=content]",
|
|
":hover > .icon",
|
|
":not(.disabled) > [aria-hidden=true]",
|
|
])("accepts states targeting descendants: %s", (selector) => {
|
|
expect(validateSelector(selector).success).toBe(true);
|
|
});
|
|
|
|
test.each([
|
|
[":hover .button", ":hover", " .button"],
|
|
[":hover > .icon", ":hover", " > .icon"],
|
|
["[data-state=open] h2", "[data-state=open]", " h2"],
|
|
[":is(.active, .selected) > .icon", ":is(.active, .selected)", " > .icon"],
|
|
])(
|
|
"separates the styled element from its descendant: %s",
|
|
(selector, scopedState, descendant) => {
|
|
expect(validateSelector(selector)).toMatchObject({
|
|
success: true,
|
|
scopedState,
|
|
descendant,
|
|
});
|
|
}
|
|
);
|
|
|
|
test.each([
|
|
":hover",
|
|
"[data-state=open]",
|
|
"::before",
|
|
" h2",
|
|
" h2:hover",
|
|
' h2[a="123"]',
|
|
" h2::before",
|
|
])(
|
|
"accepts a styled-element condition or one nested tag in Add more: %s",
|
|
(selector) => {
|
|
expect(validateSelector(selector).success).toBe(true);
|
|
}
|
|
);
|
|
|
|
test.each([
|
|
" p",
|
|
" button:hover",
|
|
" a[aria-current=page]",
|
|
' div[a="123"]',
|
|
" div::before",
|
|
])("accepts one descendant tag and its states: %s", (selector) => {
|
|
expect(validateSelector(selector)).toMatchObject({
|
|
success: true,
|
|
type: "tag",
|
|
});
|
|
});
|
|
|
|
test.each([" p strong", " .card", " #card", " p + span"])(
|
|
"rejects unsupported descendant tag selectors: %s",
|
|
(selector) => {
|
|
expect(validateSelector(selector).success).toBe(false);
|
|
}
|
|
);
|
|
|
|
test("limits a tag token to states of its own tag", () => {
|
|
expect(
|
|
validateSelector(":hover", { allowDescendants: false }).success
|
|
).toBe(true);
|
|
expect(validateSelector(" p", { allowDescendants: false }).success).toBe(
|
|
false
|
|
);
|
|
expect(
|
|
validateSelector(":hover p", { allowDescendants: false }).success
|
|
).toBe(false);
|
|
});
|
|
|
|
test.each([
|
|
":not-a-real-state:hover",
|
|
":hover:not-a-real-state",
|
|
":hover::not-a-real-element",
|
|
":not(:not-a-real-state)",
|
|
])(
|
|
"rejects invalid selectors anywhere in a compound state: %s",
|
|
(selector) => {
|
|
expect(validateSelector(selector)).toMatchObject({ success: false });
|
|
}
|
|
);
|
|
describe("valid selectors", () => {
|
|
test("simple pseudo-classes", () => {
|
|
const selectors = [
|
|
":hover",
|
|
":focus",
|
|
":active",
|
|
":visited",
|
|
":disabled",
|
|
":checked",
|
|
":focus-visible",
|
|
":focus-within",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("simple pseudo-elements", () => {
|
|
const selectors = [
|
|
"::before",
|
|
"::after",
|
|
"::placeholder",
|
|
"::first-line",
|
|
"::first-letter",
|
|
"::selection",
|
|
"::marker",
|
|
"::backdrop",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-element");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("legacy single-colon pseudo-elements", () => {
|
|
const selectors = [":before", ":after", ":first-line", ":first-letter"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-element");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("functional pseudo-classes with simple selectors", () => {
|
|
const selectors = [":not(div)", ":is(a)", ":where(span)", ":has(p)"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("functional pseudo-classes with An+B notation", () => {
|
|
const selectors = [
|
|
":nth-child(2n)",
|
|
":nth-last-child(odd)",
|
|
":nth-of-type(3n+1)",
|
|
":nth-last-of-type(even)",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("functional pseudo-classes with nested selectors", () => {
|
|
const selectors = [
|
|
":not(.class)",
|
|
":is(#id)",
|
|
":where([attr])",
|
|
":has(> div)",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("shadow DOM pseudo-classes", () => {
|
|
const selectors = [":host", ":host(.class)", ":host-context(.class)"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("linguistic pseudo-classes", () => {
|
|
const selectors = [":lang(en)", ":dir(rtl)", ":dir(ltr)"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("custom state pseudo-class", () => {
|
|
const result = validateSelector(":state(custom)");
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
});
|
|
|
|
test("time-dimensional pseudo-classes", () => {
|
|
const selectors = [":current", ":past", ":future"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-class");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("less common pseudo-elements from generated list", () => {
|
|
const selectors = [
|
|
"::grammar-error",
|
|
"::spelling-error",
|
|
"::target-text",
|
|
"::file-selector-button",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-element");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("functional pseudo-elements", () => {
|
|
const selectors = ["::part(tab)", "::slotted(span)"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("pseudo-element");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("attribute selectors", () => {
|
|
test("basic attribute selectors", () => {
|
|
const selectors = [
|
|
"[data-state]",
|
|
"[data-state=open]",
|
|
"[data-state=closed]",
|
|
"[aria-expanded=true]",
|
|
"[aria-hidden=false]",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("attribute");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("attribute selectors with operators", () => {
|
|
const selectors = [
|
|
"[class~=foo]",
|
|
"[lang|=en]",
|
|
"[href^=https]",
|
|
'[src$=".png"]',
|
|
"[title*=hello]",
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("attribute");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("attribute selectors with quotes", () => {
|
|
const selectors = [
|
|
'[data-state="open"]',
|
|
"[data-state='closed']",
|
|
'[aria-label="menu button"]',
|
|
];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("attribute");
|
|
}
|
|
}
|
|
});
|
|
|
|
test("case-insensitive attribute selectors", () => {
|
|
const selectors = ["[type=text i]", "[href$=PDF i]"];
|
|
|
|
for (const selector of selectors) {
|
|
const result = validateSelector(selector);
|
|
expect(result.success).toBe(true);
|
|
if (result.success) {
|
|
expect(result.type).toBe("attribute");
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("invalid selectors", () => {
|
|
test("empty selector", () => {
|
|
const result = validateSelector("");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Selector cannot be empty");
|
|
}
|
|
});
|
|
|
|
test("whitespace-only selector", () => {
|
|
const result = validateSelector(" ");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Selector cannot be empty");
|
|
}
|
|
});
|
|
|
|
test("selector not starting with colon or bracket", () => {
|
|
const result = validateSelector("hover");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe(
|
|
"Selector must start with a colon (:) for pseudo-classes, double colon (::) for pseudo-elements, or bracket ([) for attribute selectors"
|
|
);
|
|
}
|
|
});
|
|
|
|
test("invalid pseudo-class name", () => {
|
|
const result = validateSelector(":not-a-real-pseudo-class");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid pseudo-class");
|
|
}
|
|
});
|
|
|
|
test("invalid functional pseudo-class name", () => {
|
|
const result = validateSelector(":blablubb(div)");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid pseudo-class");
|
|
}
|
|
});
|
|
|
|
test("invalid pseudo-element name", () => {
|
|
const result = validateSelector("::not-a-real-pseudo-element");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid pseudo-element");
|
|
}
|
|
});
|
|
|
|
test("unbalanced parentheses in functional pseudo-class", () => {
|
|
const result = validateSelector(":not(div");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid CSS selector syntax");
|
|
}
|
|
});
|
|
|
|
test("unbalanced parentheses - too many closing", () => {
|
|
const result = validateSelector(":not(div))");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid CSS selector syntax");
|
|
}
|
|
});
|
|
|
|
test("invalid syntax inside functional pseudo-class", () => {
|
|
// css-tree is lenient and will parse this
|
|
// The browser will ultimately reject truly invalid selectors
|
|
const result = validateSelector(":has(>>invalid)");
|
|
// This actually parses in css-tree (it's lenient)
|
|
expect(result.success).toBe(true);
|
|
});
|
|
|
|
test("unbalanced brackets in attribute selector", () => {
|
|
const result = validateSelector("[data-state");
|
|
expect(result.success).toBe(false);
|
|
if (!result.success) {
|
|
expect(result.error).toBe("Invalid CSS selector syntax");
|
|
}
|
|
});
|
|
});
|
|
});
|