Skip to content

Commit

Permalink
Merge pull request #2589 from DSpace/backport-2580-to-dspace-7_x
Browse files Browse the repository at this point in the history
[Port dspace-7_x]   Check cssRules before css variables are read from stylesheet (again)
  • Loading branch information
tdonohue authored Oct 27, 2023
2 parents 6a182c3 + c6b66b6 commit 701c6e3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/app/shared/sass-helper/css-variable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ export class CSSVariableService {
return styleSheet.href.indexOf(window.location.origin) === 0;
};

/**
* Checks whether the specific stylesheet object has the property cssRules
* @param styleSheet The stylesheet
*/
hasCssRules = (styleSheet) => {
// Injected (cross-origin) styles might have no css rules value and throw some exception
try {
return styleSheet.cssRules;
} catch (e) {
return false;
}
};

/*
Determine if the given rule is a CSSStyleRule
See: https://developer.mozilla.org/en-US/docs/Web/API/CSSRule#Type_constants
Expand Down Expand Up @@ -93,8 +106,10 @@ export class CSSVariableService {
if (isNotEmpty(document.styleSheets)) {
// styleSheets is array-like, so we convert it to an array.
// Filter out any stylesheets not on this domain
// Filter out any stylesheets that have no cssRules property
return [...document.styleSheets]
.filter(this.isSameDomain)
.filter(this.hasCssRules)
.reduce(
(finalArr, sheet) =>
finalArr.concat(
Expand Down

0 comments on commit 701c6e3

Please sign in to comment.