From 5467f40beab2119f495340662504505a65f3250f Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Mon, 1 Apr 2024 09:40:01 -0400 Subject: [PATCH] Fix issue with head content pushed into body (#990) * Add nested head content test * Fix issue with head content pushed into body --- .changeset/khaki-schools-reflect.md | 5 +++++ internal/parser.go | 2 ++ internal/printer/printer_test.go | 31 +++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 .changeset/khaki-schools-reflect.md diff --git a/.changeset/khaki-schools-reflect.md b/.changeset/khaki-schools-reflect.md new file mode 100644 index 000000000..4de81a006 --- /dev/null +++ b/.changeset/khaki-schools-reflect.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Fix issue with head content being pushed into body diff --git a/internal/parser.go b/internal/parser.go index 18275683a..12c610163 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -392,6 +392,7 @@ func (p *parser) addExpression() { HandledScript: false, Loc: p.generateLoc(), }) + } func isFragment(data string) bool { @@ -942,6 +943,7 @@ func inHeadIM(p *parser) bool { case StartExpressionToken: p.addExpression() p.afe = append(p.afe, &scopeMarker) + p.templateStack = append(p.templateStack, inExpressionIM) if p.originalIM == nil { p.setOriginalIM() } diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go index 938e3c4e8..bbcfb40cb 100644 --- a/internal/printer/printer_test.go +++ b/internal/printer/printer_test.go @@ -551,6 +551,7 @@ import type data from "test" } `, + want: want{ code: `${$$renderComponent($$result,'Layout',Layout,{"title":"Welcome to Astro."},{"default": () => $$render` + BACKTICK + ` ${$$maybeRenderHead($$result)}
@@ -569,8 +570,8 @@ import type data from "test"

p+h2 ${dummyKey}

- ` + BACKTICK + ` - ); + ` + BACKTICK + ` + ); }) }
@@ -3505,6 +3506,31 @@ const items = ["Dog", "Cat", "Platipus"]; code: `${$$renderComponent($$result,'Component',Component,{})}${(void 0)}`, }, }, + { + name: "nested head content stays in the head", + source: `--- +const meta = { title: 'My App' }; +--- + + + + + + { + meta && {meta.title} + } + + + + +

My App

+ +`, + want: want{ + frontmatter: []string{"", `const meta = { title: 'My App' };`}, + code: ` ${ meta && $$render` + BACKTICK + `${meta.title}` + BACKTICK + ` } ${$$renderHead($$result)}

My App

`, + }, + }, } for _, tt := range tests { @@ -3535,6 +3561,7 @@ const items = ["Dog", "Cat", "Platipus"]; RenderScript: tt.transformOptions.RenderScript, } transform.Transform(doc, transformOptions, h) // note: we want to test Transform in context here, but more advanced cases could be tested separately + result := PrintToJS(code, doc, 0, transform.TransformOptions{ Scope: "XXXX", InternalURL: "http://localhost:3000/",