]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcssprovider.c
Deprecate all the public API that is using GdkColor struct
[~andy/gtk] / gtk / gtkcssprovider.c
index 89c1ee5edd96b1c2127fa99912719e6da024418a..1ac52bc533c4db90f5a49457ba3c52f7f4d469e4 100644 (file)
 
 #include "gtkcssproviderprivate.h"
 
-#include "gtkcssstringfuncsprivate.h"
+#include "gtkcssparserprivate.h"
+#include "gtkcsssectionprivate.h"
+#include "gtkcssselectorprivate.h"
 #include "gtksymboliccolor.h"
 #include "gtkstyleprovider.h"
 #include "gtkstylecontextprivate.h"
+#include "gtkstylepropertiesprivate.h"
+#include "gtkstylepropertyprivate.h"
 #include "gtkbindings.h"
 #include "gtkmarshalers.h"
 #include "gtkprivate.h"
  *
  * /* Theme any widget within a GtkBin */
  * GtkBin * {
- *     font-name: Sans 20
+ *     font: Sans 20
  * }
  *
  * /* Theme a label named title-label */
  * GtkLabel#title-label {
- *     font-name: Sans 15
+ *     font: Sans 15
  * }
  *
  * /* Theme any widget named main-entry */
  *                color-stop (1, &commat;green))</literallayout></para>
  * </example>
  * </refsect2>
- * <refsect2 id="gtkcssprovider-slices">
+ * <refsect2 id="gtkcssprovider-shadows">
+ * <title>Text shadow</title>
+ * <para>
+ * A shadow list can be applied to text or symbolic icons, using the CSS3
+ * text-shadow syntax, as defined in
+ * <ulink url="http://www.w3.org/TR/css3-text/#text-shadow">the CSS3 specification</ulink>.
+ * </para>
+ * <para>
+ * A text shadow is specified using the syntax
+ * <literallayout>text-shadow: @horizontal_offset @vertical_offset [ @blur_radius ] @color</literallayout>
+ * The offset of the shadow is specified with the @horizontal_offset and @vertical_offset
+ * parameters. The optional blur radius is parsed, but it is currently not rendered by
+ * the GTK+ theming engine.
+ * </para>
+ * <para>
+ * To set multiple shadows on an element, you can specify a comma-separated list
+ * of shadow elements in the text-shadow property. Shadows are always rendered
+ * front-back, i.e. the first shadow specified is on top of the others. Shadows
+ * can thus overlay each other, but they can never overlay the text itself,
+ * which is always rendered on top of the shadow layer.
+ * </para>
+ * </refsect2>
+ * <refsect2>
+ * <title>Box shadow</title>
+ * <para>
+ * Themes can apply shadows on framed elements using the CSS3 box-shadow syntax,
+ * as defined in 
+ * <ulink url="http://www.w3.org/TR/css3-background/#the-box-shadow">the CSS3 specification</ulink>.
+ * </para>
+ * <para>
+ * A box shadow is specified using the syntax
+ * <literallayout>box-shadow: [ @inset ] @horizontal_offset @vertical_offset [ @blur_radius ] [ @spread ] @color</literallayout>
+ * A positive offset will draw a shadow that is offset to the right (down) of the box,
+ * a negative offset to the left (top). The optional spread parameter defines an additional
+ * distance to expand the shadow shape in all directions, by the specified radius.
+ * The optional blur radius parameter is parsed, but it is currently not rendered by
+ * the GTK+ theming engine.
+ * The inset parameter defines whether the drop shadow should be rendered inside or outside
+ * the box canvas. Only inset box-shadows are currently supported by the GTK+ theming engine,
+ * non-inset elements are currently ignored.
+ * </para>
+ * <para>
+ * To set multiple box-shadows on an element, you can specify a comma-separated list
+ * of shadow elements in the box-shadow property. Shadows are always rendered
+ * front-back, i.e. the first shadow specified is on top of the others, so they may
+ * overlap other boxes or other shadows.
+ * </para>
+ * </refsect2>
+ * <refsect2 id="gtkcssprovider-border-image">
  * <title>Border images</title>
  * <para>
- * Images can be used in 'slices' for the purpose of creating scalable
- * borders.
+ * Images and gradients can also be used in slices for the purpose of creating
+ * scalable borders.
+ * For more information, see the CSS3 documentation for the border-image property,
+ * which can be found <ulink url="http://www.w3.org/TR/css3-background/#border-images">here</ulink>.
  * </para>
  * <inlinegraphic fileref="slices.png" format="PNG"/>
  * <para>
- * The syntax for specifying border images of this kind is:
- * <literallayout>url(@path) @top @right @bottom @left [repeat|stretch]? [repeat|stretch]?</literallayout>
- * The sizes of the 'cut off' portions are specified
- * with the @top, @right, @bottom and @left parameters.
- * The 'middle' sections can be repeated or stretched to create
- * the desired effect, by adding the 'repeat' or 'stretch' options after
- * the dimensions. If two options are specified, the first one affects
+ * The parameters of the slicing process are controlled by
+ * four separate properties. Note that you can use the
+ * <literallayout>border-image</literallayout> shorthand property
+ * to set values for the three properties at the same time.
+ * </para>
+ * <para>
+ * <literallayout>border-image-source: url(@path)
+ * (or border-image-source: -gtk-gradient(...))</literallayout>:
+ * Specifies the source of the border image, and it can either
+ * be an URL or a gradient (see above).
+ * </para>
+ * <para>
+ * <literallayout>border-image-slice: @top @right @bottom @left</literallayout>
+ * The sizes specified by the @top, @right, @bottom and @left parameters
+ * are the offsets, in pixels, from the relevant edge where the image
+ * should be "cut off" to build the slices used for the rendering
+ * of the border.
+ * </para>
+ * <para>
+ * <literallayout>border-image-width: @top @right @bottom @left</literallayout>
+ * The sizes specified by the @top, @right, @bottom and @left parameters
+ * are inward distances from the border box edge, used to specify the
+ * rendered size of each slice determined by border-image-slice.
+ * If this property is not specified, the values of border-width will
+ * be used as a fallback.
+ * </para>
+ * <para>
+ * <literallayout>border-image-repeat: [stretch|repeat|round|space] ? 
+ * [stretch|repeat|round|space]</literallayout>
+ * Specifies how the image slices should be rendered in the area
+ * outlined by border-width.
+ * The default (stretch) is to resize the slice to fill in the whole 
+ * allocated area.
+ * If the value of this property is 'repeat', the image slice
+ * will be tiled to fill the area.
+ * If the value of this property is 'round', the image slice will
+ * be tiled to fill the area, and scaled to fit it exactly
+ * a whole number of times.
+ * If the value of this property is 'space', the image slice will
+ * be tiled to fill the area, and if it doesn't fit it exactly a whole
+ * number of times, the extra space is distributed as padding around 
+ * the slices.
+ * If two options are specified, the first one affects
  * the horizontal behaviour and the second one the vertical behaviour.
  * If only one option is specified, it affects both.
  * </para>
  * map intuitively in a widget based environment).
  * </para>
  * <para>
- * There is also a difference in shorthand properties, for
- * example in common CSS it is fine to define a font through
- * the different @font-family, @font-style, @font-size
- * properties, meanwhile in GTK+'s CSS only the canonical
- * @font property is supported.
- * </para>
- * <para>
  * The currently supported properties are:
  * </para>
  * <informaltable>
  *       <row>
  *         <entry>background-color</entry>
  *         <entry morerows="2">color (see above)</entry>
- *         <entry morerows="2">#GdkRGBA</entry>
- *         <entry morerows="2"><literallayout>background-color: &num;fff;
+ *         <entry morerows="7">#GdkRGBA</entry>
+ *         <entry morerows="7"><literallayout>background-color: &num;fff;
  * color: &amp;color1;
  * background-color: shade (&amp;color1, 0.5);
  * color: mix (&amp;color1, &num;f0f, 0.8);</literallayout>
  *         <entry>color</entry>
  *       </row>
  *       <row>
+ *         <entry>border-top-color</entry>
+ *         <entry morerows="4">transparent|color (see above)</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-right-color</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-bottom-color</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-left-color</entry>
+ *       </row>
+ *       <row>
  *         <entry>border-color</entry>
+ *         <entry>[transparent|color]{1,4}</entry>
+ *       </row>
+ *       <row>
+ *         <entry>font-family</entry>
+ *         <entry>@family [, @family]*</entry>
+ *         <entry>#gchararray</entry>
+ *         <entry>font-family: Sans, Arial;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>font-style</entry>
+ *         <entry>[normal|oblique|italic]</entry>
+ *         <entry>#PANGO_TYPE_STYLE</entry>
+ *         <entry>font-style: italic;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>font-variant</entry>
+ *         <entry>[normal|small-caps]</entry>
+ *         <entry>#PANGO_TYPE_VARIANT</entry>
+ *         <entry>font-variant: normal;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>font-weight</entry>
+ *         <entry>[normal|bold|bolder|lighter|100|200|300|400|500|600|700|800|900]</entry>
+ *         <entry>#PANGO_TYPE_WEIGHT</entry>
+ *         <entry>font-weight: bold;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>font-size</entry>
+ *         <entry>Font size in point</entry>
+ *         <entry>#gint</entry>
+ *         <entry>font-size: 13;</entry>
  *       </row>
  *       <row>
  *         <entry>font</entry>
  *         <entry>font: Sans 15;</entry>
  *       </row>
  *       <row>
+ *         <entry>margin-top</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>margin-top: 0;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>margin-left</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>margin-left: 1;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>margin-bottom</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>margin-bottom: 2;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>margin-right</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>margin-right: 4;</entry>
+ *       </row>
+ *       <row>
  *         <entry>margin</entry>
  *         <entry morerows="1"><literallayout>@width
  * @vertical_width @horizontal_width
  *         </entry>
  *       </row>
  *       <row>
+ *         <entry>padding-top</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>padding-top: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>padding-left</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>padding-left: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>padding-bottom</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>padding-bottom: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>padding-right</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>padding-right: 5;</entry>
+ *       </row>
+ *       <row>
  *         <entry>padding</entry>
  *       </row>
  *       <row>
  *         </entry>
  *       </row>
  *       <row>
- *         <entry>border-width</entry>
+ *         <entry>background-repeat</entry>
+ *         <entry>[repeat|no-repeat]</entry>
+ *         <entry>internal</entry>
+ *         <entry><literallayout>background-repeat: no-repeat;</literallayout>
+ *                If not specified, the style doesn't respect the CSS3
+ *                specification, since the background will be
+ *                stretched to fill the area.
+ *         </entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-top-width</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>border-top-width: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-left-width</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>border-left-width: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-bottom-width</entry>
+ *         <entry>integer</entry>
+ *         <entry>#gint</entry>
+ *         <entry>border-bottom-width: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-right-width</entry>
  *         <entry>integer</entry>
  *         <entry>#gint</entry>
- *         <entry>border-width: 5;</entry>
+ *         <entry>border-right-width: 5;</entry>
+ *       </row>
+ *       <row>
+ *         <entry>border-width</entry>
+ *         <entry morerows="1">#GtkBorder</entry>
+ *         <entry morerows="1"><literallayout>border-width: 1;
+ * border-width: 1 2;
+ * border-width: 1 2 3;
+ * border-width: 1 2 3 5;</literallayout>
+ *         </entry>
  *       </row>
  *       <row>
  *         <entry>border-radius</entry>
  *         </entry>
  *       </row>
  *       <row>
+ *         <entry>text-shadow</entry>
+ *         <entry>shadow list (see above)</entry>
+ *         <entry>#GtkTextShadow</entry>
+ *         <entry><literallayout>text-shadow: 1 1 0 blue, -4 -4 red;</literallayout></entry>
+ *       </row>
+ *       <row>
  *         <entry>transition</entry>
  *         <entry>transition (see above)</entry>
  *         <entry>internal use only</entry>
  * </refsect2>
  */
 
-typedef struct SelectorElement SelectorElement;
-typedef struct SelectorPath SelectorPath;
-typedef struct SelectorStyleInfo SelectorStyleInfo;
-typedef struct _GtkCssScannerPrivate GtkCssScannerPrivate;
-typedef enum SelectorElementType SelectorElementType;
-typedef enum CombinatorType CombinatorType;
+typedef struct GtkCssRuleset GtkCssRuleset;
+typedef struct _GtkCssScanner GtkCssScanner;
 typedef enum ParserScope ParserScope;
 typedef enum ParserSymbol ParserSymbol;
 
-enum SelectorElementType {
-  SELECTOR_TYPE_NAME,
-  SELECTOR_NAME,
-  SELECTOR_GTYPE,
-  SELECTOR_REGION,
-  SELECTOR_CLASS,
-  SELECTOR_GLOB
-};
-
-enum CombinatorType {
-  COMBINATOR_DESCENDANT, /* No direct relation needed */
-  COMBINATOR_CHILD       /* Direct child */
-};
-
-struct SelectorElement
-{
-  SelectorElementType elem_type;
-  CombinatorType combinator;
-
-  union
-  {
-    GQuark name;
-    GType type;
-
-    struct
-    {
-      GQuark name;
-      GtkRegionFlags flags;
-    } region;
-  };
-};
-
-struct SelectorPath
+struct GtkCssRuleset
 {
-  GSList *elements;
-  GtkStateFlags state;
-  guint ref_count;
-};
-
-struct SelectorStyleInfo
-{
-  SelectorPath *path;
+  GtkCssSelector *selector;
+  GHashTable *widget_style;
   GHashTable *style;
+
+  guint has_inherit :1;
 };
 
-struct _GtkCssScannerPrivate
+struct _GtkCssScanner
 {
+  GtkCssProvider *provider;
+  GtkCssParser *parser;
+  GtkCssSection *section;
+  GtkCssScanner *parent;
+  GFile *file;
+  GFile *base;
   GSList *state;
-  GSList *cur_selectors;
-  GHashTable *cur_properties;
 };
 
 struct _GtkCssProviderPrivate
@@ -799,33 +981,7 @@ struct _GtkCssProviderPrivate
 
   GHashTable *symbolic_colors;
 
-  GPtrArray *selectors_info;
-};
-
-enum ParserScope {
-  SCOPE_SELECTOR,
-  SCOPE_PSEUDO_CLASS,
-  SCOPE_NTH_CHILD,
-  SCOPE_DECLARATION,
-  SCOPE_VALUE,
-  SCOPE_BINDING_SET
-};
-
-/* Extend GtkStateType, since these
- * values are also used as symbols
- */
-enum ParserSymbol {
-  /* Scope: pseudo-class */
-  SYMBOL_NTH_CHILD = GTK_STATE_FOCUSED + 1,
-  SYMBOL_FIRST_CHILD,
-  SYMBOL_LAST_CHILD,
-  SYMBOL_SORTED_CHILD,
-
-  /* Scope: nth-child */
-  SYMBOL_NTH_CHILD_EVEN,
-  SYMBOL_NTH_CHILD_ODD,
-  SYMBOL_NTH_CHILD_FIRST,
-  SYMBOL_NTH_CHILD_LAST
+  GArray *rulesets;
 };
 
 enum {
@@ -838,15 +994,12 @@ static guint css_provider_signals[LAST_SIGNAL] = { 0 };
 static void gtk_css_provider_finalize (GObject *object);
 static void gtk_css_style_provider_iface_init (GtkStyleProviderIface *iface);
 
-static void scanner_apply_scope (GScanner    *scanner,
-                                 ParserScope  scope);
-static gboolean gtk_css_provider_load_from_path_internal (GtkCssProvider  *css_provider,
-                                                          const gchar     *path,
-                                                          gboolean         reset,
-                                                          GError         **error);
-static void     gtk_css_provider_take_error (GtkCssProvider *provider,
-                                             GScanner       *scanner,
-                                             GError         *error);
+static gboolean
+gtk_css_provider_load_internal (GtkCssProvider *css_provider,
+                                GtkCssScanner  *scanner,
+                                GFile          *file,
+                                const char     *data,
+                                GError        **error);
 
 GQuark
 gtk_css_provider_error_quark (void)
@@ -860,10 +1013,8 @@ G_DEFINE_TYPE_EXTENDED (GtkCssProvider, gtk_css_provider, G_TYPE_OBJECT, 0,
 
 static void
 gtk_css_provider_parsing_error (GtkCssProvider  *provider,
-                                const gchar     *path,
-                                guint            line,
-                                guint            position,
-                                const GError *   error)
+                                GtkCssSection   *section,
+                                const GError    *error)
 {
   /* Only emit a warning when we have no error handlers. This is our
    * default handlers. And in this case erroneous CSS files are a bug
@@ -876,7 +1027,34 @@ gtk_css_provider_parsing_error (GtkCssProvider  *provider,
                                      0,
                                      TRUE))
     {
-      g_warning ("Theme parsing error: %s:%u:%u: %s", path ? path : "<unknown>", line, position, error->message);
+      GFileInfo *info;
+      GFile *file;
+      const char *path;
+
+      file = gtk_css_section_get_file (section);
+      if (file)
+        {
+          info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
+
+          if (info)
+            path = g_file_info_get_display_name (info);
+          else
+            path = "<broken file>";
+        }
+      else
+        {
+          info = NULL;
+          path = "<data>";
+        }
+
+      g_warning ("Theme parsing error: %s:%u:%u: %s",
+                 path,
+                 gtk_css_section_get_end_line (section) + 1,
+                 gtk_css_section_get_end_position (section),
+                 error->message);
+
+      if (info)
+        g_object_unref (info);
     }
 }
 
@@ -888,11 +1066,7 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
   /**
    * GtkCssProvider::parsing-error:
    * @provider: the provider that had a parsing error
-   * @path: path to the parsed file or %NULL if the file cannot be
-   *   identified or the data was not loaded from a file
-   * @line: line in the file or data or 0 if unknown
-   * @position: offset into the current line or 0 if unknown or the
-   *   whole line is affected
+   * @section: section the error happened in
    * @error: The parsing error
    *
    * Signals that a parsing error occured. the @path, @line and @position
@@ -913,9 +1087,8 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkCssProviderClass, parsing_error),
                   NULL, NULL,
-                  _gtk_marshal_VOID__STRING_UINT_UINT_BOXED,
-                  G_TYPE_NONE, 4,
-                  G_TYPE_STRING, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_ERROR);
+                  _gtk_marshal_VOID__BOXED_BOXED,
+                  G_TYPE_NONE, 2, GTK_TYPE_CSS_SECTION, G_TYPE_ERROR);
 
   object_class->finalize = gtk_css_provider_finalize;
 
@@ -924,262 +1097,243 @@ gtk_css_provider_class_init (GtkCssProviderClass *klass)
   g_type_class_add_private (object_class, sizeof (GtkCssProviderPrivate));
 }
 
-static SelectorPath *
-selector_path_new (void)
+static void
+gtk_css_ruleset_init_copy (GtkCssRuleset       *new,
+                           const GtkCssRuleset *ruleset,
+                           GtkCssSelector      *selector)
 {
-  SelectorPath *path;
-
-  path = g_slice_new0 (SelectorPath);
-  path->ref_count = 1;
-
-  return path;
-}
+  memcpy (new, ruleset, sizeof (GtkCssRuleset));
 
-static SelectorPath *
-selector_path_ref (SelectorPath *path)
-{
-  path->ref_count++;
-  return path;
+  new->selector = selector;
+  if (new->widget_style)
+    g_hash_table_ref (new->widget_style);
+  if (new->style)
+    g_hash_table_ref (new->style);
 }
 
 static void
-selector_path_unref (SelectorPath *path)
+gtk_css_ruleset_clear (GtkCssRuleset *ruleset)
 {
-  path->ref_count--;
+  if (ruleset->style)
+    g_hash_table_unref (ruleset->style);
+  if (ruleset->widget_style)
+    g_hash_table_unref (ruleset->widget_style);
+  if (ruleset->selector)
+    _gtk_css_selector_free (ruleset->selector);
 
-  if (path->ref_count > 0)
-    return;
-
-  while (path->elements)
-    {
-      g_slice_free (SelectorElement, path->elements->data);
-      path->elements = g_slist_delete_link (path->elements, path->elements);
-    }
-
-  g_slice_free (SelectorPath, path);
+  memset (ruleset, 0, sizeof (GtkCssRuleset));
 }
 
-static void
-selector_path_prepend_type (SelectorPath *path,
-                            const gchar  *type_name)
+typedef struct _PropertyValue PropertyValue;
+struct _PropertyValue {
+  GtkCssSection *section;
+  GValue         value;
+};
+
+static PropertyValue *
+property_value_new (GtkCssSection *section)
 {
-  SelectorElement *elem;
-  GType type;
+  PropertyValue *value;
 
-  elem = g_slice_new (SelectorElement);
-  elem->combinator = COMBINATOR_DESCENDANT;
-  type = g_type_from_name (type_name);
+  value = g_slice_new0 (PropertyValue);
 
-  if (type == G_TYPE_INVALID)
-    {
-      elem->elem_type = SELECTOR_TYPE_NAME;
-      elem->name = g_quark_from_string (type_name);
-    }
-  else
-    {
-      elem->elem_type = SELECTOR_GTYPE;
-      elem->type = type;
-    }
+  value->section = gtk_css_section_ref (section);
 
-  path->elements = g_slist_prepend (path->elements, elem);
+  return value;
 }
 
 static void
-selector_path_prepend_glob (SelectorPath *path)
+property_value_free (PropertyValue *value)
 {
-  SelectorElement *elem;
+  if (G_IS_VALUE (&value->value))
+    g_value_unset (&value->value);
 
-  elem = g_slice_new (SelectorElement);
-  elem->elem_type = SELECTOR_GLOB;
-  elem->combinator = COMBINATOR_DESCENDANT;
+  gtk_css_section_unref (value->section);
 
-  path->elements = g_slist_prepend (path->elements, elem);
+  g_slice_free (PropertyValue, value);
 }
 
 static void
-selector_path_prepend_region (SelectorPath   *path,
-                              const gchar    *name,
-                              GtkRegionFlags  flags)
+gtk_css_ruleset_add_style (GtkCssRuleset *ruleset,
+                           char          *name,
+                           PropertyValue *value)
 {
-  SelectorElement *elem;
-
-  elem = g_slice_new (SelectorElement);
-  elem->combinator = COMBINATOR_DESCENDANT;
-  elem->elem_type = SELECTOR_REGION;
+  if (ruleset->widget_style == NULL)
+    ruleset->widget_style = g_hash_table_new_full (g_str_hash,
+                                                   g_str_equal,
+                                                   (GDestroyNotify) g_free,
+                                                   (GDestroyNotify) property_value_free);
 
-  elem->region.name = g_quark_from_string (name);
-  elem->region.flags = flags;
-
-  path->elements = g_slist_prepend (path->elements, elem);
+  g_hash_table_insert (ruleset->widget_style, name, value);
 }
 
 static void
-selector_path_prepend_name (SelectorPath *path,
-                            const gchar  *name)
+gtk_css_ruleset_add (GtkCssRuleset          *ruleset,
+                     const GtkStyleProperty *prop,
+                     PropertyValue          *value)
 {
-  SelectorElement *elem;
-
-  elem = g_slice_new (SelectorElement);
-  elem->combinator = COMBINATOR_DESCENDANT;
-  elem->elem_type = SELECTOR_NAME;
+  if (ruleset->style == NULL)
+    ruleset->style = g_hash_table_new_full (g_direct_hash,
+                                            g_direct_equal,
+                                            NULL,
+                                            (GDestroyNotify) property_value_free);
 
-  elem->name = g_quark_from_string (name);
+  if (_gtk_style_property_is_shorthand (prop))
+    {
+      GParameter *parameters;
+      guint i, n_parameters;
 
-  path->elements = g_slist_prepend (path->elements, elem);
-}
+      parameters = _gtk_style_property_unpack (prop, &value->value, &n_parameters);
 
-static void
-selector_path_prepend_class (SelectorPath *path,
-                             const gchar  *name)
-{
-  SelectorElement *elem;
+      for (i = 0; i < n_parameters; i++)
+        {
+          const GtkStyleProperty *child;
+          PropertyValue *val;
 
-  elem = g_slice_new (SelectorElement);
-  elem->combinator = COMBINATOR_DESCENDANT;
-  elem->elem_type = SELECTOR_CLASS;
+          child = _gtk_style_property_lookup (parameters[i].name);
+          val = property_value_new (value->section);
+          memcpy (&val->value, &parameters[i].value, sizeof (GValue));
+          gtk_css_ruleset_add (ruleset, child, val);
+        }
+      g_free (parameters);
+      property_value_free (value);
+      return;
+    }
 
-  elem->name = g_quark_from_string (name);
+  ruleset->has_inherit |= _gtk_style_property_is_inherit (prop);
+  g_hash_table_insert (ruleset->style, (gpointer) prop, value);
+}
 
-  path->elements = g_slist_prepend (path->elements, elem);
+static gboolean
+gtk_css_ruleset_matches (GtkCssRuleset *ruleset,
+                         GtkWidgetPath *path,
+                         guint          length)
+{
+  return _gtk_css_selector_matches (ruleset->selector, path, length);
 }
 
 static void
-selector_path_prepend_combinator (SelectorPath   *path,
-                                  CombinatorType  combinator)
+gtk_css_scanner_destroy (GtkCssScanner *scanner)
 {
-  SelectorElement *elem;
-
-  g_assert (path->elements != NULL);
+  g_object_unref (scanner->provider);
+  if (scanner->file)
+    g_object_unref (scanner->file);
+  g_object_unref (scanner->base);
+  _gtk_css_parser_free (scanner->parser);
 
-  /* It is actually stored in the last element */
-  elem = path->elements->data;
-  elem->combinator = combinator;
+  g_slice_free (GtkCssScanner, scanner);
 }
 
-static gint
-selector_path_depth (SelectorPath *path)
+static void
+gtk_css_provider_emit_error (GtkCssProvider *provider,
+                             GtkCssScanner  *scanner,
+                             const GError   *error)
 {
-  return g_slist_length (path->elements);
+  g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
+                 scanner != NULL ? scanner->section : NULL, error);
 }
 
-static SelectorStyleInfo *
-selector_style_info_new (SelectorPath *path)
+static void
+gtk_css_scanner_parser_error (GtkCssParser *parser,
+                              const GError *error,
+                              gpointer      user_data)
 {
-  SelectorStyleInfo *info;
-
-  info = g_slice_new0 (SelectorStyleInfo);
-  info->path = selector_path_ref (path);
+  GtkCssScanner *scanner = user_data;
 
-  return info;
+  gtk_css_provider_emit_error (scanner->provider,
+                               scanner,
+                               error);
 }
 
-static void
-selector_style_info_free (SelectorStyleInfo *info)
+static GtkCssScanner *
+gtk_css_scanner_new (GtkCssProvider *provider,
+                     GtkCssScanner  *parent,
+                     GtkCssSection  *section,
+                     GFile          *file,
+                     const gchar    *text)
 {
-  if (info->style)
-    g_hash_table_unref (info->style);
+  GtkCssScanner *scanner;
 
-  if (info->path)
-    selector_path_unref (info->path);
-
-  g_slice_free (SelectorStyleInfo, info);
-}
+  scanner = g_slice_new0 (GtkCssScanner);
 
-static void
-selector_style_info_set_style (SelectorStyleInfo *info,
-                               GHashTable        *style)
-{
-  if (info->style)
-    g_hash_table_unref (info->style);
+  g_object_ref (provider);
+  scanner->provider = provider;
+  scanner->parent = parent;
+  if (section)
+    scanner->section = gtk_css_section_ref (section);
 
-  if (style)
-    info->style = g_hash_table_ref (style);
+  if (file)
+    {
+      scanner->file = g_object_ref (file);
+      scanner->base = g_file_get_parent (file);
+    }
   else
-    info->style = NULL;
-}
+    {
+      char *dir = g_get_current_dir ();
+      scanner->base = g_file_new_for_path (dir);
+      g_free (dir);
+    }
 
-static void
-property_value_free (GValue *value)
-{
-  if (G_IS_VALUE (value))
-    g_value_unset (value);
+  scanner->parser = _gtk_css_parser_new (text,
+                                         gtk_css_scanner_parser_error,
+                                         scanner);
 
-  g_slice_free (GValue, value);
+  return scanner;
 }
 
-static void
-gtk_css_scanner_reset (GScanner *scanner)
+static GFile *
+gtk_css_scanner_get_base_url (GtkCssScanner *scanner)
 {
-  GtkCssScannerPrivate *priv = scanner->user_data;
-
-  g_slist_free (priv->state);
-  priv->state = NULL;
-
-  g_slist_foreach (priv->cur_selectors, (GFunc) selector_path_unref, NULL);
-  g_slist_free (priv->cur_selectors);
-  priv->cur_selectors = NULL;
+  return scanner->base;
+}
 
-  if (priv->cur_properties)
-    g_hash_table_unref (priv->cur_properties);
+static gboolean
+gtk_css_scanner_would_recurse (GtkCssScanner *scanner,
+                               GFile         *file)
+{
+  while (scanner)
+    {
+      if (scanner->file && g_file_equal (scanner->file, file))
+        return TRUE;
 
-  priv->cur_properties = g_hash_table_new_full (g_str_hash,
-                                                g_str_equal,
-                                                (GDestroyNotify) g_free,
-                                                (GDestroyNotify) property_value_free);
+      scanner = scanner->parent;
+    }
 
-  scanner_apply_scope (scanner, SCOPE_SELECTOR);
+  return FALSE;
 }
 
 static void
-gtk_css_scanner_destroy (GScanner *scanner)
+gtk_css_scanner_push_section (GtkCssScanner     *scanner,
+                              GtkCssSectionType  section_type)
 {
-  GtkCssScannerPrivate *priv = scanner->user_data;
+  GtkCssSection *section;
 
-  gtk_css_scanner_reset (scanner);
+  section = _gtk_css_section_new (scanner->section,
+                                  section_type,
+                                  scanner->parser,
+                                  scanner->file);
 
-  g_hash_table_destroy (priv->cur_properties);
-  g_slice_free (GtkCssScannerPrivate, priv);
-  
-  g_scanner_destroy (scanner);
+  if (scanner->section)
+    gtk_css_section_unref (scanner->section);
+  scanner->section = section;
 }
 
-static GScanner *
-gtk_css_provider_create_scanner (GtkCssProvider *provider)
+static void
+gtk_css_scanner_pop_section (GtkCssScanner *scanner,
+                             GtkCssSectionType check_type)
 {
-  GtkCssScannerPrivate *priv;
-  GScanner *scanner;
-
-  scanner = g_scanner_new (NULL);
-
-  priv = scanner->user_data = g_slice_new0 (GtkCssScannerPrivate);
-
-  priv->cur_properties = g_hash_table_new_full (g_str_hash,
-                                                g_str_equal,
-                                                (GDestroyNotify) g_free,
-                                                (GDestroyNotify) property_value_free);
-
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "active", GUINT_TO_POINTER (GTK_STATE_ACTIVE));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "prelight", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "hover", GUINT_TO_POINTER (GTK_STATE_PRELIGHT));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "selected", GUINT_TO_POINTER (GTK_STATE_SELECTED));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "insensitive", GUINT_TO_POINTER (GTK_STATE_INSENSITIVE));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "inconsistent", GUINT_TO_POINTER (GTK_STATE_INCONSISTENT));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focused", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "focus", GUINT_TO_POINTER (GTK_STATE_FOCUSED));
-
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "nth-child", GUINT_TO_POINTER (SYMBOL_NTH_CHILD));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "first-child", GUINT_TO_POINTER (SYMBOL_FIRST_CHILD));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "last-child", GUINT_TO_POINTER (SYMBOL_LAST_CHILD));
-  g_scanner_scope_add_symbol (scanner, SCOPE_PSEUDO_CLASS, "sorted", GUINT_TO_POINTER (SYMBOL_SORTED_CHILD));
+  GtkCssSection *parent;
+  
+  g_assert (gtk_css_section_get_section_type (scanner->section) == check_type);
 
-  g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "even", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_EVEN));
-  g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "odd", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_ODD));
-  g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "first", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_FIRST));
-  g_scanner_scope_add_symbol (scanner, SCOPE_NTH_CHILD, "last", GUINT_TO_POINTER (SYMBOL_NTH_CHILD_LAST));
+  parent = gtk_css_section_get_parent (scanner->section);
+  if (parent)
+    gtk_css_section_ref (parent);
 
-  scanner_apply_scope (scanner, SCOPE_SELECTOR);
+  _gtk_css_section_end (scanner->section);
+  gtk_css_section_unref (scanner->section);
 
-  return scanner;
+  scanner->section = parent;
 }
 
 static void
@@ -1191,324 +1345,91 @@ gtk_css_provider_init (GtkCssProvider *css_provider)
                                                            GTK_TYPE_CSS_PROVIDER,
                                                            GtkCssProviderPrivate);
 
-  priv->selectors_info = g_ptr_array_new_with_free_func ((GDestroyNotify) selector_style_info_free);
-  priv->scanner = gtk_css_provider_create_scanner (css_provider);
+  priv->rulesets = g_array_new (FALSE, FALSE, sizeof (GtkCssRuleset));
 
   priv->symbolic_colors = g_hash_table_new_full (g_str_hash, g_str_equal,
                                                  (GDestroyNotify) g_free,
                                                  (GDestroyNotify) gtk_symbolic_color_unref);
 }
 
-typedef struct ComparePathData ComparePathData;
-
-struct ComparePathData
+static void
+css_provider_dump_symbolic_colors (GtkCssProvider     *css_provider,
+                                   GtkStyleProperties *props)
 {
-  guint64 score;
-  SelectorPath *path;
-  GSList *iter;
-};
+  GtkCssProviderPrivate *priv;
+  GHashTableIter iter;
+  gpointer key, value;
 
-static gboolean
-compare_selector_element (GtkWidgetPath   *path,
-                          guint            index,
-                          SelectorElement *elem,
-                          guint8          *score)
-{
-  *score = 0;
+  priv = css_provider->priv;
+  g_hash_table_iter_init (&iter, priv->symbolic_colors);
 
-  if (elem->elem_type == SELECTOR_TYPE_NAME)
+  while (g_hash_table_iter_next (&iter, &key, &value))
     {
-      const gchar *type_name;
-      GType resolved_type;
-
-      /* Resolve the type name */
-      type_name = g_quark_to_string (elem->name);
-      resolved_type = g_type_from_name (type_name);
+      const gchar *name;
+      GtkSymbolicColor *color;
 
-      if (resolved_type == G_TYPE_INVALID)
-        {
-          /* Type couldn't be resolved, so the selector
-           * clearly doesn't affect the given widget path
-           */
-          return FALSE;
-        }
+      name = key;
+      color = value;
 
-      elem->elem_type = SELECTOR_GTYPE;
-      elem->type = resolved_type;
+      gtk_style_properties_map_color (props, name, color);
     }
+}
 
-  if (elem->elem_type == SELECTOR_GTYPE)
-    {
-      GType type;
+static GtkStyleProperties *
+gtk_css_provider_get_style (GtkStyleProvider *provider,
+                            GtkWidgetPath    *path)
+{
+  GtkCssProvider *css_provider;
+  GtkCssProviderPrivate *priv;
+  GtkStyleProperties *props;
+  guint i, l, length;
 
-      type = gtk_widget_path_iter_get_object_type (path, index);
+  css_provider = GTK_CSS_PROVIDER (provider);
+  priv = css_provider->priv;
+  length = gtk_widget_path_length (path);
+  props = gtk_style_properties_new ();
 
-      if (!g_type_is_a (type, elem->type))
-        return FALSE;
+  css_provider_dump_symbolic_colors (css_provider, props);
 
-      if (type == elem->type)
-        *score |= 0xF;
-      else
+  for (l = 1; l <= length; l++)
+    {
+      for (i = 0; i < priv->rulesets->len; i++)
         {
-          guint diff = g_type_depth (type) - g_type_depth (elem->type);
+          GtkCssRuleset *ruleset;
+          GHashTableIter iter;
+          gpointer key, val;
+
+          ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
 
-          if (G_UNLIKELY (diff > 0xE))
+          if (ruleset->style == NULL)
+            continue;
+
+          if (l < length && (!ruleset->has_inherit || _gtk_css_selector_get_state_flags (ruleset->selector)))
+            continue;
+
+          if (!gtk_css_ruleset_matches (ruleset, path, l))
+            continue;
+
+          g_hash_table_iter_init (&iter, ruleset->style);
+
+          while (g_hash_table_iter_next (&iter, &key, &val))
             {
-              g_warning ("Hierarchy is higher than expected.");
-              diff = 0xE;
+              GtkStyleProperty *prop = key;
+              PropertyValue *value = val;
+
+              if (l != length && !_gtk_style_property_is_inherit (prop))
+                continue;
+
+              _gtk_style_properties_set_property_by_property (props,
+                                                              prop,
+                                                              _gtk_css_selector_get_state_flags (ruleset->selector),
+                                                              &value->value);
             }
-          
-          *score = 0XF - diff;
         }
-
-      return TRUE;
     }
-  else if (elem->elem_type == SELECTOR_REGION)
-    {
-      GtkRegionFlags flags;
-
-      if (!gtk_widget_path_iter_has_qregion (path, index,
-                                             elem->region.name,
-                                             &flags))
-        return FALSE;
 
-      if (elem->region.flags != 0 &&
-          (flags & elem->region.flags) == 0)
-        return FALSE;
-
-      *score = 0xF;
-      return TRUE;
-    }
-  else if (elem->elem_type == SELECTOR_GLOB)
-    {
-      /* Treat as lowest matching type */
-      *score = 1;
-      return TRUE;
-    }
-  else if (elem->elem_type == SELECTOR_NAME)
-    {
-      if (!gtk_widget_path_iter_has_qname (path, index, elem->name))
-        return FALSE;
-
-      *score = 0xF;
-      return TRUE;
-    }
-  else if (elem->elem_type == SELECTOR_CLASS)
-    {
-      if (!gtk_widget_path_iter_has_qclass (path, index, elem->name))
-        return FALSE;
-
-      *score = 0xF;
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static guint64
-compare_selector (GtkWidgetPath *path,
-                  SelectorPath  *selector)
-{
-  GSList *elements = selector->elements;
-  gboolean match = TRUE, first = TRUE, first_match = FALSE;
-  guint64 score = 0;
-  gint i;
-
-  i = gtk_widget_path_length (path) - 1;
-
-  while (elements && match && i >= 0)
-    {
-      SelectorElement *elem;
-      guint8 elem_score;
-
-      elem = elements->data;
-
-      match = compare_selector_element (path, i, elem, &elem_score);
-
-      if (match && first)
-        first_match = TRUE;
-
-      /* Only move on to the next index if there is no match
-       * with the current element (whether to continue or not
-       * handled right after in the combinator check), or a
-       * GType or glob has just been matched.
-       *
-       * Region and widget names do not trigger this because
-       * the next element in the selector path could also be
-       * related to the same index.
-       */
-      if (!match ||
-          (elem->elem_type == SELECTOR_GTYPE ||
-           elem->elem_type == SELECTOR_GLOB))
-        i--;
-
-      if (!match &&
-          elem->elem_type != SELECTOR_TYPE_NAME &&
-          elem->combinator == COMBINATOR_DESCENDANT)
-        {
-          /* With descendant combinators there may
-           * be intermediate chidren in the hierarchy
-           */
-          match = TRUE;
-        }
-      else if (match)
-        elements = elements->next;
-
-      if (match)
-        {
-          /* Only 4 bits are actually used */
-          score <<= 4;
-          score |= elem_score;
-        }
-
-      first = FALSE;
-    }
-
-  /* If there are pending selector
-   * elements to compare, it's not
-   * a match.
-   */
-  if (elements)
-    match = FALSE;
-
-  if (!match)
-    score = 0;
-  else if (first_match)
-    {
-      /* Assign more weight to these selectors
-       * that matched right from the first element.
-       */
-      score <<= 4;
-    }
-
-  return score;
-}
-
-typedef struct StylePriorityInfo StylePriorityInfo;
-
-struct StylePriorityInfo
-{
-  guint64 score;
-  GHashTable *style;
-  GtkStateFlags state;
-};
-
-static GArray *
-css_provider_get_selectors (GtkCssProvider *css_provider,
-                            GtkWidgetPath  *path)
-{
-  GtkCssProviderPrivate *priv;
-  GArray *priority_info;
-  guint i, j;
-
-  priv = css_provider->priv;
-  priority_info = g_array_new (FALSE, FALSE, sizeof (StylePriorityInfo));
-
-  for (i = 0; i < priv->selectors_info->len; i++)
-    {
-      SelectorStyleInfo *info;
-      StylePriorityInfo new;
-      gboolean added = FALSE;
-      guint64 score;
-
-      info = g_ptr_array_index (priv->selectors_info, i);
-      score = compare_selector (path, info->path);
-
-      if (score <= 0)
-        continue;
-
-      new.score = score;
-      new.style = info->style;
-      new.state = info->path->state;
-
-      for (j = 0; j < priority_info->len; j++)
-        {
-          StylePriorityInfo *cur;
-
-          cur = &g_array_index (priority_info, StylePriorityInfo, j);
-
-          if (cur->score > new.score)
-            {
-              g_array_insert_val (priority_info, j, new);
-              added = TRUE;
-              break;
-            }
-        }
-
-      if (!added)
-        g_array_append_val (priority_info, new);
-    }
-
-  return priority_info;
-}
-
-static void
-css_provider_dump_symbolic_colors (GtkCssProvider     *css_provider,
-                                   GtkStyleProperties *props)
-{
-  GtkCssProviderPrivate *priv;
-  GHashTableIter iter;
-  gpointer key, value;
-
-  priv = css_provider->priv;
-  g_hash_table_iter_init (&iter, priv->symbolic_colors);
-
-  while (g_hash_table_iter_next (&iter, &key, &value))
-    {
-      const gchar *name;
-      GtkSymbolicColor *color;
-
-      name = key;
-      color = value;
-
-      gtk_style_properties_map_color (props, name, color);
-    }
-}
-
-static GtkStyleProperties *
-gtk_css_provider_get_style (GtkStyleProvider *provider,
-                            GtkWidgetPath    *path)
-{
-  GtkCssProvider *css_provider;
-  GtkStyleProperties *props;
-  GArray *priority_info;
-  guint i;
-
-  css_provider = GTK_CSS_PROVIDER (provider);
-  props = gtk_style_properties_new ();
-
-  css_provider_dump_symbolic_colors (css_provider, props);
-  priority_info = css_provider_get_selectors (css_provider, path);
-
-  for (i = 0; i < priority_info->len; i++)
-    {
-      StylePriorityInfo *info;
-      GHashTableIter iter;
-      gpointer key, value;
-
-      info = &g_array_index (priority_info, StylePriorityInfo, i);
-      g_hash_table_iter_init (&iter, info->style);
-
-      while (g_hash_table_iter_next (&iter, &key, &value))
-        {
-          gchar *prop = key;
-
-          /* Properties starting with '-' may be both widget style properties
-           * or custom properties from the theming engine, so check whether
-           * the type is registered or not.
-           */
-          if (prop[0] == '-' &&
-              !gtk_style_properties_lookup_property (prop, NULL, NULL))
-            continue;
-
-          gtk_style_properties_set_property (props, key, info->state, value);
-        }
-    }
-
-  g_array_free (priority_info, TRUE);
-
-  return props;
-}
+  return props;
+}
 
 static gboolean
 gtk_css_provider_get_style_property (GtkStyleProvider *provider,
@@ -1517,7 +1438,9 @@ gtk_css_provider_get_style_property (GtkStyleProvider *provider,
                                      GParamSpec       *pspec,
                                      GValue           *value)
 {
-  GArray *priority_info;
+  GtkCssProvider *css_provider = GTK_CSS_PROVIDER (provider);
+  GtkCssProviderPrivate *priv = css_provider->priv;
+  PropertyValue *val;
   gboolean found = FALSE;
   gchar *prop_name;
   gint i;
@@ -1526,37 +1449,48 @@ gtk_css_provider_get_style_property (GtkStyleProvider *provider,
                                g_type_name (pspec->owner_type),
                                pspec->name);
 
-  priority_info = css_provider_get_selectors (GTK_CSS_PROVIDER (provider), path);
-
-  for (i = priority_info->len - 1; i >= 0; i--)
+  for (i = priv->rulesets->len - 1; i >= 0; i--)
     {
-      StylePriorityInfo *info;
-      GValue *val;
+      GtkCssRuleset *ruleset;
+      GtkStateFlags selector_state;
+
+      ruleset = &g_array_index (priv->rulesets, GtkCssRuleset, i);
+
+      if (ruleset->widget_style == NULL)
+        continue;
+
+      if (!gtk_css_ruleset_matches (ruleset, path, gtk_widget_path_length (path)))
+        continue;
 
-      info = &g_array_index (priority_info, StylePriorityInfo, i);
-      val = g_hash_table_lookup (info->style, prop_name);
+      selector_state = _gtk_css_selector_get_state_flags (ruleset->selector);
+      val = g_hash_table_lookup (ruleset->widget_style, prop_name);
 
       if (val &&
-          (info->state == 0 ||
-           info->state == state ||
-           ((info->state & state) != 0 &&
-            (info->state & ~(state)) == 0)))
+          (selector_state == 0 ||
+           selector_state == state ||
+           ((selector_state & state) != 0 &&
+            (selector_state & ~(state)) == 0)))
         {
-          GError *error = NULL;
+          GtkCssScanner *scanner;
+
+          scanner = gtk_css_scanner_new (css_provider,
+                                         NULL,
+                                         val->section,
+                                         gtk_css_section_get_file (val->section),
+                                         g_value_get_string (&val->value));
+
+          found = _gtk_style_property_parse_value (NULL,
+                                                   value,
+                                                   scanner->parser,
+                                                   NULL);
 
-          found = _gtk_css_value_from_string (value,
-                                              NULL,
-                                              g_value_get_string (val),
-                                              &error);
+          gtk_css_scanner_destroy (scanner);
 
           if (found)
             break;
-          
-          gtk_css_provider_take_error (GTK_CSS_PROVIDER (provider), NULL, error);
         }
     }
 
-  g_array_free (priority_info, TRUE);
   g_free (prop_name);
 
   return found;
@@ -1574,13 +1508,15 @@ gtk_css_provider_finalize (GObject *object)
 {
   GtkCssProvider *css_provider;
   GtkCssProviderPrivate *priv;
+  guint i;
 
   css_provider = GTK_CSS_PROVIDER (object);
   priv = css_provider->priv;
 
-  gtk_css_scanner_destroy (priv->scanner);
+  for (i = 0; i < priv->rulesets->len; i++)
+    gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
 
-  g_ptr_array_free (priv->selectors_info, TRUE);
+  g_array_free (priv->rulesets, TRUE);
 
   if (priv->symbolic_colors)
     g_hash_table_destroy (priv->symbolic_colors);
@@ -1603,34 +1539,19 @@ gtk_css_provider_new (void)
 
 static void
 gtk_css_provider_take_error (GtkCssProvider *provider,
-                             GScanner       *scanner,
+                             GtkCssScanner  *scanner,
                              GError         *error)
 {
-  const char *filename;
-  guint line, position;
-
-  if (scanner)
-    {
-      filename = scanner->input_name;
-      line = scanner->line;
-      position = scanner->position;
-    }
-  else
-    {
-      filename = NULL;
-      line = 0;
-      position = 0;
-    }
-
-  g_signal_emit (provider, css_provider_signals[PARSING_ERROR], 0,
-                 filename, line, position, error);
+  gtk_css_provider_emit_error (provider,
+                               scanner,
+                               error);
 
   g_error_free (error);
 }
 
 static void
 gtk_css_provider_error_literal (GtkCssProvider *provider,
-                                GScanner       *scanner,
+                                GtkCssScanner  *scanner,
                                 GQuark          domain,
                                 gint            code,
                                 const char     *message)
@@ -1642,14 +1563,14 @@ gtk_css_provider_error_literal (GtkCssProvider *provider,
 
 static void
 gtk_css_provider_error (GtkCssProvider *provider,
-                        GScanner       *scanner,
+                        GtkCssScanner  *scanner,
                         GQuark          domain,
                         gint            code,
                         const char     *format,
                         ...)  G_GNUC_PRINTF (5, 6);
 static void
 gtk_css_provider_error (GtkCssProvider *provider,
-                        GScanner       *scanner,
+                        GtkCssScanner  *scanner,
                         GQuark          domain,
                         gint            code,
                         const char     *format,
@@ -1667,7 +1588,7 @@ gtk_css_provider_error (GtkCssProvider *provider,
 
 static void
 gtk_css_provider_invalid_token (GtkCssProvider *provider,
-                                GScanner       *scanner,
+                                GtkCssScanner  *scanner,
                                 const char     *expected)
 {
   gtk_css_provider_error (provider,
@@ -1677,922 +1598,910 @@ gtk_css_provider_invalid_token (GtkCssProvider *provider,
                           "expected a valid %s", expected);
 }
 
-static void
-scanner_apply_scope (GScanner    *scanner,
-                     ParserScope  scope)
+static void 
+css_provider_commit (GtkCssProvider *css_provider,
+                     GSList         *selectors,
+                     GtkCssRuleset  *ruleset)
 {
-  g_scanner_set_scope (scanner, scope);
+  GtkCssProviderPrivate *priv;
+  GSList *l;
 
-  if (scope == SCOPE_VALUE)
-    {
-      scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
-      scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(),.%\t\n'/\"";
-      scanner->config->scan_identifier_1char = TRUE;
-    }
-  else if (scope == SCOPE_BINDING_SET)
-    {
-      scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_";
-      scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "@#-_ +(){}<>,.%\t\n'/\"";
-      scanner->config->scan_identifier_1char = TRUE;
-    }
-  else if (scope == SCOPE_SELECTOR)
+  priv = css_provider->priv;
+
+  if (ruleset->style == NULL && ruleset->widget_style == NULL)
     {
-      scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "*@";
-      scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_#.";
-      scanner->config->scan_identifier_1char = TRUE;
+      g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+      return;
     }
-  else if (scope == SCOPE_PSEUDO_CLASS ||
-           scope == SCOPE_NTH_CHILD ||
-           scope == SCOPE_DECLARATION)
+
+  for (l = selectors; l; l = l->next)
     {
-      scanner->config->cset_identifier_first = G_CSET_a_2_z G_CSET_A_2_Z "-_";
-      scanner->config->cset_identifier_nth = G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "-_";
-      scanner->config->scan_identifier_1char = FALSE;
+      GtkCssRuleset new;
+
+      gtk_css_ruleset_init_copy (&new, ruleset, l->data);
+
+      g_array_append_val (priv->rulesets, new);
     }
-  else
-    g_assert_not_reached ();
 
-  scanner->config->scan_float = FALSE;
-  scanner->config->cpair_comment_single = NULL;
+  g_slist_free (selectors);
 }
 
 static void
-gtk_css_scanner_push_scope (GScanner    *scanner,
-                            ParserScope  scope)
+gtk_css_provider_reset (GtkCssProvider *css_provider)
 {
-  GtkCssScannerPrivate *priv;
+  GtkCssProviderPrivate *priv;
+  guint i;
 
-  priv = scanner->user_data;
-  priv->state = g_slist_prepend (priv->state, GUINT_TO_POINTER (scope));
+  priv = css_provider->priv;
+
+  g_hash_table_remove_all (priv->symbolic_colors);
 
-  scanner_apply_scope (scanner, scope);
+  for (i = 0; i < priv->rulesets->len; i++)
+    gtk_css_ruleset_clear (&g_array_index (priv->rulesets, GtkCssRuleset, i));
+  g_array_set_size (priv->rulesets, 0);
 }
 
 static void
-gtk_css_scanner_pop_scope (GScanner *scanner)
+gtk_css_provider_propagate_error (GtkCssProvider  *provider,
+                                  GtkCssSection   *section,
+                                  const GError    *error,
+                                  GError         **propagate_to)
 {
-  GtkCssScannerPrivate *priv;
-  ParserScope scope = SCOPE_SELECTOR;
 
-  priv = scanner->user_data;
+  GFileInfo *info;
+  GFile *file;
+  const char *path;
+
+  file = gtk_css_section_get_file (section);
+  if (file)
+    {
+      info = g_file_query_info (file,G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, 0, NULL, NULL);
+
+      if (info)
+        path = g_file_info_get_display_name (info);
+      else
+        path = "<broken file>";
+    }
+  else
+    {
+      info = NULL;
+      path = "<unknown>";
+    }
 
-  if (!priv->state)
+  /* don't fail for deprecations */
+  if (g_error_matches (error, GTK_CSS_PROVIDER_ERROR, GTK_CSS_PROVIDER_ERROR_DEPRECATED))
     {
-      g_warning ("Push/pop calls to parser scope aren't paired");
-      scanner_apply_scope (scanner, SCOPE_SELECTOR);
+      g_warning ("Theme parsing error: %s:%u:%u: %s", path,
+                 gtk_css_section_get_end_line (section) + 1,
+                 gtk_css_section_get_end_position (section), error->message);
       return;
     }
 
-  priv->state = g_slist_delete_link (priv->state, priv->state);
+  /* we already set an error. And we'd like to keep the first one */
+  if (*propagate_to)
+    return;
 
-  /* Fetch new scope */
-  if (priv->state)
-    scope = GPOINTER_TO_INT (priv->state->data);
+  *propagate_to = g_error_copy (error);
+  g_prefix_error (propagate_to, "%s:%u:%u: ", path,
+                  gtk_css_section_get_end_line (section) + 1,
+                  gtk_css_section_get_end_position (section));
 
-  scanner_apply_scope (scanner, scope);
+  if (info)
+    g_object_unref (info);
 }
 
-static void
-css_provider_commit (GtkCssProvider *css_provider,
-                     GScanner       *scanner)
+static gboolean
+parse_import (GtkCssScanner *scanner)
 {
-  GtkCssScannerPrivate *scanner_priv;
-  GtkCssProviderPrivate *priv;
-  GSList *l;
-
-  priv = css_provider->priv;
-  scanner_priv = scanner->user_data;
-
-  l = scanner_priv->cur_selectors;
+  GFile *file;
+  char *uri;
 
-  if (g_hash_table_size (scanner_priv->cur_properties) == 0)
-    return;
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_IMPORT);
 
-  while (l)
+  if (!_gtk_css_parser_try (scanner->parser, "@import", TRUE))
     {
-      SelectorPath *path = l->data;
-      SelectorStyleInfo *info;
-
-      info = selector_style_info_new (path);
-      selector_style_info_set_style (info, scanner_priv->cur_properties);
-
-      g_ptr_array_add (priv->selectors_info, info);
-      l = l->next;
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
+      return FALSE;
     }
-}
-
-static GTokenType
-parse_nth_child (GtkCssProvider *css_provider,
-                 GScanner       *scanner,
-                 GtkRegionFlags *flags)
-{
-  ParserSymbol symbol;
-
-  g_scanner_get_next_token (scanner);
-
-  if (scanner->token != G_TOKEN_SYMBOL)
-    return G_TOKEN_SYMBOL;
 
-  symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
+  if (_gtk_css_parser_is_string (scanner->parser))
+    uri = _gtk_css_parser_read_string (scanner->parser);
+  else
+    uri = _gtk_css_parser_read_uri (scanner->parser);
 
-  if (symbol == SYMBOL_NTH_CHILD)
+  if (uri == NULL)
     {
-      g_scanner_get_next_token (scanner);
-
-      if (scanner->token != G_TOKEN_LEFT_PAREN)
-        return G_TOKEN_LEFT_PAREN;
-
-      gtk_css_scanner_push_scope (scanner, SCOPE_NTH_CHILD);
-      g_scanner_get_next_token (scanner);
-
-      if (scanner->token != G_TOKEN_SYMBOL)
-        return G_TOKEN_SYMBOL;
-
-      symbol = GPOINTER_TO_INT (scanner->value.v_symbol);
-
-      switch (symbol)
-        {
-        case SYMBOL_NTH_CHILD_EVEN:
-          *flags = GTK_REGION_EVEN;
-          break;
-        case SYMBOL_NTH_CHILD_ODD:
-          *flags = GTK_REGION_ODD;
-          break;
-        case SYMBOL_NTH_CHILD_FIRST:
-          *flags = GTK_REGION_FIRST;
-          break;
-        case SYMBOL_NTH_CHILD_LAST:
-          *flags = GTK_REGION_LAST;
-          break;
-        default:
-          break;
-        }
-
-      g_scanner_get_next_token (scanner);
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
+      return TRUE;
+    }
 
-      if (scanner->token != G_TOKEN_RIGHT_PAREN)
-        return G_TOKEN_RIGHT_PAREN;
+  file = g_file_resolve_relative_path (gtk_css_scanner_get_base_url (scanner), uri);
+  g_free (uri);
 
-      gtk_css_scanner_pop_scope (scanner);
+  if (gtk_css_scanner_would_recurse (scanner, file))
+    {
+       char *path = g_file_get_path (file);
+       gtk_css_provider_error (scanner->provider,
+                               scanner,
+                               GTK_CSS_PROVIDER_ERROR,
+                               GTK_CSS_PROVIDER_ERROR_IMPORT,
+                               "Loading '%s' would recurse",
+                               path);
+       g_free (path);
     }
-  else if (symbol == SYMBOL_FIRST_CHILD)
-    *flags = GTK_REGION_FIRST;
-  else if (symbol == SYMBOL_LAST_CHILD)
-    *flags = GTK_REGION_LAST;
-  else if (symbol == SYMBOL_SORTED_CHILD)
-    *flags = GTK_REGION_SORTED;
   else
     {
-      *flags = 0;
-      return G_TOKEN_SYMBOL;
+      gtk_css_provider_load_internal (scanner->provider,
+                                      scanner,
+                                      file,
+                                      NULL,
+                                      NULL);
     }
 
-  return G_TOKEN_NONE;
-}
-
-static GTokenType
-parse_pseudo_class (GtkCssProvider *css_provider,
-                    GScanner       *scanner,
-                    SelectorPath   *selector)
-{
-  GtkStateType state;
-
-  g_scanner_get_next_token (scanner);
-
-  if (scanner->token != G_TOKEN_SYMBOL)
-    return G_TOKEN_SYMBOL;
-
-  state = GPOINTER_TO_INT (scanner->value.v_symbol);
-
-  switch (state)
+  if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
     {
-    case GTK_STATE_ACTIVE:
-      selector->state |= GTK_STATE_FLAG_ACTIVE;
-      break;
-    case GTK_STATE_PRELIGHT:
-      selector->state |= GTK_STATE_FLAG_PRELIGHT;
-      break;
-    case GTK_STATE_SELECTED:
-      selector->state |= GTK_STATE_FLAG_SELECTED;
-      break;
-    case GTK_STATE_INSENSITIVE:
-      selector->state |= GTK_STATE_FLAG_INSENSITIVE;
-      break;
-    case GTK_STATE_INCONSISTENT:
-      selector->state |= GTK_STATE_FLAG_INCONSISTENT;
-      break;
-    case GTK_STATE_FOCUSED:
-      selector->state |= GTK_STATE_FLAG_FOCUSED;
-      break;
-    default:
-      return G_TOKEN_SYMBOL;
+      gtk_css_provider_invalid_token (scanner->provider, scanner, "semicolon");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
     }
 
-  return G_TOKEN_NONE;
+  g_object_unref (file);
+
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_IMPORT);
+  return TRUE;
 }
 
-/* Parses a number of concatenated classes */
-static void
-parse_classes (SelectorPath   *path,
-               const gchar    *str)
+static gboolean
+parse_color_definition (GtkCssScanner *scanner)
 {
-  gchar *pos;
+  GtkSymbolicColor *symbolic;
+  char *name;
 
-  if ((pos = strchr (str, '.')) != NULL)
-    {
-      /* Leave the last class to the call after the loop */
-      while (pos)
-        {
-          *pos = '\0';
-          selector_path_prepend_class (path, str);
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
 
-          str = pos + 1;
-          pos = strchr (str, '.');
-        }
+  if (!_gtk_css_parser_try (scanner->parser, "@define-color", TRUE))
+    {
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
+      return FALSE;
     }
 
-  selector_path_prepend_class (path, str);
-}
+  name = _gtk_css_parser_try_name (scanner->parser, TRUE);
+  if (name == NULL)
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Not a valid color name");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
+      return TRUE;
+    }
 
-static gboolean
-is_widget_class_name (const gchar *str)
-{
-  /* Do a pretty lax check here, not all
-   * widget class names contain only CamelCase
-   * (gtkmm widgets don't), but at least part of
-   * the name will be CamelCase, so check for
-   * the first uppercase char
-   */
-  while (*str)
+  symbolic = _gtk_css_parser_read_symbolic_color (scanner->parser);
+  if (symbolic == NULL)
     {
-      if (g_ascii_isupper (*str))
-        return TRUE;
+      g_free (name);
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
+      return TRUE;
+    }
 
-      str++;
+  if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
+    {
+      g_free (name);
+      gtk_symbolic_color_unref (symbolic);
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Missing semicolon at end of color definition");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
+      return TRUE;
     }
 
-  return FALSE;
+  g_hash_table_insert (scanner->provider->priv->symbolic_colors, name, symbolic);
+
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_COLOR_DEFINITION);
+  return TRUE;
 }
 
-static GTokenType
-parse_selector (GtkCssProvider  *css_provider,
-                GScanner        *scanner,
-                SelectorPath   **selector_out)
+static gboolean
+parse_binding_set (GtkCssScanner *scanner)
 {
-  SelectorPath *path;
-
-  path = selector_path_new ();
-  *selector_out = path;
+  GtkBindingSet *binding_set;
+  char *name;
 
-  if (scanner->token != ':' &&
-      scanner->token != '#' &&
-      scanner->token != '.' &&
-      scanner->token != G_TOKEN_IDENTIFIER)
-    return G_TOKEN_IDENTIFIER;
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_BINDING_SET);
 
-  while (scanner->token == '#' ||
-         scanner->token == '.' ||
-         scanner->token == G_TOKEN_IDENTIFIER)
+  if (!_gtk_css_parser_try (scanner->parser, "@binding-set", TRUE))
     {
-      if (scanner->token == '#' ||
-          scanner->token == '.')
-        {
-          gboolean is_class;
-          gchar *pos;
-
-          is_class = (scanner->token == '.');
-
-          g_scanner_get_next_token (scanner);
-
-          if (scanner->token != G_TOKEN_IDENTIFIER)
-            return G_TOKEN_IDENTIFIER;
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
+      return FALSE;
+    }
 
-          selector_path_prepend_glob (path);
-          selector_path_prepend_combinator (path, COMBINATOR_CHILD);
+  name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
+  if (name == NULL)
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Expected name for binding set");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+      goto skip_semicolon;
+    }
 
-          if (is_class)
-            parse_classes (path, scanner->value.v_identifier);
-          else
-            {
-              if ((pos = strchr (scanner->value.v_identifier, '.')) != NULL)
-                *pos = '\0';
+  binding_set = gtk_binding_set_find (name);
+  if (!binding_set)
+    {
+      binding_set = gtk_binding_set_new (name);
+      binding_set->parsed = TRUE;
+    }
+  g_free (name);
 
-              selector_path_prepend_name (path, scanner->value.v_identifier);
+  if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Expected '{' for binding set");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+      goto skip_semicolon;
+    }
 
-              /* Parse any remaining classes */
-              if (pos)
-                parse_classes (path, pos + 1);
-            }
-        }
-      else if (is_widget_class_name (scanner->value.v_identifier))
+  while (!_gtk_css_parser_is_eof (scanner->parser) &&
+         !_gtk_css_parser_begins_with (scanner->parser, '}'))
+    {
+      name = _gtk_css_parser_read_value (scanner->parser);
+      if (name == NULL)
         {
-          gchar *pos;
-
-          if ((pos = strchr (scanner->value.v_identifier, '#')) != NULL ||
-              (pos = strchr (scanner->value.v_identifier, '.')) != NULL)
-            {
-              gchar *type_name, *name;
-              gboolean is_class;
-
-              is_class = (*pos == '.');
-
-              /* Widget type and name/class put together */
-              name = pos + 1;
-              *pos = '\0';
-              type_name = scanner->value.v_identifier;
-
-              selector_path_prepend_type (path, type_name);
-
-              /* This is only so there is a direct relationship
-               * between widget type and its name.
-               */
-              selector_path_prepend_combinator (path, COMBINATOR_CHILD);
-
-              if (is_class)
-                parse_classes (path, name);
-              else
-                {
-                  if ((pos = strchr (name, '.')) != NULL)
-                    *pos = '\0';
-
-                  selector_path_prepend_name (path, name);
-
-                  /* Parse any remaining classes */
-                  if (pos)
-                    parse_classes (path, pos + 1);
-                }
-            }
-          else
-            selector_path_prepend_type (path, scanner->value.v_identifier);
+          _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+          continue;
         }
-      else if (_gtk_style_context_check_region_name (scanner->value.v_identifier))
-        {
-          GtkRegionFlags flags = 0;
-          gchar *region_name;
-
-          region_name = g_strdup (scanner->value.v_identifier);
-
-          if (g_scanner_peek_next_token (scanner) == ':')
-            {
-              ParserSymbol symbol;
 
-              g_scanner_get_next_token (scanner);
-              gtk_css_scanner_push_scope (scanner, SCOPE_PSEUDO_CLASS);
-
-              /* Check for the next token being nth-child, parse in that
-               * case, and fallback into common state parsing if not.
-               */
-              if (g_scanner_peek_next_token (scanner) != G_TOKEN_SYMBOL)
-                return G_TOKEN_SYMBOL;
-
-              symbol = GPOINTER_TO_INT (scanner->next_value.v_symbol);
-
-              if (symbol == SYMBOL_FIRST_CHILD ||
-                  symbol == SYMBOL_LAST_CHILD ||
-                  symbol == SYMBOL_NTH_CHILD ||
-                  symbol == SYMBOL_SORTED_CHILD)
-                {
-                  GTokenType token;
-
-                  if ((token = parse_nth_child (css_provider, scanner, &flags)) != G_TOKEN_NONE)
-                    return token;
-
-                  gtk_css_scanner_pop_scope (scanner);
-                }
-              else
-                {
-                  gtk_css_scanner_pop_scope (scanner);
-                  selector_path_prepend_region (path, region_name, 0);
-                  g_free (region_name);
-                  break;
-                }
-            }
-
-          selector_path_prepend_region (path, region_name, flags);
-          g_free (region_name);
+      if (gtk_binding_entry_add_signal_from_string (binding_set, name) != G_TOKEN_NONE)
+        {
+          gtk_css_provider_error_literal (scanner->provider,
+                                          scanner,
+                                          GTK_CSS_PROVIDER_ERROR,
+                                          GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                          "Failed to parse binding set.");
         }
-      else if (scanner->value.v_identifier[0] == '*')
-        selector_path_prepend_glob (path);
-      else
-        return G_TOKEN_IDENTIFIER;
 
-      g_scanner_get_next_token (scanner);
+      g_free (name);
 
-      if (scanner->token == '>')
+      if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
         {
-          selector_path_prepend_combinator (path, COMBINATOR_CHILD);
-          g_scanner_get_next_token (scanner);
+          if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
+              !_gtk_css_parser_is_eof (scanner->parser))
+            {
+              gtk_css_provider_error_literal (scanner->provider,
+                                              scanner,
+                                              GTK_CSS_PROVIDER_ERROR,
+                                              GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                              "Expected semicolon");
+              _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+            }
         }
     }
 
-  if (scanner->token == ':')
+  if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
     {
-      /* Add glob selector if path is empty */
-      if (selector_path_depth (path) == 0)
-        selector_path_prepend_glob (path);
-
-      gtk_css_scanner_push_scope (scanner, SCOPE_PSEUDO_CLASS);
-
-      while (scanner->token == ':')
-        {
-          GTokenType token;
-
-          if ((token = parse_pseudo_class (css_provider, scanner, path)) != G_TOKEN_NONE)
-            return token;
-
-          g_scanner_get_next_token (scanner);
-        }
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "expected '}' after declarations");
+      if (!_gtk_css_parser_is_eof (scanner->parser))
+        _gtk_css_parser_resync (scanner->parser, FALSE, 0);
+    }
 
-      gtk_css_scanner_pop_scope (scanner);
+skip_semicolon:
+  if (_gtk_css_parser_begins_with (scanner->parser, ';'))
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_DEPRECATED,
+                                      "Nonstandard semicolon at end of binding set");
+      _gtk_css_parser_try (scanner->parser, ";", TRUE);
     }
 
-  return G_TOKEN_NONE;
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_BINDING_SET);
+
+  return TRUE;
 }
 
 static void
-resolve_binding_sets (const gchar *value_str,
-                      GValue      *value)
+parse_at_keyword (GtkCssScanner *scanner)
 {
-  GPtrArray *array;
-  gchar **bindings, **str;
-
-  bindings = g_strsplit (value_str, ",", -1);
-  array = g_ptr_array_new ();
+  if (parse_import (scanner))
+    return;
+  if (parse_color_definition (scanner))
+    return;
+  if (parse_binding_set (scanner))
+    return;
 
-  for (str = bindings; *str; str++)
+  else
     {
-      GtkBindingSet *binding_set;
-
-      binding_set = gtk_binding_set_find (g_strstrip (*str));
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "unknown @ rule");
+      _gtk_css_parser_resync (scanner->parser, TRUE, 0);
+    }
+}
 
-      if (!binding_set)
-        continue;
+static gboolean
+parse_selector_class (GtkCssScanner *scanner, GArray *classes)
+{
+  GQuark qname;
+  char *name;
+    
+  name = _gtk_css_parser_try_name (scanner->parser, FALSE);
 
-      g_ptr_array_add (array, binding_set);
+  if (name == NULL)
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Expected a valid name for class");
+      return FALSE;
     }
 
-  g_value_take_boxed (value, array);
-  g_strfreev (bindings);
+  qname = g_quark_from_string (name);
+  g_array_append_val (classes, qname);
+  g_free (name);
+  return TRUE;
 }
 
-static GTokenType
-parse_rule (GtkCssProvider  *css_provider,
-            GScanner        *scanner)
+static gboolean
+parse_selector_name (GtkCssScanner *scanner, GArray *names)
 {
-  GtkCssScannerPrivate *priv;
-  GTokenType expected_token;
-  SelectorPath *selector;
+  GQuark qname;
+  char *name;
+    
+  name = _gtk_css_parser_try_name (scanner->parser, FALSE);
 
-  priv = scanner->user_data;
+  if (name == NULL)
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Expected a valid name for id");
+      return FALSE;
+    }
 
-  gtk_css_scanner_push_scope (scanner, SCOPE_SELECTOR);
+  qname = g_quark_from_string (name);
+  g_array_append_val (names, qname);
+  g_free (name);
+  return TRUE;
+}
 
-  /* Handle directives */
-  if (scanner->token == G_TOKEN_IDENTIFIER &&
-      scanner->value.v_identifier[0] == '@')
+static gboolean
+parse_selector_pseudo_class (GtkCssScanner  *scanner,
+                             GtkRegionFlags *region_to_modify,
+                             GtkStateFlags  *state_to_modify)
+{
+  struct {
+    const char *name;
+    GtkRegionFlags region_flag;
+    GtkStateFlags state_flag;
+  } pseudo_classes[] = {
+    { "first-child",  GTK_REGION_FIRST, 0 },
+    { "last-child",   GTK_REGION_LAST, 0 },
+    { "only-child",   GTK_REGION_ONLY, 0 },
+    { "sorted",       GTK_REGION_SORTED, 0 },
+    { "active",       0, GTK_STATE_FLAG_ACTIVE },
+    { "prelight",     0, GTK_STATE_FLAG_PRELIGHT },
+    { "hover",        0, GTK_STATE_FLAG_PRELIGHT },
+    { "selected",     0, GTK_STATE_FLAG_SELECTED },
+    { "insensitive",  0, GTK_STATE_FLAG_INSENSITIVE },
+    { "inconsistent", 0, GTK_STATE_FLAG_INCONSISTENT },
+    { "focused",      0, GTK_STATE_FLAG_FOCUSED },
+    { "focus",        0, GTK_STATE_FLAG_FOCUSED },
+    { "window-unfocused", 0, GTK_STATE_FLAG_WINDOW_UNFOCUSED },
+    { NULL, }
+  }, nth_child_classes[] = {
+    { "first",        GTK_REGION_FIRST, 0 },
+    { "last",         GTK_REGION_LAST, 0 },
+    { "even",         GTK_REGION_EVEN, 0 },
+    { "odd",          GTK_REGION_ODD, 0 },
+    { NULL, }
+  }, *classes;
+  guint i;
+  char *name;
+
+  name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
+  if (name == NULL)
     {
-      gchar *directive;
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "Missing name of pseudo-class");
+      return FALSE;
+    }
 
-      directive = &scanner->value.v_identifier[1];
+  if (_gtk_css_parser_try (scanner->parser, "(", TRUE))
+    {
+      char *function = name;
 
-      if (strcmp (directive, "define-color") == 0)
+      name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
+      if (!_gtk_css_parser_try (scanner->parser, ")", FALSE))
         {
-          GtkSymbolicColor *color;
-          gchar *color_name, *color_str;
-          GError *error = NULL;
-
-          /* Directive is a color mapping */
-          g_scanner_get_next_token (scanner);
-
-          if (scanner->token != G_TOKEN_IDENTIFIER)
-            {
-              gtk_css_provider_invalid_token (css_provider, scanner, "Color name");
-              return G_TOKEN_IDENTIFIER;
-            }
+          gtk_css_provider_error_literal (scanner->provider,
+                                          scanner,
+                                          GTK_CSS_PROVIDER_ERROR,
+                                          GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                          "Missing closing bracket for pseudo-class");
+          return FALSE;
+        }
 
-          color_name = g_strdup (scanner->value.v_identifier);
-          gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
-          g_scanner_get_next_token (scanner);
+      if (g_ascii_strcasecmp (function, "nth-child") != 0)
+        {
+          gtk_css_provider_error (scanner->provider,
+                                  scanner,
+                                  GTK_CSS_PROVIDER_ERROR,
+                                  GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+                                  "Unknown pseudo-class '%s(%s)'", function, name ? name : "");
+          g_free (function);
+          g_free (name);
+          return FALSE;
+        }
+      
+      g_free (function);
+    
+      if (name == NULL)
+        {
+          gtk_css_provider_error (scanner->provider,
+                                  scanner,
+                                  GTK_CSS_PROVIDER_ERROR,
+                                  GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+                                  "nth-child() requires an argument");
+          return FALSE;
+        }
 
-          if (scanner->token != G_TOKEN_IDENTIFIER)
-            {
-              gtk_css_provider_invalid_token (css_provider, scanner, "Color definition");
-              return G_TOKEN_IDENTIFIER;
-            }
+      classes = nth_child_classes;
+    }
+  else
+    classes = pseudo_classes;
 
-          color_str = g_strstrip (scanner->value.v_identifier);
-          color = _gtk_css_parse_symbolic_color (color_str, &error);
-          if (!color)
+  for (i = 0; classes[i].name != NULL; i++)
+    {
+      if (g_ascii_strcasecmp (name, classes[i].name) == 0)
+        {
+          if ((*region_to_modify & classes[i].region_flag) ||
+              (*state_to_modify & classes[i].state_flag))
             {
-              gtk_css_provider_take_error (css_provider, scanner, error);
-              return G_TOKEN_IDENTIFIER;
+              if (classes == nth_child_classes)
+                gtk_css_provider_error (scanner->provider,
+                                        scanner,
+                                        GTK_CSS_PROVIDER_ERROR,
+                                        GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                        "Duplicate pseudo-class 'nth-child(%s)'", name);
+              else
+                gtk_css_provider_error (scanner->provider,
+                                        scanner,
+                                        GTK_CSS_PROVIDER_ERROR,
+                                        GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                        "Duplicate pseudo-class '%s'", name);
             }
+          *region_to_modify |= classes[i].region_flag;
+          *state_to_modify |= classes[i].state_flag;
 
-          g_hash_table_insert (css_provider->priv->symbolic_colors, color_name, color);
+          g_free (name);
+          return TRUE;
+        }
+    }
 
-          gtk_css_scanner_pop_scope (scanner);
-          g_scanner_get_next_token (scanner);
+  if (classes == nth_child_classes)
+    gtk_css_provider_error (scanner->provider,
+                            scanner,
+                            GTK_CSS_PROVIDER_ERROR,
+                            GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+                            "Unknown pseudo-class 'nth-child(%s)'", name);
+  else
+    gtk_css_provider_error (scanner->provider,
+                            scanner,
+                            GTK_CSS_PROVIDER_ERROR,
+                            GTK_CSS_PROVIDER_ERROR_UNKNOWN_VALUE,
+                            "Unknown pseudo-class '%s'", name);
+  g_free (name);
+  return FALSE;
+}
 
-          if (scanner->token != ';')
-            return ';';
+static gboolean
+parse_simple_selector (GtkCssScanner *scanner,
+                       char **name,
+                       GArray *ids,
+                       GArray *classes,
+                       GtkRegionFlags *pseudo_classes,
+                       GtkStateFlags *state)
+{
+  gboolean parsed_something;
+  
+  *name = _gtk_css_parser_try_ident (scanner->parser, FALSE);
+  if (*name)
+    parsed_something = TRUE;
+  else
+    parsed_something = _gtk_css_parser_try (scanner->parser, "*", FALSE);
 
-          return G_TOKEN_NONE;
+  do {
+      if (_gtk_css_parser_try (scanner->parser, "#", FALSE))
+        {
+          if (!parse_selector_name (scanner, ids))
+            return FALSE;
         }
-      else if (strcmp (directive, "import") == 0)
+      else if (_gtk_css_parser_try (scanner->parser, ".", FALSE))
         {
-          GScanner *new_scanner;
-          gboolean loaded;
-          gchar *path = NULL;
-          GFile *base, *actual;
-          char *dirname;
-          GError *error = NULL;
-
-          gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
-          g_scanner_get_next_token (scanner);
-
-          if (scanner->token == G_TOKEN_IDENTIFIER &&
-              g_str_has_prefix (scanner->value.v_identifier, "url"))
-            path = g_strstrip (scanner->value.v_identifier);
-          else if (scanner->token == G_TOKEN_STRING)
-            path = g_strstrip (scanner->value.v_string);
-          else
-            {
-              gtk_css_provider_invalid_token (css_provider, scanner, "File URL");
-              return G_TOKEN_IDENTIFIER;
-            }
+          if (!parse_selector_class (scanner, classes))
+            return FALSE;
+        }
+      else if (_gtk_css_parser_try (scanner->parser, ":", FALSE))
+        {
+          if (!parse_selector_pseudo_class (scanner, pseudo_classes, state))
+            return FALSE;
+        }
+      else if (!parsed_something)
+        {
+          gtk_css_provider_error_literal (scanner->provider,
+                                          scanner,
+                                          GTK_CSS_PROVIDER_ERROR,
+                                          GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                          "Expected a valid selector");
+          return FALSE;
+        }
+      else
+        break;
 
-          if (scanner->input_name)
-            dirname = g_path_get_dirname (scanner->input_name);
-          else
-            dirname = g_get_current_dir ();
+      parsed_something = TRUE;
+    }
+  while (!_gtk_css_parser_is_eof (scanner->parser));
 
-          base = g_file_new_for_path (dirname);
-          g_free (dirname);
+  _gtk_css_parser_skip_whitespace (scanner->parser);
+  return TRUE;
+}
 
-          actual = _gtk_css_parse_url (base, path, NULL, &error);
-          g_object_unref (base);
+static GtkCssSelector *
+parse_selector (GtkCssScanner *scanner)
+{
+  GtkCssSelector *selector = NULL;
 
-          if (actual == NULL)
-            {
-              gtk_css_provider_take_error (css_provider, scanner, error);
-              return G_TOKEN_IDENTIFIER;
-            }
+  do {
+      char *name = NULL;
+      GArray *ids = g_array_new (TRUE, FALSE, sizeof (GQuark));
+      GArray *classes = g_array_new (TRUE, FALSE, sizeof (GQuark));
+      GtkRegionFlags pseudo_classes = 0;
+      GtkStateFlags state = 0;
+      GtkCssCombinator combine = GTK_CSS_COMBINE_DESCANDANT;
 
-          gtk_css_scanner_pop_scope (scanner);
-          g_scanner_get_next_token (scanner);
+      if (selector)
+        {
+          if (_gtk_css_parser_try (scanner->parser, ">", TRUE))
+            combine = GTK_CSS_COMBINE_CHILD;
+        }
 
-          if (scanner->token != ';')
-            {
-              g_object_unref (actual);
-              return ';';
-            }
+      if (!parse_simple_selector (scanner, &name, ids, classes, &pseudo_classes, &state))
+        {
+          g_array_free (ids, TRUE);
+          g_array_free (classes, TRUE);
+          if (selector)
+            _gtk_css_selector_free (selector);
+          return NULL;
+        }
 
-          path = g_file_get_path (actual);
-          g_object_unref (actual);
+      selector = _gtk_css_selector_new (selector,
+                                        combine,
+                                        name,
+                                        (GQuark *) g_array_free (ids, ids->len == 0),
+                                        (GQuark *) g_array_free (classes, classes->len == 0),
+                                        pseudo_classes,
+                                        state);
+      g_free (name);
+    }
+  while (!_gtk_css_parser_is_eof (scanner->parser) &&
+         !_gtk_css_parser_begins_with (scanner->parser, ',') &&
+         !_gtk_css_parser_begins_with (scanner->parser, '{'));
 
-          new_scanner = gtk_css_provider_create_scanner (css_provider);
-          css_provider->priv->scanner = new_scanner;
+  return selector;
+}
 
-          /* FIXME: Avoid recursive importing */
-          loaded = gtk_css_provider_load_from_path_internal (css_provider, path,
-                                                             FALSE, NULL);
+static GSList *
+parse_selector_list (GtkCssScanner *scanner)
+{
+  GSList *selectors = NULL;
 
-          /* Restore previous state */
-          gtk_css_scanner_destroy (new_scanner);
-          css_provider->priv->scanner = scanner;
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_SELECTOR);
 
-          g_free (path);
+  do {
+      GtkCssSelector *select = parse_selector (scanner);
 
-          if (!loaded)
-            return G_TOKEN_IDENTIFIER;
-          else
-            return G_TOKEN_NONE;
-        }
-      else if (strcmp (directive, "binding-set") == 0)
+      if (select == NULL)
         {
-          GtkBindingSet *binding_set;
-          gchar *binding_set_name;
+          g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+          _gtk_css_parser_resync (scanner->parser, FALSE, 0);
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
+          return NULL;
+        }
 
-          g_scanner_get_next_token (scanner);
+      selectors = g_slist_prepend (selectors, select);
+    }
+  while (_gtk_css_parser_try (scanner->parser, ",", TRUE));
 
-          if (scanner->token != G_TOKEN_IDENTIFIER)
-            {
-              gtk_css_provider_invalid_token (css_provider, scanner, "Binding name");
-              return G_TOKEN_IDENTIFIER;
-            }
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_SELECTOR);
 
-          binding_set_name = scanner->value.v_identifier;
-          binding_set = gtk_binding_set_find (binding_set_name);
+  return selectors;
+}
 
-          if (!binding_set)
-            {
-              binding_set = gtk_binding_set_new (binding_set_name);
-              binding_set->parsed = TRUE;
-            }
+static void
+parse_declaration (GtkCssScanner *scanner,
+                   GtkCssRuleset *ruleset)
+{
+  const GtkStyleProperty *property;
+  char *name;
 
-          g_scanner_get_next_token (scanner);
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DECLARATION);
 
-          if (scanner->token != G_TOKEN_LEFT_CURLY)
-            return G_TOKEN_LEFT_CURLY;
+  name = _gtk_css_parser_try_ident (scanner->parser, TRUE);
+  if (name == NULL)
+    goto check_for_semicolon;
 
-          gtk_css_scanner_push_scope (scanner, SCOPE_BINDING_SET);
-          g_scanner_get_next_token (scanner);
+  property = _gtk_style_property_lookup (name);
+  if (property == NULL && name[0] != '-')
+    {
+      gtk_css_provider_error (scanner->provider,
+                              scanner,
+                              GTK_CSS_PROVIDER_ERROR,
+                              GTK_CSS_PROVIDER_ERROR_NAME,
+                              "'%s' is not a valid property name",
+                              name);
+      _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+      g_free (name);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
+      return;
+    }
 
-          do
-            {
-              GTokenType ret;
+  if (!_gtk_css_parser_try (scanner->parser, ":", TRUE))
+    {
+      gtk_css_provider_invalid_token (scanner->provider, scanner, "':'");
+      _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+      g_free (name);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
+      return;
+    }
 
-              if (scanner->token != G_TOKEN_IDENTIFIER)
-                {
-                  gtk_css_provider_invalid_token (css_provider, scanner, "Binding definition");
-                  return G_TOKEN_IDENTIFIER;
-                }
+  if (property)
+    {
+      PropertyValue *val;
 
-              ret = gtk_binding_entry_add_signal_from_string (binding_set,
-                                                              scanner->value.v_identifier);
-              if (ret != G_TOKEN_NONE)
-                {
-                  gtk_css_provider_invalid_token (css_provider, scanner, "Binding definition");
-                  return ret;
-                }
+      g_free (name);
 
-              g_scanner_get_next_token (scanner);
+      gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
 
-              if (scanner->token != ';')
-                return ';';
+      val = property_value_new (scanner->section);
+      g_value_init (&val->value, property->pspec->value_type);
 
-              g_scanner_get_next_token (scanner);
+      if (_gtk_style_property_parse_value (property,
+                                           &val->value,
+                                           scanner->parser,
+                                           gtk_css_scanner_get_base_url (scanner)))
+        {
+          if (_gtk_css_parser_begins_with (scanner->parser, ';') ||
+              _gtk_css_parser_begins_with (scanner->parser, '}') ||
+              _gtk_css_parser_is_eof (scanner->parser))
+            {
+              gtk_css_ruleset_add (ruleset, property, val);
+            }
+          else
+            {
+              gtk_css_provider_error_literal (scanner->provider,
+                                              scanner,
+                                              GTK_CSS_PROVIDER_ERROR,
+                                              GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                              "Junk at end of value");
+              _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+              property_value_free (val);
+              gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
+              gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
+              return;
             }
-          while (scanner->token != G_TOKEN_RIGHT_CURLY);
-
-          gtk_css_scanner_pop_scope (scanner);
-          g_scanner_get_next_token (scanner);
-
-          return G_TOKEN_NONE;
         }
       else
         {
-          gtk_css_provider_invalid_token (css_provider, scanner, "Directive");
-          return G_TOKEN_IDENTIFIER;
+          property_value_free (val);
+          _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
+          return;
         }
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
     }
-
-  expected_token = parse_selector (css_provider, scanner, &selector);
-
-  if (expected_token != G_TOKEN_NONE)
+  else if (name[0] == '-')
     {
-      selector_path_unref (selector);
-      gtk_css_provider_invalid_token (css_provider, scanner, "Selector");
-      return expected_token;
-    }
+      char *value_str;
 
-  priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
+      gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_VALUE);
 
-  while (scanner->token == ',')
-    {
-      g_scanner_get_next_token (scanner);
+      value_str = _gtk_css_parser_read_value (scanner->parser);
+      if (value_str)
+        {
+          PropertyValue *val;
 
-      expected_token = parse_selector (css_provider, scanner, &selector);
+          val = property_value_new (scanner->section);
+          g_value_init (&val->value, G_TYPE_STRING);
+          g_value_take_string (&val->value, value_str);
 
-      if (expected_token != G_TOKEN_NONE)
+          gtk_css_ruleset_add_style (ruleset, name, val);
+        }
+      else
         {
-          selector_path_unref (selector);
-          gtk_css_provider_invalid_token (css_provider, scanner, "Selector");
-          return expected_token;
+          _gtk_css_parser_resync (scanner->parser, TRUE, '}');
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
+          return;
         }
 
-      priv->cur_selectors = g_slist_prepend (priv->cur_selectors, selector);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_VALUE);
     }
+  else
+    g_free (name);
 
-  gtk_css_scanner_pop_scope (scanner);
-
-  if (scanner->token != G_TOKEN_LEFT_CURLY)
-    return G_TOKEN_LEFT_CURLY;
-
-  /* Declarations parsing */
-  gtk_css_scanner_push_scope (scanner, SCOPE_DECLARATION);
+check_for_semicolon:
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DECLARATION);
 
-  g_scanner_get_next_token (scanner);
-    
-  while (scanner->token != G_TOKEN_RIGHT_CURLY &&
-         !g_scanner_eof (scanner))
+  if (!_gtk_css_parser_try (scanner->parser, ";", TRUE))
     {
-      gchar *value_str = NULL;
-      GtkStylePropertyParser parse_func = NULL;
-      GParamSpec *pspec = NULL;;
-      gchar *prop;
-
-      if (scanner->token == ';')
-        {
-          g_scanner_get_next_token (scanner);
-          continue;
-        }
-
-      if (scanner->token != G_TOKEN_IDENTIFIER)
+      if (!_gtk_css_parser_begins_with (scanner->parser, '}') &&
+          !_gtk_css_parser_is_eof (scanner->parser))
         {
-          gtk_css_provider_error_literal (css_provider,
+          gtk_css_provider_error_literal (scanner->provider,
                                           scanner,
                                           GTK_CSS_PROVIDER_ERROR,
-                                          GTK_CSS_PROVIDER_ERROR_PROPERTY_NAME,
-                                          "Expected a valid property name");
-          goto find_end_of_declaration;
-        }
-
-      prop = g_strdup (scanner->value.v_identifier);
-
-      if (!gtk_style_properties_lookup_property (prop, &parse_func, &pspec) &&
-          prop[0] != '-')
-        {
-          gtk_css_provider_error (css_provider,
-                                  scanner,
-                                  GTK_CSS_PROVIDER_ERROR,
-                                  GTK_CSS_PROVIDER_ERROR_PROPERTY_NAME,
-                                  "'%s' is not a valid property name",
-                                  prop);
-          g_free (prop);
-          goto find_end_of_declaration;
+                                          GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                          "Expected semicolon");
+          _gtk_css_parser_resync (scanner->parser, TRUE, '}');
         }
+    }
+}
 
-      g_scanner_get_next_token (scanner);
+static void
+parse_declarations (GtkCssScanner *scanner,
+                    GtkCssRuleset *ruleset)
+{
+  while (!_gtk_css_parser_is_eof (scanner->parser) &&
+         !_gtk_css_parser_begins_with (scanner->parser, '}'))
+    {
+      parse_declaration (scanner, ruleset);
+    }
+}
 
-      if (scanner->token != ':')
-        {
-          g_free (prop);
-          gtk_css_provider_invalid_token (css_provider, scanner, "':'");
-          goto find_end_of_declaration;
-        }
+static void
+parse_ruleset (GtkCssScanner *scanner)
+{
+  GSList *selectors;
+  GtkCssRuleset ruleset = { 0, };
 
-      gtk_css_scanner_push_scope (scanner, SCOPE_VALUE);
-      g_scanner_get_next_token (scanner);
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_RULESET);
 
-      if (scanner->token != G_TOKEN_IDENTIFIER)
-        {
-          g_free (prop);
-          /* the error value here is hacky. But strings should be used for
-           * strings really, so a string is not a syntax error but a broken
-           * value for everything that we support. */
-          gtk_css_provider_error (css_provider,
-                                  scanner,
-                                  GTK_CSS_PROVIDER_ERROR,
-                                  scanner->token == G_TOKEN_STRING ? GTK_CSS_PROVIDER_ERROR_PROPERTY_VALUE
-                                                                   : GTK_CSS_PROVIDER_ERROR_SYNTAX,
-                                  "Not a property value");
-          gtk_css_scanner_pop_scope (scanner);
-          goto find_end_of_declaration;
-        }
+  selectors = parse_selector_list (scanner);
+  if (selectors == NULL)
+    {
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
+      return;
+    }
 
-      value_str = scanner->value.v_identifier;
-      g_strchomp (value_str);
+  if (!_gtk_css_parser_try (scanner->parser, "{", TRUE))
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "expected '{' after selectors");
+      _gtk_css_parser_resync (scanner->parser, FALSE, 0);
+      g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+      gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
+      return;
+    }
 
-      gtk_css_scanner_pop_scope (scanner);
-      g_scanner_peek_next_token (scanner);
+  parse_declarations (scanner, &ruleset);
 
-      if (scanner->next_token != ';' &&
-          scanner->next_token != G_TOKEN_RIGHT_CURLY &&
-          scanner->next_token != G_TOKEN_EOF)
+  if (!_gtk_css_parser_try (scanner->parser, "}", TRUE))
+    {
+      gtk_css_provider_error_literal (scanner->provider,
+                                      scanner,
+                                      GTK_CSS_PROVIDER_ERROR,
+                                      GTK_CSS_PROVIDER_ERROR_SYNTAX,
+                                      "expected '}' after declarations");
+      if (!_gtk_css_parser_is_eof (scanner->parser))
         {
-          gtk_css_provider_invalid_token (css_provider, scanner, "';'");
-          g_free (prop);
-          goto find_end_of_declaration;
+          _gtk_css_parser_resync (scanner->parser, FALSE, 0);
+          g_slist_free_full (selectors, (GDestroyNotify) _gtk_css_selector_free);
+          gtk_css_ruleset_clear (&ruleset);
+          gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
         }
+    }
 
-      if (pspec)
-        {
-          GValue *val;
-
-          val = g_slice_new0 (GValue);
-          g_value_init (val, pspec->value_type);
-
-          if (strcmp (value_str, "none") == 0)
-            {
-              /* Insert the default value, so it has an opportunity
-               * to override other style providers when merged
-               */
-              g_param_value_set_default (pspec, val);
-              g_hash_table_insert (priv->cur_properties, prop, val);
-            }
-          else if (strcmp (prop, "gtk-key-bindings") == 0)
-            {
-              /* Private property holding the binding sets */
-              resolve_binding_sets (value_str, val);
-              g_hash_table_insert (priv->cur_properties, prop, val);
-            }
-          else if (pspec->value_type == G_TYPE_STRING)
-            {
-              g_value_set_string (val, value_str);
-              g_hash_table_insert (priv->cur_properties, prop, val);
-            }
-          else if (parse_func)
-            {
-              GError *error = NULL;
-              
-              if ((*parse_func) (value_str, val, &error))
-                g_hash_table_insert (priv->cur_properties, prop, val);
-              else
-                gtk_css_provider_take_error (css_provider, scanner, error);
-            }
-          else
-            {
-              GError *error = NULL;
-              GFile *base;
-              char *dirname;
-              gboolean success;
-
-              if (scanner->input_name)
-                dirname = g_path_get_dirname (scanner->input_name);
-              else
-                dirname = g_get_current_dir ();
-
-              base = g_file_new_for_path (dirname);
-              g_free (dirname);
-
-              success = _gtk_css_value_from_string (val,
-                                                    base,
-                                                    value_str,
-                                                    &error);
-
-              g_object_unref (base);
-
-              if (success)
-                {
-                  g_hash_table_insert (priv->cur_properties, prop, val);
-                }
-              else
-                {
-                  g_value_unset (val);
-                  g_slice_free (GValue, val);
-                  g_free (prop);
-
-                  gtk_css_provider_take_error (css_provider, scanner, error);
-                }
-            }
-        }
-      else if (prop[0] == '-')
-        {
-          GValue *val;
+  css_provider_commit (scanner->provider, selectors, &ruleset);
+  gtk_css_ruleset_clear (&ruleset);
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_RULESET);
+}
 
-          val = g_slice_new0 (GValue);
-          g_value_init (val, G_TYPE_STRING);
-          g_value_set_string (val, value_str);
+static void
+parse_statement (GtkCssScanner *scanner)
+{
+  if (_gtk_css_parser_begins_with (scanner->parser, '@'))
+    parse_at_keyword (scanner);
+  else
+    parse_ruleset (scanner);
+}
 
-          g_hash_table_insert (priv->cur_properties, prop, val);
-        }
-      else
-        g_free (prop);
+static void
+parse_stylesheet (GtkCssScanner *scanner)
+{
+  gtk_css_scanner_push_section (scanner, GTK_CSS_SECTION_DOCUMENT);
 
-      g_scanner_get_next_token (scanner);
+  _gtk_css_parser_skip_whitespace (scanner->parser);
 
-      if (g_scanner_eof (scanner))
-        {
-          gtk_css_provider_invalid_token (css_provider, scanner, "}");
-          break;
-        }
+  while (!_gtk_css_parser_is_eof (scanner->parser))
+    {
+      if (_gtk_css_parser_try (scanner->parser, "<!--", TRUE) ||
+          _gtk_css_parser_try (scanner->parser, "-->", TRUE))
+        continue;
 
-find_end_of_declaration:
-      while (scanner->token != ';' &&
-             scanner->token != G_TOKEN_RIGHT_CURLY &&
-             !g_scanner_eof (scanner))
-        g_scanner_get_next_token (scanner);
+      parse_statement (scanner);
     }
 
-  gtk_css_scanner_pop_scope (scanner);
-
-  return G_TOKEN_NONE;
+  gtk_css_scanner_pop_section (scanner, GTK_CSS_SECTION_DOCUMENT);
 }
 
-static void
-gtk_css_provider_reset (GtkCssProvider *css_provider)
+static int
+gtk_css_provider_compare_rule (gconstpointer a_,
+                               gconstpointer b_)
 {
-  GtkCssProviderPrivate *priv;
+  const GtkCssRuleset *a = (const GtkCssRuleset *) a_;
+  const GtkCssRuleset *b = (const GtkCssRuleset *) b_;
+  int compare;
 
-  priv = css_provider->priv;
+  compare = _gtk_css_selector_compare (a->selector, b->selector);
+  if (compare != 0)
+    return compare;
+
+  /* compare pointers in array to ensure a stable sort */
+  if (a_ < b_)
+    return -1;
 
-  if (priv->selectors_info->len > 0)
-    g_ptr_array_remove_range (priv->selectors_info, 0, priv->selectors_info->len);
+  if (a_ > b_)
+    return 1;
+
+  return 0;
 }
 
 static void
-gtk_css_provider_propagate_error (GtkCssProvider  *provider,
-                                  const gchar     *path,
-                                  guint            line,
-                                  guint            position,
-                                  const GError    *error,
-                                  GError         **propagate_to)
+gtk_css_provider_postprocess (GtkCssProvider *css_provider)
 {
-  /* we already set an error. And we'd like to keep the first one */
-  if (*propagate_to)
-    return;
+  GtkCssProviderPrivate *priv = css_provider->priv;
 
-  *propagate_to = g_error_copy (error);
-  g_prefix_error (propagate_to, "%s:%u:%u: ", path ? path : "<unknown>", line, position);
+  g_array_sort (priv->rulesets, gtk_css_provider_compare_rule);
 }
 
 static gboolean
-parse_stylesheet (GtkCssProvider  *css_provider,
-                  GScanner        *scanner,
-                  GError         **error)
+gtk_css_provider_load_internal (GtkCssProvider *css_provider,
+                                GtkCssScanner  *parent,
+                                GFile          *file,
+                                const char     *text,
+                                GError        **error)
 {
+  GtkCssScanner *scanner;
   gulong error_handler;
+  char *free_data = NULL;
 
   if (error)
     error_handler = g_signal_connect (css_provider,
@@ -2602,32 +2511,58 @@ parse_stylesheet (GtkCssProvider  *css_provider,
   else
     error_handler = 0; /* silence gcc */
 
-  g_scanner_get_next_token (scanner);
-
-  while (!g_scanner_eof (scanner))
+  if (text == NULL)
     {
-      GTokenType expected_token;
-
-      expected_token = parse_rule (css_provider, scanner);
+      GError *load_error = NULL;
 
-      if (expected_token != G_TOKEN_NONE)
+      if (g_file_load_contents (file, NULL,
+                                &free_data, NULL,
+                                NULL, &load_error))
         {
-          while (!g_scanner_eof (scanner) &&
-                 scanner->token != G_TOKEN_RIGHT_CURLY)
-            g_scanner_get_next_token (scanner);
+          text = free_data;
         }
       else
-        css_provider_commit (css_provider, scanner);
+        {
+          GtkCssSection *section;
+          
+          if (parent)
+            section = gtk_css_section_ref (parent->section);
+          else
+            section = _gtk_css_section_new_for_file (GTK_CSS_SECTION_DOCUMENT, file);
 
-      g_scanner_get_next_token (scanner);
-      
-      gtk_css_scanner_reset (scanner);
+          gtk_css_provider_error (css_provider,
+                                  parent,
+                                  GTK_CSS_PROVIDER_ERROR,
+                                  GTK_CSS_PROVIDER_ERROR_IMPORT,
+                                  "Failed to import: %s",
+                                  load_error->message);
+
+          gtk_css_section_unref (section);
+        }
+    }
+
+  if (text)
+    {
+      scanner = gtk_css_scanner_new (css_provider,
+                                     parent,
+                                     parent ? parent->section : NULL,
+                                     file,
+                                     text);
+
+      parse_stylesheet (scanner);
+
+      gtk_css_scanner_destroy (scanner);
+
+      if (parent == NULL)
+        gtk_css_provider_postprocess (css_provider);
     }
 
+  g_free (free_data);
+
   if (error)
     {
       g_signal_handler_disconnect (css_provider, error_handler);
-      
+
       if (*error)
         {
           /* We clear all contents from the provider for backwards compat reasons */
@@ -2642,8 +2577,10 @@ parse_stylesheet (GtkCssProvider  *css_provider,
 /**
  * gtk_css_provider_load_from_data:
  * @css_provider: a #GtkCssProvider
- * @data: CSS data loaded in memory
- * @length: the length of @data in bytes, or -1 for NUL terminated strings
+ * @data: (array length=length) (element-type guint8): CSS data loaded in memory
+ * @length: the length of @data in bytes, or -1 for NUL terminated strings. If
+ *   @length is not -1, the code will assume it is not NUL terminated and will
+ *   potentially do a copy.
  * @error: (out) (allow-none): return location for a #GError, or %NULL
  *
  * Loads @data into @css_provider, making it clear any previously loaded
@@ -2657,22 +2594,30 @@ gtk_css_provider_load_from_data (GtkCssProvider  *css_provider,
                                  gssize           length,
                                  GError         **error)
 {
-  GtkCssProviderPrivate *priv;
+  char *free_data;
+  gboolean ret;
 
   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
   g_return_val_if_fail (data != NULL, FALSE);
 
-  priv = css_provider->priv;
-
   if (length < 0)
-    length = strlen (data);
+    {
+      length = strlen (data);
+      free_data = NULL;
+    }
+  else
+    {
+      free_data = g_strndup (data, length);
+      data = free_data;
+    }
 
   gtk_css_provider_reset (css_provider);
 
-  priv->scanner->input_name = NULL;
-  g_scanner_input_text (priv->scanner, data, (guint) length);
+  ret = gtk_css_provider_load_internal (css_provider, NULL, NULL, data, error);
 
-  return parse_stylesheet (css_provider, priv->scanner, error);
+  g_free (free_data);
+
+  return ret;
 }
 
 /**
@@ -2691,83 +2636,12 @@ gtk_css_provider_load_from_file (GtkCssProvider  *css_provider,
                                  GFile           *file,
                                  GError         **error)
 {
-  GtkCssProviderPrivate *priv;
-  GError *internal_error = NULL;
-  char *path;
-  gchar *data;
-  gsize length;
-  gboolean ret;
-
   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
   g_return_val_if_fail (G_IS_FILE (file), FALSE);
 
-  priv = css_provider->priv;
-
-  if (!g_file_load_contents (file, NULL,
-                             &data, &length,
-                             NULL, &internal_error))
-    {
-      g_propagate_error (error, internal_error);
-      return FALSE;
-    }
-
   gtk_css_provider_reset (css_provider);
 
-  path = g_file_get_path (file);
-  priv->scanner->input_name = path;
-  g_scanner_input_text (priv->scanner, data, (guint) length);
-
-  ret = parse_stylesheet (css_provider, priv->scanner, error);
-
-  g_free (path);
-  priv->scanner->input_name = NULL;
-  g_free (data);
-
-  return ret;
-}
-
-static gboolean
-gtk_css_provider_load_from_path_internal (GtkCssProvider  *css_provider,
-                                          const gchar     *path,
-                                          gboolean         reset,
-                                          GError         **error)
-{
-  GtkCssProviderPrivate *priv;
-  GError *internal_error = NULL;
-  GMappedFile *mapped_file;
-  const gchar *data;
-  gsize length;
-  gboolean ret;
-
-  priv = css_provider->priv;
-
-  mapped_file = g_mapped_file_new (path, FALSE, &internal_error);
-
-  if (internal_error)
-    {
-      g_propagate_error (error, internal_error);
-      return FALSE;
-    }
-
-  length = g_mapped_file_get_length (mapped_file);
-  data = g_mapped_file_get_contents (mapped_file);
-
-  if (!data)
-    data = "";
-
-  if (reset)
-    gtk_css_provider_reset (css_provider);
-
-  priv->scanner->input_name = path;
-  g_scanner_input_text (priv->scanner, data, (guint) length);
-
-  ret = parse_stylesheet (css_provider, priv->scanner, error);
-
-  priv->scanner->input_name = NULL;
-
-  g_mapped_file_unref (mapped_file);
-
-  return ret;
+  return gtk_css_provider_load_internal (css_provider, NULL, file, NULL, error);
 }
 
 /**
@@ -2786,11 +2660,19 @@ gtk_css_provider_load_from_path (GtkCssProvider  *css_provider,
                                  const gchar     *path,
                                  GError         **error)
 {
+  GFile *file;
+  gboolean result;
+
   g_return_val_if_fail (GTK_IS_CSS_PROVIDER (css_provider), FALSE);
   g_return_val_if_fail (path != NULL, FALSE);
 
-  return gtk_css_provider_load_from_path_internal (css_provider, path,
-                                                   TRUE, error);
+  file = g_file_new_for_path (path);
+  
+  result = gtk_css_provider_load_from_file (css_provider, file, error);
+
+  g_object_unref (file);
+
+  return result;
 }
 
 /**
@@ -2935,7 +2817,8 @@ gtk_css_provider_get_default (void)
         "  border-width: 1;\n"
         "}\n"
         "\n"
-        ".tooltip {\n"
+        ".tooltip,\n"
+        ".tooltip * {\n"
         "  background-color: @tooltip_bg_color; \n"
         "  color: @tooltip_fg_color; \n"
         "  border-color: @tooltip_fg_color; \n"
@@ -3027,7 +2910,7 @@ gtk_css_provider_get_default (void)
         "  background-color: @selected_bg_color;\n"
         "}\n"
         "\n"
-        ".menu.check, .menu.radio {\n"
+        ".menuitem.check, .menuitem.radio {\n"
         "  color: @fg_color;\n"
         "  border-style: none;\n"
         "  border-width: 0;\n"
@@ -3066,8 +2949,9 @@ gtk_css_provider_get_default (void)
         "\n"
         ".menu:hover,\n"
         ".menubar:hover,\n"
-        ".menu.check:hover,\n"
-        ".menu.radio:hover {\n"
+        ".menuitem:hover,\n"
+        ".menuitem.check:hover,\n"
+        ".menuitem.radio:hover {\n"
         "  background-color: @selected_bg_color;\n"
         "  color: @selected_fg_color;\n"
         "}\n"
@@ -3194,11 +3078,13 @@ _gtk_css_provider_get_theme_dir (void)
   if (var)
     path = g_build_filename (var, "share", "themes", NULL);
   else
-    path = g_build_filename (GTK_DATA_PREFIX, "share", "themes", NULL);
+    path = g_build_filename (_gtk_get_data_prefix (), "share", "themes", NULL);
 
   return path;
 }
 
+#include "gtkwin32css.h"
+
 /**
  * gtk_css_provider_get_named:
  * @name: A theme name
@@ -3219,7 +3105,18 @@ gtk_css_provider_get_named (const gchar *name,
   gchar *key;
 
   if (G_UNLIKELY (!themes))
-    themes = g_hash_table_new (g_str_hash, g_str_equal);
+    {
+      themes = g_hash_table_new (g_str_hash, g_str_equal);
+
+      provider = gtk_css_provider_new ();
+      if (!gtk_css_provider_load_from_data (provider, gtk_win32_default_css, -1, NULL))
+        {
+          g_warning ("Failed to load the internal win32 default CSS.");
+         g_object_unref (provider);
+        }
+      else
+       g_hash_table_insert (themes, "gtk-win32", provider);
+    }
 
   if (variant == NULL)
     key = (gchar *)name;
@@ -3271,15 +3168,10 @@ gtk_css_provider_get_named (const gchar *name,
 
       if (path)
         {
-          GError *error;
-
           provider = gtk_css_provider_new ();
-          error = NULL;
-          if (!gtk_css_provider_load_from_path (provider, path, &error))
-            {
-              g_warning ("Could not load named theme \"%s\": %s", name, error->message);
-              g_error_free (error);
 
+          if (!gtk_css_provider_load_from_path (provider, path, NULL))
+            {
               g_object_unref (provider);
               provider = NULL;
             }
@@ -3296,141 +3188,65 @@ gtk_css_provider_get_named (const gchar *name,
   return provider;
 }
 
+static int
+compare_properties (gconstpointer a, gconstpointer b)
+{
+  return strcmp (((const GtkStyleProperty *) a)->pspec->name,
+                 ((const GtkStyleProperty *) b)->pspec->name);
+}
+
 static void
-selector_path_print (const SelectorPath *path,
-                     GString *           str)
+gtk_css_ruleset_print (const GtkCssRuleset *ruleset,
+                       GString             *str)
 {
-  GSList *walk, *reverse;
+  GList *keys, *walk;
 
-  reverse = g_slist_copy (path->elements);
-  reverse = g_slist_reverse (reverse);
+  _gtk_css_selector_print (ruleset->selector, str);
+
+  g_string_append (str, " {\n");
 
-  for (walk = reverse; walk; walk = walk->next)
+  if (ruleset->style)
     {
-      SelectorElement *elem = walk->data;
+      keys = g_hash_table_get_keys (ruleset->style);
+      /* so the output is identical for identical selector styles */
+      keys = g_list_sort (keys, compare_properties);
 
-      switch (elem->elem_type)
+      for (walk = keys; walk; walk = walk->next)
         {
-        case SELECTOR_TYPE_NAME:
-        case SELECTOR_NAME:
-          g_string_append (str, g_quark_to_string (elem->name));
-          break;
-        case SELECTOR_GTYPE:
-          g_string_append (str, g_type_name (elem->type));
-          break;
-        case SELECTOR_REGION:
-          g_string_append (str, g_quark_to_string (elem->region.name));
-          if (elem->region.flags)
-            {
-              char * flag_names[] = {
-                "nth-child(even)",
-                "nth-child(odd)",
-                "first-child",
-                "last-child",
-                "sorted"
-              };
-              guint i;
-
-              for (i = 0; i < G_N_ELEMENTS (flag_names); i++)
-                {
-                  if (elem->region.flags & (1 << i))
-                    {
-                      g_string_append_c (str, ':');
-                      g_string_append (str, flag_names[i]);
-                    }
-                }
-            }
-          break;
-        case SELECTOR_CLASS:
-          g_string_append_c (str, '.');
-          g_string_append (str, g_quark_to_string (elem->name));
-          break;
-        case SELECTOR_GLOB:
-          if (walk->next == NULL ||
-              elem->combinator != COMBINATOR_CHILD ||
-              ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
-            g_string_append (str, "*");
-          break;
-        default:
-          g_assert_not_reached ();
+          GtkStyleProperty *prop = walk->data;
+          const PropertyValue *value = g_hash_table_lookup (ruleset->style, prop);
+
+          g_string_append (str, "  ");
+          g_string_append (str, prop->pspec->name);
+          g_string_append (str, ": ");
+          _gtk_style_property_print_value (prop, &value->value, str);
+          g_string_append (str, ";\n");
         }
 
-      if (walk->next)
-        {
-          switch (elem->combinator)
-            {
-            case COMBINATOR_DESCENDANT:
-              if (elem->elem_type != SELECTOR_CLASS ||
-                  ((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
-                g_string_append_c (str, ' ');
-              break;
-            case COMBINATOR_CHILD:
-              if (((SelectorElement *) walk->next->data)->elem_type != SELECTOR_CLASS)
-                g_string_append (str, " > ");
-              break;
-            default:
-              g_assert_not_reached ();
-            }
-        }
-        
+      g_list_free (keys);
     }
 
-  if (path->state)
+  if (ruleset->widget_style)
     {
-      char * state_names[] = {
-        "active",
-        "hover",
-        "selected",
-        "insensitive",
-        "inconsistent",
-        "focus"
-      };
-      guint i;
-
-      for (i = 0; i < G_N_ELEMENTS (state_names); i++)
+      keys = g_hash_table_get_keys (ruleset->widget_style);
+      /* so the output is identical for identical selector styles */
+      keys = g_list_sort (keys, (GCompareFunc) strcmp);
+
+      for (walk = keys; walk; walk = walk->next)
         {
-          if (path->state & (1 << i))
-            {
-              g_string_append_c (str, ':');
-              g_string_append (str, state_names[i]);
-            }
+          const char *name = walk->data;
+          const PropertyValue *value = g_hash_table_lookup (ruleset->widget_style, (gpointer) name);
+
+          g_string_append (str, "  ");
+          g_string_append (str, name);
+          g_string_append (str, ": ");
+          g_string_append (str, g_value_get_string (&value->value));
+          g_string_append (str, ";\n");
         }
-    }
-
-  g_slist_free (reverse);
-}
-
-static void
-selector_style_info_print (const SelectorStyleInfo *info,
-                           GString                 *str)
-{
-  GList *keys, *walk;
-  char *s;
-
-  selector_path_print (info->path, str);
-
-  g_string_append (str, " {\n");
-
-  keys = g_hash_table_get_keys (info->style);
-  /* so the output is identical for identical selector styles */
-  keys = g_list_sort (keys, (GCompareFunc) strcmp);
-
-  for (walk = keys; walk; walk = walk->next)
-    {
-      const char *name = walk->data;
-      const GValue *value = g_hash_table_lookup (info->style, (gpointer) name);
 
-      g_string_append (str, "  ");
-      g_string_append (str, name);
-      g_string_append (str, ": ");
-      s = _gtk_css_value_to_string (value);
-      g_string_append (str, s);
-      g_free (s);
-      g_string_append (str, ";\n");
+      g_list_free (keys);
     }
 
-  g_list_free (keys);
-
   g_string_append (str, "}\n");
 }
 
@@ -3491,12 +3307,11 @@ gtk_css_provider_to_string (GtkCssProvider *provider)
 
   gtk_css_provider_print_colors (priv->symbolic_colors, str);
 
-  for (i = 0; i < priv->selectors_info->len; i++)
+  for (i = 0; i < priv->rulesets->len; i++)
     {
       if (i > 0)
         g_string_append (str, "\n");
-      selector_style_info_print (g_ptr_array_index (priv->selectors_info, i),
-                                 str);
+      gtk_css_ruleset_print (&g_array_index (priv->rulesets, GtkCssRuleset, i), str);
     }
 
   return g_string_free (str, FALSE);