From 06255a72e52d1831ce6a1237f2826235faadc90e Mon Sep 17 00:00:00 2001 From: Le Duy Quang Date: Wed, 24 Mar 2021 16:09:46 +0700 Subject: [PATCH] Fixed font names that are not valid CSS identifiers not working. Prevented invalid font names. Added notification messages when something unusual happens while changing font family. --- src/js/userconfig.js | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/js/userconfig.js b/src/js/userconfig.js index f28fe3b62..cd6ea0c6f 100644 --- a/src/js/userconfig.js +++ b/src/js/userconfig.js @@ -106,6 +106,12 @@ function hideTestConfig() { $("#top .config").css("opacity", 0).addClass("hidden"); } +function isConfigKeyValid(name) { + if (name === null || name === undefined || name === "") return false; + if (name.length > 30) return false; + return /^[0-9a-zA-Z_.\-#+]+$/.test(name); +} + function setPlaySoundOnError(val, nosave) { if (val == undefined) { val = false; @@ -943,16 +949,37 @@ function previewFontFamily(font) { if (font == undefined) { font = "Roboto_Mono"; } - document.documentElement.style.setProperty("--font", font.replace(/_/g, " ")); + document.documentElement.style.setProperty( + "--font", + '"' + font.replace(/_/g, " ") + '"' + ); } //font family function setFontFamily(font, nosave) { if (font == undefined || font === "") { font = "Roboto_Mono"; + Notifications.add( + "Empty input received, reverted to the default font.", + 0, + 3, + "Custom font" + ); + } + if (!isConfigKeyValid(font)) { + Notifications.add( + `Invalid font name value: "${font}".`, + -1, + 3, + "Custom font" + ); + return; } ConfigSet.fontFamily(font); - document.documentElement.style.setProperty("--font", font.replace(/_/g, " ")); + document.documentElement.style.setProperty( + "--font", + '"' + font.replace(/_/g, " ") + '"' + ); Chart.defaults.global.defaultFontFamily = font.replace(/_/g, " "); if (!nosave) saveConfigToCookie(); }