Skip to content

Commit

Permalink
Fix issue with head content pushed into body (#990)
Browse files Browse the repository at this point in the history
* Add nested head content test

* Fix issue with head content pushed into body
  • Loading branch information
matthewp authored Apr 1, 2024
1 parent 854ac4f commit 5467f40
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-schools-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix issue with head content being pushed into body
2 changes: 2 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ func (p *parser) addExpression() {
HandledScript: false,
Loc: p.generateLoc(),
})

}

func isFragment(data string) bool {
Expand Down Expand Up @@ -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()
}
Expand Down
31 changes: 29 additions & 2 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ import type data from "test"
}
</main>
</Layout>`,

want: want{
code: `${$$renderComponent($$result,'Layout',Layout,{"title":"Welcome to Astro."},{"default": () => $$render` + BACKTICK + `
${$$maybeRenderHead($$result)}<main>
Expand All @@ -569,8 +570,8 @@ import type data from "test"
</div>
<p>
</p><h2>p+h2 ${dummyKey}</h2>
` + BACKTICK + `
);
` + BACKTICK + `
);
})
}
</main>
Expand Down Expand Up @@ -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' };
---
<html>
<head>
<meta charset="utf-8" />
{
meta && <title>{meta.title}</title>
}
<meta name="after">
</head>
<body>
<h1>My App</h1>
</body>
</html>`,
want: want{
frontmatter: []string{"", `const meta = { title: 'My App' };`},
code: `<html> <head> <meta charset="utf-8"> ${ meta && $$render` + BACKTICK + `<title>${meta.title}</title>` + BACKTICK + ` } <meta name="after"> ${$$renderHead($$result)}</head> <body> <h1>My App</h1> </body></html>`,
},
},
}

for _, tt := range tests {
Expand Down Expand Up @@ -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/",
Expand Down

0 comments on commit 5467f40

Please sign in to comment.