fix(studio): paste and duplicate no longer write the copy's HTML comments (#8657)

This commit is contained in:
Miguel Ángel
2026-11-24 07:52:27 +00:00
committed by GitHub
parent 03698f2511
commit add940201e
3 changed files with 37 additions and 2 deletions
@@ -220,7 +220,8 @@ function handlersServed(written: string): string[] {
);
}
// fails without the fix: a comment in an svg <title> passed the check, and the server's parser serves it as markup.
// fails without the fix: a comment in an svg <title> passed the check, and the server's parser serves it as markup;
// then the live copy still wrote the comment.
it("writes nothing the server serves as running code, from the live copy or a saved one", async () => {
const svg = "<svg><title><!-- </title><img src=x onerror=f()> --></title></svg>";
const live = sealedClipboard(
@@ -229,7 +230,9 @@ it("writes nothing the server serves as running code, from the live copy or a sa
);
expect(live.clipboard().handleCopy()).toBe(true);
await live.clipboard().handlePaste();
expect(live.writes.length).toBeGreaterThan(0);
for (const file of live.writes) expect(handlersServed(file)).toEqual([]);
for (const file of live.writes) expect(file).not.toContain("<!--");
act(() => root?.unmount());
root = null;
@@ -245,7 +248,11 @@ it("writes nothing the server serves as running code, from the live copy or a sa
);
expect(saved.clipboard().handleCopy()).toBe(true);
await saved.clipboard().handlePaste();
for (const file of saved.writes) expect(handlersServed(file)).toEqual([]);
// The saved file is read through linkedom, which ends the title early: its handler shows, so the paste is refused.
expect(saved.writes).toEqual([]);
expect(saved.toasts).toContain(
"Couldn't paste: the copied element holds markup that can't be pasted here.",
);
});
// fails without the fix: markup the copy found in another file was taken as saved and pasted unchecked.
@@ -214,6 +214,23 @@ describe("a paste is checked as the preview server serves the file", () => {
expect(runningCode(file)).toEqual([]);
});
// fails without the fix: a copy's comments, svg <title> ones included, were written into the file.
it.each([
svgClip,
'<div id="note" class="clip" data-start="0" data-duration="1" data-track-index="1">Note<!-- draft --></div>',
'<div id="tpl" class="clip" data-start="0" data-duration="1" data-track-index="1"><template><!-- x --></template></div>',
])("writes no comment from the copy %s", (html) => {
for (const readFrom of [null, "compositions/recv.html"]) {
const pasted = pasteElementHtml(FILE, { html, readFrom }, false, "index.html");
const clip = { html, readFrom, start: 0, duration: 1, track: 1 };
const placed = pasteTimelineClips(FILE, [clip], 0, [], false, "index.html").content;
for (const file of [pasted, placed]) {
expect(file).not.toContain("<!--");
expect(file).toMatch(/id="(icon|note|tpl)"/);
}
}
});
it("still pastes an svg whose title is plain text", () => {
const icon =
'<svg viewBox="0 0 4 4"><title>Icon &lt;/title&gt;</title><circle r="2"></circle></svg>';
@@ -27,6 +27,16 @@ function fragment(html: string): HTMLTemplateElement {
return template;
}
function dropComments(root: Element): void {
const templates = subtree(root).filter(htmlTemplate);
const found: Node[] = [];
for (const scope of [root, ...templates.map((t) => t.content)]) {
const walker = document.createTreeWalker(scope, NodeFilter.SHOW_COMMENT);
while (walker.nextNode()) found.push(walker.currentNode);
}
for (const node of found) node.parentNode?.removeChild(node);
}
/** Fresh data-hf-ids minted in the edit (else the server stamps them behind undo's back); taken ids renamed. */
export function prepareCopy(
html: string,
@@ -36,6 +46,7 @@ export function prepareCopy(
): PreparedCopy {
const root = fragment(html).content.firstElementChild;
if (!root) throw new Error(REFUSED);
dropComments(root);
const originalHfId = root.getAttribute("data-hf-id");
const elements = [root];
walkCompositionDescendants(root, (el) => elements.push(el));