Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit d31b3a6

Browse files
committedJul 23, 2016
fix(ngOptions): remove selected attribute from unselected options
When the select model changes, we add the "selected" attribute to the selected option, so that screen readers know which option is selected. Previously, we failed to remove the attribute from the selected / empty option when the model changed to match a different option, or the unknown / empty option. When using "track by", the behavior would also show when a user selected an option, and then the model was changed, because track by watches the tracked expression, and calls the $render function on change. This fix reads the current select value, finds the matching option and removes the "selected" attribute. IE9 had to be special cased, as it will report option.hasAttribute('selected') === true even if the option's property and attribute have been unset (even the dev tools show not selected attribute). I've added a custom matcher that accounts for this behavior. In all other browsers, property and attribute should always be in the same state. Since few people will use screen readers with IE9, I hope this is a satisfactory solution to the problem. Fixes #14892 Fixes #14419 Related #12731 PR (#14894)
1 parent 5a70888 commit d31b3a6

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed
 

‎src/ng/directive/ngOptions.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,11 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
449449
var removeEmptyOption = function() {
450450
if (!providedEmptyOption) {
451451
emptyOption.remove();
452+
} else {
453+
emptyOption.removeAttr('selected');
452454
}
453455
};
454456

455-
456457
var renderUnknownOption = function() {
457458
selectElement.prepend(unknownOption);
458459
selectElement.val('?');
@@ -468,8 +469,13 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
468469
if (!multiple) {
469470

470471
selectCtrl.writeValue = function writeNgOptionsValue(value) {
472+
var selectedOption = options.selectValueMap[selectElement.val()];
471473
var option = options.getOptionFromViewValue(value);
472474

475+
// Make sure to remove the selected attribute from the previously selected option
476+
// Otherwise, screen readers might get confused
477+
if (selectedOption) selectedOption.element.removeAttribute('selected');
478+
473479
if (option) {
474480
// Don't update the option when it is already selected.
475481
// For example, the browser will select the first option by default. In that case,
@@ -510,6 +516,7 @@ var ngOptionsDirective = ['$compile', '$document', '$parse', function($compile,
510516

511517
// If we are using `track by` then we must watch the tracked value on the model
512518
// since ngModel only watches for object identity change
519+
// FIXME: When a user selects an option, this watch will fire needlessly
513520
if (ngOptions.trackBy) {
514521
scope.$watch(
515522
function() { return ngOptions.getTrackByValue(ngModelCtrl.$viewValue); },

‎test/ng/directive/ngOptionsSpec.js

+96
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,47 @@ describe('ngOptions', function() {
120120
return { pass: errors.length === 0, message: message };
121121
}
122122
};
123+
},
124+
toBeMarkedAsSelected: function() {
125+
// Selected is special because the element property and attribute reflect each other's state.
126+
// IE9 will wrongly report hasAttribute('selected') === true when the property is
127+
// undefined or null, and the dev tools show that no attribute is set
128+
return {
129+
compare: function(actual) {
130+
var errors = [];
131+
if (actual.selected === null || typeof actual.selected === 'undefined' || actual.selected === false) {
132+
errors.push('Expected option property "selected" to be truthy');
133+
}
134+
135+
if (msie !== 9 && actual.hasAttribute('selected') === false) {
136+
errors.push('Expected option to have attribute "selected"');
137+
}
138+
139+
var result = {
140+
pass: errors.length === 0,
141+
message: errors.join('\n')
142+
};
143+
144+
return result;
145+
},
146+
negativeCompare: function(actual) {
147+
var errors = [];
148+
if (actual.selected) {
149+
errors.push('Expected option property "selected" to be falsy');
150+
}
151+
152+
if (msie !== 9 && actual.hasAttribute('selected')) {
153+
errors.push('Expected option not to have attribute "selected"');
154+
}
155+
156+
var result = {
157+
pass: errors.length === 0,
158+
message: errors.join('\n')
159+
};
160+
161+
return result;
162+
}
163+
};
123164
}
124165
});
125166
});
@@ -744,6 +785,41 @@ describe('ngOptions', function() {
744785
});
745786

746787

788+
it('should remove the "selected" attribute from the previous option when the model changes', function() {
789+
scope.values = [{id: 10, label: 'ten'}, {id:20, label: 'twenty'}];
790+
791+
createSelect({
792+
'ng-model': 'selected',
793+
'ng-options': 'item.label for item in values'
794+
}, true);
795+
796+
var options = element.find('option');
797+
expect(options[0]).toBeMarkedAsSelected();
798+
expect(options[1]).not.toBeMarkedAsSelected();
799+
expect(options[2]).not.toBeMarkedAsSelected();
800+
801+
scope.selected = scope.values[0];
802+
scope.$digest();
803+
804+
expect(options[0]).not.toBeMarkedAsSelected();
805+
expect(options[1]).toBeMarkedAsSelected();
806+
expect(options[2]).not.toBeMarkedAsSelected();
807+
808+
scope.selected = scope.values[1];
809+
scope.$digest();
810+
811+
expect(options[0]).not.toBeMarkedAsSelected();
812+
expect(options[1]).not.toBeMarkedAsSelected();
813+
expect(options[2]).toBeMarkedAsSelected();
814+
815+
scope.selected = 'no match';
816+
scope.$digest();
817+
818+
expect(options[0]).toBeMarkedAsSelected();
819+
expect(options[1]).not.toBeMarkedAsSelected();
820+
expect(options[2]).not.toBeMarkedAsSelected();
821+
});
822+
747823
describe('disableWhen expression', function() {
748824

749825
describe('on single select', function() {
@@ -1395,6 +1471,26 @@ describe('ngOptions', function() {
13951471
});
13961472
}).not.toThrow();
13971473
});
1474+
1475+
it('should remove the "selected" attribute when the model changes', function() {
1476+
createSelect({
1477+
'ng-model': 'selected',
1478+
'ng-options': 'item.label for item in arr track by item.id'
1479+
});
1480+
1481+
var options = element.find('option');
1482+
browserTrigger(options[2], 'click');
1483+
1484+
expect(scope.selected).toEqual(scope.arr[1]);
1485+
1486+
scope.selected = {};
1487+
scope.$digest();
1488+
1489+
expect(options[0]).toBeMarkedAsSelected();
1490+
expect(options[2]).not.toBeMarkedAsSelected();
1491+
expect(options[2]).not.toBeMarkedAsSelected();
1492+
});
1493+
13981494
});
13991495

14001496

0 commit comments

Comments
 (0)