cssstyle: Don't store custom css properties

This makes custom CSS properties no longer configurable. But it avoids
crashes when loading custom themes, so that's a good thing.

Testcase included.

https://bugzilla.redhat.com/show_bug.cgi?id=1281234
This commit is contained in:
Benjamin Otte 2015-11-20 19:37:52 +01:00
parent 6489ec440f
commit d75989a52b
5 changed files with 45 additions and 24 deletions

View File

@ -28,9 +28,8 @@ GtkCssLookup *
_gtk_css_lookup_new (const GtkBitmask *relevant)
{
GtkCssLookup *lookup;
guint n = _gtk_css_style_property_get_n_properties ();
lookup = g_malloc0 (sizeof (GtkCssLookup) + sizeof (GtkCssLookupValue) * n);
lookup = g_malloc0 (sizeof (GtkCssLookup));
if (relevant)
{
@ -39,7 +38,7 @@ _gtk_css_lookup_new (const GtkBitmask *relevant)
else
{
lookup->missing = _gtk_bitmask_new ();
lookup->missing = _gtk_bitmask_invert_range (lookup->missing, 0, n);
lookup->missing = _gtk_bitmask_invert_range (lookup->missing, 0, GTK_CSS_PROPERTY_N_PROPERTIES);
}
return lookup;
@ -109,16 +108,14 @@ _gtk_css_lookup_resolve (GtkCssLookup *lookup,
GtkCssStaticStyle *style,
GtkCssStyle *parent_style)
{
guint i, n;
guint i;
gtk_internal_return_if_fail (lookup != NULL);
gtk_internal_return_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider));
gtk_internal_return_if_fail (GTK_IS_CSS_STATIC_STYLE (style));
gtk_internal_return_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style));
n = _gtk_css_style_property_get_n_properties ();
for (i = 0; i < n; i++)
for (i = 0; i < GTK_CSS_PROPERTY_N_PROPERTIES; i++)
{
if (lookup->values[i].value ||
_gtk_bitmask_get (lookup->missing, i))

View File

@ -35,7 +35,7 @@ typedef struct {
struct _GtkCssLookup {
GtkBitmask *missing;
GtkCssLookupValue values[1];
GtkCssLookupValue values[GTK_CSS_PROPERTY_N_PROPERTIES];
};
GtkCssLookup * _gtk_css_lookup_new (const GtkBitmask *relevant);

View File

@ -46,11 +46,14 @@ gtk_css_static_style_get_value (GtkCssStyle *style,
{
GtkCssStaticStyle *sstyle = GTK_CSS_STATIC_STYLE (style);
if (sstyle->values == NULL ||
id >= sstyle->values->len)
return NULL;
if (G_UNLIKELY (id >= GTK_CSS_PROPERTY_N_PROPERTIES))
{
GtkCssStyleProperty *prop = _gtk_css_style_property_lookup_by_id (id);
return g_ptr_array_index (sstyle->values, id);
return _gtk_css_style_property_get_initial_value (prop);
}
return sstyle->values[id];
}
static GtkCssSection *
@ -70,11 +73,12 @@ static void
gtk_css_static_style_dispose (GObject *object)
{
GtkCssStaticStyle *style = GTK_CSS_STATIC_STYLE (object);
guint i;
if (style->values)
for (i = 0; i < GTK_CSS_PROPERTY_N_PROPERTIES; i++)
{
g_ptr_array_unref (style->values);
style->values = NULL;
if (style->values[i])
_gtk_css_value_unref (style->values[i]);
}
if (style->sections)
{
@ -115,14 +119,9 @@ gtk_css_static_style_set_value (GtkCssStaticStyle *style,
GtkCssValue *value,
GtkCssSection *section)
{
if (style->values == NULL)
style->values = g_ptr_array_new_with_free_func ((GDestroyNotify)_gtk_css_value_unref);
if (id >= style->values->len)
g_ptr_array_set_size (style->values, id + 1);
if (g_ptr_array_index (style->values, id))
_gtk_css_value_unref (g_ptr_array_index (style->values, id));
g_ptr_array_index (style->values, id) = _gtk_css_value_ref (value);
if (style->values[id])
_gtk_css_value_unref (style->values[id]);
style->values[id] = _gtk_css_value_ref (value);
if (style->sections && style->sections->len > id && g_ptr_array_index (style->sections, id))
{
@ -208,6 +207,7 @@ gtk_css_static_style_compute_value (GtkCssStaticStyle *style,
gtk_internal_return_if_fail (GTK_IS_CSS_STATIC_STYLE (style));
gtk_internal_return_if_fail (GTK_IS_STYLE_PROVIDER_PRIVATE (provider));
gtk_internal_return_if_fail (parent_style == NULL || GTK_IS_CSS_STYLE (parent_style));
gtk_internal_return_if_fail (id < GTK_CSS_PROPERTY_N_PROPERTIES);
/* http://www.w3.org/TR/css3-cascade/#cascade
* Then, for every element, the value for each property can be found

View File

@ -39,7 +39,7 @@ struct _GtkCssStaticStyle
{
GtkCssStyle parent;
GPtrArray *values; /* the values */
GtkCssValue *values[GTK_CSS_PROPERTY_N_PROPERTIES]; /* the values */
GPtrArray *sections; /* sections the values are defined in */
GtkCssChange change; /* change as returned by value lookup */

View File

@ -407,6 +407,29 @@ test_style_classes (void)
g_object_unref (context);
}
void
test_new_css_property (void)
{
GtkWidget *widget;
GtkStyleContext *context;
GtkBorder padding;
widget = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_widget_realize (widget);
context = gtk_widget_get_style_context (widget);
gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
gtk_style_properties_register_property (NULL,
g_param_spec_int ("test", "test", "test",
0, G_MAXINT, 42, G_PARAM_READWRITE));
G_GNUC_END_IGNORE_DEPRECATIONS;
gtk_style_context_add_class (context, "nonexisting");
gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
}
int
main (int argc, char *argv[])
{
@ -421,6 +444,7 @@ main (int argc, char *argv[])
g_test_add_func ("/style/set-widget-path-saved", test_set_widget_path_saved);
g_test_add_func ("/style/widget-path-parent", test_widget_path_parent);
g_test_add_func ("/style/classes", test_style_classes);
g_test_add_func ("/style/new-css-property", test_new_css_property);
return g_test_run ();
}