Skip to content

Commit 26b1b18

Browse files
committed
refactor(studio): share the id-attribute regex and hoist the clip DOMParser
Simplify pass on the previous commit: the id-capture lookbehind was a second copy of the same pattern deduplicateIds already uses, now a single exported ID_ATTR_RE both call. The per-clip DOMParser instance in pasteTimelineClips is now created once per call, not once per clip. Adds a test pinning stripHfIds's no-parseable-root fallback.
1 parent 0ac18a4 commit 26b1b18

3 files changed

Lines changed: 24 additions & 8 deletions

File tree

‎packages/studio/src/hooks/useClipboard.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ describe("pasteTimelineClips", () => {
170170
expect(content).not.toContain("data-hf-id");
171171
});
172172

173+
it("leaves html unstripped when it has no parseable element root (documents the fallback)", () => {
174+
// stripHfIds only strips when DOMParser finds an element; bare text has
175+
// none, so this pins the fallback rather than a real safety guarantee.
176+
const noRoot: TimelineClipboardClip = {
177+
html: 'text mentioning data-hf-id="hf-leak" with no tag at all',
178+
start: 0,
179+
duration: 1,
180+
track: 0,
181+
};
182+
const { content } = pasteTimelineClips(ROOT, [noRoot], 0, []);
183+
expect(content).toContain('data-hf-id="hf-leak"');
184+
});
185+
173186
it("captures the root's own id, not a data-id that happens to precede it", () => {
174187
const withDataId: TimelineClipboardClip = {
175188
html: '<audio data-id="not-the-id" id="sfx-23" src="typenew.mp3" data-start="21.61" data-duration="0.57" data-track-index="132"></audio>',

‎packages/studio/src/hooks/useClipboard.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { DomEditSelection } from "../components/editor/domEditing";
55
import {
66
type ClipboardPayload,
77
type TimelineClipboardClip,
8+
ID_ATTR_RE,
89
deduplicateIds,
910
insertAsSibling,
1011
} from "../utils/clipboardPayload";
@@ -98,8 +99,8 @@ export function resolveFreeTrack(preferred: PlacedClip, taken: readonly PlacedCl
9899
/** Strips data-hf-id from the root and every descendant so a clone re-mints
99100
* its own. DOMParser, not a bracket-scoped regex, so a `>` inside an earlier
100101
* attribute value or a single-quoted id can't defeat the strip. */
101-
function stripHfIds(html: string): string {
102-
const root = new DOMParser().parseFromString(html, "text/html").body.firstElementChild;
102+
function stripHfIds(html: string, parser: DOMParser): string {
103+
const root = parser.parseFromString(html, "text/html").body.firstElementChild;
103104
if (!root) return html;
104105
root.querySelectorAll("[data-hf-id]").forEach((el) => el.removeAttribute("data-hf-id"));
105106
root.removeAttribute("data-hf-id");
@@ -126,8 +127,9 @@ export function pasteTimelineClips(
126127
const ids: string[] = [];
127128
let result = content;
128129
let requiredEnd = 0;
130+
const domParser = new DOMParser();
129131
for (const clip of clips) {
130-
const stripped = stripHfIds(clip.html);
132+
const stripped = stripHfIds(clip.html, domParser);
131133
const deduped = deduplicateIds(stripped, existingIds);
132134
existingIds = existingIds.concat(collectHtmlIds(deduped));
133135
const newStart = anchorTime + (clip.start - groupMinStart);
@@ -149,10 +151,7 @@ export function pasteTimelineClips(
149151
const withPatched = patchedRootTag + deduped.slice(rootTagEnd + 1);
150152
result = insertTimelineAssetIntoSource(result, withPatched);
151153

152-
// Lookbehind requires the attribute to start at a word boundary preceded
153-
// by whitespace, same pattern deduplicateIds uses above, so `id="x"`
154-
// matches but the `id` inside `data-id="x"` does not.
155-
const id = patchedRootTag.match(/(?<=\s)id="([^"]+)"/)?.[1];
154+
const id = patchedRootTag.match(ID_ATTR_RE)?.[1];
156155
if (id) ids.push(id);
157156
}
158157
return { content: result, ids, requiredEnd };

‎packages/studio/src/utils/clipboardPayload.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,13 @@ function findClosingTagPosition(html: string, openTagStart: number): number {
177177
return -1;
178178
}
179179

180+
/** An `id="..."` attribute, not `data-id="..."` or similar — only matches
181+
* when preceded by whitespace, the way every generated attribute is. */
182+
export const ID_ATTR_RE = /(?<=\s)id="([^"]+)"/;
183+
180184
export function deduplicateIds(html: string, existingIds: string[]): string {
181185
const existingSet = new Set(existingIds);
182-
return html.replace(/(?<=\s)id="([^"]+)"/g, (full, id: string) => {
186+
return html.replace(new RegExp(ID_ATTR_RE.source, "g"), (full, id: string) => {
183187
if (!existingSet.has(id)) return full;
184188
let counter = 2;
185189
while (existingSet.has(`${id}-${counter}`)) counter++;

0 commit comments

Comments
 (0)