From 9c918ad531ac6fe25c1c99211dacbf7e0595d308 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Thu, 11 Jan 2024 12:38:31 -0600
Subject: [PATCH 01/21] fix(#935): set transform to literal mode
---
.changeset/shiny-plums-smile.md | 5 ++++
cmd/astro-wasm/astro-wasm.go | 6 ++---
internal/printer/printer_test.go | 15 +++++++++++-
internal/transform/transform.go | 3 +--
internal/transform/transform_test.go | 18 +++++++++-----
.../compiler/test/basic/body-in-component.ts | 24 +++++++++++++++++++
.../compiler/test/basic/trailing-spaces-ii.ts | 2 +-
7 files changed, 60 insertions(+), 13 deletions(-)
create mode 100644 .changeset/shiny-plums-smile.md
create mode 100644 packages/compiler/test/basic/body-in-component.ts
diff --git a/.changeset/shiny-plums-smile.md b/.changeset/shiny-plums-smile.md
new file mode 100644
index 000000000..c928011c1
--- /dev/null
+++ b/.changeset/shiny-plums-smile.md
@@ -0,0 +1,5 @@
+---
+'@astrojs/compiler': minor
+---
+
+Fixes an edge case that caused `html` and `body` tags with attributes to be ignored when they were wrapped in a component.
diff --git a/cmd/astro-wasm/astro-wasm.go b/cmd/astro-wasm/astro-wasm.go
index ce652b850..0e0c4c369 100644
--- a/cmd/astro-wasm/astro-wasm.go
+++ b/cmd/astro-wasm/astro-wasm.go
@@ -226,7 +226,7 @@ func Parse() any {
h := handler.NewHandler(source, parseOptions.Filename)
var doc *astro.Node
- doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h), astro.ParseOptionEnableLiteral(true))
+ doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
h.AppendError(err)
}
@@ -250,7 +250,7 @@ func ConvertToTSX() any {
h := handler.NewHandler(source, transformOptions.Filename)
var doc *astro.Node
- doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h), astro.ParseOptionEnableLiteral(true))
+ doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
h.AppendError(err)
}
@@ -301,7 +301,7 @@ func Transform() any {
}
}()
- doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionWithHandler(h))
+ doc, err := astro.ParseWithOptions(strings.NewReader(source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
reject.Invoke(wasm_utils.ErrorToJSError(h, err))
return
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index b5380effa..b54c28581 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -1411,6 +1411,14 @@ const name = 'named';
code: "${cond && $$render``}${cond && $$render`My title`}",
},
},
+ {
+ name: "top-level component does not drop body attributes",
+ source: ``,
+ only: true,
+ want: want{
+ code: "${$$renderComponent($$result,'Base',Base,{},{\"default\": () => $$render`${$$maybeRenderHead($$result)}${$$renderSlot($$result,$$slots[\"default\"])}`,})}",
+ },
+ },
{
name: "custom elements",
source: `---
@@ -3227,8 +3235,8 @@ const items = ["Dog", "Cat", "Platipus"];
// transform output from source
code := test_utils.Dedent(tt.source)
- doc, err := astro.Parse(strings.NewReader(code))
h := handler.NewHandler(code, "")
+ doc, err := astro.ParseWithOptions(strings.NewReader(code), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
t.Error(err)
@@ -3481,6 +3489,11 @@ const c = '\''
source: ``,
want: []ASTNode{{Type: "element", Name: "main", Attributes: []ASTNode{{Type: "attribute", Kind: "template-literal", Name: "id", Value: "gotcha", Raw: "`gotcha"}}}},
},
+ {
+ name: "top-level component does not drop body attributes",
+ source: ``,
+ want: []ASTNode{{Type: "component", Name: "Base", Attributes: []ASTNode{}, Children: []ASTNode{{Type: "element", Name: "body", Attributes: []ASTNode{{Type: "attribute", Kind: "quoted", Name: "class", Value: "foobar", Raw: "\"foobar\""}}, Children: []ASTNode{{Type: "element", Name: "slot"}}}}}},
+ },
}
for _, tt := range tests {
diff --git a/internal/transform/transform.go b/internal/transform/transform.go
index d4240adbb..fedf82760 100644
--- a/internal/transform/transform.go
+++ b/internal/transform/transform.go
@@ -234,7 +234,7 @@ func TrimTrailingSpace(doc *astro.Node) {
return
}
- if doc.LastChild.Type == astro.TextNode {
+ if doc.LastChild.Type == astro.TextNode && len(doc.LastChild.Data) < len(strings.TrimRightFunc(doc.LastChild.Data, unicode.IsSpace)) {
doc.LastChild.Data = strings.TrimRightFunc(doc.LastChild.Data, unicode.IsSpace)
return
}
@@ -246,7 +246,6 @@ func TrimTrailingSpace(doc *astro.Node) {
n = n.LastChild
continue
} else {
- n = nil
break
}
}
diff --git a/internal/transform/transform_test.go b/internal/transform/transform_test.go
index a52dc11b6..2eaf49e41 100644
--- a/internal/transform/transform_test.go
+++ b/internal/transform/transform_test.go
@@ -253,13 +253,13 @@ func TestFullTransform(t *testing.T) {
},
{
name: "Component before html I",
- source: `Astro
`,
- want: `Astro
`,
+ source: `Astro
`,
+ want: `Astro
`,
},
{
name: "Component before html II",
source: ``,
- want: ``,
+ want: ``,
},
{
name: "respects explicitly authored elements",
@@ -281,6 +281,11 @@ func TestFullTransform(t *testing.T) {
source: ``,
want: ``,
},
+ {
+ name: "top-level component does not drop body attributes",
+ source: ``,
+ want: ``,
+ },
{
name: "works with nested components",
source: `
`,
@@ -302,7 +307,7 @@ func TestFullTransform(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.Reset()
- doc, err := astro.Parse(strings.NewReader(tt.source))
+ doc, err := astro.ParseWithOptions(strings.NewReader(tt.source), astro.ParseOptionEnableLiteral(true))
if err != nil {
t.Error(err)
}
@@ -513,11 +518,12 @@ func TestAnnotation(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b.Reset()
- doc, err := astro.Parse(strings.NewReader(tt.source))
+
+ h := handler.NewHandler(tt.source, "/src/pages/index.astro")
+ doc, err := astro.ParseWithOptions(strings.NewReader(tt.source), astro.ParseOptionEnableLiteral(true), astro.ParseOptionWithHandler(h))
if err != nil {
t.Error(err)
}
- h := handler.NewHandler(tt.source, "/src/pages/index.astro")
Transform(doc, TransformOptions{
AnnotateSourceFile: true,
Filename: "/src/pages/index.astro",
diff --git a/packages/compiler/test/basic/body-in-component.ts b/packages/compiler/test/basic/body-in-component.ts
new file mode 100644
index 000000000..5776a1c8e
--- /dev/null
+++ b/packages/compiler/test/basic/body-in-component.ts
@@ -0,0 +1,24 @@
+import { test } from 'uvu';
+import * as assert from 'uvu/assert';
+import { transform } from '@astrojs/compiler';
+
+const FIXTURE = `
+---
+let value = 'world';
+---
+
+
+`;
+
+let result;
+test.before(async () => {
+ result = await transform(FIXTURE);
+});
+
+test('top-level component does not drop body attributes', () => {
+ console.log(result.code);
+ assert.match(result.code, "${$$renderComponent($$result,'Base',Base,{},{\"default\": () => $$render`${$$maybeRenderHead($$result)}${$$renderSlot($$result,$$slots[\"default\"])}`,})}", `Expected body to be included!`);
+});
+
+
+test.run();
diff --git a/packages/compiler/test/basic/trailing-spaces-ii.ts b/packages/compiler/test/basic/trailing-spaces-ii.ts
index 198626215..44277dcb2 100644
--- a/packages/compiler/test/basic/trailing-spaces-ii.ts
+++ b/packages/compiler/test/basic/trailing-spaces-ii.ts
@@ -23,7 +23,7 @@ test.before(async () => {
});
test('trailing space', () => {
- assert.ok(result.code, 'Expected to compiler');
+ assert.ok(result.code, 'Expected to compile');
assert.match(
result.code,
`
From cff221c92e90a335d45a9825216bb9f613b0ad2f Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Thu, 11 Jan 2024 15:13:01 -0600
Subject: [PATCH 02/21] fix: whitespace handling
---
internal/transform/transform.go | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/internal/transform/transform.go b/internal/transform/transform.go
index fedf82760..e7e81e12b 100644
--- a/internal/transform/transform.go
+++ b/internal/transform/transform.go
@@ -253,7 +253,11 @@ func TrimTrailingSpace(doc *astro.Node) {
// Collapse all trailing text nodes
for n != nil && n.Type == astro.TextNode {
n.Data = strings.TrimRightFunc(n.Data, unicode.IsSpace)
- n = n.PrevSibling
+ if len(n.Data) > 0 {
+ break
+ } else {
+ n = n.PrevSibling
+ }
}
}
From eaa580a962c7f81a47510091a2aab93c79c2e745 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Thu, 11 Jan 2024 15:13:21 -0600
Subject: [PATCH 03/21] chore: remove only
---
internal/printer/printer_test.go | 1 -
1 file changed, 1 deletion(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index b54c28581..d989c7de7 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -1414,7 +1414,6 @@ const name = 'named';
{
name: "top-level component does not drop body attributes",
source: ``,
- only: true,
want: want{
code: "${$$renderComponent($$result,'Base',Base,{},{\"default\": () => $$render`${$$maybeRenderHead($$result)}${$$renderSlot($$result,$$slots[\"default\"])}`,})}",
},
From 4abbf3c61c067a4e1baef226c2e1688353a1d890 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Thu, 11 Jan 2024 16:28:48 -0600
Subject: [PATCH 04/21] chore: fix test typos
---
internal/printer/printer_test.go | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index d989c7de7..8905eb08f 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -226,7 +226,7 @@ func TestPrinter(t *testing.T) {
},
{
name: "slot with fallback",
- source: `Hello world!
`,
+ source: `Hello world!
`,
want: want{
code: `${$$maybeRenderHead($$result)}${$$renderSlot($$result,$$slots["default"],$$render` + BACKTICK + `Hello world!
` + BACKTICK + `)}`,
},
@@ -1477,7 +1477,7 @@ ${$$renderComponent($$result,'my-element','my-element',{"client:load":true,"clie
},
{
name: "Self-closing script in head works",
- source: ``,
+ source: ``,
want: want{
code: `` + RENDER_HEAD_RESULT + ``,
},
@@ -1498,7 +1498,7 @@ ${$$renderComponent($$result,'my-element','my-element',{"client:load":true,"clie
},
{
name: "Self-closing components in head can have siblings",
- source: ``,
+ source: ``,
want: want{
code: `${$$renderComponent($$result,'BaseHead',BaseHead,{})}` + RENDER_HEAD_RESULT + ``,
},
From 2b06c85715e347dc9355f91afd05f393e54e6372 Mon Sep 17 00:00:00 2001
From: Nate Moore
Date: Thu, 11 Jan 2024 16:29:03 -0600
Subject: [PATCH 05/21] chore: add regression test for tokenizer
---
internal/token_test.go | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/internal/token_test.go b/internal/token_test.go
index 39992f6b9..8ab374fff 100644
--- a/internal/token_test.go
+++ b/internal/token_test.go
@@ -500,6 +500,11 @@ func TestBasic(t *testing.T) {
`Hello world!
`,
+ []TokenType{StartTagToken, StartTagToken, StartTagToken, TextToken, EndTagToken, EndTagToken, EndTagToken},
+ },
}
runTokenTypeTest(t, Basic)
From 58888df98a17913bdf34b1553fa4ab22ca5c5155 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Thu, 29 Feb 2024 21:10:53 +0000
Subject: [PATCH 06/21] test: fix incorrect tests
---
internal/printer/printer_test.go | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index d34175d27..767089717 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -568,9 +568,9 @@ import type data from "test"
div+h2 ${dummyKey}
-
p+h2 ${dummyKey}
- ` + BACKTICK + `
- );
+ p+h2 ${dummyKey}
+
` + BACKTICK + `
+ );
})
}
@@ -588,8 +588,8 @@ import type data from "test"
want: want{
code: `
${$$maybeRenderHead($$result)}
-${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `hello
` + BACKTICK + `)}
-${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `${author}
` + BACKTICK + `)}
+${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `hello
` + BACKTICK + `)}
+${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `${author}
` + BACKTICK + `)}
`,
},
@@ -2089,7 +2089,7 @@ import { Container, Col, Row } from 'react-bootstrap';
name: "Preserve namespaces in expressions",
source: ``,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
From 1c00621b74f7e4efa7148ddd34b646127ca7d32e Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Thu, 29 Feb 2024 21:18:49 +0000
Subject: [PATCH 07/21] test: add test for #958
---
internal/printer/printer_test.go | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 767089717..ed72d65fc 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -2304,6 +2304,25 @@ const content = "lol";
`,
},
},
+ {
+ name: "#958",
+ source: `
+
+`,
+ want: want{
+ code: `${$$maybeRenderHead($$result)}
+
+`,
+ },
+ },
{
name: "complex table",
source: `
From b827dbb4e50a4232d17fbde97cc02ed4f091f503 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 03:36:25 +0000
Subject: [PATCH 08/21] refactor `addFrontmatter`
---
internal/parser.go | 19 ++++---------------
1 file changed, 4 insertions(+), 15 deletions(-)
diff --git a/internal/parser.go b/internal/parser.go
index 18275683a..a413e502c 100644
--- a/internal/parser.go
+++ b/internal/parser.go
@@ -354,7 +354,7 @@ func (p *parser) addText(text string) {
})
}
-func (p *parser) addFrontmatter(empty bool) {
+func (p *parser) addFrontmatter() {
if p.frontmatterState == FrontmatterInitial {
if p.doc.FirstChild != nil {
p.fm = &Node{
@@ -369,13 +369,8 @@ func (p *parser) addFrontmatter(empty bool) {
}
p.doc.AppendChild(p.fm)
}
- if empty {
- p.frontmatterState = FrontmatterClosed
- p.fm.Attr = append(p.fm.Attr, Attribute{Key: ImplicitNodeMarker, Type: EmptyAttribute})
- } else {
- p.frontmatterState = FrontmatterOpen
- p.oe = append(p.oe, p.fm)
- }
+ p.frontmatterState = FrontmatterOpen
+ p.oe = append(p.oe, p.fm)
}
}
@@ -646,9 +641,6 @@ func initialIM(p *parser) bool {
p.im = beforeHTMLIM
return true
}
- if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(true)
- }
p.quirks = true
p.im = beforeHTMLIM
return false
@@ -1534,9 +1526,6 @@ func inBodyIM(p *parser) bool {
}
}
}
- if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(true)
- }
return true
}
@@ -2642,7 +2631,7 @@ func frontmatterIM(p *parser) bool {
switch p.tok.Type {
case FrontmatterFenceToken:
if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(false)
+ p.addFrontmatter()
return true
} else {
p.frontmatterState = FrontmatterClosed
From 85fa4dc345560a4cb791db358083b153945e515b Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 03:36:43 +0000
Subject: [PATCH 09/21] test: add json test case
---
internal/printer/printer_test.go | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index ed72d65fc..2528657d2 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3764,6 +3764,11 @@ const c = '\''
source: `Hello world!
`,
want: []ASTNode{{Type: "element", Name: "style"}, {Type: "element", Name: "html", Children: []ASTNode{{Type: "element", Name: "body", Children: []ASTNode{{Type: "element", Name: "h1", Children: []ASTNode{{Type: "text", Value: "Hello world!"}}}}}}}},
},
+ {
+ name: "empty style",
+ source: ``,
+ want: []ASTNode{{Type: "element", Name: "style", Attributes: []ASTNode{{Type: "attribute", Kind: "expression", Name: "define:vars", Value: "{ color: \"Gainsboro\" }"}}}},
+ },
{
name: "style after html",
source: `Hello world!
`,
From 2b93f834fa234e77717585c4500175ff2b39fea8 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:30:45 +0000
Subject: [PATCH 10/21] update transition scopes
---
internal/printer/printer_test.go | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 2528657d2..207b63a98 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3470,7 +3470,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3479,7 +3479,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3488,7 +3488,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3497,7 +3497,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$renderComponent($$result,'Component',Component,{"class":"bar","data-astro-transition-scope":($$renderTransition($$result, "wkm5vset", "morph", ""))})}`,
+ code: `${$$renderComponent($$result,'Component',Component,{"class":"bar","data-astro-transition-scope":($$renderTransition($$result, "byigm4lx", "morph", ""))})}`,
},
},
{
@@ -3505,7 +3505,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: ``,
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3513,7 +3513,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: ``,
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
From 34dddcd9d3a76f30ea4f70fe420814b35143fb76 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:35:23 +0000
Subject: [PATCH 11/21] test: fix incorrect tests
---
internal/printer/printer_test.go | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index d34175d27..767089717 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -568,9 +568,9 @@ import type data from "test"
div+h2 ${dummyKey}
-
p+h2 ${dummyKey}
- ` + BACKTICK + `
- );
+ p+h2 ${dummyKey}
+ ` + BACKTICK + `
+ );
})
}
@@ -588,8 +588,8 @@ import type data from "test"
want: want{
code: `
${$$maybeRenderHead($$result)}
-${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `hello
` + BACKTICK + `)}
-${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `${author}
` + BACKTICK + `)}
+${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `hello
` + BACKTICK + `)}
+${Object.keys(importedAuthors).map(author => $$render` + BACKTICK + `${author}
` + BACKTICK + `)}
`,
},
@@ -2089,7 +2089,7 @@ import { Container, Col, Row } from 'react-bootstrap';
name: "Preserve namespaces in expressions",
source: ``,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
From 3d2f26357dde56e674763a4797fb0f07ebbe1c0c Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:35:58 +0000
Subject: [PATCH 12/21] test: add test for #958
---
internal/printer/printer_test.go | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 767089717..ed72d65fc 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -2304,6 +2304,25 @@ const content = "lol";
`,
},
},
+ {
+ name: "#958",
+ source: `
+
+`,
+ want: want{
+ code: `${$$maybeRenderHead($$result)}
+
+`,
+ },
+ },
{
name: "complex table",
source: `
From 9b1def4d2fc105e5a1bae3e13c945d99b85b29d4 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:36:27 +0000
Subject: [PATCH 13/21] test: add json test case
---
internal/printer/printer_test.go | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index ed72d65fc..2528657d2 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3764,6 +3764,11 @@ const c = '\''
source: `Hello world!
`,
want: []ASTNode{{Type: "element", Name: "style"}, {Type: "element", Name: "html", Children: []ASTNode{{Type: "element", Name: "body", Children: []ASTNode{{Type: "element", Name: "h1", Children: []ASTNode{{Type: "text", Value: "Hello world!"}}}}}}}},
},
+ {
+ name: "empty style",
+ source: ``,
+ want: []ASTNode{{Type: "element", Name: "style", Attributes: []ASTNode{{Type: "attribute", Kind: "expression", Name: "define:vars", Value: "{ color: \"Gainsboro\" }"}}}},
+ },
{
name: "style after html",
source: `Hello world!
`,
From 51d7a6dd120350aae97dd61ea2b9269a83b31a9f Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:36:59 +0000
Subject: [PATCH 14/21] test: update transition scopes
---
internal/printer/printer_test.go | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 2528657d2..207b63a98 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3470,7 +3470,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3479,7 +3479,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3488,7 +3488,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3497,7 +3497,7 @@ const items = ["Dog", "Cat", "Platipus"];
filename: "/projects/app/src/pages/page.astro",
transitions: true,
want: want{
- code: `${$$renderComponent($$result,'Component',Component,{"class":"bar","data-astro-transition-scope":($$renderTransition($$result, "wkm5vset", "morph", ""))})}`,
+ code: `${$$renderComponent($$result,'Component',Component,{"class":"bar","data-astro-transition-scope":($$renderTransition($$result, "byigm4lx", "morph", ""))})}`,
},
},
{
@@ -3505,7 +3505,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: ``,
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
@@ -3513,7 +3513,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: ``,
transitions: true,
want: want{
- code: `${$$maybeRenderHead($$result)}`,
+ code: `${$$maybeRenderHead($$result)}`,
},
},
{
From e4178e20f000e35a0c520ac8376404bf7c52e380 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:37:14 +0000
Subject: [PATCH 15/21] refactor `addFrontmatter`
---
internal/parser.go | 19 ++++---------------
1 file changed, 4 insertions(+), 15 deletions(-)
diff --git a/internal/parser.go b/internal/parser.go
index 18275683a..a413e502c 100644
--- a/internal/parser.go
+++ b/internal/parser.go
@@ -354,7 +354,7 @@ func (p *parser) addText(text string) {
})
}
-func (p *parser) addFrontmatter(empty bool) {
+func (p *parser) addFrontmatter() {
if p.frontmatterState == FrontmatterInitial {
if p.doc.FirstChild != nil {
p.fm = &Node{
@@ -369,13 +369,8 @@ func (p *parser) addFrontmatter(empty bool) {
}
p.doc.AppendChild(p.fm)
}
- if empty {
- p.frontmatterState = FrontmatterClosed
- p.fm.Attr = append(p.fm.Attr, Attribute{Key: ImplicitNodeMarker, Type: EmptyAttribute})
- } else {
- p.frontmatterState = FrontmatterOpen
- p.oe = append(p.oe, p.fm)
- }
+ p.frontmatterState = FrontmatterOpen
+ p.oe = append(p.oe, p.fm)
}
}
@@ -646,9 +641,6 @@ func initialIM(p *parser) bool {
p.im = beforeHTMLIM
return true
}
- if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(true)
- }
p.quirks = true
p.im = beforeHTMLIM
return false
@@ -1534,9 +1526,6 @@ func inBodyIM(p *parser) bool {
}
}
}
- if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(true)
- }
return true
}
@@ -2642,7 +2631,7 @@ func frontmatterIM(p *parser) bool {
switch p.tok.Type {
case FrontmatterFenceToken:
if p.frontmatterState == FrontmatterInitial {
- p.addFrontmatter(false)
+ p.addFrontmatter()
return true
} else {
p.frontmatterState = FrontmatterClosed
From 0784dd755dd849ed6a1f9cb944e5df64aa433a10 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 18:51:05 +0000
Subject: [PATCH 16/21] test: add test for #971
---
internal/printer/printer_test.go | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 207b63a98..3be0eb6f6 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -2323,6 +2323,19 @@ const content = "lol";
`,
},
},
+ {
+ name: "#971",
+ source: `
+ {text}.
+
+This should not be a link
`,
+ want: want{
+ code: `
+ ${$$maybeRenderHead($$result)}${text}.
+
+This should not be a link
`,
+ },
+ },
{
name: "complex table",
source: `
From abf1c11a1f95a5109e244868a98ff05a37bdf3d6 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Fri, 1 Mar 2024 19:03:35 +0000
Subject: [PATCH 17/21] chore: proper name for added tests
---
internal/printer/printer_test.go | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 3be0eb6f6..4bf5e59bf 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -2305,7 +2305,9 @@ const content = "lol";
},
},
{
- name: "#958",
+ // ensurethere are no duplicate elements matching the ones in the link below (`` in this test)
+ // https://github.com/withastro/compiler/blob/a90d99ee8cc3ad92d1b39d73df1f7301011ee970/internal/parser.go#L1490
+ name: " tag with expression in table ",
source: `
@@ -2324,7 +2326,9 @@ const content = "lol";
},
},
{
- name: "#971",
+ // makes sure that there are no duplicate elements matching the ones in the link below (`` in this test)
+ // https://github.com/withastro/compiler/blob/a90d99ee8cc3ad92d1b39d73df1f7301011ee970/internal/parser.go#L1490
+ name: " tag with expression in template",
source: `
{text}.
From d4370126c160b2ac8b6f6206ed9691b1a3d02804 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Wed, 6 Mar 2024 17:13:58 +0000
Subject: [PATCH 18/21] test: update test to reflect `addFrontmatter` refactor
---
packages/compiler/test/tsx/basic.ts | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/packages/compiler/test/tsx/basic.ts b/packages/compiler/test/tsx/basic.ts
index e5f705f6d..d2e7d80b2 100644
--- a/packages/compiler/test/tsx/basic.ts
+++ b/packages/compiler/test/tsx/basic.ts
@@ -267,8 +267,8 @@ test('return ranges - no frontmatter', async () => {
assert.equal(metaRanges, {
frontmatter: {
- start: 30,
- end: 30,
+ start: 0,
+ end: 0,
},
body: {
start: 41,
From 968b6c4be49704d1c897b89907a45ac7c549f775 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Wed, 6 Mar 2024 17:15:02 +0000
Subject: [PATCH 19/21] test: remove code from bad merge commit
---
internal/printer/printer_test.go | 3 ---
1 file changed, 3 deletions(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 8d2405f87..4bf5e59bf 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -570,9 +570,6 @@ import type data from "test"
p+h2 ${dummyKey}
` + BACKTICK + `
- );
- p+h2 ${dummyKey}
- ` + BACKTICK + `
);
})
}
From c5a3f949b985f4d3dc5b29f0771cc14e2e5b34d1 Mon Sep 17 00:00:00 2001
From: Moustapha HappyDev
Date: Wed, 6 Mar 2024 17:30:30 +0000
Subject: [PATCH 20/21] chore: update transition scope
---
internal/printer/printer_test.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index d558b741b..4959dfbd9 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3538,7 +3538,7 @@ const items = ["Dog", "Cat", "Platipus"];
source: ``,
transitions: true,
want: want{
- code: `${$$renderComponent($$result,'my-island','my-island',{"data-astro-transition-persist-props":"false","data-astro-transition-persist":($$createTransitionScope($$result, "otghnj5u"))})}`,
+ code: `${$$renderComponent($$result,'my-island','my-island',{"data-astro-transition-persist-props":"false","data-astro-transition-persist":($$createTransitionScope($$result, "rho3aldc"))})}`,
},
},
{
From 2974e55b20f85558728f5f0198479d076ebd1fcc Mon Sep 17 00:00:00 2001
From: Matthew Phillips
Date: Fri, 29 Mar 2024 09:49:02 -0400
Subject: [PATCH 21/21] Add nested head content test
---
internal/printer/printer_test.go | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/internal/printer/printer_test.go b/internal/printer/printer_test.go
index 4959dfbd9..fc34006c0 100644
--- a/internal/printer/printer_test.go
+++ b/internal/printer/printer_test.go
@@ -3548,6 +3548,31 @@ const items = ["Dog", "Cat", "Platipus"];
code: `${$$renderComponent($$result,'Component',Component,{})}${(void 0)}`,
},
},
+ {
+ name: "nested head content stays in the head",
+ source: `---
+const meta = { title: 'My App' };
+---
+
+
+
+
+
+ {
+ meta && {meta.title}
+ }
+
+
+
+
+ My App
+
+`,
+ want: want{
+ frontmatter: []string{"", `const meta = { title: 'My App' };`},
+ code: ` ${ meta && $$render` + BACKTICK + `${meta.title}` + BACKTICK + ` } ${$$renderHead($$result)} My App
`,
+ },
+ },
}
for _, tt := range tests {
@@ -3578,6 +3603,7 @@ const items = ["Dog", "Cat", "Platipus"];
RenderScript: tt.transformOptions.RenderScript,
}
transform.Transform(doc, transformOptions, h) // note: we want to test Transform in context here, but more advanced cases could be tested separately
+
result := PrintToJS(code, doc, 0, transform.TransformOptions{
Scope: "XXXX",
InternalURL: "http://localhost:3000/",