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.
This commit is contained in:
Charlie DeTar 2012-09-17 10:59:12 -04:00
parent bc6e495e8c
commit 53113644a0
2 changed files with 9 additions and 11 deletions

View File

@ -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)
{

View File

@ -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 = $("<span/>").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;