mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(studio): an element description keeps only a selector Studio would have built (#8789)
* fix(studio): an element description keeps only a selector Studio would have built A sealed preview describes its elements to Studio, and the description's selector later names the element an edit is saved to. The reader took any string. It now keeps a description only when its selector is the described id, one class, a composition id or a group, the forms the runtime builds. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * fix(studio): hold the selector check to exactly what the runtime builds A form whose control is named id no longer yields an id selector made from the control. Class selectors that start with a digit and empty attribute values are refused. A test ties the check to the builder. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
dcdb181896
commit
32681eedc2
@@ -161,7 +161,7 @@ export function escapeCssString(value: string): string {
|
||||
}
|
||||
|
||||
export function buildStableSelector(el: Element): string | undefined {
|
||||
if (el.id) return `#${escapeCssIdentifier(el.id)}`;
|
||||
if (el.id && typeof el.id === "string") return `#${escapeCssIdentifier(el.id)}`;
|
||||
|
||||
const compositionId = el.getAttribute("data-composition-id");
|
||||
if (compositionId) return `[data-composition-id="${escapeCssString(compositionId)}"]`;
|
||||
@@ -175,6 +175,18 @@ export function buildStableSelector(el: Element): string | undefined {
|
||||
return getPreferredClassSelector(el);
|
||||
}
|
||||
|
||||
const ESCAPED_IDENTIFIER = String.raw`(?:[A-Za-z0-9_-]|[^\x00-\x7f]|\\[0-9a-f]{1,6} |\\[^0-9a-f\n\r\f])+`;
|
||||
const ESCAPED_STRING = String.raw`"(?:[^"\\\n\r\f]|\\\\|\\"|\\a |\\d |\\c )+"`;
|
||||
const CLASS_SELECTOR = new RegExp(`^\\.(?!-?[0-9]|-$)${ESCAPED_IDENTIFIER}$`);
|
||||
const ATTRIBUTE_SELECTOR = new RegExp(`^\\[data-(?:composition-id|hf-group)=${ESCAPED_STRING}\\]$`);
|
||||
|
||||
/** Is `selector` one `buildStableSelector` makes for an element whose id is `id`. */
|
||||
export function isStableSelector(selector: string, id: string | null): boolean {
|
||||
if (selector.startsWith("#"))
|
||||
return Boolean(id) && selector === `#${escapeCssIdentifier(id ?? "")}`;
|
||||
return !id && (CLASS_SELECTOR.test(selector) || ATTRIBUTE_SELECTOR.test(selector));
|
||||
}
|
||||
|
||||
export function getPreferredClassSelector(el: Element): string | undefined {
|
||||
const classes = Array.from(el.classList)
|
||||
.map((value) => value.trim())
|
||||
|
||||
@@ -115,6 +115,7 @@ export {
|
||||
MAX_POINT_STACK,
|
||||
MAX_QUERY_MATCHES,
|
||||
MAX_SOURCE_GROUPS,
|
||||
isStableSelector,
|
||||
type EditingElementDescription,
|
||||
type EditingElementHandle,
|
||||
type EditingElementHit,
|
||||
|
||||
@@ -6,7 +6,9 @@ import {
|
||||
MAX_ELEMENT_TEXT,
|
||||
MAX_ELEMENT_VALUE,
|
||||
type EditingElementDescription,
|
||||
isStableSelector,
|
||||
} from "@hyperframes/core/runtime/editing-port";
|
||||
import { buildStableSelector } from "../../../../core/src/runtime/editingElement";
|
||||
import { gestureStart } from "../../../../core/src/runtime/editingGestureStart";
|
||||
import { handleFor } from "../../../../core/src/runtime/editingHandles";
|
||||
import {
|
||||
@@ -107,13 +109,18 @@ describe("readPreviewElement", () => {
|
||||
|
||||
it("refuses a description longer anywhere than the runtime ever sends one", () => {
|
||||
const value = "x".repeat(MAX_ELEMENT_VALUE);
|
||||
const atLimit = described(1, { selector: value, id: value, attributes: [["src", value]] });
|
||||
const atLimit = described(1, {
|
||||
selector: `.${value.slice(1)}`,
|
||||
attributes: [["src", value]],
|
||||
});
|
||||
expect(readPreviewElement(atLimit)).toEqual(atLimit);
|
||||
const idAtLimit = described(1, { selector: `#${value.slice(1)}`, id: value.slice(1) });
|
||||
expect(readPreviewElement(idAtLimit)).toEqual(idAtLimit);
|
||||
const over = `${value}x`;
|
||||
const text = "y".repeat(MAX_ELEMENT_TEXT + 1);
|
||||
const many = Array.from({ length: MAX_ELEMENT_ENTRIES + 1 }, (_, i) => [`data-${i}`, ""]);
|
||||
for (const bad of [
|
||||
described(1, { selector: over }),
|
||||
described(1, { selector: `.${value}` }),
|
||||
described(1, { id: over }),
|
||||
described(1, { tag: over }),
|
||||
described(1, { label: over }),
|
||||
@@ -248,6 +255,61 @@ describe("element requests over an opaque frame's port", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("the selector a description may carry", () => {
|
||||
it("keeps the forms Studio builds for an element itself", () => {
|
||||
for (const kept of [
|
||||
described(1, { selector: "#title", id: "title" }),
|
||||
described(1, { selector: "#\\31 st\\.hero", id: "1st.hero" }),
|
||||
described(1, { selector: ".tile" }),
|
||||
described(1, { selector: ".md\\:w-1\\/2" }),
|
||||
described(1, { selector: '[data-composition-id="card__hf2"]' }),
|
||||
described(1, { selector: '[data-hf-group="a \\"b\\" c"]' }),
|
||||
]) {
|
||||
expect(readPreviewElement(kept)).toEqual(kept);
|
||||
}
|
||||
});
|
||||
|
||||
it("drops a description whose selector Studio would not have built", () => {
|
||||
for (const forged of [
|
||||
described(1, { selector: "script" }),
|
||||
described(1, { selector: "body > script:nth-of-type(2)" }),
|
||||
described(1, { selector: ".tile script" }),
|
||||
described(1, { selector: ".tile, script" }),
|
||||
described(1, { selector: ".tile.other" }),
|
||||
described(1, { selector: '[data-composition-id="card"] script' }),
|
||||
described(1, { selector: '[data-composition-id="card"], script' }),
|
||||
described(1, { selector: '[src="app.js"]' }),
|
||||
described(1, { selector: "*" }),
|
||||
described(1, { selector: "" }),
|
||||
described(1, { selector: ".1st" }),
|
||||
described(1, { selector: ".-" }),
|
||||
described(1, { selector: '[data-hf-group=""]' }),
|
||||
]) {
|
||||
expect(readPreviewElement(forged)).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("drops an id selector that names another element than the described id", () => {
|
||||
expect(readPreviewElement(described(1, { selector: "#boot", id: "title" }))).toBeNull();
|
||||
expect(readPreviewElement(described(1, { selector: "#boot" }))).toBeNull();
|
||||
expect(readPreviewElement(described(1, { selector: "#title script", id: "title" }))).toBeNull();
|
||||
expect(readPreviewElement(described(1, { selector: ".tile", id: "title" }))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("the selector the runtime builds for an element", () => {
|
||||
it("is always one a description may carry", () => {
|
||||
document.body.innerHTML = `
|
||||
<div id="1st.hero"></div><div id="-"></div><div class="md:w-1/2 clip"></div>
|
||||
<div class="-1x"></div><div class="clip"></div><div data-composition-id='a "b"'></div>
|
||||
<div data-hf-group="g\\1"></div><form class="signup"><input name="id" /></form>`;
|
||||
for (const el of Array.from(document.body.querySelectorAll("div, form"))) {
|
||||
const id = typeof el.id === "string" && el.id ? el.id : null;
|
||||
expect(isStableSelector(buildStableSelector(el) ?? "", id), el.outerHTML).toBe(true);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("source file and selector index from a description", () => {
|
||||
it("answers what Studio reads off the frame's own element", () => {
|
||||
setCompositionSourceMap(new Map([["card", "cards.html"]]));
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
MAX_QUERY_MATCHES,
|
||||
MAX_SNAP_TARGETS,
|
||||
MAX_SOURCE_GROUPS,
|
||||
isStableSelector,
|
||||
isTransformList,
|
||||
type EditingElementBox,
|
||||
type EditingElementDescription,
|
||||
@@ -86,12 +87,19 @@ function readHit(value: unknown): PreviewElementHit | null {
|
||||
return isRect(value.rect) ? (value as unknown as PreviewElementHit) : null;
|
||||
}
|
||||
|
||||
function hasOwnIdentity({ id, selector }: Fields): boolean {
|
||||
if (!isValueOrNull(id) || !isValueOrNull(selector)) return false;
|
||||
return (
|
||||
typeof selector !== "string" || isStableSelector(selector, typeof id === "string" ? id : null)
|
||||
);
|
||||
}
|
||||
|
||||
/** Frame data checked against `EditingElementDescription`; null when any field is off. */
|
||||
export function readPreviewElement(value: unknown): PreviewElement | null {
|
||||
if (!readHit(value) || !isFields(value)) return null;
|
||||
if (value.parent !== null && !isHandle(value.parent)) return null;
|
||||
if (value.label !== undefined && !isValue(value.label)) return null;
|
||||
if (!isValueOrNull(value.id) || !isValueOrNull(value.selector)) return null;
|
||||
if (!hasOwnIdentity(value)) return null;
|
||||
if (!isSource(value.source) || typeof value.locked !== "boolean") return null;
|
||||
if (!isStyles(value.inlineStyles) || !isLongText(value.text)) return null;
|
||||
if (value.textMarkup !== undefined && typeof value.textMarkup !== "boolean") return null;
|
||||
|
||||
Reference in New Issue
Block a user