mirror of
https://github.com/element-hq/element-web
synced 2024-11-28 20:38:55 +03:00
validate hex color
This commit is contained in:
parent
19fc6a93ec
commit
4978f8a2a9
1 changed files with 15 additions and 2 deletions
|
@ -53,6 +53,13 @@ export function avatarUrlForUser(user, width, height, resizeMethod) {
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isValidHexColor(color) {
|
||||||
|
return typeof color === "string" &&
|
||||||
|
(color.length === 7 || color.lengh === 9) &&
|
||||||
|
color.charAt(0) === "#" &&
|
||||||
|
!color.substr(1).split("").some(c => isNaN(parseInt(c, 16)));
|
||||||
|
}
|
||||||
|
|
||||||
function urlForColor(color) {
|
function urlForColor(color) {
|
||||||
const size = 40;
|
const size = 40;
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
|
@ -86,8 +93,14 @@ export function defaultAvatarUrlForString(s) {
|
||||||
const color = cssValue || defaultColors[colorIndex];
|
const color = cssValue || defaultColors[colorIndex];
|
||||||
let dataUrl = colorToDataURLCache.get(color);
|
let dataUrl = colorToDataURLCache.get(color);
|
||||||
if (!dataUrl) {
|
if (!dataUrl) {
|
||||||
dataUrl = urlForColor(color);
|
// validate color as this can come from account_data
|
||||||
colorToDataURLCache.set(color, dataUrl);
|
// with custom theming
|
||||||
|
if (isValidHexColor(color)) {
|
||||||
|
dataUrl = urlForColor(color);
|
||||||
|
colorToDataURLCache.set(color, dataUrl);
|
||||||
|
} else {
|
||||||
|
dataUrl = "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return dataUrl;
|
return dataUrl;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue