From 8303867d36e50321049a2a2b480f87b9a3922d71 Mon Sep 17 00:00:00 2001 From: dpiercey Date: Tue, 12 Aug 2025 16:13:15 -0700 Subject: [PATCH] feat: strip marko generated scripts when normalizing --- src/shared.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/shared.ts b/src/shared.ts index ea947b5..fece7ef 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -37,7 +37,9 @@ export type FireObject = { const SHOW_ELEMENT = 1; const SHOW_COMMENT = 128; +const ELEMENT_NODE = 1; const COMMENT_NODE = 8; +const MARKO_SCRIPT_REG = /\b\$MC\b|\bM\._\b|\("M"\)\("_"\)/; export function normalize(container: T) { const idMap: Map = new Map(); @@ -52,7 +54,7 @@ export function normalize(container: T) { let nextNode = commentAndElementWalker.nextNode(); while ((node = nextNode as Comment | Element)) { nextNode = commentAndElementWalker.nextNode(); - if (isComment(node)) { + if (isComment(node) || isMarkoScript(node)) { node.remove(); } else { const { id, attributes } = node; @@ -184,3 +186,16 @@ function waitForBatchedUpdates() { function isComment(node: Node): node is Comment { return node.nodeType === COMMENT_NODE; } + +function isElement(node: Node): node is HTMLElement { + return node.nodeType === ELEMENT_NODE; +} + +function isMarkoScript(node: Node): node is HTMLScriptElement { + return ( + isElement(node) && + node.tagName === "SCRIPT" && + !(node as HTMLScriptElement).src && + MARKO_SCRIPT_REG.test(node.textContent || "") + ); +}