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

Commit 1d77c92

Browse files
crisbetokara
authored andcommitted
fix(subheader): add accessibility support (#9817)
Fixes #9392.
1 parent b903153 commit 1d77c92

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/components/subheader/subheader.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ angular
5252
* </hljs>
5353
*/
5454

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

81+
// Set the ARIA attributes on the original element since it keeps it's original place in
82+
// the DOM, whereas the clones are in reverse order. Should be done after the outerHTML,
83+
// in order to avoid having multiple element be marked as headers.
84+
attr.$set('role', 'heading');
85+
$mdAria.expect(element, 'aria-level', '2');
86+
8187
// Transclude the user-given contents of the subheader
8288
// the conventional way.
8389
transclude(scope, function(clone) {
@@ -92,7 +98,7 @@ function MdSubheaderDirective($mdSticky, $compile, $mdTheming, $mdUtil) {
9298
// compiled clone below will only be a comment tag (since they replace their elements with
9399
// a comment) which cannot be properly passed to the $mdSticky; so we wrap it in our own
94100
// DIV to ensure we have something $mdSticky can use
95-
var wrapper = $compile('<div class="md-subheader-wrapper">' + outerHTML + '</div>')(scope);
101+
var wrapper = $compile('<div class="md-subheader-wrapper" aria-hidden="true">' + outerHTML + '</div>')(scope);
96102

97103
// Delay initialization until after any `ng-if`/`ng-repeat`/etc has finished before
98104
// attempting to create the clone

src/components/subheader/subheader.spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('mdSubheader', function() {
1111
$delegate.checkStickySupport = angular.noop;
1212

1313
return $delegate;
14-
})
14+
});
1515
}));
1616

1717
beforeEach(inject(function($injector) {
@@ -172,6 +172,30 @@ describe('mdSubheader', function() {
172172
expect($exceptionHandler.errors).toEqual([]);
173173
});
174174

175+
it('adds the proper aria attributes only to the source element', function() {
176+
build(
177+
'<div>' +
178+
'<md-subheader>Subheader</md-subheader>' +
179+
'</div>'
180+
);
181+
182+
expect(element.attr('role')).toBe('heading');
183+
expect(element.attr('aria-level')).toBe('2');
184+
185+
expect(cloneElement.attr('role')).toBeFalsy();
186+
expect(cloneElement.parent().attr('aria-hidden')).toBe('true');
187+
});
188+
189+
it('allows for the aria-level to be overwritten', function() {
190+
build(
191+
'<div>' +
192+
'<md-subheader aria-level="1">Subheader</md-subheader>' +
193+
'</div>'
194+
);
195+
196+
expect(element.attr('aria-level')).toBe('1');
197+
});
198+
175199
function build(template) {
176200
inject(function($compile, $timeout) {
177201
pageScope = $rootScope.$new();

0 commit comments

Comments
 (0)