Skip to content

Commit 21bf004

Browse files
committed
fix: switch to using getComputedStyle
1 parent f31134e commit 21bf004

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

src/lib/core/_core.scss

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,9 @@
2626

2727
// Mixin that renders all of the core styles that depend on the theme.
2828
@mixin md-core-theme($theme) {
29-
// Marker that is used to determine whether the user has added a theme to their page.
30-
// Note that only the selector is being used, but we add a property, in order to avoid
31-
// it being removed by minifiers.
32-
.md-theme-loaded-marker {
33-
color: #000;
34-
}
29+
@include md-ripple-theme($theme);
30+
@include md-option-theme($theme);
31+
@include md-pseudo-checkbox-theme($theme);
3532

3633
// Wrapper element that provides the theme background when the
3734
// user's content isn't inside of a `md-sidenav-container`.
@@ -40,7 +37,8 @@
4037
background-color: md-color($background, background);
4138
}
4239

43-
@include md-ripple-theme($theme);
44-
@include md-option-theme($theme);
45-
@include md-pseudo-checkbox-theme($theme);
40+
// Marker that is used to determine whether the user has added a theme to their page.
41+
.md-theme-loaded-marker {
42+
display: none;
43+
}
4644
}

src/lib/core/theming/theme-check.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,40 @@
11
import {NgModule, isDevMode} from '@angular/core';
22

3+
/** Whether the theme presence has already been checked. */
4+
let hasBeenChecked = false;
5+
36
/**
47
* Module that verifies that the user has loaded the core theming file,
58
* without which most Material module won't work as expected.
9+
*
10+
* Note on testing methodology: A more efficient way to check for the theme
11+
* would be to loop through the `document.styleSheets`, however most browsers
12+
* don't expose the stylesheet rules, if the file was loaded from another domain.
13+
* This would trigger false positives if the theme is being loaded from a CDN.
14+
*
615
* @docs-private
716
*/
817
@NgModule()
918
export class MdThemeCheckModule {
1019
constructor() {
11-
if (!isDevMode() || typeof document === 'undefined') {
20+
if (hasBeenChecked || typeof document === 'undefined' || !isDevMode()) {
1221
return;
1322
}
1423

15-
for (let i = 0; i < document.styleSheets.length; i++) {
16-
// The try/catch is needed, because some browsers can throw a security
17-
// error when accessing the `cssRules` from another domain.
18-
try {
19-
let rules = (document.styleSheets.item(i) as CSSStyleSheet).cssRules;
24+
let testElement = document.createElement('div');
2025

21-
if (rules) {
22-
for (let j = 0; j < rules.length; j++) {
23-
let selector = (rules.item(j) as CSSStyleRule).selectorText;
26+
testElement.classList.add('md-theme-loaded-marker');
27+
document.body.appendChild(testElement);
2428

25-
if (selector && selector.includes('.md-theme-loaded-marker')) {
26-
return;
27-
}
28-
}
29-
}
30-
} catch (e) { }
29+
if (getComputedStyle(testElement).display !== 'none') {
30+
console.warn(
31+
'Could not find Angular Material core theme. Most Material ' +
32+
'components may not work as expected. For more info refer ' +
33+
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md'
34+
);
3135
}
3236

33-
console.warn(
34-
'Could not find Angular Material core theme. Most Material ' +
35-
'components may not work as expected. For more info refer ' +
36-
'to the theming guide: https://github.com/angular/material2/blob/master/guides/theming.md'
37-
);
37+
document.body.removeChild(testElement);
38+
hasBeenChecked = true;
3839
}
3940
}

0 commit comments

Comments
 (0)