diff --git a/packages/studio-server/src/helpers/sourceMutation.test.ts b/packages/studio-server/src/helpers/sourceMutation.test.ts index 5d3ac7d212..126811032e 100644 --- a/packages/studio-server/src/helpers/sourceMutation.test.ts +++ b/packages/studio-server/src/helpers/sourceMutation.test.ts @@ -330,6 +330,84 @@ describe("patchElementInHtml", () => { expect(result).not.toContain(">B<"); }); + // fails without the fix: each of these wrote the given text into the script, style or template. + describe("a target that is code or page plumbing, not a layer", () => { + const PAGE = ` +
+
Hello
+
+ + +
+`; + const TEXT = "window.changed = 1"; + + for (const type of ["text-content", "rich-text"] as const) + for (const target of [ + { selector: "script#code" }, + { id: "code" }, + { selector: "#root > script" }, + { selector: "style" }, + { id: "css" }, + { id: "tpl" }, + ]) + it(`declines ${type} aimed at ${JSON.stringify(target)}`, () => { + const result = patchElementInHtml(PAGE, target, [{ type, property: "", value: TEXT }]); + expect(result.matched).toBe(false); + expect(result.html).toBe(PAGE); + }); + + it("declines every other operation on such a target too", () => { + for (const op of [ + { type: "inline-style", property: "color", value: "red" }, + { type: "html-attribute", property: "src", value: "https://example.com/a.js" }, + { type: "attribute", property: "data-start", value: "1" }, + { type: "ensure-id", property: "", value: "x" }, + ] as const) { + const result = patchElementInHtml(PAGE, { selector: "#root > script" }, [op]); + expect(result.matched, op.type).toBe(false); + expect(result.html, op.type).toBe(PAGE); + } + }); + + it("declines an operation whose child selector lands on a script", () => { + const result = patchElementInHtml(PAGE, { id: "wrap" }, [ + { type: "text-content", property: "", value: TEXT, childSelector: "script" }, + ]); + expect(result.matched).toBe(false); + expect(result.html).toBe(PAGE); + }); + + it("writes a layer's text as text when its only child is a script", () => { + const result = patchElementInHtml(PAGE, { id: "wrap" }, [ + { type: "text-content", property: "", value: TEXT }, + ]); + expect(result.matched).toBe(true); + const { document } = parseHTML(result.html); + expect(document.getElementById("inner")).toBeNull(); + expect(document.getElementById("wrap")?.textContent).toBe(TEXT); + expect(document.querySelectorAll("script").length).toBe(1); + }); + + it("still saves a text edit of an ordinary layer on the same page", () => { + const result = patchElementInHtml(PAGE, { id: "hero" }, [ + { type: "text-content", property: "", value: "New" }, + ]); + expect(result.matched).toBe(true); + expect(result.html).toContain(">New"); + expect(result.html).toContain("window.ready = true;"); + }); + + it("still patches a layer inside a template", () => { + const result = patchElementInHtml(PAGE, { selector: "template p" }, [ + { type: "text-content", property: "", value: "edited" }, + ]); + expect(result.matched).toBe(true); + expect(result.html).toContain(" { const { html: result, matched } = patchElementInHtml(FIXTURE, { id: "nonexistent" }, [ { type: "inline-style", property: "color", value: "red" }, diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts index 94d05034aa..59021f03a2 100644 --- a/packages/studio-server/src/helpers/sourceMutation.ts +++ b/packages/studio-server/src/helpers/sourceMutation.ts @@ -305,6 +305,10 @@ function stampNewChildIds(parent: Element): void { } } +function isNonContentElement(el: Element): boolean { + return EXCLUDED_TAGS.has(el.tagName.toLowerCase()); +} + // fallow-ignore-next-line complexity export function patchElementInHtml( source: string, @@ -315,13 +319,14 @@ export function patchElementInHtml( const { document, wrappedFragment } = parseSourceDocument(source); const el = findTargetElement(document, target); if (!el || !isHTMLElement(el)) return { html: source, matched: false }; + if (isNonContentElement(el)) return { html: source, matched: false }; const htmlEl = el; const originalHtml = wrappedFragment ? document.body.innerHTML || "" : document.toString(); const resolved: ResolvedPatchOperation[] = []; for (const op of operations) { const opTarget = resolveOperationTarget(htmlEl, op); - if (!opTarget) return { html: source, matched: false }; + if (!opTarget || isNonContentElement(opTarget)) return { html: source, matched: false }; resolved.push({ op, target: opTarget }); } @@ -368,7 +373,8 @@ export function patchElementInHtml( case "text-content": if (op.value != null) { const inner = opTarget.children.length === 1 ? opTarget.firstElementChild : null; - const textTarget = inner && isHTMLElement(inner) ? inner : opTarget; + const textTarget = + inner && isHTMLElement(inner) && !isNonContentElement(inner) ? inner : opTarget; textTarget.textContent = op.value; } break;