[gxml] GomElement: Fixed set/get_attribute



commit 7aafe8b2a3d88b4ce82db3db297d0f82e24090cb
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Oct 31 16:25:18 2016 -0600

    GomElement: Fixed set/get_attribute
    
    GomObject now process set/get_attribute and return
    if it they actually changes/return a value from object's
    properties, if not, attribute is searched in DOM Element's
    attributes

 gxml/GomElement.vala      |   12 ++++++++++--
 gxml/GomObject.vala       |   23 ++++++++++++-----------
 test/GomDocumentTest.vala |    2 +-
 3 files changed, 23 insertions(+), 14 deletions(-)
---
diff --git a/gxml/GomElement.vala b/gxml/GomElement.vala
index edfaddf..8a6452d 100644
--- a/gxml/GomElement.vala
+++ b/gxml/GomElement.vala
@@ -32,6 +32,7 @@ public class GXml.GomElement : GomNode,
                               DomParentNode,
                               DomElement,
                               GomObject {
+  protected Attributes _attributes;
   // DomNode overrides
   public new string? lookup_prefix (string? nspace) {
     if (namespace_uri == nspace && this.prefix != null)
@@ -268,9 +269,14 @@ public class GXml.GomElement : GomNode,
                                     ns, p, n, node.node_value);
     }
   }
-  protected Attributes _attributes;
   public DomNamedNodeMap attributes { owned get { return (DomNamedNodeMap) _attributes; } }
-  public string? get_attribute (string name) { return (this as GomObject).get_attribute (name); }
+  public string? get_attribute (string name) {
+    string s = (this as GomObject).get_attribute (name);
+    if (s != null) return s;
+    var a = _attributes.get_named_item (name);
+    if (a == null) return null;
+    return a.node_value;
+  }
   public string? get_attribute_ns (string? namespace, string local_name) {
     DomNode p = null;
     try { p = _attributes.get_named_item_ns (namespace, local_name); }
@@ -283,6 +289,8 @@ public class GXml.GomElement : GomNode,
     return p.node_value;
   }
   public void set_attribute (string name, string? value) {
+    bool res = (this as GomObject).set_attribute (name, value);
+    if (res) return;
     var a = new GomAttr (this, name, value);
     attributes.set_named_item (a);
   }
diff --git a/gxml/GomObject.vala b/gxml/GomObject.vala
index 7e613d8..54c1f59 100644
--- a/gxml/GomObject.vala
+++ b/gxml/GomObject.vala
@@ -51,7 +51,8 @@ public interface GXml.GomObject : GLib.Object,
    * this object, see {@link get_child}
    */
   public virtual string? get_attribute (string name) {
-    var prop = get_class ().find_property (name);
+    GLib.message ("GomObject: attribute: "+name);
+    var prop = get_class ().find_property (name); // FIXME: Find by nick and lower case
     if (prop != null) {
       if (prop.value_type == typeof(SerializableProperty)) {
         var ov = Value(prop.value_type);
@@ -61,7 +62,7 @@ public interface GXml.GomObject : GLib.Object,
         return so.get_serializable_property_value ();
       }
     }
-    return (this as DomElement).get_attribute (name);
+    return null;
   }
   /**
    * Search for a {@link GLib.Object} property with
@@ -72,19 +73,19 @@ public interface GXml.GomObject : GLib.Object,
    * By default all {@link GLib.Object} are children of
    * this object, see {@link set_child}
    */
-  public virtual void set_attribute (string name, string val) {
+  public virtual bool set_attribute (string name, string val) {
     var prop = get_class ().find_property (name);
     if (prop != null) {
       if (prop.value_type == typeof(SerializableProperty)) {
         var ov = Value (prop.value_type);
         get_property (name, ref ov);
         SerializableProperty so = (Object) ov as SerializableProperty;
-        if (so == null) return;
+        if (so == null) return false;
         so.set_serializable_property_value (val);
-        return;
+        return true;
       }
     }
-    (this as DomElement).set_property (name, val);
+    return false;
   }
   /**
    * Search a {@link GLib.Object} property with given name
@@ -112,20 +113,20 @@ public interface GXml.GomObject : GLib.Object,
     }
     return null;
   }
-  public virtual void remove_attribute (string name) {
+  public virtual bool remove_attribute (string name) {
     var prop = get_class ().find_property (name);
     if (prop != null) {
       if (prop.value_type.is_a (typeof (SerializableProperty))) {
         (this as SerializableProperty).set_serializable_property_value (null);
-        return;
+        return true;
       }
       if (prop.value_type.is_a (typeof (SerializableCollection))) {
-        return;
+        return true;
       }
       Value v = Value (typeof (Object));
       (this as Object).set_property (name, v);
-      return;
+      return true;
     }
-    (this as DomElement).remove_attribute (name);
+    return false;
   }
 }
diff --git a/test/GomDocumentTest.vala b/test/GomDocumentTest.vala
index 2b0a5c4..a166904 100644
--- a/test/GomDocumentTest.vala
+++ b/test/GomDocumentTest.vala
@@ -254,7 +254,7 @@ class GomDocumentTest : GXmlTest {
                                DomDocument doc = new GomDocument.from_string ("<document_element />");
                                assert (doc.document_element != null);
                                ((DomElement) doc.document_element).set_attribute ("attrname", "attrvalue");
-                               assert_not_reached ();
+                               assert (doc.document_element.attributes.size == 1);
                                //Test.message ("DOC:"+doc.to_string ());
                                var attr = ((DomElement) doc.document_element).get_attribute ("attrname");
                                Test.message ("Attr value: "+attr);


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