Skip to content

Commit ba18a1e

Browse files
Disallow aria-expanded for collapsible elements without an allowed role (#9464)
* disallow aria-expanded when the collapsed element does not have an allowed role * fix false > true error
1 parent 58fec4c commit ba18a1e

3 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/Bootstrap/dist/js/bootstrap.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,8 @@ if (typeof jQuery === 'undefined') {
585585
toggle: true
586586
}
587587

588+
Collapse.ARIA_EXPANDED_ALLOWED_ROLES = ['application', 'button', 'checkbox', 'combobox', 'gridcell', 'link', 'listbox', 'menuitem', 'row', 'rowheader', 'tab', 'treeitem']
589+
588590
Collapse.prototype.dimension = function () {
589591
var hasWidth = this.$element.hasClass('width')
590592
return hasWidth ? 'width' : 'height'
@@ -615,7 +617,11 @@ if (typeof jQuery === 'undefined') {
615617
this.$element
616618
.removeClass('collapse')
617619
.addClass('collapsing')[dimension](0)
618-
.attr('aria-expanded', true)
620+
621+
// the aria-expanded attribute is only allowed when the element has an allowed role
622+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
623+
this.$element.attr('aria-expanded', true)
624+
}
619625

620626
this.$trigger
621627
.removeClass('collapsed')
@@ -655,7 +661,11 @@ if (typeof jQuery === 'undefined') {
655661
this.$element
656662
.addClass('collapsing')
657663
.removeClass('collapse in')
658-
.attr('aria-expanded', false)
664+
665+
// the aria-expanded attribute is only allowed when the element has an allowed role
666+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
667+
this.$element.attr('aria-expanded', false)
668+
}
659669

660670
this.$trigger
661671
.addClass('collapsed')
@@ -696,7 +706,10 @@ if (typeof jQuery === 'undefined') {
696706
Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) {
697707
var isOpen = $element.hasClass('in')
698708

699-
$element.attr('aria-expanded', isOpen)
709+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
710+
$element.attr('aria-expanded', isOpen)
711+
}
712+
700713
$trigger
701714
.toggleClass('collapsed', !isOpen)
702715
.attr('aria-expanded', isOpen)

src/Bootstrap/js/collapse.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
toggle: true
3939
}
4040

41+
Collapse.ARIA_EXPANDED_ALLOWED_ROLES = ['application', 'button', 'checkbox', 'combobox', 'gridcell', 'link', 'listbox', 'menuitem', 'row', 'rowheader', 'tab', 'treeitem']
42+
4143
Collapse.prototype.dimension = function () {
4244
var hasWidth = this.$element.hasClass('width')
4345
return hasWidth ? 'width' : 'height'
@@ -68,7 +70,11 @@
6870
this.$element
6971
.removeClass('collapse')
7072
.addClass('collapsing')[dimension](0)
71-
.attr('aria-expanded', true)
73+
74+
// the aria-expanded attribute is only allowed when the element has an allowed role
75+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
76+
this.$element.attr('aria-expanded', true)
77+
}
7278

7379
this.$trigger
7480
.removeClass('collapsed')
@@ -108,7 +114,11 @@
108114
this.$element
109115
.addClass('collapsing')
110116
.removeClass('collapse in')
111-
.attr('aria-expanded', false)
117+
118+
// the aria-expanded attribute is only allowed when the element has an allowed role
119+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
120+
this.$element.attr('aria-expanded', false)
121+
}
112122

113123
this.$trigger
114124
.addClass('collapsed')
@@ -149,7 +159,10 @@
149159
Collapse.prototype.addAriaAndCollapsedClass = function ($element, $trigger) {
150160
var isOpen = $element.hasClass('in')
151161

152-
$element.attr('aria-expanded', isOpen)
162+
if (Collapse.ARIA_EXPANDED_ALLOWED_ROLES.includes(this.$element.attr('role'))) {
163+
$element.attr('aria-expanded', isOpen)
164+
}
165+
153166
$trigger
154167
.toggleClass('collapsed', !isOpen)
155168
.attr('aria-expanded', isOpen)

src/NuGetGallery/App_Code/ViewHelpers.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ var hlp = new AccordionHelper(name, formModelStatePrefix, expanded, page);
577577
</div>
578578
if (!disabled)
579579
{
580-
<div aria-controls="panel-body" class="panel panel-default panel-collapse collapse @(expanded ? "in" : string.Empty)"
580+
<div class="panel panel-default panel-collapse collapse @(expanded ? "in" : string.Empty)"
581581
id="@id-container">
582582
<div class="panel-body">
583583
@content(MvcHtmlString.Empty)

0 commit comments

Comments
 (0)