Skip to content

Commit

Permalink
fix: prevent runtime error on malformed custom theme object (SAP#4375)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladitasev authored and ndeshev committed Dec 13, 2021
1 parent 590659f commit e7ce5a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/base/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
vO5p8hSMa0eIz2qsnZOGDD6Icvk=
FNJQMNguBdwfNetzIV90Ud4CNxA=
12 changes: 8 additions & 4 deletions packages/base/src/theming/applyTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const loadThemeBase = async theme => {
return;
}

const cssText = await getThemeProperties(BASE_THEME_PACKAGE, theme);
createOrUpdateStyle(cssText, "data-ui5-theme-properties", BASE_THEME_PACKAGE);
const cssData = await getThemeProperties(BASE_THEME_PACKAGE, theme);
if (cssData) {
createOrUpdateStyle(cssData, "data-ui5-theme-properties", BASE_THEME_PACKAGE);
}
};

const deleteThemeBase = () => {
Expand All @@ -31,8 +33,10 @@ const loadComponentPackages = async theme => {
return;
}

const cssText = await getThemeProperties(packageName, theme);
createOrUpdateStyle(cssText, "data-ui5-theme-properties", packageName);
const cssData = await getThemeProperties(packageName, theme);
if (cssData) {
createOrUpdateStyle(cssData, "data-ui5-theme-properties", packageName);
}
});
};

Expand Down
29 changes: 24 additions & 5 deletions packages/base/src/theming/getThemeDesignerTheme.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const warnings = new Set();

const getThemeMetadata = () => {
// Check if the class was already applied, most commonly to the link/style tag with the CSS Variables
let el = document.querySelector(".sapThemeMetaData-Base-baseLib") || document.querySelector(".sapThemeMetaData-UI5-sap-ui-core");
Expand All @@ -7,10 +9,18 @@ const getThemeMetadata = () => {

el = document.createElement("span");
el.style.display = "none";

// Try with sapThemeMetaData-Base-baseLib first
el.classList.add("sapThemeMetaData-Base-baseLib");
el.classList.add("sapThemeMetaData-UI5-sap-ui-core");
document.body.appendChild(el);
const metadata = getComputedStyle(el).backgroundImage;
let metadata = getComputedStyle(el).backgroundImage;

// Try with sapThemeMetaData-UI5-sap-ui-core only if the previous selector was not found
if (metadata === "none") {
el.classList.add("sapThemeMetaData-UI5-sap-ui-core");
metadata = getComputedStyle(el).backgroundImage;
}

document.body.removeChild(el);

return metadata;
Expand All @@ -25,14 +35,20 @@ const parseThemeMetadata = metadataString => {
try {
paramsString = decodeURIComponent(paramsString);
} catch (ex) {
console.warn("Malformed theme metadata string, unable to decodeURIComponent"); // eslint-disable-line
if (!warnings.has("decode")) {
console.warn("Malformed theme metadata string, unable to decodeURIComponent"); // eslint-disable-line
warnings.add("decode");
}
return;
}
}
try {
return JSON.parse(paramsString);
} catch (ex) {
console.warn("Malformed theme metadata string, unable to parse JSON"); // eslint-disable-line
if (!warnings.has("parse")) {
console.warn("Malformed theme metadata string, unable to parse JSON"); // eslint-disable-line
warnings.add("parse");
}
}
}
};
Expand All @@ -45,7 +61,10 @@ const processThemeMetadata = metadata => {
themeName = metadata.Path.match(/\.([^.]+)\.css_variables$/)[1];
baseThemeName = metadata.Extends[0];
} catch (ex) {
console.warn("Malformed theme metadata Object", metadata); // eslint-disable-line
if (!warnings.has("object")) {
console.warn("Malformed theme metadata Object", metadata); // eslint-disable-line
warnings.add("object");
}
return;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/icons/hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
GEYlbexzLpC077jVvsewU+//GU4=
ioZT+Xb+acqvxGzqsYJu+Y7Jxy4=

0 comments on commit e7ce5a3

Please sign in to comment.