compiler icon indicating copy to clipboard operation
compiler copied to clipboard

Undefined class attribute is not filtered out with `scopedStyleStrategy: 'class'`

Open delucis opened this issue 1 year ago • 1 comments

Astro Info

Astro                    v4.8.3
Node                     v18.18.0
System                   Linux (x64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

n/a / All

Describe the Bug

If a component sets the class attribute to undefined, this is stringified and included in build output when using scopedStyleStrategy: 'class', unlike behaviour for other attributes where setting them to undefinded causes the attribute to be omitted in build output.

For example, a component that does this:

<div class={undefined}>

Will render something like the following:

<div class="undefined astro-HASH">

What's the expected result?

The build output should not include undefined in the class attribute.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-sgqbuf?file=src%2FTest.astro

Participation

  • [ ] I am willing to submit a pull request for this issue.

delucis avatar May 14 '24 22:05 delucis

Looks like a bug with the compiler. It generates code like this:

return $$render`<div${$$addAttribute((undefined) + " astro-ojezl33t", "class")}></div>`;

We probably need to update https://github.com/withastro/compiler/blob/face55431403f20d81c4b5cd3cc4173320632ad9/internal/transform/scope-html.go#L148-L153 to generate (undefined ?? '') instead.

bluwy avatar May 15 '24 12:05 bluwy