diff --git a/.changeset/grumpy-clocks-jump.md b/.changeset/grumpy-clocks-jump.md
new file mode 100644
index 000000000..5f71d99c8
--- /dev/null
+++ b/.changeset/grumpy-clocks-jump.md
@@ -0,0 +1,5 @@
+---
+'@astrojs/compiler': patch
+---
+
+Preserve namespaced attributes when using expressions
diff --git a/internal/printer/printer.go b/internal/printer/printer.go
index e71a73cf6..eeaaf0977 100644
--- a/internal/printer/printer.go
+++ b/internal/printer/printer.go
@@ -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 == "" {
@@ -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) + "`")
}
@@ -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 {
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 83f6847a8..059daaf29 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -1665,6 +1665,20 @@ import { Container, Col, Row } from 'react-bootstrap';
code: `${$$maybeRenderHead($$result)}`,
},
},
+ {
+ name: "Preserve namespaces in expressions",
+ source: ``,
+ want: want{
+ code: `${$$maybeRenderHead($$result)}`,
+ },
+ },
+ {
+ name: "Preserve namespaces for components",
+ source: ``,
+ want: want{
+ code: `${$$renderComponent($$result,'Component',Component,{"some:thing":"foobar"})}`,
+ },
+ },
{
name: "import.meta.env",
source: fmt.Sprintf(`---