css: Report some var() syntax errors

Detect cases such as var(), var(-), var("a") or var(21) early
and report them as syntax errors.

Test included.

Related: #6715
This commit is contained in:
Matthias Clasen 2024-05-19 15:42:18 -04:00
parent af0c277bba
commit b60c60f03b
4 changed files with 35 additions and 2 deletions

View File

@ -1482,9 +1482,9 @@ gtk_css_parser_parse_value_into_token_stream (GtkCssParser *self)
GtkCssVariableValueReference ref;
char *var_name = g_strdup (gtk_css_token_get_string (token));
if (var_name[0] != '-' || var_name[1] != '-')
if (strlen (var_name) < 3 || var_name[0] != '-' || var_name[1] != '-')
{
gtk_css_parser_error_value (self, "Invalid variable name: %s", var_name);
gtk_css_parser_error_syntax (self, "Invalid variable name: %s", var_name);
g_free (var_name);
goto error;
}
@ -1527,6 +1527,20 @@ gtk_css_parser_parse_value_into_token_stream (GtkCssParser *self)
gtk_css_parser_references_append (&refs, &ref);
}
else
{
if (gtk_css_token_is (token, GTK_CSS_TOKEN_EOF))
{
gtk_css_parser_error_syntax (self, "Missing variable name");
}
else
{
char *s = gtk_css_token_to_string (token);
gtk_css_parser_error_syntax (self, "Expected a variable name, not %s", s);
g_free (s);
}
goto error;
}
}
}
}

View File

@ -0,0 +1,15 @@
a {
--accent-fg-color: var();
}
b {
--accent-fg-color: var(-);
}
c {
--accent-fg-color: var(a);
}
d {
--accent-fg-color: var(21);
}

View File

@ -0,0 +1,4 @@
variables-invalid-07.css:2:26-27: error: GTK_CSS_PARSER_ERROR_SYNTAX
variables-invalid-07.css:6:26-27: error: GTK_CSS_PARSER_ERROR_SYNTAX
variables-invalid-07.css:10:26-27: error: GTK_CSS_PARSER_ERROR_SYNTAX
variables-invalid-07.css:14:26-28: error: GTK_CSS_PARSER_ERROR_SYNTAX