Skip to content

Commit 549d622

Browse files
crisbetoandrewseguin
authored andcommitted
chore: move sanity checks to common module (#4987)
* chore: move sanity checks to common module Moves the sanity checks to the `MdCommonModule`. Fixes #4979. * chore: re-export sanity checks
1 parent 8fa7726 commit 549d622

File tree

3 files changed

+55
-63
lines changed

3 files changed

+55
-63
lines changed
Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import {NgModule} from '@angular/core';
1+
import {NgModule, InjectionToken, Optional, Inject, isDevMode} from '@angular/core';
2+
import {DOCUMENT} from '@angular/platform-browser';
23
import {CompatibilityModule} from '../compatibility/compatibility';
34

45

6+
/** Injection token that configures whether the Material sanity checks are enabled. */
7+
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('md-sanity-checks');
8+
9+
510
/**
611
* Module that captures anything that should be loaded and/or run for *all* Angular Material
712
* components. This includes Bidi, compatibility mode, etc.
@@ -11,5 +16,50 @@ import {CompatibilityModule} from '../compatibility/compatibility';
1116
@NgModule({
1217
imports: [CompatibilityModule],
1318
exports: [CompatibilityModule],
19+
providers: [{
20+
provide: MATERIAL_SANITY_CHECKS, useValue: true,
21+
}],
1422
})
15-
export class MdCommonModule { }
23+
export class MdCommonModule {
24+
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
25+
private _hasDoneGlobalChecks = false;
26+
27+
constructor(
28+
@Optional() @Inject(DOCUMENT) private _document: any,
29+
@Optional() @Inject(MATERIAL_SANITY_CHECKS) _sanityChecksEnabled: boolean) {
30+
31+
if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
32+
this._checkDoctype();
33+
this._checkTheme();
34+
this._hasDoneGlobalChecks = true;
35+
}
36+
}
37+
38+
private _checkDoctype(): void {
39+
if (!this._document.doctype) {
40+
console.warn(
41+
'Current document does not have a doctype. This may cause ' +
42+
'some Angular Material components not to behave as expected.'
43+
);
44+
}
45+
}
46+
47+
private _checkTheme(): void {
48+
if (typeof getComputedStyle === 'function') {
49+
const testElement = this._document.createElement('div');
50+
51+
testElement.classList.add('mat-theme-loaded-marker');
52+
this._document.body.appendChild(testElement);
53+
54+
if (getComputedStyle(testElement).display !== 'none') {
55+
console.warn(
56+
'Could not find Angular Material core theme. Most Material ' +
57+
'components may not work as expected. For more info refer ' +
58+
'to the theming guide: https://material.angular.io/guide/theming'
59+
);
60+
}
61+
62+
this._document.body.removeChild(testElement);
63+
}
64+
}
65+
}

src/lib/core/compatibility/compatibility.ts

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
import {
2-
NgModule,
3-
Directive,
4-
Inject,
5-
Optional,
6-
isDevMode,
7-
ElementRef,
8-
InjectionToken,
9-
} from '@angular/core';
10-
import {DOCUMENT} from '@angular/platform-browser';
1+
import {NgModule, Directive, Inject, Optional, ElementRef, InjectionToken} from '@angular/core';
112

123
export const MATERIAL_COMPATIBILITY_MODE = new InjectionToken<boolean>('md-compatibility-mode');
134

14-
/** Injection token that configures whether the Material sanity checks are enabled. */
15-
export const MATERIAL_SANITY_CHECKS = new InjectionToken<boolean>('md-sanity-checks');
16-
175
/**
186
* Returns an exception to be thrown if the consumer has used
197
* an invalid Material prefix on a component.
@@ -183,54 +171,8 @@ export class MdPrefixRejector {
183171
@NgModule({
184172
declarations: [MatPrefixRejector, MdPrefixRejector],
185173
exports: [MatPrefixRejector, MdPrefixRejector],
186-
providers: [{
187-
provide: MATERIAL_SANITY_CHECKS, useValue: true,
188-
}],
189174
})
190-
export class CompatibilityModule {
191-
/** Whether we've done the global sanity checks (e.g. a theme is loaded, there is a doctype). */
192-
private _hasDoneGlobalChecks = false;
193-
194-
constructor(
195-
@Optional() @Inject(DOCUMENT) private _document: any,
196-
@Optional() @Inject(MATERIAL_SANITY_CHECKS) _sanityChecksEnabled: boolean) {
197-
198-
if (_sanityChecksEnabled && !this._hasDoneGlobalChecks && _document && isDevMode()) {
199-
// Delay running the check to allow more time for the user's styles to load.
200-
this._checkDoctype();
201-
this._checkTheme();
202-
this._hasDoneGlobalChecks = true;
203-
}
204-
}
205-
206-
private _checkDoctype(): void {
207-
if (!this._document.doctype) {
208-
console.warn(
209-
'Current document does not have a doctype. This may cause ' +
210-
'some Angular Material components not to behave as expected.'
211-
);
212-
}
213-
}
214-
215-
private _checkTheme(): void {
216-
if (typeof getComputedStyle === 'function') {
217-
const testElement = this._document.createElement('div');
218-
219-
testElement.classList.add('mat-theme-loaded-marker');
220-
this._document.body.appendChild(testElement);
221-
222-
if (getComputedStyle(testElement).display !== 'none') {
223-
console.warn(
224-
'Could not find Angular Material core theme. Most Material ' +
225-
'components may not work as expected. For more info refer ' +
226-
'to the theming guide: https://material.angular.io/guide/theming'
227-
);
228-
}
229-
230-
this._document.body.removeChild(testElement);
231-
}
232-
}
233-
}
175+
export class CompatibilityModule {}
234176

235177

236178
/**

src/lib/core/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export {coerceNumberProperty} from './coercion/number-property';
9898
export {CompatibilityModule, NoConflictStyleCompatibilityMode} from './compatibility/compatibility';
9999

100100
// Common material module
101-
export {MdCommonModule} from './common-behaviors/common-module';
101+
export {MdCommonModule, MATERIAL_SANITY_CHECKS} from './common-behaviors/common-module';
102102

103103
// Datetime
104104
export * from './datetime/index';

0 commit comments

Comments
 (0)