Skip to content

Commit c048567

Browse files
authored
Fix <select> elements with expression children (#371)
* fix(#366): return to inBodyIM after expression inside inSelectIM * chore: add changeset
1 parent 15e3ff8 commit c048567

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

.changeset/nice-bulldogs-unite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
Fix edge case with `select` elements and expression children

internal/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,7 @@ func inSelectIM(p *parser) bool {
22052205
case StartExpressionToken:
22062206
p.addExpression()
22072207
p.afe = append(p.afe, &scopeMarker)
2208-
p.setOriginalIM()
2208+
p.originalIM = inBodyIM
22092209
p.im = inExpressionIM
22102210
return true
22112211
case EndExpressionToken:

internal/printer/printer_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,13 @@ const items = ["Dog", "Cat", "Platipus"];
16231623
code: `<html>${$$renderComponent($$result,'Component',Component,{},{"default": () => $$render` + BACKTICK + `<form><textarea></textarea></form>` + BACKTICK + `,})}</html>`,
16241624
},
16251625
},
1626+
{
1627+
name: "select in form",
1628+
source: `<form><select>{options.map((option) => (<option value={option.id}>{option.title}</option>))}</select><div><label>Title 3</label><input type="text" /></div><button type="submit">Submit</button></form>`,
1629+
want: want{
1630+
code: `<form><select>${options.map((option) => ($$render` + BACKTICK + `<option${$$addAttribute(option.id, "value")}>${option.title}</option>` + BACKTICK + `))}</select><div><label>Title 3</label><input type="text"></div><button type="submit">Submit</button></form>`,
1631+
},
1632+
},
16261633
{
16271634
name: "slot inside of Base",
16281635
source: `<Base title="Home"><div>Hello</div></Base>`,

internal/token_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ func TestBasic(t *testing.T) {
346346
`<select>{[1, 2, 3].map(num => <option>{num}</option>)}</select>`,
347347
[]TokenType{StartTagToken, StartExpressionToken, TextToken, StartTagToken, StartExpressionToken, TextToken, EndExpressionToken, EndTagToken, TextToken, EndExpressionToken, EndTagToken},
348348
},
349+
{
350+
"select with expression",
351+
`<select>{[1, 2, 3].map(num => <option>{num}</option>)}</select><div>Hello</div>`,
352+
[]TokenType{StartTagToken, StartExpressionToken, TextToken, StartTagToken, StartExpressionToken, TextToken, EndExpressionToken, EndTagToken, TextToken, EndExpressionToken, EndTagToken, StartTagToken, TextToken, EndTagToken},
353+
},
349354
{
350355
"Markdown codeblock",
351356
fmt.Sprintf(`<Markdown>

0 commit comments

Comments
 (0)