Skip to content

Commit

Permalink
Fix table parsing with expressions (#925)
Browse files Browse the repository at this point in the history
* add test

* consume text tokens in inHeadIM with the textIM

* simplify resetting IM after closed expression

* fix voluntarily incorrect test

* remove apparently unneeded code

* remove outdated comment

* chore: changeset
  • Loading branch information
MoustaphaDev authored Jan 2, 2024
1 parent 4de359b commit 14ccba5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-fishes-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fixes an issue where a `tr` element which contained an expression would cause its parent table to swallow any trailing element inside said table
14 changes: 2 additions & 12 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ func inHeadIM(p *parser) bool {
return true
}
p.tok.Data = s
return textIM(p)
} else if p.oe.top() != nil && (isComponent(p.oe.top().Data) || isFragment((p.oe.top().Data))) {
p.addText(p.tok.Data)
return true
Expand Down Expand Up @@ -2730,10 +2731,6 @@ func inLiteralIM(p *parser) bool {
}

func inExpressionIM(p *parser) bool {
if p.oe.contains(a.Table) {
p.clearActiveFormattingElements()
return inLiteralIM(p)
}
switch p.tok.Type {
case ErrorToken:
p.oe.pop()
Expand Down Expand Up @@ -2780,14 +2777,7 @@ func inExpressionIM(p *parser) bool {
case EndExpressionToken:
p.addLoc()
p.oe.pop()
nextOpenElement := p.oe.top()
if nextOpenElement == nil {
return true
}
// only switch the insertion mode when we're no longer inside an expression
if !nextOpenElement.Parent.Expression {
p.im = textIM
}
p.resetInsertionMode()
return true
case CommentToken:
p.addChild(&Node{
Expand Down
14 changes: 10 additions & 4 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2020,8 +2020,6 @@ const content = "lol";
`,
want: want{
frontmatter: []string{"", `const content = "lol";`},
// TODO: This output is INCORRECT, but we're testing a regression
// The trailing text (`Hello`) shouldn't be consumed by the <table> element!
code: `<html>
${$$maybeRenderHead($$result)}<body>
<table>
Expand All @@ -2034,8 +2032,9 @@ const content = "lol";
<td>1</td>
</tr>` + BACKTICK + `
)
} Hello
</table></body>
}
</table>Hello
</body>
</html>`,
},
},
Expand All @@ -2057,6 +2056,13 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$maybeRenderHead($$result)}<table><caption>${title}</caption><tr><td>Hello</td></tr></table>`,
},
},
{
name: "table expression with trailing div",
source: `<table><tr><td>{title}</td></tr></table><div>Div</div>`,
want: want{
code: `${$$maybeRenderHead($$result)}<table><tr><td>${title}</td></tr></table><div>Div</div>`,
},
},
{
name: "tbody expressions",
source: `---
Expand Down

0 comments on commit 14ccba5

Please sign in to comment.