Skip to content

Commit 081816f

Browse files
authored
🚧 Updates List of boolean attributes to follow spec (#4325)
* 🚧 Updates List of boolean attributes to follow spec * ✅ Updates Test for new attributes
1 parent 6ac9782 commit 081816f

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

packages/alpinejs/src/utils/bind.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,39 @@ export function safeParseBoolean(rawValue) {
146146
return rawValue ? Boolean(rawValue) : null
147147
}
148148

149+
// As per HTML spec table https://html.spec.whatwg.org/multipage/indices.html#attributes-3:boolean-attribute
150+
const booleanAttributes = new Set([
151+
'allowfullscreen',
152+
'async',
153+
'autofocus',
154+
'autoplay',
155+
'checked',
156+
'controls',
157+
'default',
158+
'defer',
159+
'disabled',
160+
'formnovalidate',
161+
'inert',
162+
'ismap',
163+
'itemscope',
164+
'loop',
165+
'multiple',
166+
'muted',
167+
'nomodule',
168+
'novalidate',
169+
'open',
170+
'playsinline',
171+
'readonly',
172+
'required',
173+
'reversed',
174+
'selected',
175+
'shadowrootclonable',
176+
'shadowrootdelegatesfocus',
177+
'shadowrootserializable',
178+
])
179+
149180
function isBooleanAttr(attrName) {
150-
// As per HTML spec table https://html.spec.whatwg.org/multipage/indices.html#attributes-3:boolean-attribute
151-
// Array roughly ordered by estimated usage
152-
const booleanAttributes = [
153-
'disabled','checked','required','readonly','open', 'selected',
154-
'autofocus', 'itemscope', 'multiple', 'novalidate','allowfullscreen',
155-
'allowpaymentrequest', 'formnovalidate', 'autoplay', 'controls', 'loop',
156-
'muted', 'playsinline', 'default', 'ismap', 'reversed', 'async', 'defer',
157-
'nomodule'
158-
]
159-
160-
return booleanAttributes.includes(attrName)
181+
return booleanAttributes.has(attrName)
161182
}
162183

163184
function attributeShouldntBePreservedIfFalsy(name) {

tests/cypress/integration/directives/x-bind.spec.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ test('boolean attribute values are set to their attribute name if true and remov
105105
<option x-bind:selected="isSet"></option>
106106
<textarea x-bind:autofocus="isSet"></textarea>
107107
<dl x-bind:itemscope="isSet"></dl>
108-
<form x-bind:novalidate="isSet"></form>
108+
<form
109+
x-bind:novalidate="isSet"
110+
x-bind:inert="isSet"
111+
></form>
109112
<iframe
110113
x-bind:allowfullscreen="isSet"
111-
x-bind:allowpaymentrequest="isSet"
112114
></iframe>
113115
<button x-bind:formnovalidate="isSet"></button>
114116
<audio
@@ -121,6 +123,11 @@ test('boolean attribute values are set to their attribute name if true and remov
121123
<track x-bind:default="isSet" />
122124
<img x-bind:ismap="isSet" />
123125
<ol x-bind:reversed="isSet"></ol>
126+
<template
127+
x-bind:shadowrootclonable="isSet"
128+
x-bind:shadowrootdelegatesfocus="isSet"
129+
x-bind:shadowrootserializable="isSet"
130+
></template>
124131
</div>
125132
`,
126133
({ get }) => {
@@ -135,7 +142,6 @@ test('boolean attribute values are set to their attribute name if true and remov
135142
get('dl').should(haveAttribute('itemscope', 'itemscope'))
136143
get('form').should(haveAttribute('novalidate', 'novalidate'))
137144
get('iframe').should(haveAttribute('allowfullscreen', 'allowfullscreen'))
138-
get('iframe').should(haveAttribute('allowpaymentrequest', 'allowpaymentrequest'))
139145
get('button').should(haveAttribute('formnovalidate', 'formnovalidate'))
140146
get('audio').should(haveAttribute('autoplay', 'autoplay'))
141147
get('audio').should(haveAttribute('controls', 'controls'))
@@ -145,6 +151,9 @@ test('boolean attribute values are set to their attribute name if true and remov
145151
get('track').should(haveAttribute('default', 'default'))
146152
get('img').should(haveAttribute('ismap', 'ismap'))
147153
get('ol').should(haveAttribute('reversed', 'reversed'))
154+
get('template').should(haveAttribute('shadowrootclonable', 'shadowrootclonable'))
155+
get('template').should(haveAttribute('shadowrootdelegatesfocus', 'shadowrootdelegatesfocus'))
156+
get('template').should(haveAttribute('shadowrootserializable', 'shadowrootserializable'))
148157

149158
get('#setToFalse').click()
150159

0 commit comments

Comments
 (0)