fix(studio): a paste from another file cannot choose where its edits are written (#8730)

* fix(studio): a paste from another file cannot carry the marks Studio reads as its source file

* refactor(core): the marks Studio reads as an element's source file have one owner
This commit is contained in:
Miguel Ángel
2026-11-24 17:44:28 +00:00
committed by GitHub
parent bf07a78b6f
commit 673f4b57ce
5 changed files with 63 additions and 9 deletions
+6
View File
@@ -1,3 +1,9 @@
export const COMPOSITION_SRC_ATTR = "data-composition-src";
export const CODE_LOADING_ATTRS: ReadonlySet<string> = new Set([COMPOSITION_SRC_ATTR]);
export const SOURCE_HOST_ATTRS = ["data-composition-file", COMPOSITION_SRC_ATTR] as const;
export const COMPOSITION_ROOT_ATTRS = [
"data-composition-id",
"data-hf-original-composition-id",
] as const;
+8 -8
View File
@@ -1,4 +1,5 @@
// How an element is addressed in source: one rule for Studio and the runtime's editing agent.
import { COMPOSITION_ROOT_ATTRS, SOURCE_HOST_ATTRS } from "../compositionSource";
import { isHtmlElement } from "./domRealm";
/** Names an element's source file. The parent resolves it: `file`, else its composition map. */
@@ -193,14 +194,13 @@ function closestWithAny(el: Element, names: readonly string[]): Element | null {
}
export function readElementSource(el: Element): EditingElementSource {
const host = closestWithAny(el, ["data-composition-file", "data-composition-src"]);
const root = closestWithAny(el, ["data-composition-id"]);
const [fileAttr, srcAttr] = SOURCE_HOST_ATTRS;
const [idAttr, originalIdAttr] = COMPOSITION_ROOT_ATTRS;
const host = closestWithAny(el, SOURCE_HOST_ATTRS);
const root = closestWithAny(el, [idAttr]);
return {
file:
host?.getAttribute("data-composition-file") ??
host?.getAttribute("data-composition-src") ??
null,
compositionId: root?.getAttribute("data-composition-id") ?? null,
originalCompositionId: root?.getAttribute("data-hf-original-composition-id") ?? null,
file: host?.getAttribute(fileAttr) ?? host?.getAttribute(srcAttr) ?? null,
compositionId: root?.getAttribute(idAttr) ?? null,
originalCompositionId: root?.getAttribute(originalIdAttr) ?? null,
};
}
@@ -58,6 +58,19 @@ describe("markupOutsideAllowlist", () => {
expect(markupOutsideAllowlist(parse(html, false).documentElement), html).not.toEqual([]);
});
// fails without the fix: a copy kept the marks Studio reads as its file, so later edits went to that other file.
it.each(["data-composition-file", "data-composition-id", "data-hf-original-composition-id"])(
"refuses %s at any depth and inside templates",
(mark) => {
for (const html of [
`<div class="clip" ${mark}="compositions/b.html"></div>`,
`<div class="clip"><p ${mark}="compositions/b.html">x</p></div>`,
`<div class="clip"><template><p ${mark}="compositions/b.html">x</p></template></div>`,
])
expect(markupOutsideAllowlist(parse(html, false).documentElement), html).not.toEqual([]);
},
);
it("shows why: a noscript the check reads as inert runs a handler once scripting is on", () => {
const html = REFUSED[0]!;
expect(runsCode(parse(html, false))).toBe(false);
+11 -1
View File
@@ -6,7 +6,11 @@
*/
import { SVG_ATTRIBUTES, SVG_ELEMENTS } from "./svgAllowlist";
import { CODE_LOADING_ATTRS } from "../compositionSource";
import {
CODE_LOADING_ATTRS,
COMPOSITION_ROOT_ATTRS,
SOURCE_HOST_ATTRS,
} from "../compositionSource";
export const ALLOWED_HTML_ATTRS = new Set([
"id",
@@ -134,8 +138,14 @@ function elementAllowed(element: Element, tag: string): boolean {
return ELEMENTS_PARSED_ALIKE_WITH_SCRIPTING_ON.has(tag) || /^hf-[a-z0-9-]+$/.test(tag);
}
const MARKS_STUDIO_EDITS_THE_FILE_OF: ReadonlySet<string> = new Set([
...SOURCE_HOST_ATTRS,
...COMPOSITION_ROOT_ATTRS,
]);
function attributeAllowed(element: Element, name: string, value: string): boolean {
if (!isSafeAttributeValue(name, value) || name.toLowerCase().startsWith("on")) return false;
if (MARKS_STUDIO_EDITS_THE_FILE_OF.has(name.toLowerCase())) return false;
if (isAllowedHtmlAttribute(name)) return true;
if (!isSvg(element)) return false;
const namespace = FIXED_NAMESPACES.get(name);
@@ -253,3 +253,28 @@ it("compares each pasted copy with only the elements that could be it", () => {
expect(compared.mock.calls.length).toBeLessThanOrEqual(clips.length * 2);
compared.mockRestore();
});
// fails without the fix: a copy kept data-composition-file, and Studio then wrote this element's edits to that file.
describe("a copy from another file cannot choose which file its edits go to", () => {
const marked = (mark: string) =>
`<div id="moved" class="clip" data-start="0" data-duration="1" data-track-index="1"><p ${mark}="compositions/b.html">x</p></div>`;
const MARKS = ["data-composition-file", "data-composition-id", "data-hf-original-composition-id"];
it.each(MARKS)("refuses a copy carrying %s, as an element or a clip", (mark) => {
const html = marked(mark);
const readFrom = "compositions/recv.html";
expect(() => pasteElementHtml(FILE, { html, readFrom }, false, "index.html")).toThrow(
/Couldn't paste/,
);
const clip = { html, readFrom, start: 0, duration: 1, track: 1 };
expect(() => pasteTimelineClips(FILE, [clip], 0, [], false, "index.html")).toThrow(
/Couldn't paste/,
);
});
it.each(MARKS)("keeps %s on a copy pasted into the file it was read from", (mark) => {
const html = marked(mark);
const pasted = pasteElementHtml(FILE, { html, readFrom: "index.html" }, true, "index.html");
expect(pasted).toContain(`${mark}="compositions/b.html"`);
});
});