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

Commit 6113648

Browse files
fix(button): aria-label injection
* fix to use textContent as aria-label (if not preset) * fix support for bindings in textContent to update aria-label
1 parent 74fe691 commit 6113648

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/components/button/button.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ angular
6161
* </md-button>
6262
* </hljs>
6363
*/
64-
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout) {
64+
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout, $interpolate) {
6565

6666
return {
6767
restrict: 'EA',
@@ -86,14 +86,11 @@ function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout) {
8686
}
8787

8888
function postLink(scope, element, attr) {
89-
var node = element[0];
9089
$mdTheming(element);
9190
$mdButtonInkRipple.attach(scope, element);
9291

93-
var elementHasText = node.textContent.trim();
94-
if (!elementHasText) {
95-
$mdAria.expect(element, 'aria-label');
96-
}
92+
// Use async expect to support possible bindings in the button label
93+
$mdAria.expectWithText(element, 'aria-label');
9794

9895
// For anchor elements, we have to set tabindex manually when the
9996
// element is disabled

src/components/button/button.spec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
describe('md-button', function() {
1+
fdescribe('md-button', function() {
22

33
beforeEach(module('material.components.button'));
44

@@ -29,6 +29,28 @@ describe('md-button', function() {
2929
expect($log.warn).not.toHaveBeenCalled();
3030
}));
3131

32+
it('should expect an aria-label if element has text content', inject(function($compile, $rootScope, $log) {
33+
spyOn($log, 'warn');
34+
35+
var button = $compile('<md-button>Hello</md-button>')($rootScope);
36+
expect(button.attr('aria-label')).toBe("Hello");
37+
expect($log.warn).not.toHaveBeenCalled();
38+
}));
39+
40+
it('should set an aria-label if the text content using bindings', inject(function($$rAF, $compile, $rootScope, $log) {
41+
spyOn($log, 'warn');
42+
43+
var scope = angular.extend($rootScope.$new(),{greetings : "Welcome"});
44+
var button = $compile('<md-button>{{greetings}}</md-button>')(scope);
45+
46+
$rootScope.$apply();
47+
$$rAF.flush(); // needed for $mdAria.expectAsync()
48+
49+
expect(button.attr('aria-label')).toBe("Welcome");
50+
expect($log.warn).not.toHaveBeenCalled();
51+
}));
52+
53+
3254
it('should allow attribute directive syntax', inject(function($compile, $rootScope) {
3355
var button = $compile('<a md-button href="https://google.com">google</a>')($rootScope.$new());
3456
expect(button.hasClass('md-button')).toBe(true);

0 commit comments

Comments
 (0)