diff --git a/src/core/services/aria/aria.js b/src/core/services/aria/aria.js index 8a254844fa5..b0fbd0c6e7b 100644 --- a/src/core/services/aria/aria.js +++ b/src/core/services/aria/aria.js @@ -50,7 +50,7 @@ function AriaService($$rAF, $log, $window, $interpolate) { function expectWithText(element, attrName) { var content = getText(element) || ""; - var hasBinding = content.indexOf($interpolate.startSymbol())>-1; + var hasBinding = content.indexOf($interpolate.startSymbol()) > -1; if ( hasBinding ) { expectAsync(element, attrName, function() { @@ -62,7 +62,26 @@ function AriaService($$rAF, $log, $window, $interpolate) { } function getText(element) { - return (element.text() || "").trim(); + element = element[0] || element; + var walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false); + var text = ''; + + var node; + while (node = walker.nextNode()) { + if (!isAriaHiddenNode(node)) { + text += node.textContent; + } + } + + return text.trim() || ''; + + function isAriaHiddenNode(node) { + while (node.parentNode && (node = node.parentNode) !== element) { + if (node.getAttribute && node.getAttribute('aria-hidden') === 'true') { + return true; + } + } + } } function childHasAttribute(node, attrName) { @@ -74,17 +93,18 @@ function AriaService($$rAF, $log, $window, $interpolate) { return (style.display === 'none'); } - if(hasChildren) { + if (hasChildren) { var children = node.childNodes; - for(var i=0; i' + + 'PLAIN' + + 'SPAN' + + '
DIV
' + + '' + )($rootScope); + + $mdAria.expectWithText(container, 'aria-label'); + + expect(container[0].textContent).toBe('PLAINSPANDIV'); + expect(container.attr('aria-label')).toBe('PLAINSPANDIV'); + })); + + it('should ignore aria-hidden texts when retrieving aria-label', inject(function($compile, $rootScope, $mdAria) { + var container = $compile( + '
' + + 'PLAIN' + + '' + + '' + + '
' + )($rootScope); + + $mdAria.expectWithText(container, 'aria-label'); + + expect(container[0].textContent).toBe('PLAINSPANDIV'); + expect(container.attr('aria-label')).toBe('PLAIN'); + })); + }); });