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 = `
+kept
+ +{ 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;