From 53113644a0464929316be67ea878d47d13714aba Mon Sep 17 00:00:00 2001 From: Charlie DeTar Date: Mon, 17 Sep 2012 10:59:12 -0400 Subject: [PATCH] Require userColor to be valid css hex The utility functions colorutils.js assume that background colors are in CSS hex format, so require userColor to do the same, rather than allowing inputs like "red" and "rgba(...)", to insure that inversion checks will succeed. --- src/static/js/colorutils.js | 7 +++++++ src/static/js/pad.js | 13 ++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/static/js/colorutils.js b/src/static/js/colorutils.js index 5fbefb4d..74a2e463 100644 --- a/src/static/js/colorutils.js +++ b/src/static/js/colorutils.js @@ -24,6 +24,13 @@ var colorutils = {}; +// Check that a given value is a css hex color value, e.g. +// "#ffffff" or "#fff" +colorutils.isCssHex = function(cssColor) +{ + return /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.test(cssColor); +} + // "#ffffff" or "#fff" or "ffffff" or "fff" to [1.0, 1.0, 1.0] colorutils.css2triple = function(cssColor) { diff --git a/src/static/js/pad.js b/src/static/js/pad.js index 3e2663c8..21dea440 100644 --- a/src/static/js/pad.js +++ b/src/static/js/pad.js @@ -43,6 +43,7 @@ var padmodals = require('./pad_modals').padmodals; var padsavedrevs = require('./pad_savedrevs'); var paduserlist = require('./pad_userlist').paduserlist; var padutils = require('./pad_utils').padutils; +var colorutils = require('./colorutils').colorutils; var createCookie = require('./pad_utils').createCookie; var readCookie = require('./pad_utils').readCookie; @@ -369,18 +370,8 @@ function handshake() pad.myUserInfo.name = settings.globalUserName; $('#myusernameedit').attr({"value":settings.globalUserName}); // Updates the current users UI } - if (settings.globalUserColor !== false) + if (settings.globalUserColor !== false && colorutils.isCssHex(settings.globalUserColor)) { - // First, check the color to ensure it's a valid css color value. - var check = $("").css("background-color", "transparent"); - $("body").append(check); - var transparent = check.css("background-color"); - check.css("background-color", settings.globalUserColor); - // Ensure that setting the element changed the color. - if (check.css("background-color") === transparent) { - settings.globalUserColor = "#ff0000"; - } - check.remove(); // Add a 'globalUserColor' property to myUserInfo, so collabClient knows we have a query parameter. pad.myUserInfo.globalUserColor = settings.globalUserColor;