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

Commit d63e4d0

Browse files
devversionThomasBurleson
authored andcommitted
feat(aria): add provider to disable console warnings.
Fixes #3507. Closes #8709
1 parent 024e979 commit d63e4d0

File tree

2 files changed

+81
-7
lines changed

2 files changed

+81
-7
lines changed

src/core/services/aria/aria.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1+
/**
2+
* @ngdoc module
3+
* @name material.core.aria
4+
* @description
5+
* Aria Expectations for ngMaterial components.
6+
*/
7+
angular
8+
.module('material.core')
9+
.provider('$mdAria', MdAriaProvider);
10+
11+
/**
12+
* @ngdoc service
13+
* @name $mdAriaProvider
14+
* @module material.core.aria
15+
*
16+
* @description
17+
*
18+
* Modify options of the `$mdAria` service, which will be used by most of the Angular Material components.
19+
**
20+
*
21+
* You are able to disable `$mdAria` warnings, by using the following markup.
22+
* <hljs lang="js">
23+
* app.config(function($mdAriaProvider) {
24+
* // Globally disables all ARIA warnings.
25+
* $mdAriaProvider.disableWarnings();
26+
* });
27+
* </hljs>
28+
*
29+
*/
30+
function MdAriaProvider() {
131

2-
angular.module('material.core')
3-
.service('$mdAria', AriaService);
32+
var self = this;
33+
34+
/**
35+
* Whether we should show ARIA warnings in the console, if labels are missing on the element
36+
* By default the warnings are enabled
37+
*/
38+
self.showWarnings = true;
39+
40+
return {
41+
disableWarnings: disableWarnings,
42+
$get: function($$rAF, $log, $window, $interpolate) {
43+
return MdAriaService.apply(self, arguments);
44+
}
45+
};
46+
47+
/**
48+
* @ngdoc method
49+
* @name $mdAriaProvider#disableWarnings
50+
*/
51+
function disableWarnings() {
52+
self.showWarnings = false;
53+
}
54+
}
455

556
/*
657
* @ngInject
758
*/
8-
function AriaService($$rAF, $log, $window, $interpolate) {
59+
function MdAriaService($$rAF, $log, $window, $interpolate) {
60+
61+
// Load the showWarnings option from the current context and store it inside of a scope variable,
62+
// because the context will be probably lost in some function calls.
63+
var showWarnings = this.showWarnings;
964

1065
return {
1166
expect: expect,
@@ -32,7 +87,7 @@ function AriaService($$rAF, $log, $window, $interpolate) {
3287
defaultValue = angular.isString(defaultValue) ? defaultValue.trim() : '';
3388
if (defaultValue.length) {
3489
element.attr(attrName, defaultValue);
35-
} else {
90+
} else if (showWarnings) {
3691
$log.warn('ARIA: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
3792
}
3893

src/core/services/aria/aria.spec.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
describe('$mdAria service', function() {
2+
23
beforeEach(module('material.core'));
34

4-
describe('expecting attributes', function(){
5+
describe('expecting attributes', function() {
6+
57
it('should warn if an invalid element is specified', inject(function($compile, $rootScope, $log, $mdAria) {
68
spyOn($log, 'warn');
79
var target = $compile('<div></div>')($rootScope);
@@ -28,7 +30,7 @@ describe('$mdAria service', function() {
2830
expect($log.warn).toHaveBeenCalled();
2931
}));
3032

31-
it('should warn if element is emtpry attribute', inject(function($compile, $rootScope, $log, $mdAria) {
33+
it('should warn if element is empty attribute', inject(function($compile, $rootScope, $log, $mdAria) {
3234
spyOn($log, 'warn');
3335
var button = $compile('<button aria-label=""><md-icon></md-icon></button>')($rootScope);
3436

@@ -93,4 +95,21 @@ describe('$mdAria service', function() {
9395

9496
});
9597

96-
});
98+
describe('with disabled warnings', function() {
99+
100+
beforeEach(module('material.core', function($mdAriaProvider) {
101+
$mdAriaProvider.disableWarnings();
102+
}));
103+
104+
it('should not warn if warnings are disabled', inject(function($compile, $rootScope, $log, $mdAria) {
105+
spyOn($log, 'warn');
106+
var button = $compile('<button aria-label><md-icon></md-icon></button>')($rootScope);
107+
108+
$mdAria.expect(button, 'aria-label');
109+
110+
expect($log.warn).not.toHaveBeenCalled();
111+
}));
112+
113+
})
114+
115+
});

0 commit comments

Comments
 (0)