-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix attribute rendering for boolean values (take 2) (#11660)
Co-authored-by: Sarah Rainsberger <[email protected]> Co-authored-by: Armand Philippot <[email protected]>
- Loading branch information
1 parent
5a3c1d1
commit e90f559
Showing
5 changed files
with
112 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
'astro': major | ||
--- | ||
|
||
Fixes attribute rendering for non-[boolean HTML attributes](https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML) with boolean values to match proper attribute handling in browsers. | ||
|
||
Previously, non-boolean attributes may not have included their values when rendered to HTML. In Astro v5.0, the values are now explicitly rendered as `="true"` or `="false"` | ||
|
||
In the following `.astro` examples, only `allowfullscreen` is a boolean attribute: | ||
|
||
```astro | ||
<!-- src/pages/index.astro --> | ||
<!-- `allowfullscreen` is a boolean attribute --> | ||
<p allowfullscreen={true}></p> | ||
<p allowfullscreen={false}></p> | ||
<!-- `inherit` is *not* a boolean attribute --> | ||
<p inherit={true}></p> | ||
<p inherit={false}></p> | ||
<!-- `data-*` attributes are not boolean attributes --> | ||
<p data-light={true}></p> | ||
<p data-light={false}></p> | ||
``` | ||
|
||
Astro v5.0 now preserves the full data attribute with its value when rendering the HTML of non-boolean attributes: | ||
|
||
```diff | ||
<p allowfullscreen></p> | ||
<p></p> | ||
|
||
<p inherit="true"></p> | ||
- <p inherit></p> | ||
+ <p inherit="false"></p> | ||
|
||
- <p data-light></p> | ||
+ <p data-light="true"></p> | ||
- <p></p> | ||
+ <p data-light="false"></p> | ||
``` | ||
|
||
If you rely on attribute values, for example to locate elements or to conditionally render, update your code to match the new non-boolean attribute values: | ||
|
||
```diff | ||
- el.getAttribute('inherit') === '' | ||
+ el.getAttribute('inherit') === 'false' | ||
|
||
- el.hasAttribute('data-light') | ||
+ el.dataset.light === 'true' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 23 additions & 12 deletions
35
packages/astro/test/fixtures/astro-attrs/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters