Skip to content

Commit 0b28d59

Browse files
dylanbjzaefferer
authored andcommitted
Autocomplete: Announce autocomplete correctly in all ATs.
Fixes #9631 Closes gh-1153
1 parent 6ec452c commit 0b28d59

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

demos/autocomplete/categories.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
var that = this,
3030
currentCategory = "";
3131
$.each( items, function( index, item ) {
32+
var li;
3233
if ( item.category != currentCategory ) {
3334
ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
3435
currentCategory = item.category;
3536
}
36-
that._renderItemData( ul, item );
37+
li = that._renderItemData( ul, item );
38+
if ( item.category ) {
39+
li.attr( "aria-label", item.category + " : " + item.label );
40+
}
3741
});
3842
}
3943
});

tests/unit/autocomplete/autocomplete_core.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -221,40 +221,76 @@ asyncTest( "simultaneous searches (#9334)", function() {
221221
});
222222

223223
test( "ARIA", function() {
224-
expect( 7 );
224+
expect( 13 );
225225
var element = $( "#autocomplete" ).autocomplete({
226226
source: [ "java", "javascript" ]
227227
}),
228228
liveRegion = element.autocomplete( "instance" ).liveRegion;
229229

230-
equal( liveRegion.text(), "", "Empty live region on create" );
230+
equal( liveRegion.children().length, 0, "Empty live region on create" );
231+
equal( liveRegion.attr( "aria-live" ), "assertive",
232+
"Live region's aria-live attribute must be assertive" );
233+
equal( liveRegion.attr( "aria-relevant" ), "additions",
234+
"Live region's aria-relevant attribute must be additions" );
235+
equal( liveRegion.attr( "role" ), "status",
236+
"Live region's role attribute must be status" );
231237

232238
element.autocomplete( "search", "j" );
233-
equal( liveRegion.text(), "2 results are available, use up and down arrow keys to navigate.",
239+
equal( liveRegion.children().first().text(),
240+
"2 results are available, use up and down arrow keys to navigate.",
234241
"Live region for multiple values" );
235242

236243
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
237-
equal( liveRegion.text(), "2 results are available, use up and down arrow keys to navigate.",
238-
"Live region not changed on focus" );
244+
equal( liveRegion.children().filter( ":visible" ).text(), "java",
245+
"Live region changed on keydown to announce the highlighted value" );
239246

240247
element.one( "autocompletefocus", function( event ) {
241248
event.preventDefault();
242249
});
243250
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
244-
equal( liveRegion.text(), "javascript",
251+
equal( liveRegion.children().filter( ":visible" ).text(), "javascript",
245252
"Live region updated when default focus is prevented" );
246253

247254
element.autocomplete( "search", "javas" );
248-
equal( liveRegion.text(), "1 result is available, use up and down arrow keys to navigate.",
255+
equal( liveRegion.children().filter( ":visible" ).text(),
256+
"1 result is available, use up and down arrow keys to navigate.",
249257
"Live region for one value" );
250258

251259
element.autocomplete( "search", "z" );
252-
equal( liveRegion.text(), "No search results.",
260+
equal( liveRegion.children().filter( ":visible" ).text(), "No search results.",
253261
"Live region for no values" );
254262

255-
element.autocomplete( "search", "j" );
256-
equal( liveRegion.text(), "2 results are available, use up and down arrow keys to navigate.",
257-
"Live region for multiple values" );
263+
equal( liveRegion.children().length, 5,
264+
"Should be five children in the live region after the above" );
265+
equal( liveRegion.children().filter( ":visible" ).length, 1,
266+
"Only one should be still visible" );
267+
ok( liveRegion.children().filter( ":visible" )[ 0 ] === liveRegion.children().last()[ 0 ],
268+
"The last one should be the visible one" );
269+
270+
element.autocomplete( "destroy" );
271+
equal( liveRegion.parent().length, 0,
272+
"The liveRegion should be detached after destroy" );
273+
});
274+
275+
test( "ARIA, aria-label announcement", function() {
276+
expect( 1 );
277+
$.widget( "custom.catcomplete", $.ui.autocomplete, {
278+
_renderMenu: function( ul, items ) {
279+
var that = this;
280+
$.each( items, function( index, item ) {
281+
that._renderItemData( ul, item )
282+
.attr( "aria-label", item.category + " : " + item.label );
283+
});
284+
}
285+
});
286+
var element = $( "#autocomplete" ).catcomplete({
287+
source: [ { label: "anders andersson", category: "People" } ]
288+
}),
289+
liveRegion = element.catcomplete( "instance" ).liveRegion;
290+
element.catcomplete( "search", "a" );
291+
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
292+
equal( liveRegion.children().filter( ":visible" ).text(), "People : anders andersson",
293+
"Live region changed on keydown to announce the highlighted value's aria-label attribute" );
258294
});
259295

260296
test( "ARIA, init on detached input", function() {

ui/jquery.ui.autocomplete.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ $.widget( "ui.autocomplete", {
230230
}
231231
},
232232
menufocus: function( event, ui ) {
233+
var label, item;
233234
// support: Firefox
234235
// Prevent accidental activation of menu items in Firefox (#7024 #9118)
235236
if ( this.isNewMenu ) {
@@ -245,19 +246,19 @@ $.widget( "ui.autocomplete", {
245246
}
246247
}
247248

248-
var item = ui.item.data( "ui-autocomplete-item" );
249+
item = ui.item.data( "ui-autocomplete-item" );
249250
if ( false !== this._trigger( "focus", event, { item: item } ) ) {
250251
// use value to match what will end up in the input, if it was a key event
251252
if ( event.originalEvent && /^key/.test( event.originalEvent.type ) ) {
252253
this._value( item.value );
253254
}
254-
} else {
255-
// Normally the input is populated with the item's value as the
256-
// menu is navigated, causing screen readers to notice a change and
257-
// announce the item. Since the focus event was canceled, this doesn't
258-
// happen, so we update the live region so that screen readers can
259-
// still notice the change and announce it.
260-
this.liveRegion.text( item.value );
255+
}
256+
257+
// Announce the value in the liveRegion
258+
label = ui.item.attr( "aria-label" ) || item.value;
259+
if ( label && jQuery.trim( label ).length ) {
260+
this.liveRegion.children().hide();
261+
$( "<div>" ).text( label ).appendTo( this.liveRegion );
261262
}
262263
},
263264
menuselect: function( event, ui ) {
@@ -291,7 +292,8 @@ $.widget( "ui.autocomplete", {
291292

292293
this.liveRegion = $( "<span>", {
293294
role: "status",
294-
"aria-live": "polite"
295+
"aria-live": "assertive",
296+
"aria-relevant": "additions"
295297
})
296298
.addClass( "ui-helper-hidden-accessible" )
297299
.appendTo( this.document[ 0 ].body );
@@ -595,7 +597,8 @@ $.widget( "ui.autocomplete", $.ui.autocomplete, {
595597
} else {
596598
message = this.options.messages.noResults;
597599
}
598-
this.liveRegion.text( message );
600+
this.liveRegion.children().hide();
601+
$( "<div>" ).text( message ).appendTo( this.liveRegion );
599602
}
600603
});
601604

0 commit comments

Comments
 (0)