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

Commit

Permalink
feat(mdAria): Checks child nodes for aria-label
Browse files Browse the repository at this point in the history
Closes #567
  • Loading branch information
Marcy Sutton committed Nov 18, 2014
1 parent 768cc09 commit b96516e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/core/services/aria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ function AriaService($$rAF, $log) {
};

/**
* Check if expected attribute has been specified on the target element
* Check if expected attribute has been specified on the target element or child
* @param element
* @param attrName
* @param {optional} defaultValue What to set the attr to if no value is found
*/
function expect(element, attrName, defaultValue) {
var node = element[0];
if (!node.hasAttribute(attrName)) {
var node = element[0],
hasChildren = node.hasChildNodes(),
childHasAttribute = false;

if(hasChildren) {
var children = node.childNodes;
for(var i=0; i<children.length; i++){
var child = children[i];
if(child.nodeType === 1 && child.hasAttribute(attrName)) {
if(!isHidden(child)){
childHasAttribute = true;
}
}
}
}
if (!node.hasAttribute(attrName) && !childHasAttribute) {

defaultValue = angular.isString(defaultValue) && defaultValue.trim() || '';
if (defaultValue.length) {
Expand All @@ -47,5 +61,10 @@ function AriaService($$rAF, $log) {
});
}

function isHidden(el) {
var style = el.currentStyle ? el.currentStyle :
getComputedStyle(el, null);
return (style.display === 'none');
}
}
})();
33 changes: 33 additions & 0 deletions src/core/services/aria/aria.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
describe('$mdAria service', function() {
beforeEach(module('material.core'));

describe('expecting attributes', function(){
it('should warn if element is missing text', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button><md-icon></md-icon></button>')($rootScope);

$mdAria.expect(button, 'aria-label');

expect($log.warn).toHaveBeenCalled();
}));

it('should not warn if child element has attribute', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button><md-icon aria-label="text"></md-icon></button>')($rootScope);

$mdAria.expect(button, 'aria-label');

expect($log.warn).not.toHaveBeenCalled();
}));

xit('should warn if child with attribute is hidden', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button><md-icon aria-label="text" style="display: none;"></md-icon></button>')($rootScope);

$mdAria.expect(button, 'aria-label');

expect($log.warn).toHaveBeenCalled();
}));
});

});

0 comments on commit b96516e

Please sign in to comment.