diff --git a/packages/core/src/compiler/htmlDocument.test.ts b/packages/core/src/compiler/htmlDocument.test.ts
index e9ae8a4730..615fd6c66c 100644
--- a/packages/core/src/compiler/htmlDocument.test.ts
+++ b/packages/core/src/compiler/htmlDocument.test.ts
@@ -196,6 +196,14 @@ describe("htmlDocument helpers", () => {
);
});
+ it("puts markup before the outer , past a nested one and a commented one", () => {
+ const sub =
+ "
\n";
+ expect(insertBeforeCloseTag(sub, "template", "X")).toBe(
+ "
X\n",
+ );
+ });
+
it("ends a comment at --!>, but not on its own opening dashes", () => {
expect(insertBeforeCloseTag("
", "head", "X")).toBe(
"X",
diff --git a/packages/core/src/compiler/htmlDocument.ts b/packages/core/src/compiler/htmlDocument.ts
index f18c994dd8..cf98279623 100644
--- a/packages/core/src/compiler/htmlDocument.ts
+++ b/packages/core/src/compiler/htmlDocument.ts
@@ -207,7 +207,7 @@ function inlineScriptTags(scripts: readonly string[]): string {
const RAW_TEXT_TAGS = ["script", "style", "title", "textarea"] as const;
-type DocumentTag = "/g;
function* markupStarts(lowered: string): Generator {
@@ -290,15 +290,25 @@ function findRawTextClose(lowered: string, name: string, from: number): number {
return -1;
}
+function findOuterTemplateClose(html: string): number {
+ const lowered = lowerAscii(html);
+ let depth = 0;
+ for (const open of markupStarts(lowered)) {
+ if (isTagAt(lowered, open, "` or ``; null when it has none. */
+/** Insert markup before the document's own ``, `