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

Commit

Permalink
fix(button): aria-label injection
Browse files Browse the repository at this point in the history
* fix to use textContent as aria-label (if not preset)
* fix support for bindings in textContent to update aria-label
  • Loading branch information
ThomasBurleson committed Jan 19, 2016
1 parent 74fe691 commit 6113648
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
9 changes: 3 additions & 6 deletions src/components/button/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ angular
* </md-button>
* </hljs>
*/
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout) {
function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout, $interpolate) {

return {
restrict: 'EA',
Expand All @@ -86,14 +86,11 @@ function MdButtonDirective($mdButtonInkRipple, $mdTheming, $mdAria, $timeout) {
}

function postLink(scope, element, attr) {
var node = element[0];
$mdTheming(element);
$mdButtonInkRipple.attach(scope, element);

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

// For anchor elements, we have to set tabindex manually when the
// element is disabled
Expand Down
24 changes: 23 additions & 1 deletion src/components/button/button.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('md-button', function() {
fdescribe('md-button', function() {

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

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

it('should expect an aria-label if element has text content', inject(function($compile, $rootScope, $log) {
spyOn($log, 'warn');

var button = $compile('<md-button>Hello</md-button>')($rootScope);
expect(button.attr('aria-label')).toBe("Hello");
expect($log.warn).not.toHaveBeenCalled();
}));

it('should set an aria-label if the text content using bindings', inject(function($$rAF, $compile, $rootScope, $log) {
spyOn($log, 'warn');

var scope = angular.extend($rootScope.$new(),{greetings : "Welcome"});
var button = $compile('<md-button>{{greetings}}</md-button>')(scope);

$rootScope.$apply();
$$rAF.flush(); // needed for $mdAria.expectAsync()

expect(button.attr('aria-label')).toBe("Welcome");
expect($log.warn).not.toHaveBeenCalled();
}));


it('should allow attribute directive syntax', inject(function($compile, $rootScope) {
var button = $compile('<a md-button href="https://google.com">google</a>')($rootScope.$new());
expect(button.hasClass('md-button')).toBe(true);
Expand Down

0 comments on commit 6113648

Please sign in to comment.