Skip to content

Commit c564daf

Browse files
authored
fix(studio): avoid inline style regex backtracking (#3708)
1 parent eec0434 commit c564daf

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ describe("applyPatchByTarget", () => {
6666
expect(result).not.toContain("/ style");
6767
});
6868

69+
it.each(["", " ", "\t\r\n", "\u00a0\u2028", " ".repeat(100_000)])(
70+
"preserves self-closing whitespace handling (case %#)",
71+
(whitespace) => {
72+
const prefix = '<img id="hero" class="hero"';
73+
const op: PatchOperation = { type: "inline-style", property: "opacity", value: "0.5" };
74+
for (const suffix of ["/", "/\n", "/\r", "/\r\n", "/\u2028", "", "x", "/ "]) {
75+
const tag = prefix + whitespace + suffix;
76+
const selfClosing = suffix === "/";
77+
// The original operation removes whitespace before the slash only.
78+
const expectedBase = selfClosing ? prefix + suffix.slice(1) : tag;
79+
const expected = `${expectedBase} style="opacity: 0.5"${selfClosing ? " /" : ""}>`;
80+
expect(applyPatch(tag + ">", "hero", op)).toBe(expected);
81+
expect(applyPatchByTarget(tag + ">", { selector: ".hero" }, op)).toBe(expected);
82+
}
83+
},
84+
);
85+
6986
it("patches inline move styles by target", () => {
7087
const html = `<div id="card" style="position: absolute; left: 108px; top: 112px"></div>`;
7188

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ function patchInlineStyleInTag(
212212
} else {
213213
// No existing style attribute
214214
if (value === null) return html; // nothing to remove
215-
const selfClosing = /\s*\/$/.test(tag);
216-
const base = selfClosing ? tag.replace(/\s*\/$/, "") : tag;
215+
const selfClosing = tag.endsWith("/");
216+
const base = selfClosing ? tag.slice(0, -1).trimEnd() : tag;
217217
const newTag = `${base} style="${prop}: ${escapeStyleAttributeValue(value, '"')}"${selfClosing ? " /" : ""}`;
218218
return html.replace(tag, newTag);
219219
}

0 commit comments

Comments
 (0)