Skip to content

Commit b2a1606

Browse files
committed
fix: allow users to disable the sanity checks
Allows users to disable the Material sanity checks, by providing a value for the `MATERIAL_SANITY_CHECKS` token: ```ts @NgModule({ providers: [ {provide: MATERIAL_SANITY_CHECKS, useValue: false} ] // other config }); ``` Fixes #4125.
1 parent 8d0cd04 commit b2a1606

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/lib/core/compatibility/compatibility.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import {
77
Optional,
88
isDevMode,
99
ElementRef,
10+
InjectionToken,
1011
} from '@angular/core';
1112
import {DOCUMENT} from '@angular/platform-browser';
1213
import {MdError} from '../errors/error';
1314

14-
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
15-
let hasDoneGlobalChecks = false;
16-
1715
export const MATERIAL_COMPATIBILITY_MODE = new OpaqueToken('md-compatibility-mode');
1816

17+
/** Injection token that configures whether the Material sanity checks are enabled. */
18+
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('md-sanity-checks');
19+
1920
/**
2021
* Exception thrown if the consumer has used an invalid Material prefix on a component.
2122
* @docs-private
@@ -184,25 +185,35 @@ export class MdPrefixRejector {
184185
@NgModule({
185186
declarations: [MatPrefixRejector, MdPrefixRejector],
186187
exports: [MatPrefixRejector, MdPrefixRejector],
188+
providers: [{
189+
provide: MATERIAL_SANITY_CHECKS, useValue: true,
190+
}],
187191
})
188192
export class CompatibilityModule {
193+
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
194+
private _hasDoneGlobalChecks = false;
195+
189196
static forRoot(): ModuleWithProviders {
190197
return {
191198
ngModule: CompatibilityModule,
192199
providers: [],
193200
};
194201
}
195202

196-
constructor(@Optional() @Inject(DOCUMENT) private _document: any) {
197-
if (!hasDoneGlobalChecks && isDevMode()) {
203+
constructor(
204+
@Optional() @Inject(DOCUMENT) private _document: any,
205+
@Optional() @Inject(MATERIAL_SANITY_CHECKS) _sanityChecksEnabled: boolean) {
206+
207+
if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
208+
// Delay running the check to allow more time for the user's styles to load.
198209
this._checkDoctype();
199210
this._checkTheme();
200-
hasDoneGlobalChecks = true;
211+
this._hasDoneGlobalChecks = true;
201212
}
202213
}
203214

204215
private _checkDoctype(): void {
205-
if (this._document && !this._document.doctype) {
216+
if (!this._document.doctype) {
206217
console.warn(
207218
'Current document does not have a doctype. This may cause ' +
208219
'some Angular Material components not to behave as expected.'
@@ -211,7 +222,7 @@ export class CompatibilityModule {
211222
}
212223

213224
private _checkTheme(): void {
214-
if (this._document && typeof getComputedStyle === 'function') {
225+
if (typeof getComputedStyle === 'function') {
215226
const testElement = this._document.createElement('div');
216227

217228
testElement.classList.add('mat-theme-loaded-marker');

0 commit comments

Comments
 (0)