nodeparser: Rewrite the color parsing

Use gtk_css_parser_consume_function, for better error handling.
We are now getting the expected error for

  color(srgb 1 2 3 4 5 6)

Test included.
This commit is contained in:
Matthias Clasen 2024-08-06 00:23:07 -04:00
parent dbd16cd9da
commit 71d6392572
5 changed files with 57 additions and 42 deletions

View File

@ -1635,6 +1635,51 @@ gtk_css_parser_consume_number_or_percentage (GtkCssParser *parser,
}
}
typedef struct {
Context *context;
GdkColor *color;
} ColorArgData;
static guint
parse_color_arg (GtkCssParser *parser,
guint arg,
gpointer data)
{
ColorArgData *d = data;
GdkColorState *color_state;
float values[4];
if (!parse_color_state (parser, d->context, &color_state))
return 0;
for (int i = 0; i < 3; i++)
{
double number;
if (!gtk_css_parser_consume_number_or_percentage (parser, 0, 1, &number))
return 0;
values[i] = number;
}
if (gtk_css_parser_try_delim (parser, '/'))
{
double number;
if (!gtk_css_parser_consume_number_or_percentage (parser, 0, 1, &number))
return 0;
values[3] = number;
}
else
{
values[3] = 1;
}
gdk_color_init (d->color, color_state, values);
return 1;
}
static gboolean
parse_color2 (GtkCssParser *parser,
Context *context,
@ -1644,50 +1689,11 @@ parse_color2 (GtkCssParser *parser,
if (gtk_css_parser_has_function (parser, "color"))
{
GdkColorState *color_state;
float values[4];
ColorArgData data = { context, color };
gtk_css_parser_start_block (parser);
if (!gtk_css_parser_consume_function (parser, 1, 1, parse_color_arg, &data))
return FALSE;
if (!parse_color_state (parser, context, &color_state))
{
gtk_css_parser_end_block (parser);
return FALSE;
}
for (int i = 0; i < 3; i++)
{
double number;
if (!gtk_css_parser_consume_number_or_percentage (parser, 0, 1, &number))
{
gtk_css_parser_end_block (parser);
return FALSE;
}
values[i] = number;
}
if (gtk_css_parser_try_delim (parser, '/'))
{
double number;
if (!gtk_css_parser_consume_number_or_percentage (parser, 0, 1, &number))
{
gtk_css_parser_end_block (parser);
return FALSE;
}
values[3] = number;
}
else
{
values[3] = 1;
}
gtk_css_parser_end_block (parser);
gdk_color_init ((GdkColor *) color, color_state, values);
return TRUE;
}
else if (gdk_rgba_parser_parse (parser, &rgba))

View File

@ -325,6 +325,7 @@ node_parser_tests = [
'color.node',
'color2.node',
'color3.node',
'color4.node',
'conic-gradient.node',
'conic-gradient.ref.node',
'crash1.errors',

View File

@ -0,0 +1 @@
<data>:2:27-28: error: GTK_CSS_PARSER_ERROR_SYNTAX

View File

@ -0,0 +1,3 @@
color {
color: color(srgb 1 2 3 4 5 6);
}

View File

@ -0,0 +1,4 @@
color {
bounds: 0 0 50 50;
color: rgb(255,255,255);
}