cssparser: Split integer gettig into has/consume functions

We can't try to get an integer because ultimately integer getters
support the same shenanigans that numbers and percentages do with calc()
and whatnot.
This commit is contained in:
Benjamin Otte 2019-04-05 13:41:08 +02:00
parent 5f3e5a0406
commit 7f99c1e588
6 changed files with 60 additions and 16 deletions

View File

@ -273,9 +273,8 @@ gtk_css_ease_value_parse_steps_arg (GtkCssParser *parser,
switch (arg)
{
case 0:
if (!_gtk_css_parser_try_int (parser, &data->n_steps))
if (!gtk_css_parser_consume_integer (parser, &data->n_steps))
{
_gtk_css_parser_error (parser, "Expected number of steps");
return 0;
}
else if (data->n_steps < 1)

View File

@ -256,8 +256,19 @@ gtk_css_font_features_value_parse (GtkCssParser *parser)
val = _gtk_css_number_value_new (1.0, GTK_CSS_NUMBER);
else if (gtk_css_parser_try_ident (parser, "off"))
val = _gtk_css_number_value_new (0.0, GTK_CSS_NUMBER);
else if (_gtk_css_parser_try_int (parser, &num))
val = _gtk_css_number_value_new ((double)num, GTK_CSS_NUMBER);
else if (gtk_css_parser_has_integer (parser))
{
if (gtk_css_parser_consume_integer (parser, &num))
{
val = _gtk_css_number_value_new ((double)num, GTK_CSS_NUMBER);
}
else
{
g_free (name);
_gtk_css_value_unref (result);
return NULL;
}
}
else
val = _gtk_css_number_value_new (1.0, GTK_CSS_NUMBER);

View File

@ -160,10 +160,12 @@ gtk_css_image_scaled_parse_arg (GtkCssParser *parser,
child = _gtk_css_image_new_parse (parser);
if (child == NULL)
return FALSE;
return 0;
if (!_gtk_css_parser_try_int (parser, &scale))
if (!gtk_css_parser_has_integer (parser))
scale = arg > 0 ? g_array_index (data->scales, int, arg - 1) + 1 : 1;
else if (!gtk_css_parser_consume_integer (parser, &scale))
return 0;
g_ptr_array_add (data->images, child);
g_array_append_val (data->scales, scale);

View File

@ -673,6 +673,21 @@ gtk_css_parser_has_ident (GtkCssParser *parser,
parser->data[len] != '(';
}
gboolean
gtk_css_parser_has_integer (GtkCssParser *parser)
{
guint i;
i = 0;
if (parser->data[0] == '-')
i++;
if (parser->data[i] >= '0' && parser->data[i] <= '9')
return TRUE;
return FALSE;
}
gboolean
gtk_css_parser_has_function (GtkCssParser *parser,
const char *name)
@ -755,8 +770,8 @@ gtk_css_parser_consume_string (GtkCssParser *parser)
}
gboolean
_gtk_css_parser_try_int (GtkCssParser *parser,
int *value)
gtk_css_parser_consume_integer (GtkCssParser *parser,
int *value)
{
gint64 result;
char *end;
@ -766,16 +781,16 @@ _gtk_css_parser_try_int (GtkCssParser *parser,
/* strtoll parses a plus, but we are not allowed to */
if (*parser->data == '+')
return FALSE;
goto fail;
errno = 0;
result = g_ascii_strtoll (parser->data, &end, 10);
if (errno)
return FALSE;
goto fail;
if (result > G_MAXINT || result < G_MININT)
return FALSE;
goto fail;
if (parser->data == end)
return FALSE;
goto fail;
parser->data = end;
*value = result;
@ -783,6 +798,10 @@ _gtk_css_parser_try_int (GtkCssParser *parser,
_gtk_css_parser_skip_whitespace (parser);
return TRUE;
fail:
_gtk_css_parser_error (parser, "Expected an integer");
return FALSE;
}
gboolean

View File

@ -66,6 +66,7 @@ gboolean gtk_css_parser_has_token (GtkCssParser *parser
GtkCssTokenType token_type);
gboolean gtk_css_parser_has_ident (GtkCssParser *parser,
const char *name);
gboolean gtk_css_parser_has_integer (GtkCssParser *parser);
gboolean gtk_css_parser_has_function (GtkCssParser *parser,
const char *name);
@ -92,8 +93,6 @@ char * _gtk_css_parser_try_ident (GtkCssParser *parser
gboolean skip_whitespace);
char * _gtk_css_parser_try_name (GtkCssParser *parser,
gboolean skip_whitespace);
gboolean _gtk_css_parser_try_int (GtkCssParser *parser,
int *value);
gboolean _gtk_css_parser_try_hash_color (GtkCssParser *parser,
GdkRGBA *rgba);
@ -101,6 +100,8 @@ char * gtk_css_parser_consume_ident (GtkCssParser *self);
char * gtk_css_parser_consume_string (GtkCssParser *self);
gboolean gtk_css_parser_consume_number (GtkCssParser *self,
double *number);
gboolean gtk_css_parser_consume_integer (GtkCssParser *parser,
int *value);
gboolean gtk_css_parser_consume_function (GtkCssParser *self,
guint min_args,
guint max_args,

View File

@ -1025,8 +1025,14 @@ parse_selector_pseudo_class_nth_child (GtkCssParser *parser,
else
multiplier = 1;
if (_gtk_css_parser_try_int (parser, &a))
if (gtk_css_parser_has_integer (parser))
{
if (!gtk_css_parser_consume_integer (parser, &a))
{
if (selector)
_gtk_css_selector_free (selector);
return NULL;
}
if (a < 0)
{
_gtk_css_parser_error (parser, "Expected an integer");
@ -1057,8 +1063,14 @@ parse_selector_pseudo_class_nth_child (GtkCssParser *parser,
else
multiplier = 1;
if (_gtk_css_parser_try_int (parser, &b))
if (gtk_css_parser_has_integer (parser))
{
if (!gtk_css_parser_consume_integer (parser, &b))
{
if (selector)
_gtk_css_selector_free (selector);
return NULL;
}
if (b < 0)
{
_gtk_css_parser_error (parser, "Expected an integer");