From 686c98fafd093ccc179277c18f81091d84212d66 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Wed, 3 Oct 2007 06:47:45 +0000 Subject: Allow hex CSS colour values with omitted hash. Fix #rgb and #rrggbb handling to only accept hex characters. svn path=/trunk/netsurf/; revision=3620 --- css/ruleset.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'css') diff --git a/css/ruleset.c b/css/ruleset.c index f47c27bd4..5295558c6 100644 --- a/css/ruleset.c +++ b/css/ruleset.c @@ -31,6 +31,7 @@ #define _GNU_SOURCE /* for strndup */ #include +#include #include #include #include @@ -685,13 +686,22 @@ colour parse_colour(const struct css_node * const v) unsigned int r, g, b; struct css_colour_entry *col; char colour_name[21]; + bool valid_hex = true; switch (v->type) { case CSS_NODE_HASH: - if (v->data_length == 4) { + + for (unsigned int i = 1; i < v->data_length; i++) { + if (!isxdigit(v->data[i])) { + valid_hex = false; + break; + } + } + + if (v->data_length == 4 && valid_hex == true) { if (sscanf(v->data + 1, "%1x%1x%1x", &r, &g, &b) == 3) c = (b << 20) | (b << 16) | (g << 12) | (g << 8) | (r << 4) | r; - } else if (v->data_length == 7) { + } else if (v->data_length == 7 && valid_hex == true) { if (sscanf(v->data + 1, "%2x%2x%2x", &r, &g, &b) == 3) c = (b << 16) | (g << 8) | r; } @@ -722,6 +732,28 @@ colour parse_colour(const struct css_node * const v) break; } + /* Hex colour vaules without a preceding # are invalid but it is a + * common omission that other browsers cater for. */ + if (c == CSS_COLOR_NONE && (v->type == CSS_NODE_DELIM || + v->type == CSS_NODE_IDENT || v->type == CSS_NODE_NUMBER || + v->type == CSS_NODE_DIMENSION)) { + + for (unsigned int i = 0; i < v->data_length; i++) { + if (!isxdigit(v->data[i])) { + valid_hex = false; + break; + } + } + + if (v->data_length == 3 && valid_hex == true) { + if (sscanf(v->data, "%1x%1x%1x", &r, &g, &b) == 3) + c = (b << 20) | (b << 16) | (g << 12) | (g << 8) | (r << 4) | r; + } else if (v->data_length == 6 && valid_hex == true) { + if (sscanf(v->data, "%2x%2x%2x", &r, &g, &b) == 3) + c = (b << 16) | (g << 8) | r; + } + LOG(("Invalid CSS colour: %.*s", v->data_length, v->data)); + } return c; } -- cgit v1.2.3