From a14211dc1d646f8e23a8e3f23cbd15c1c4786dff Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 9 Dec 2022 10:25:55 -0600 Subject: [PATCH 1/4] fix(#660): allow script before HTML --- internal/parser.go | 18 +++++++- internal/printer/print-to-js.go | 2 +- internal/transform/transform.go | 6 ++- .../compiler/test/basic/script-before-html.ts | 41 +++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 packages/compiler/test/basic/script-before-html.ts diff --git a/internal/parser.go b/internal/parser.go index 62ca2ec64..a56c145b4 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -670,10 +670,23 @@ func beforeHTMLIM(p *parser) bool { return true } case StartTagToken: - if p.tok.DataAtom == a.Html { + switch p.tok.DataAtom { + case a.Html: p.addElement() p.im = beforeHeadIM return true + case a.Script, a.Style: + p.addElement() + if p.originalIM == nil { + p.setOriginalIM() + } + p.im = textIM + if p.hasSelfClosingToken { + p.addLoc() + p.oe.pop() + p.acknowledgeSelfClosingTag() + } + return true } if isComponent(p.tok.Data) { p.addElement() @@ -691,6 +704,9 @@ func beforeHTMLIM(p *parser) bool { } case EndTagToken: switch p.tok.DataAtom { + case a.Script, a.Style: + p.oe.pop() + return true case a.Head, a.Body, a.Html, a.Br: p.parseImpliedToken(StartTagToken, a.Html, a.Html.String()) return false diff --git a/internal/printer/print-to-js.go b/internal/printer/print-to-js.go index 016111df8..7a9b2e162 100644 --- a/internal/printer/print-to-js.go +++ b/internal/printer/print-to-js.go @@ -682,7 +682,7 @@ func render1(p *printer, n *Node, opts RenderOptions) { // Section 12.1.2, "Elements", gives this list of void elements. Void elements // are those that can't have any contents. -//nolint +// nolint var voidElements = map[string]bool{ "area": true, "base": true, diff --git a/internal/transform/transform.go b/internal/transform/transform.go index 1c160e711..b5c6bd1a8 100644 --- a/internal/transform/transform.go +++ b/internal/transform/transform.go @@ -68,8 +68,10 @@ func Transform(doc *astro.Node, opts TransformOptions, h *handler.Handler) *astr renderHeadNode := &astro.Node{ Type: astro.RenderHeadNode, } - script.Parent.InsertBefore(renderHeadNode, script) - addedHeadRenderingInsertion = true + if script.Parent.Type != astro.DocumentNode { + script.Parent.InsertBefore(renderHeadNode, script) + addedHeadRenderingInsertion = true + } } script.Parent.RemoveChild(script) diff --git a/packages/compiler/test/basic/script-before-html.ts b/packages/compiler/test/basic/script-before-html.ts new file mode 100644 index 000000000..185ca1af2 --- /dev/null +++ b/packages/compiler/test/basic/script-before-html.ts @@ -0,0 +1,41 @@ +import { test } from 'uvu'; +import * as assert from 'uvu/assert'; +import { transform } from '@astrojs/compiler'; + +const FIXTURE = ` + + + + + + + + + Astro strips html lang tag + + +
+ +
+ + + + +`; + +let result; +test.before(async () => { + result = await transform(FIXTURE); +}); + +test('includes html element', () => { + assert.ok(result.code.includes(''), 'Expected compile result to include html element!'); +}); + +test.run(); From 3aadc9e1cf908bf4294f45a9209edb76cef16074 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 9 Dec 2022 10:26:19 -0600 Subject: [PATCH 2/4] chore: add changeset --- .changeset/orange-bottles-bow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/orange-bottles-bow.md diff --git a/.changeset/orange-bottles-bow.md b/.changeset/orange-bottles-bow.md new file mode 100644 index 000000000..62c47d622 --- /dev/null +++ b/.changeset/orange-bottles-bow.md @@ -0,0 +1,5 @@ +--- +'@astrojs/compiler': patch +--- + +Allow `script` and `style` before HTML From fc0feeaad63a46b1e1e24c20577f288c07851778 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 9 Dec 2022 10:38:43 -0600 Subject: [PATCH 3/4] fix: remove style from logic (already works) --- internal/parser.go | 4 ++-- internal/transform/transform.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/internal/parser.go b/internal/parser.go index a56c145b4..53613d15c 100644 --- a/internal/parser.go +++ b/internal/parser.go @@ -675,7 +675,7 @@ func beforeHTMLIM(p *parser) bool { p.addElement() p.im = beforeHeadIM return true - case a.Script, a.Style: + case a.Script: p.addElement() if p.originalIM == nil { p.setOriginalIM() @@ -704,7 +704,7 @@ func beforeHTMLIM(p *parser) bool { } case EndTagToken: switch p.tok.DataAtom { - case a.Script, a.Style: + case a.Script: p.oe.pop() return true case a.Head, a.Body, a.Html, a.Br: diff --git a/internal/transform/transform.go b/internal/transform/transform.go index b5c6bd1a8..1c160e711 100644 --- a/internal/transform/transform.go +++ b/internal/transform/transform.go @@ -68,10 +68,8 @@ func Transform(doc *astro.Node, opts TransformOptions, h *handler.Handler) *astr renderHeadNode := &astro.Node{ Type: astro.RenderHeadNode, } - if script.Parent.Type != astro.DocumentNode { - script.Parent.InsertBefore(renderHeadNode, script) - addedHeadRenderingInsertion = true - } + script.Parent.InsertBefore(renderHeadNode, script) + addedHeadRenderingInsertion = true } script.Parent.RemoveChild(script) From a35a27d90b2a9f137ea14161ff244560e3850ce8 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Fri, 9 Dec 2022 15:51:10 -0600 Subject: [PATCH 4/4] chore: add doctype --- packages/compiler/test/basic/script-before-html.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/compiler/test/basic/script-before-html.ts b/packages/compiler/test/basic/script-before-html.ts index 185ca1af2..93965527d 100644 --- a/packages/compiler/test/basic/script-before-html.ts +++ b/packages/compiler/test/basic/script-before-html.ts @@ -7,6 +7,7 @@ const FIXTURE = ` // anything ... +