Skip to content

Commit 16bba72

Browse files
crisbetommalerba
authored andcommitted
fix: allow users to disable the sanity checks (#4178)
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 edcbc0d commit 16bba72

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
@@ -188,25 +189,35 @@ export class MdPrefixRejector {
188189
@NgModule({
189190
declarations: [MatPrefixRejector, MdPrefixRejector],
190191
exports: [MatPrefixRejector, MdPrefixRejector],
192+
providers: [{
193+
provide: MATERIAL_SANITY_CHECKS, useValue: true,
194+
}],
191195
})
192196
export class CompatibilityModule {
197+
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
198+
private _hasDoneGlobalChecks = false;
199+
193200
static forRoot(): ModuleWithProviders {
194201
return {
195202
ngModule: CompatibilityModule,
196203
providers: [],
197204
};
198205
}
199206

200-
constructor(@Optional() @Inject(DOCUMENT) private _document: any) {
201-
if (!hasDoneGlobalChecks && isDevMode()) {
207+
constructor(
208+
@Optional() @Inject(DOCUMENT) private _document: any,
209+
@Optional() @Inject(MATERIAL_SANITY_CHECKS) _sanityChecksEnabled: boolean) {
210+
211+
if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
212+
// Delay running the check to allow more time for the user's styles to load.
202213
this._checkDoctype();
203214
this._checkTheme();
204-
hasDoneGlobalChecks = true;
215+
this._hasDoneGlobalChecks = true;
205216
}
206217
}
207218

208219
private _checkDoctype(): void {
209-
if (this._document && !this._document.doctype) {
220+
if (!this._document.doctype) {
210221
console.warn(
211222
'Current document does not have a doctype. This may cause ' +
212223
'some Angular Material components not to behave as expected.'
@@ -215,7 +226,7 @@ export class CompatibilityModule {
215226
}
216227

217228
private _checkTheme(): void {
218-
if (this._document && typeof getComputedStyle === 'function') {
229+
if (typeof getComputedStyle === 'function') {
219230
const testElement = this._document.createElement('div');
220231

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

0 commit comments

Comments
 (0)