GtkBuilder: Enforce "class" as a mandatory attribute for <object>

https://bugzilla.gnome.org/show_bug.cgi?id=786931
This commit is contained in:
Debarshi Ray 2017-09-18 18:28:55 +02:00
parent d09f695172
commit 60cee7339f
2 changed files with 28 additions and 8 deletions

View File

@ -268,6 +268,12 @@ parse_object (GMarkupParseContext *context,
return; return;
} }
/* Even though 'class' is a mandatory attribute, we don't flag its
* absence here because it's supposed to throw
* GTK_BUILDER_ERROR_MISSING_ATTRIBUTE, not
* G_MARKUP_ERROR_MISSING_ATTRIBUTE. It's handled immediately
* afterwards.
*/
if (!g_markup_collect_attributes (element_name, names, values, error, if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "class", &object_class, G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "class", &object_class,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "constructor", &constructor, G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "constructor", &constructor,
@ -279,6 +285,12 @@ parse_object (GMarkupParseContext *context,
return; return;
} }
if (!object_class)
{
error_missing_attribute (data, element_name, "class", error);
return;
}
if (type_func) if (type_func)
{ {
/* Call the GType function, and return the GType, it's guaranteed afterwards /* Call the GType function, and return the GType, it's guaranteed afterwards
@ -295,8 +307,10 @@ parse_object (GMarkupParseContext *context,
return; return;
} }
} }
else if (object_class) else
{ {
g_assert_nonnull (object_class);
object_type = gtk_builder_get_type_from_name (data->builder, object_class); object_type = gtk_builder_get_type_from_name (data->builder, object_class);
if (object_type == G_TYPE_INVALID) if (object_type == G_TYPE_INVALID)
{ {
@ -308,11 +322,6 @@ parse_object (GMarkupParseContext *context,
return; return;
} }
} }
else
{
error_missing_attribute (data, element_name, "class", error);
return;
}
if (!object_id) if (!object_id)
{ {

View File

@ -670,7 +670,7 @@ test_types (void)
"</interface>"; "</interface>";
const gchar buffer2[] = const gchar buffer2[] =
"<interface>" "<interface>"
" <object type-func=\"gtk_window_get_type\" id=\"window\"/>" " <object class=\"GtkWindow\" type-func=\"gtk_window_get_type\" id=\"window\"/>"
"</interface>"; "</interface>";
const gchar buffer3[] = const gchar buffer3[] =
"<interface>" "<interface>"
@ -678,7 +678,11 @@ test_types (void)
"</interface>"; "</interface>";
const gchar buffer4[] = const gchar buffer4[] =
"<interface>" "<interface>"
" <object type-func=\"xxx_invalid_get_type_function\" id=\"window\"/>" " <object class=\"GtkWindow\" type-func=\"xxx_invalid_get_type_function\" id=\"window\"/>"
"</interface>";
const gchar buffer5[] =
"<interface>"
" <object type-func=\"gtk_window_get_type\" id=\"window\"/>"
"</interface>"; "</interface>";
GtkBuilder *builder; GtkBuilder *builder;
GObject *window; GObject *window;
@ -707,6 +711,13 @@ test_types (void)
g_assert_error (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION); g_assert_error (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION);
g_error_free (error); g_error_free (error);
g_object_unref (builder); g_object_unref (builder);
error = NULL;
builder = gtk_builder_new ();
gtk_builder_add_from_string (builder, buffer5, -1, &error);
g_assert_error (error, GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_MISSING_ATTRIBUTE);
g_error_free (error);
g_object_unref (builder);
} }
static void static void