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 diff --git a/internal/parser.go b/internal/parser.go index 62ca2ec64..53613d15c 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: + 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: + 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/packages/compiler/test/basic/script-before-html.ts b/packages/compiler/test/basic/script-before-html.ts new file mode 100644 index 000000000..93965527d --- /dev/null +++ b/packages/compiler/test/basic/script-before-html.ts @@ -0,0 +1,42 @@ +import { test } from 'uvu'; +import * as assert from 'uvu/assert'; +import { transform } from '@astrojs/compiler'; + +const FIXTURE = ` + + + + +
+ + + + +