Skip to content

Commit

Permalink
Support expressions in SVG elements (#164)
Browse files Browse the repository at this point in the history
* Add failing test to printer_test.go

* Add fix to parser.go

* Add advanced test to parser.go

* remove/fulfill TODO

* changeset
Jonathan Neal authored Nov 23, 2021
1 parent bca7e00 commit 44ee189
Showing 3 changed files with 35 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/perfect-terms-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Fixed issue where expressions did not work within SVG elements
9 changes: 8 additions & 1 deletion internal/parser.go
Original file line number Diff line number Diff line change
@@ -2533,7 +2533,6 @@ func ignoreTheRemainingTokens(p *parser) bool {

const whitespaceOrNUL = whitespace + "\x00"

// TODO: handle expressions in SVG
// Section 12.2.6.5
func parseForeignContent(p *parser) bool {
switch p.tok.Type {
@@ -2612,6 +2611,14 @@ func parseForeignContent(p *parser) bool {
}
}
return true
case StartExpressionToken:
p.reconstructActiveFormattingElements()
p.addExpression()
return true
case EndExpressionToken:
p.addLoc()
p.oe.pop()
return true
default:
// Ignore the token.
}
22 changes: 22 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
@@ -850,6 +850,28 @@ import ZComponent from '../components/ZComponent.jsx';`},
code: `<html><head></head><body><svg><style>path { fill: red; }</style></svg></body></html>`,
},
},
{
name: "svg expressions",
source: `---
const title = 'icon';
---
<svg>{title ?? null}</svg>`,
want: want{
frontmatter: []string{"", "const title = 'icon';"},
code: `<html><head></head><body><svg>${title ?? null}</svg></body></html>`,
},
},
{
name: "advanced svg expression",
source: `---
const title = 'icon';
---
<svg>{title ? <title>{title}</title> : null}</svg>`,
want: want{
frontmatter: []string{"", "const title = 'icon';"},
code: `<html><head></head><body><svg>${title ? $$render` + BACKTICK + `<title>${title}</title>` + BACKTICK + ` : null}</svg></body></html>`,
},
},
{
name: "Empty script",
source: `<script hoist></script>`,

0 comments on commit 44ee189

Please sign in to comment.