-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: switch to using getComputedStyle
- Loading branch information
Showing
2 changed files
with
28 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
import {NgModule, isDevMode} from '@angular/core'; | ||
|
||
/** Whether the theme presence has already been checked. */ | ||
let hasBeenChecked = false; | ||
|
||
/** | ||
* Module that verifies that the user has loaded the core theming file, | ||
* without which most Material module won't work as expected. | ||
* | ||
* Note on testing methodology: A more efficient way to check for the theme | ||
* would be to loop through the `document.styleSheets`, however most browsers | ||
* don't expose the stylesheet rules, if the file was loaded from another domain. | ||
* This would trigger false positives if the theme is being loaded from a CDN. | ||
* | ||
* @docs-private | ||
*/ | ||
@NgModule() | ||
export class MdThemeCheckModule { | ||
constructor() { | ||
if (!isDevMode() || typeof document === 'undefined') { | ||
if (hasBeenChecked || typeof document === 'undefined' || !isDevMode()) { | ||
return; | ||
} | ||
|
||
for (let i = 0; i < document.styleSheets.length; i++) { | ||
// The try/catch is needed, because some browsers can throw a security | ||
// error when accessing the `cssRules` from another domain. | ||
try { | ||
let rules = (document.styleSheets.item(i) as CSSStyleSheet).cssRules; | ||
let testElement = document.createElement('div'); | ||
|
||
if (rules) { | ||
for (let j = 0; j < rules.length; j++) { | ||
let selector = (rules.item(j) as CSSStyleRule).selectorText; | ||
testElement.classList.add('md-theme-loaded-marker'); | ||
document.body.appendChild(testElement); | ||
|
||
if (selector && selector.includes('.md-theme-loaded-marker')) { | ||
return; | ||
} | ||
} | ||
} | ||
} catch (e) { } | ||
if (getComputedStyle(testElement).display !== 'none') { | ||
console.warn( | ||
'Could not find Angular Material core theme. Most Material ' + | ||
'components may not work as expected. For more info refer ' + | ||
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md' | ||
); | ||
} | ||
|
||
console.warn( | ||
'Could not find Angular Material core theme. Most Material ' + | ||
'components may not work as expected. For more info refer ' + | ||
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md' | ||
); | ||
document.body.removeChild(testElement); | ||
hasBeenChecked = true; | ||
} | ||
} |