@@ -18,16 +18,30 @@ import {VERSION as CDK_VERSION} from '@angular/cdk';
1818const VERSION = new Version ( '0.0.0-PLACEHOLDER' ) ;
1919
2020/** @docs -private */
21- export function MATERIAL_SANITY_CHECKS_FACTORY ( ) : boolean {
21+ export function MATERIAL_SANITY_CHECKS_FACTORY ( ) : SanityChecks {
2222 return true ;
2323}
2424
2525/** Injection token that configures whether the Material sanity checks are enabled. */
26- export const MATERIAL_SANITY_CHECKS = new InjectionToken < boolean > ( 'mat-sanity-checks' , {
26+ export const MATERIAL_SANITY_CHECKS = new InjectionToken < SanityChecks > ( 'mat-sanity-checks' , {
2727 providedIn : 'root' ,
2828 factory : MATERIAL_SANITY_CHECKS_FACTORY ,
2929} ) ;
3030
31+ /**
32+ * Possible sanity checks that can be enabled. If set to
33+ * true/false, all checks will be enabled/disabled.
34+ */
35+ export type SanityChecks = boolean | GranularSanityChecks ;
36+
37+ /** Object that allows for the sanity checks to be configured granularly. */
38+ export interface GranularSanityChecks {
39+ doctype : boolean ;
40+ theme : boolean ;
41+ version : boolean ;
42+ hammer : boolean ;
43+ }
44+
3145/**
3246 * Module that captures anything that should be loaded and/or run for *all* Angular Material
3347 * components. This includes Bidi, etc.
@@ -52,10 +66,10 @@ export class MatCommonModule {
5266 private _window = typeof window === 'object' && window ? window : null ;
5367
5468 constructor (
55- @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) private _sanityChecksEnabled : boolean ,
69+ @Optional ( ) @Inject ( MATERIAL_SANITY_CHECKS ) private _sanityChecks : SanityChecks ,
5670 @Optional ( ) @Inject ( HAMMER_LOADER ) private _hammerLoader ?: HammerLoader ) {
5771
58- if ( this . _areChecksEnabled ( ) && ! this . _hasDoneGlobalChecks ) {
72+ if ( ! this . _hasDoneGlobalChecks ) {
5973 this . _checkDoctypeIsDefined ( ) ;
6074 this . _checkThemeIsPresent ( ) ;
6175 this . _checkCdkVersionMatch ( ) ;
@@ -64,8 +78,13 @@ export class MatCommonModule {
6478 }
6579
6680 /** Whether any sanity checks are enabled */
67- private _areChecksEnabled ( ) : boolean {
68- return this . _sanityChecksEnabled && isDevMode ( ) && ! this . _isTestEnv ( ) ;
81+ private _isCheckEnabled ( check : keyof GranularSanityChecks ) : boolean {
82+ if ( ! isDevMode ( ) || this . _isTestEnv ( ) ) {
83+ // All checks are disabled in production mode and in test environments.
84+ return false ;
85+ }
86+
87+ return typeof this . _sanityChecks === 'boolean' ? this . _sanityChecks : this . _sanityChecks [ check ] ;
6988 }
7089
7190 /** Whether the code is running in tests. */
@@ -75,7 +94,7 @@ export class MatCommonModule {
7594 }
7695
7796 private _checkDoctypeIsDefined ( ) : void {
78- if ( this . _document && ! this . _document . doctype ) {
97+ if ( this . _isCheckEnabled ( 'doctype' ) && this . _document && ! this . _document . doctype ) {
7998 console . warn (
8099 'Current document does not have a doctype. This may cause ' +
81100 'some Angular Material components not to behave as expected.'
@@ -86,7 +105,8 @@ export class MatCommonModule {
86105 private _checkThemeIsPresent ( ) : void {
87106 // We need to assert that the `body` is defined, because these checks run very early
88107 // and the `body` won't be defined if the consumer put their scripts in the `head`.
89- if ( ! this . _document || ! this . _document . body || typeof getComputedStyle !== 'function' ) {
108+ if ( ! this . _isCheckEnabled ( 'theme' ) || ! this . _document || ! this . _document . body ||
109+ typeof getComputedStyle !== 'function' ) {
90110 return ;
91111 }
92112
@@ -113,7 +133,7 @@ export class MatCommonModule {
113133
114134 /** Checks whether the material version matches the cdk version */
115135 private _checkCdkVersionMatch ( ) : void {
116- if ( VERSION . full !== CDK_VERSION . full ) {
136+ if ( this . _isCheckEnabled ( 'version' ) && VERSION . full !== CDK_VERSION . full ) {
117137 console . warn (
118138 'The Angular Material version (' + VERSION . full + ') does not match ' +
119139 'the Angular CDK version (' + CDK_VERSION . full + ').\n' +
@@ -128,7 +148,7 @@ export class MatCommonModule {
128148 return ;
129149 }
130150
131- if ( this . _areChecksEnabled ( ) && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
151+ if ( this . _isCheckEnabled ( 'hammer' ) && ! ( this . _window as any ) [ 'Hammer' ] && ! this . _hammerLoader ) {
132152 console . warn (
133153 'Could not find HammerJS. Certain Angular Material components may not work correctly.' ) ;
134154 }
0 commit comments