Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression parsing in foreign context #1062

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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