From c6b66b62e339c7c98cf6d7255c21f265642b9eaf Mon Sep 17 00:00:00 2001 From: "Gantner, Florian Klaus" Date: Thu, 26 Oct 2023 15:56:52 +0200 Subject: [PATCH] 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 https://github.com/DSpace/dspace-angular/issues/2450 (cherry picked from commit 4dd334f2e76adfabb972095acc80a391c6c91b38) --- .../shared/sass-helper/css-variable.service.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/shared/sass-helper/css-variable.service.ts b/src/app/shared/sass-helper/css-variable.service.ts index 0190a05036f..5ff7345d85d 100644 --- a/src/app/shared/sass-helper/css-variable.service.ts +++ b/src/app/shared/sass-helper/css-variable.service.ts @@ -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 @@ -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(