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 19, 2014
1 parent 768cc09 commit 9995e6d
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/core/services/aria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ 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)) {

if (!node.hasAttribute(attrName) && !childHasAttribute(node, attrName)) {

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

function childHasAttribute(node, attrName) {
var hasChildren = node.hasChildNodes(),
childHasAttribute = false;

function isHidden(el) {
var style = el.currentStyle ? el.currentStyle :
getComputedStyle(el);
return (style.display === 'none');
}

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;
}
}
}
}
return childHasAttribute;
}
}
})();
39 changes: 39 additions & 0 deletions src/core/services/aria/aria.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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();
}));

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

container.append(button);

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

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

button.remove();

}));
});

});

0 comments on commit 9995e6d

Please sign in to comment.