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

Commit 3556d57

Browse files
devversiontinayuangao
authored andcommitted
fix(list): empty aria-label attributes for list-items with interpolation (#10218)
* fix(list): empty aria-label attributes for list-items with interpolation * The `md-list-item` component determines the aria-label from the content if no `aria-label` is specified. Right now the `md-list-item` is not able to determine the label from interpolated content and shows a lot of $mdAria warnings * Fixes that the `md-list-item` complains twice about a missing `aria-label`, because the label was expected on the list and on the underlaying button element. * Fixes a potential issue in the `$mdAriaProvider` where the provider class instance is applied to the service. * Remove $q injection
1 parent 32235b2 commit 3556d57

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

src/components/list/list.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,11 +330,14 @@ function mdListItemDirective($mdAria, $mdConstant, $mdUtil, $timeout) {
330330
'<md-button class="md-no-style"></md-button>'
331331
);
332332

333-
// Expect the root element to have a label set. If not set, determine the label from the text content.
334-
$mdAria.expectWithText(tEl, 'aria-label');
335-
336333
copyAttributes(tEl[0], buttonWrap[0]);
337334

335+
// If there is no aria-label set on the button (previously copied over if present)
336+
// we determine the label from the content and copy it to the button.
337+
if (!buttonWrap.attr('aria-label')) {
338+
buttonWrap.attr('aria-label', $mdAria.getText(tEl));
339+
}
340+
338341
// We allow developers to specify the `md-no-focus` class, to disable the focus style
339342
// on the button executor. Once more classes should be forwarded, we should probably make the
340343
// class forward more generic.

src/components/list/list.spec.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,30 @@ describe('mdListItem directive', function() {
532532
expect(buttonEl.attr('aria-label')).toBe('Content');
533533
});
534534

535+
it('should determine the label from the bound content if aria-label is not set', function() {
536+
var listItem = setup(
537+
'<md-list-item ng-click="null">' +
538+
'<span>{{ content }}</span>' +
539+
'<span aria-hidden="true">Hidden</span>' +
540+
'</md-list-item>'
541+
);
542+
543+
$rootScope.$apply('content = "Content"');
544+
545+
var buttonEl = listItem.find('button');
546+
547+
// The aria-label attribute should be determined from the content.
548+
expect(buttonEl.attr('aria-label')).toBe('Content');
549+
});
550+
535551
it('should warn when label is missing and content is empty', inject(function($log) {
536552
// Clear the log stack to assert that a new warning has been added.
537553
$log.reset();
538554

539555
setup('<md-list-item ng-click="null">');
540556

541-
// Expect $log to have two warnings. A warning for the md-list-item and a second one for the later compiled
542-
// button executor.
543-
expect($log.warn.logs.length).toBe(2);
557+
// Expect $log to have one $mdAria warning, because the button misses an aria-label.
558+
expect($log.warn.logs.length).toBe(1);
544559
}));
545560

546561
});

src/core/services/aria/aria.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,15 @@ angular
3030
*/
3131
function MdAriaProvider() {
3232

33-
var self = this;
34-
35-
/**
36-
* Whether we should show ARIA warnings in the console, if labels are missing on the element
37-
* By default the warnings are enabled
38-
*/
39-
self.showWarnings = true;
33+
var config = {
34+
/** Whether we should show ARIA warnings in the console if labels are missing on the element */
35+
showWarnings: true
36+
};
4037

4138
return {
4239
disableWarnings: disableWarnings,
4340
$get: function($$rAF, $log, $window, $interpolate) {
44-
return MdAriaService.apply(self, arguments);
41+
return MdAriaService.apply(config, arguments);
4542
}
4643
};
4744

@@ -51,7 +48,7 @@ function MdAriaProvider() {
5148
* @description Disables all ARIA warnings generated by Angular Material.
5249
*/
5350
function disableWarnings() {
54-
self.showWarnings = false;
51+
config.showWarnings = false;
5552
}
5653
}
5754

@@ -68,7 +65,8 @@ function MdAriaService($$rAF, $log, $window, $interpolate) {
6865
expect: expect,
6966
expectAsync: expectAsync,
7067
expectWithText: expectWithText,
71-
expectWithoutText: expectWithoutText
68+
expectWithoutText: expectWithoutText,
69+
getText: getText
7270
};
7371

7472
/**
@@ -110,7 +108,7 @@ function MdAriaService($$rAF, $log, $window, $interpolate) {
110108
var content = getText(element) || "";
111109
var hasBinding = content.indexOf($interpolate.startSymbol()) > -1;
112110

113-
if ( hasBinding ) {
111+
if (hasBinding) {
114112
expectAsync(element, attrName, function() {
115113
return getText(element);
116114
});

0 commit comments

Comments
 (0)