Skip to content

Commit 01470a0

Browse files
vanceingallsclaude
andcommitted
fix(studio): warn on duplicate match in execDataAttrPattern (R1, T3 review)
Addresses Rames' review on #1271: execDataAttrPattern returned the first regex match without checking for a second. A duplicate id/data-hf-id in source (id drift) would silently patch one element and leave the other stale. Now warns when more than one element matches. By the mint contract it should never fire. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 7195a2c commit 01470a0

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,18 @@ function replaceTagAtMatch(html: string, match: TagMatch, newTag: string): strin
236236
function execDataAttrPattern(html: string, attr: string, value: string): TagMatch | null {
237237
const pattern = new RegExp(`(<[^>]*\\b${attr}=(["'])${escapeRegex(value)}\\2[^>]*)>`, "i");
238238
const match = pattern.exec(html);
239-
return match?.index != null
240-
? { tag: match[1], start: match.index, end: match.index + match[1].length }
241-
: null;
239+
if (match?.index == null) return null;
240+
// Defensive: a second exact match means a duplicate id/attr in the source
241+
// (id drift). Don't silently patch the first while leaving the other stale —
242+
// surface it. By the mint contract this should never fire.
243+
const all = html.match(new RegExp(`<[^>]*\\b${attr}=(["'])${escapeRegex(value)}\\1[^>]*>`, "gi"));
244+
if (all && all.length > 1) {
245+
// eslint-disable-next-line no-console
246+
console.warn(
247+
`sourcePatcher: ${attr}="${value}" matched ${all.length} elements; patching the first. ids/attrs must be unique per document.`,
248+
);
249+
}
250+
return { tag: match[1], start: match.index, end: match.index + match[1].length };
242251
}
243252

244253
function findTagByClass(html: string, target: PatchTarget): TagMatch | null {

0 commit comments

Comments
 (0)