Skip to content

Commit

Permalink
Preserve namespaced attributes when using expressions (#732)
Browse files Browse the repository at this point in the history
* add test fixture

* fix: signal to not add space for namespaced attributes

* pass `false` instead

* fix test

* changeset

* correct argument position

* fix(#6316): handle attribute namespaces

* chore: remove outdated logic

* chore: remove outdated code

* chore: update changeset

---------

Co-authored-by: Nate Moore <nate@astro.build>
  • Loading branch information
MoustaphaDev and natemoo-re authored Feb 22, 2023
1 parent 5eb4fff commit 2de6128
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-clocks-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Preserve namespaced attributes when using expressions
14 changes: 8 additions & 6 deletions internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,25 @@ func (p *printer) printAttributesToObject(n *astro.Node) {
if a.Key == "set:text" || a.Key == "set:html" || a.Key == "is:raw" {
continue
}
if a.Namespace != "" {
a.Key = fmt.Sprintf(`%s:%s`, a.Namespace, a.Key)
}
lastAttributeSkipped = false
switch a.Type {
case astro.QuotedAttribute:
p.addSourceMapping(a.KeyLoc)
p.print(`"` + a.Key + `"`)
p.printf(`"%s"`, a.Key)
p.print(":")
p.addSourceMapping(a.ValLoc)
p.print(`"` + escapeDoubleQuote(a.Val) + `"`)
case astro.EmptyAttribute:
p.addSourceMapping(a.KeyLoc)
p.print(`"` + a.Key + `"`)
p.printf(`"%s"`, a.Key)
p.print(":")
p.print("true")
case astro.ExpressionAttribute:
p.addSourceMapping(a.KeyLoc)
p.print(`"` + a.Key + `"`)
p.printf(`"%s"`, a.Key)
p.print(":")
p.addSourceMapping(a.ValLoc)
if a.Val == "" {
Expand All @@ -305,7 +308,7 @@ func (p *printer) printAttributesToObject(n *astro.Node) {
p.print(`(` + strings.TrimSpace(a.Key) + `)`)
case astro.TemplateLiteralAttribute:
p.addSourceMapping(a.KeyLoc)
p.print(`"` + strings.TrimSpace(a.Key) + `"`)
p.printf(`"%s"`, strings.TrimSpace(a.Key))
p.print(":")
p.print("`" + strings.TrimSpace(a.Key) + "`")
}
Expand All @@ -323,8 +326,7 @@ func (p *printer) printAttribute(attr astro.Attribute, n *astro.Node) {
}

if attr.Namespace != "" {
p.print(attr.Namespace)
p.print(":")
attr.Key = fmt.Sprintf("%s:%s", attr.Namespace, attr.Key)
}

switch attr.Type {
Expand Down
14 changes: 14 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,20 @@ import { Container, Col, Row } from 'react-bootstrap';
code: `${$$maybeRenderHead($$result)}<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect xlink:href="#id"></rect></svg>`,
},
},
{
name: "Preserve namespaces in expressions",
source: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect xlink:href={` + BACKTICK + `#${iconId}` + BACKTICK + `}></svg>`,
want: want{
code: `${$$maybeRenderHead($$result)}<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect ${$$addAttribute(` + BACKTICK + `#${iconId}` + BACKTICK + `, "xlink:href")}></rect></svg>`,
},
},
{
name: "Preserve namespaces for components",
source: `<Component some:thing="foobar">`,
want: want{
code: `${$$renderComponent($$result,'Component',Component,{"some:thing":"foobar"})}`,
},
},
{
name: "import.meta.env",
source: fmt.Sprintf(`---
Expand Down

0 comments on commit 2de6128

Please sign in to comment.