From 76e8e2809b622659a9c5ffdc4f19922b7a68cfa3 Mon Sep 17 00:00:00 2001 From: David Benson Date: Wed, 12 Jun 2019 10:51:21 +0100 Subject: [PATCH] Added validation of input color codes --- javascript/examples/grapheditor/www/js/Dialogs.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/javascript/examples/grapheditor/www/js/Dialogs.js b/javascript/examples/grapheditor/www/js/Dialogs.js index 2ac178a74..f168e69e9 100644 --- a/javascript/examples/grapheditor/www/js/Dialogs.js +++ b/javascript/examples/grapheditor/www/js/Dialogs.js @@ -204,14 +204,21 @@ var ColorDialog = function(editorUi, color, apply, cancelFn) var applyBtn = mxUtils.button(mxResources.get('apply'), function() { var color = input.value; - ColorDialog.addRecentColor(color, 12); + // https://stackoverflow.com/questions/8027423/how-to-check-if-a-string-is-a-valid-hex-color-representation/8027444 + var colorValid = /(^#?[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(color); - if (color != 'none' && color.charAt(0) != '#') + if (colorValid) { - color = '#' + color; + ColorDialog.addRecentColor(color, 12); + + if (color != 'none' && color.charAt(0) != '#') + { + color = '#' + color; + } + + applyFunction(color); } - applyFunction(color); editorUi.hideDialog(); }); applyBtn.className = 'geBtn gePrimaryBtn';