Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with head content pushed into body #990

Merged
merged 5 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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