Skip to content

Commit 852fc1b

Browse files
authored
fix: index out of range [0] error when compent is before the html tag (#891)
* test: add test * fix * chore: changeset
1 parent 53bc69c commit 852fc1b

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

.changeset/late-readers-reflect.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 `index out of range [0]` error when there is a component before the `<html>` tag

internal/parser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,9 @@ func inBodyIM(p *parser) bool {
11541154
if p.oe.contains(a.Template) {
11551155
return true
11561156
}
1157-
copyAttributes(p.oe[0], p.tok)
1157+
if len(p.oe) > 0 {
1158+
copyAttributes(p.oe[0], p.tok)
1159+
}
11581160
case a.Base, a.Basefont, a.Bgsound, a.Link, a.Meta, a.Noframes, a.Script, a.Style, a.Template, a.Title:
11591161
return inHeadIM(p)
11601162
case a.Body:

internal/transform/transform_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func TestFullTransform(t *testing.T) {
251251
`,
252252
want: `<Component><h1>Hello world</h1></Component>`,
253253
},
254+
{
255+
name: "Component before html",
256+
source: `<Navigation /><html><body><h1>Astro</h1></body></html>`,
257+
want: `<Navigation></Navigation><h1>Astro</h1>`,
258+
},
254259
{
255260
name: "respects explicitly authored elements",
256261
source: `<html><Component /></html>`,

0 commit comments

Comments
 (0)