');
+ expect(synced[0]?.["index.html"]).toContain('id="intro" data-hf-id="hf-x" style="color: red"');
+ });
+
it("writes nothing when neither the hf-id nor the id picks one element", () => {
const saved =
'
a' +
diff --git a/packages/studio/src/utils/sourcePatcher.test.ts b/packages/studio/src/utils/sourcePatcher.test.ts
index 503124d80c..5fddd1d439 100644
--- a/packages/studio/src/utils/sourcePatcher.test.ts
+++ b/packages/studio/src/utils/sourcePatcher.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
applyPatch,
applyPatchByTarget,
+ countTagsWithAttr,
readAttributeByTarget,
readTagSnippetByTarget,
type PatchOperation,
@@ -646,3 +647,23 @@ describe("T3 — hfId targeting (spec for R1)", () => {
expect(result).toContain('
B
');
});
});
+
+describe("attribute names inside longer ones", () => {
+ const html =
+ '
T
';
+
+ it("patches the tag whose id matches, not one whose data-composition-id does", () => {
+ const patched = applyPatch(html, "intro", {
+ type: "inline-style",
+ property: "color",
+ value: "red",
+ });
+ expect(patched).toContain('
');
+ expect(patched).toContain('
');
+ });
+
+ it("counts and reads an attribute by its own name only", () => {
+ expect(countTagsWithAttr(html, "id", "intro")).toBe(1);
+ expect(readAttributeByTarget(html, { id: "intro" }, "src")).toBe("a.png");
+ });
+});
diff --git a/packages/studio/src/utils/sourcePatcher.ts b/packages/studio/src/utils/sourcePatcher.ts
index 2de9a8f0e0..7137427b71 100644
--- a/packages/studio/src/utils/sourcePatcher.ts
+++ b/packages/studio/src/utils/sourcePatcher.ts
@@ -167,7 +167,7 @@ function patchInlineStyle(
value: string | null,
): string {
// Find the element tag with this id
- const idPattern = new RegExp(`(<[^>]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
+ const idPattern = new RegExp(`(<[^>]*(?]*)>`, "i");
const match = idPattern.exec(html);
if (!match) return html;
@@ -242,7 +242,7 @@ function replaceTagAtMatch(html: string, match: TagMatch, newTag: string): strin
}
function execDataAttrPattern(html: string, attr: string, value: string): TagMatch | null {
- const pattern = new RegExp(`(<[^>]*\\b${attr}=(["'])${escapeRegex(value)}\\2[^>]*)>`, "i");
+ const pattern = new RegExp(`(<[^>]*(?]*)>`, "i");
const match = pattern.exec(html);
if (match?.index == null) return null;
return { tag: match[1], start: match.index, end: match.index + match[1].length };
@@ -253,7 +253,7 @@ function findTagByClass(html: string, target: PatchTarget): TagMatch | null {
if (!classMatch) return null;
const cls = classMatch[1];
const pattern = new RegExp(
- `(<[^>]*\\bclass=(["'])[^"']*\\b${escapeRegex(cls)}\\b[^"']*\\2[^>]*)>`,
+ `(<[^>]*(?]*)>`,
"gi",
);
const selectorIndex = target.selectorIndex ?? 0;
@@ -273,7 +273,7 @@ function findTagByClass(html: string, target: PatchTarget): TagMatch | null {
}
export function countTagsWithAttr(html: string, attr: string, value: string): number {
- const pattern = new RegExp(`<[^>]*\\s${attr}=(["'])${escapeRegex(value)}\\1[^>]*>`, "gi");
+ const pattern = new RegExp(`<[^>]*(?]*>`, "gi");
return html.match(pattern)?.length ?? 0;
}
@@ -308,7 +308,7 @@ export function readAttributeByTarget(
if (!match) return undefined;
const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
- const valueMatch = new RegExp(`\\b${fullAttr}=(["'])([^"']*)\\1`).exec(match.tag);
+ const valueMatch = new RegExp(`(?]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
+ const idPattern = new RegExp(`(<[^>]*(?]*)>`, "i");
const match = idPattern.exec(html);
if (!match) return html;
const tag = match[1];
const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
- const attrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}=(["'])([^"']*)\\1`);
+ const attrPattern = new RegExp(`(?]*\\bid=(["'])${escapeRegex(elementId)}\\3[^>]*>)`,
+ `(<([a-z0-9-]+)[^>]*(?]*>)`,
"i",
);
const match = openTagPattern.exec(html);
@@ -468,7 +468,7 @@ function patchHtmlAttributeInTag(
return html.replace(tag, newTag);
}
- const attrPattern = new RegExp(`\\b${escapeRegex(attr)}=(["'])([^"']*)\\1`);
+ const attrPattern = new RegExp(`(?]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
+ const idPattern = new RegExp(`(<[^>]*(?]*)>`, "i");
const match = idPattern.exec(html);
if (!match) return html;
return patchHtmlAttributeInTag(html, match[1], attr, value);