Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not hoist script or style inside of noscript or template #473

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-sheep-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Do not attempt to hoist styles or scripts inside of `<noscript>`
14 changes: 14 additions & 0 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,20 @@ import * as ns from '../components';
`,
},
},
{
name: "noscript styles",
source: `<noscript><style>div { color: red; }</style></noscript>`,
want: want{
code: `${$$maybeRenderHead($$result)}<noscript><style>div { color: red; }</style></noscript>`,
},
},
{
name: "noscript deep styles",
source: `<body><noscript><div><div><div><style>div { color: red; }</style></div></div></div></noscript></body>`,
want: want{
code: `${$$maybeRenderHead($$result)}<body><noscript><div><div><div><style>div { color: red; }</style></div></div></div></noscript></body>`,
},
},
{
name: "client:only component (default)",
source: `---
Expand Down
9 changes: 6 additions & 3 deletions internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
astro "github.com/withastro/compiler/internal"
"github.com/withastro/compiler/internal/js_scanner"
"github.com/withastro/compiler/internal/loc"
"golang.org/x/net/html/atom"
a "golang.org/x/net/html/atom"
)

Expand Down Expand Up @@ -70,8 +69,8 @@ func ExtractStyles(doc *astro.Node) {
if HasSetDirective(n) || HasInlineDirective(n) {
return
}
// Do not extract <style> inside of SVGs
if n.Parent != nil && n.Parent.DataAtom == atom.Svg {
// Ignore styles in svg/noscript/etc
if !IsHoistable(n) {
return
}
// prepend node to maintain authored order
Expand Down Expand Up @@ -243,6 +242,10 @@ func ExtractScript(doc *astro.Node, n *astro.Node, opts *TransformOptions) {
if HasSetDirective(n) || HasInlineDirective(n) {
return
}
// Ignore scripts in svg/noscript/etc
if !IsHoistable(n) {
return
}

// if <script>, hoist to the document root
// If also using define:vars, that overrides the hoist tag.
Expand Down
8 changes: 8 additions & 0 deletions internal/transform/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transform

import (
astro "github.com/withastro/compiler/internal"
"golang.org/x/net/html/atom"
)

func hasTruthyAttr(n *astro.Node, key string) bool {
Expand Down Expand Up @@ -33,6 +34,13 @@ func HasAttr(n *astro.Node, key string) bool {
return false
}

func IsHoistable(n *astro.Node) bool {
parent := n.Closest(func(p *astro.Node) bool {
return p.DataAtom == atom.Svg || p.DataAtom == atom.Noscript || p.DataAtom == atom.Template
})
return parent == nil
}

func IsImplictNode(n *astro.Node) bool {
return HasAttr(n, astro.ImplicitNodeMarker)
}
Expand Down