Skip to content

Commit

Permalink
Fix style tag moving inside html (#587)
Browse files Browse the repository at this point in the history
* fix(#585): followup to fix style tag moving inside html

* chore: remove change from diff

* chore: update test

* chore: add changeset

Co-authored-by: Nate Moore <[email protected]>
  • Loading branch information
natemoo-re and natemoo-re authored Oct 24, 2022
1 parent fd2fc28 commit 6b204bd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/thin-wasps-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fix bug with trailing `style` tags being moved into the `html` element
5 changes: 5 additions & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,11 @@ func inBodyIM(p *parser) bool {
p.framesetOK = false
}
case StartTagToken:
// if literal and we only have html and body open
if p.literal {
p.im = inLiteralIM
return false
}
// It's possible we were moved here from inHeadIM
// via the children of a Component. We need to clear the originalIM
// and switch the implicit `head` tag to `body`
Expand Down
10 changes: 10 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2545,8 +2545,18 @@ const c = '\''
{
name: "style after html",
source: `<html><body><h1>Hello world!</h1></body></html><style></style>`,
want: []ASTNode{{Type: "element", Name: "html", Children: []ASTNode{{Type: "element", Name: "body", Children: []ASTNode{{Type: "element", Name: "h1", Children: []ASTNode{{Type: "text", Value: "Hello world!"}}}}}}}, {Type: "element", Name: "style"}},
},
{
name: "style in html",
source: `<html><body><h1>Hello world!</h1></body><style></style></html>`,
want: []ASTNode{{Type: "element", Name: "html", Children: []ASTNode{{Type: "element", Name: "body", Children: []ASTNode{{Type: "element", Name: "h1", Children: []ASTNode{{Type: "text", Value: "Hello world!"}}}}}, {Type: "element", Name: "style"}}}},
},
{
name: "style in body",
source: `<html><body><h1>Hello world!</h1><style></style></body></html>`,
want: []ASTNode{{Type: "element", Name: "html", Children: []ASTNode{{Type: "element", Name: "body", Children: []ASTNode{{Type: "element", Name: "h1", Children: []ASTNode{{Type: "text", Value: "Hello world!"}}}, {Type: "element", Name: "style"}}}}}},
},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/test/parse/orphan-head.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('orphan head', () => {
assert.ok(result, 'able to parse');

const [doctype, html, ...others] = result.ast.children;
assert.equal(others.length, 0, `Expected only two child nodes!`);
assert.equal(others.length, 1, `Expected only three child nodes!`);
assert.equal(doctype.type, 'doctype', `Expected first child node to be of type "doctype"`);
assert.equal(html.type, 'element', `Expected first child node to be of type "element"`);
});
Expand Down

0 comments on commit 6b204bd

Please sign in to comment.