mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 16:37:32 +00:00
fix(studio): paste and duplicate no longer write the copy's HTML comments (#8657)
This commit is contained in:
@@ -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 </title></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));
|
||||
|
||||
Reference in New Issue
Block a user