Skip to content

Commit

Permalink
Fix expression parsing in foreign context (#1062)
Browse files Browse the repository at this point in the history
* fix(parser): persist the current namespace when adding an expression node

* test: add printer test for `image` tag inside svg context

* chore: add changeset
  • Loading branch information
Trombach authored Feb 7, 2025
1 parent 6b6a134 commit 970f085
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-geese-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Fixes an issue when parsing elements inside foreign content (e.g. SVG), when they were inside an expression
1 change: 1 addition & 0 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func (p *parser) addExpression() {
CustomElement: false,
HandledScript: false,
Loc: p.generateLoc(),
Namespace: p.top().Namespace,
})

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

[TestPrinter/namespace_is_preserved_when_inside_an_expression - 1]
## Input

```
<svg>{<image />}</svg>
```

## Output

```js
import {
Fragment,
render as $$render,
createAstro as $$createAstro,
createComponent as $$createComponent,
renderComponent as $$renderComponent,
renderHead as $$renderHead,
maybeRenderHead as $$maybeRenderHead,
unescapeHTML as $$unescapeHTML,
renderSlot as $$renderSlot,
mergeSlots as $$mergeSlots,
addAttribute as $$addAttribute,
spreadAttributes as $$spreadAttributes,
defineStyleVars as $$defineStyleVars,
defineScriptVars as $$defineScriptVars,
renderTransition as $$renderTransition,
createTransitionScope as $$createTransitionScope,
renderScript as $$renderScript,
createMetadata as $$createMetadata
} from "http://localhost:3000/";

export const $$metadata = $$createMetadata(import.meta.url, { modules: [], hydratedComponents: [], clientOnlyComponents: [], hydrationDirectives: new Set([]), hoisted: [] });

const $$Component = $$createComponent(($$result, $$props, $$slots) => {

return $$render`${$$maybeRenderHead($$result)}<svg>${$$render`<image></image>`}</svg>`;
}, undefined, undefined);
export default $$Component;
```
---
8 changes: 6 additions & 2 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -905,8 +905,8 @@ import Widget2 from '../components/Widget2.astro';
{
// maintain the original behavior, though it may be
// unneeded as renderScript is now on by default
name: "script external in expression (renderScript: false)",
source: `<main>{<script src="./hello.js"></script>}`,
name: "script external in expression (renderScript: false)",
source: `<main>{<script src="./hello.js"></script>}`,
filename: "/src/pages/index.astro",
},
{
Expand Down Expand Up @@ -2072,6 +2072,10 @@ const meta = { title: 'My App' };
</body>
</html>`,
},
{
name: "namespace is preserved when inside an expression",
source: `<svg>{<image />}</svg>`,
},
}
for _, tt := range tests {
if tt.only {
Expand Down

0 comments on commit 970f085

Please sign in to comment.