Skip to content

Commit 4dd334f

Browse files
more error-prone check of cssRules existence before css variables are get from stylesheet
check the existence off cssRules property before the variables are readed from this stylesheet DSpace#2450
1 parent 81e3b36 commit 4dd334f

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/app/shared/sass-helper/css-variable.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ export class CSSVariableService {
2626
return styleSheet.href.indexOf(window.location.origin) === 0;
2727
};
2828

29+
/**
30+
* Checks whether the specific stylesheet object has the property cssRules
31+
* @param styleSheet The stylesheet
32+
*/
33+
hasCssRules = (styleSheet) => {
34+
// Injected (cross-origin) styles might have no css rules value and throw some exception
35+
try {
36+
return styleSheet.cssRules;
37+
} catch (e) {
38+
return false;
39+
}
40+
};
41+
2942
/*
3043
Determine if the given rule is a CSSStyleRule
3144
See: https://developer.mozilla.org/en-US/docs/Web/API/CSSRule#Type_constants
@@ -93,8 +106,10 @@ export class CSSVariableService {
93106
if (isNotEmpty(document.styleSheets)) {
94107
// styleSheets is array-like, so we convert it to an array.
95108
// Filter out any stylesheets not on this domain
109+
// Filter out any stylesheets that have no cssRules property
96110
return [...document.styleSheets]
97111
.filter(this.isSameDomain)
112+
.filter(this.hasCssRules)
98113
.reduce(
99114
(finalArr, sheet) =>
100115
finalArr.concat(

0 commit comments

Comments
 (0)