[gtk+/wip/otte/tokenizer: 54/78] cssimportrule: Actually import the style sheet



commit afe70c456804024c62c982af3fac1a078c20422b
Author: Benjamin Otte <otte redhat com>
Date:   Fri Mar 25 06:29:46 2016 +0100

    cssimportrule: Actually import the style sheet
    
    Also add code to the inspector's ruleview to display rules of imported
    style sheets.

 gtk/gtkcssimportrule.c        |   70 ++++++++++++++++++++++++++++++++---------
 gtk/gtkcssimportruleprivate.h |    1 +
 gtk/inspector/cssruleview.c   |   56 ++++++++++++++++++++------------
 3 files changed, 91 insertions(+), 36 deletions(-)
---
diff --git a/gtk/gtkcssimportrule.c b/gtk/gtkcssimportrule.c
index 8d478bc..b6f7216 100644
--- a/gtk/gtkcssimportrule.c
+++ b/gtk/gtkcssimportrule.c
@@ -37,8 +37,10 @@ gtk_css_import_rule_finalize (GObject *object)
   GtkCssImportRule *import_rule = GTK_CSS_IMPORT_RULE (object);
   GtkCssImportRulePrivate *priv = gtk_css_import_rule_get_instance_private (import_rule);
 
-  g_object_unref (priv->file);
-  g_object_unref (priv->style_sheet);
+  if (priv->file)
+    g_object_unref (priv->file);
+  if (priv->style_sheet)
+    g_object_unref (priv->style_sheet);
 
   G_OBJECT_CLASS (gtk_css_import_rule_parent_class)->finalize (object);
 }
@@ -54,15 +56,11 @@ gtk_css_import_rule_class_init (GtkCssImportRuleClass *klass)
 static void
 gtk_css_import_rule_init (GtkCssImportRule *import_rule)
 {
-  GtkCssImportRulePrivate *priv = gtk_css_import_rule_get_instance_private (import_rule);
-
-  priv->style_sheet = gtk_css_style_sheet_new ();
 }
 
 static GtkCssRule *
 gtk_css_import_rule_new (GtkCssRule       *parent_rule,
-                         GtkCssStyleSheet *parent_style_sheet,
-                         GFile            *file)
+                         GtkCssStyleSheet *parent_style_sheet)
 {
   return g_object_new (GTK_TYPE_CSS_IMPORT_RULE,
                        "parent-rule", parent_rule,
@@ -75,14 +73,24 @@ gtk_css_import_rule_new_parse (GtkCssTokenSource *source,
                                GtkCssRule        *parent_rule,
                                GtkCssStyleSheet  *parent_style_sheet)
 {
+  GtkCssImportRulePrivate *priv;
   const GtkCssToken *token;
   GtkCssRule *result;
-  GFile *file;
+  GtkCssTokenizer *tokenizer;
+  GtkCssTokenSource *import_source;
+  GError *load_error = NULL;
+  GBytes *bytes;
+  char *data;
+  gsize size;
 
   g_return_val_if_fail (source != NULL, NULL);
   g_return_val_if_fail (parent_rule == NULL || GTK_IS_CSS_RULE (parent_rule), NULL);
   g_return_val_if_fail (GTK_IS_CSS_STYLE_SHEET (parent_style_sheet), NULL);
 
+  result = gtk_css_import_rule_new (parent_rule, parent_style_sheet);
+  gtk_css_token_source_set_consumer (source, G_OBJECT (result));
+  priv = gtk_css_import_rule_get_instance_private (GTK_CSS_IMPORT_RULE (result));
+
   token = gtk_css_token_source_get_token (source);
   if (token->type != GTK_CSS_TOKEN_AT_KEYWORD ||
       g_ascii_strcasecmp (token->string.string, "import") != 0)
@@ -96,28 +104,60 @@ gtk_css_import_rule_new_parse (GtkCssTokenSource *source,
   token = gtk_css_token_source_get_token (source);
   if (gtk_css_token_is (token, GTK_CSS_TOKEN_STRING))
     {
-      file = gtk_css_token_source_resolve_url (source, token->string.string);
+      priv->file = gtk_css_token_source_resolve_url (source, token->string.string);
       gtk_css_token_source_consume_token (source);
     }
   else
     {
-      file = gtk_css_token_source_consume_url (source);
+      priv->file = gtk_css_token_source_consume_url (source);
+      if (priv->file == NULL)
+        {
+          g_object_unref (result);
+          return NULL;
+        }
     }
 
-  if (file == NULL)
-    return NULL;
-
   token = gtk_css_token_source_get_token (source);
   if (!gtk_css_token_is (token, GTK_CSS_TOKEN_SEMICOLON))
     {
       gtk_css_token_source_error (source, "Expected ';' at end of @import");
       gtk_css_token_source_consume_all (source);
+      g_object_unref (result);
       return NULL;
     }
+
+  if (g_file_load_contents (priv->file, NULL,
+                            &data, &size,
+                            NULL, &load_error))
+    {
+      bytes = g_bytes_new_take (data, size);
+    }
+  else
+    {
+      gtk_css_token_source_emit_error (source, load_error);
+      bytes = g_bytes_new (NULL, 0);
+      g_error_free (load_error);
+    }
+  tokenizer = gtk_css_tokenizer_new (bytes, NULL, NULL, NULL);
+  import_source = gtk_css_token_source_new_for_tokenizer (tokenizer, priv->file);
+  priv->style_sheet = gtk_css_style_sheet_new_import (import_source, result);
+  gtk_css_token_source_unref (import_source);
+  gtk_css_tokenizer_unref (tokenizer);
+
   gtk_css_token_source_consume_token (source);
 
-  result = gtk_css_import_rule_new (parent_rule, parent_style_sheet, file);
-  g_object_unref (file);
   return result;
 }
 
+GtkCssStyleSheet *
+gtk_css_import_rule_get_style_sheet (GtkCssImportRule *rule)
+{
+  GtkCssImportRulePrivate *priv;
+
+  g_return_val_if_fail (GTK_IS_CSS_IMPORT_RULE (rule), NULL);
+
+  priv = gtk_css_import_rule_get_instance_private (rule);
+
+  return priv->style_sheet;
+}
+
diff --git a/gtk/gtkcssimportruleprivate.h b/gtk/gtkcssimportruleprivate.h
index 135a71f..df41f53 100644
--- a/gtk/gtkcssimportruleprivate.h
+++ b/gtk/gtkcssimportruleprivate.h
@@ -50,6 +50,7 @@ GtkCssRule *            gtk_css_import_rule_new_parse           (GtkCssTokenSour
                                                                  GtkCssRule             *parent_rule,
                                                                  GtkCssStyleSheet       *parent_style_sheet);
 
+GtkCssStyleSheet *      gtk_css_import_rule_get_style_sheet     (GtkCssImportRule       *rule);
 
 G_END_DECLS
 
diff --git a/gtk/inspector/cssruleview.c b/gtk/inspector/cssruleview.c
index ba02230..37ece37 100644
--- a/gtk/inspector/cssruleview.c
+++ b/gtk/inspector/cssruleview.c
@@ -28,6 +28,7 @@
 #include "cssruleviewrow.h"
 
 #include "gtkcssrulelistprivate.h"
+#include "gtkcssimportruleprivate.h"
 #include "gtkcssstyleruleprivate.h"
 
 typedef struct _GtkInspectorCssRuleViewPrivate GtkInspectorCssRuleViewPrivate;
@@ -86,6 +87,39 @@ gtk_inspector_css_rule_view_init (GtkInspectorCssRuleView *ruleview)
                               NULL);
 }
 
+static void
+gtk_inspector_css_rule_view_add_rules (GtkInspectorCssRuleView *ruleview,
+                                       GtkCssStyleSheet        *style_sheet)
+{
+  GtkInspectorCssRuleViewPrivate *priv = gtk_inspector_css_rule_view_get_instance_private (ruleview);
+  GtkCssRuleList *rule_list;
+  GtkCssRule *rule;
+  guint i, j;
+
+  rule_list = gtk_css_style_sheet_get_css_rules (style_sheet);
+
+  for (i = 0; i < gtk_css_rule_list_get_length (rule_list); i++)
+    {
+      rule = gtk_css_rule_list_get_item (rule_list, i);
+
+      if (GTK_IS_CSS_STYLE_RULE (rule))
+        {
+          for (j = 0; j < gtk_css_style_rule_get_n_selectors (GTK_CSS_STYLE_RULE (rule)); j++)
+            {
+              GtkWidget *row = gtk_inspector_css_rule_view_row_new (GTK_CSS_STYLE_RULE (rule), j);
+
+              gtk_widget_show (row);
+              gtk_container_add (GTK_CONTAINER (priv->list_widget), row);
+            }
+        }
+      else if (GTK_IS_CSS_IMPORT_RULE (rule))
+        {
+          gtk_inspector_css_rule_view_add_rules (ruleview,
+                                                 gtk_css_import_rule_get_style_sheet (GTK_CSS_IMPORT_RULE 
(rule)));
+        }
+    }
+}
+
 void
 gtk_inspector_css_rule_view_set_style_sheet (GtkInspectorCssRuleView *ruleview,
                                              GtkCssStyleSheet        *style_sheet)
@@ -118,29 +152,9 @@ gtk_inspector_css_rule_view_set_style_sheet (GtkInspectorCssRuleView *ruleview,
 
   if (style_sheet)
     {
-      GtkCssRuleList *rule_list;
-      GtkCssRule *rule;
-      guint i, j;
-
       g_object_ref (style_sheet);
 
-      rule_list = gtk_css_style_sheet_get_css_rules (style_sheet);
-
-      for (i = 0; i < gtk_css_rule_list_get_length (rule_list); i++)
-        {
-          rule = gtk_css_rule_list_get_item (rule_list, i);
-
-          if (!GTK_IS_CSS_STYLE_RULE (rule))
-            continue;
-
-          for (j = 0; j < gtk_css_style_rule_get_n_selectors (GTK_CSS_STYLE_RULE (rule)); j++)
-            {
-              GtkWidget *row = gtk_inspector_css_rule_view_row_new (GTK_CSS_STYLE_RULE (rule), j);
-
-              gtk_widget_show (row);
-              gtk_container_add (GTK_CONTAINER (priv->list_widget), row);
-            }
-        }
+      gtk_inspector_css_rule_view_add_rules (ruleview, style_sheet);
     }
 }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]