Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(subheader): add accessibility support (#9817)
Browse files Browse the repository at this point in the history
Fixes #9392.
  • Loading branch information
crisbeto authored and kara committed Nov 16, 2016
1 parent b903153 commit 1d77c92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/components/subheader/subheader.js
Expand Up @@ -52,7 +52,7 @@ angular
* </hljs>
*/

function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil, $mdAria) {
return {
restrict: 'E',
replace: true,
Expand All @@ -78,6 +78,12 @@ function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
return angular.element(el[0].querySelector('.md-subheader-content'));
}

// Set the ARIA attributes on the original element since it keeps it's original place in
// the DOM, whereas the clones are in reverse order. Should be done after the outerHTML,
// in order to avoid having multiple element be marked as headers.
attr.$set('role', 'heading');
$mdAria.expect(element, 'aria-level', '2');

// Transclude the user-given contents of the subheader
// the conventional way.
transclude(scope, function(clone) {
Expand All @@ -92,7 +98,7 @@ function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
// compiled clone below will only be a comment tag (since they replace their elements with
// a comment) which cannot be properly passed to the $mdSticky; so we wrap it in our own
// DIV to ensure we have something $mdSticky can use
var wrapper = $compile('<div class="md-subheader-wrapper">' + outerHTML + '</div>')(scope);
var wrapper = $compile('<div class="md-subheader-wrapper" aria-hidden="true">' + outerHTML + '</div>')(scope);

// Delay initialization until after any `ng-if`/`ng-repeat`/etc has finished before
// attempting to create the clone
Expand Down
26 changes: 25 additions & 1 deletion src/components/subheader/subheader.spec.js
Expand Up @@ -11,7 +11,7 @@ describe('mdSubheader', function() {
$delegate.checkStickySupport = angular.noop;

return $delegate;
})
});
}));

beforeEach(inject(function($injector) {
Expand Down Expand Up @@ -172,6 +172,30 @@ describe('mdSubheader', function() {
expect($exceptionHandler.errors).toEqual([]);
});

it('adds the proper aria attributes only to the source element', function() {
build(
'<div>' +
'<md-subheader>Subheader</md-subheader>' +
'</div>'
);

expect(element.attr('role')).toBe('heading');
expect(element.attr('aria-level')).toBe('2');

expect(cloneElement.attr('role')).toBeFalsy();
expect(cloneElement.parent().attr('aria-hidden')).toBe('true');
});

it('allows for the aria-level to be overwritten', function() {
build(
'<div>' +
'<md-subheader aria-level="1">Subheader</md-subheader>' +
'</div>'
);

expect(element.attr('aria-level')).toBe('1');
});

function build(template) {
inject(function($compile, $timeout) {
pageScope = $rootScope.$new();
Expand Down

0 comments on commit 1d77c92

Please sign in to comment.