Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/studio/src/utils/sourcePatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ describe("applyPatchByTarget", () => {
expect(result).not.toContain("/ style");
});

it.each(["", " ", "\t\r\n", "\u00a0\u2028", " ".repeat(100_000)])(
"preserves self-closing whitespace handling (case %#)",
(whitespace) => {
const prefix = '<img id="hero" class="hero"';
const op: PatchOperation = { type: "inline-style", property: "opacity", value: "0.5" };
for (const suffix of ["/", "/\n", "/\r", "/\r\n", "/\u2028", "", "x", "/ "]) {
const tag = prefix + whitespace + suffix;
const selfClosing = suffix === "/";
// The original operation removes whitespace before the slash only.
const expectedBase = selfClosing ? prefix + suffix.slice(1) : tag;
const expected = `${expectedBase} style="opacity: 0.5"${selfClosing ? " /" : ""}>`;
expect(applyPatch(tag + ">", "hero", op)).toBe(expected);
expect(applyPatchByTarget(tag + ">", { selector: ".hero" }, op)).toBe(expected);
}
},
);

it("patches inline move styles by target", () => {
const html = `<div id="card" style="position: absolute; left: 108px; top: 112px"></div>`;

Expand Down
4 changes: 2 additions & 2 deletions packages/studio/src/utils/sourcePatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ function patchInlineStyleInTag(
} else {
// No existing style attribute
if (value === null) return html; // nothing to remove
const selfClosing = /\s*\/$/.test(tag);
const base = selfClosing ? tag.replace(/\s*\/$/, "") : tag;
const selfClosing = tag.endsWith("/");
const base = selfClosing ? tag.slice(0, -1).trimEnd() : tag;
const newTag = `${base} style="${prop}: ${escapeStyleAttributeValue(value, '"')}"${selfClosing ? " /" : ""}`;
return html.replace(tag, newTag);
}
Expand Down
Loading