[gxml] Fixed DomNode.compare_document_position() implementation on GNode with Unit Tests



commit 197eec28598fefaa4aebd4f55441bb23e688a850
Author: Daniel Espinosa <esodan gmail com>
Date:   Tue Jul 19 12:22:19 2016 -0500

    Fixed DomNode.compare_document_position() implementation on GNode with Unit Tests

 gxml/DomNode.vala          |    1 +
 gxml/GXmlNode.vala         |   28 +++++++++++++++-------------
 test/DomGDocumentTest.vala |   35 ++++++++++++++++++++++++++++++++++-
 3 files changed, 50 insertions(+), 14 deletions(-)
---
diff --git a/gxml/DomNode.vala b/gxml/DomNode.vala
index cc612b6..0652f75 100644
--- a/gxml/DomNode.vala
+++ b/gxml/DomNode.vala
@@ -61,6 +61,7 @@ public interface GXml.DomNode : GLib.Object, GXml.DomEventTarget {
 
   [Flags]
   public enum DocumentPosition {
+    NONE,
     DISCONNECTED,
     PRECEDING,
     FOLLOWING,
diff --git a/gxml/GXmlNode.vala b/gxml/GXmlNode.vala
index 75d63fd..df45f8c 100644
--- a/gxml/GXmlNode.vala
+++ b/gxml/GXmlNode.vala
@@ -235,8 +235,8 @@ public abstract class GXml.GNode : Object,
   }
 
   public DomNode.DocumentPosition compare_document_position (DomNode other) {
-    if ((&this as GXml.DomNode) == &other) return (DomNode.DocumentPosition) 0;
-    if (this.document != (other as GXml.Node).document) {
+    if ((this as GXml.DomNode) == other) return DomNode.DocumentPosition.NONE;
+    if (this.document != (other as GXml.Node).document || other.parent_node == null) {
       var p = DomNode.DocumentPosition.DISCONNECTED & DomNode.DocumentPosition.IMPLEMENTATION_SPECIFIC;
       if ((&this) > (&other))
         p = p & DomNode.DocumentPosition.PRECEDING;
@@ -244,21 +244,23 @@ public abstract class GXml.GNode : Object,
        p = p & DomNode.DocumentPosition.FOLLOWING;
       return p;
     }
-    if ((&other as GXml.Node).parent == &this)
-      return DomNode.DocumentPosition.CONTAINS & DomNode.DocumentPosition.PRECEDING;
-    var op = this.parent as DomNode;
-    if (&other == &op)
+    if ((this as DomNode).contains (other))
       return DomNode.DocumentPosition.CONTAINED_BY & DomNode.DocumentPosition.FOLLOWING;
-    if (&other < &this) return DomNode.DocumentPosition.PRECEDING;
-    return DomNode.DocumentPosition.FOLLOWING;
+    if (this.parent_node.contains (other)) {
+      var par = this.parent_node;
+      if (par.child_nodes.index_of (this) > par.child_nodes.index_of (other))
+        return DomNode.DocumentPosition.PRECEDING;
+      else
+        return DomNode.DocumentPosition.FOLLOWING;
+    }
+    if (other.contains (this))
+      return DomNode.DocumentPosition.CONTAINS & DomNode.DocumentPosition.PRECEDING;
+    GLib.warning (_("Can't find node position"));
+    return DomNode.DocumentPosition.NONE;
   }
   public bool contains (DomNode? other) {
     if (other == null) return false;
-    var o = other as GXml.Node;
-    if (&o == &this) return true;
-    var op = o.parent;
-    if (&this == &op) return true;
-    return false;
+    return this.child_nodes.contains (other);
   }
 
   public string? lookup_prefix (string? nspace) {
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index b9b04d4..c813a06 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -147,8 +147,41 @@ static const string HTMLDOC ="
                        assert (e.parent_node.text_content == "\n\n\n\n");
                        assert (e.parent_node.has_child_nodes ());
                        e.parent_node.normalize ();
-                       GLib.message ("Normalized: '"+e.parent_node.text_content+"' -> Node: 
"+(e.parent_element as GXml.Node).to_string ());
                        assert (e.parent_node.text_content == null);
+                       var cn = e.clone_node (false) as DomElement;
+                       assert (cn.node_name == "p");
+                       assert (cn.get_attribute ("id") == "p01");
+                       assert (cn.child_nodes != null);
+                       assert (cn.child_nodes.size == 0);
+                       var cn2 = e.clone_node (true) as DomElement;
+                       assert (cn2.node_name == "p");
+                       assert (cn2.get_attribute ("id") == "p01");
+                       assert (cn2.child_nodes != null);
+                       assert (cn2.child_nodes.size == 1);
+                       assert (cn2.child_nodes[0] is DomText);
+                       assert (cn2.text_content == "p01 p id");
+                       assert (!e.is_equal_node (cn));
+                       assert (e.is_equal_node (cn2));
+                       var e0 = doc.document_element.children[0].children[0];
+                       assert (e0.node_name == "p");
+                       assert (doc.document_element.children[0].child_nodes.index_of (e0) == 0);
+                       var e1 = doc.document_element.children[0].children[1];
+                       assert (e1.node_name == "p");
+                       assert (doc.document_element.children[0].child_nodes.index_of (e1) == 1);
+                       var e2 = doc.document_element.children[0].children[2];
+                       assert (e2.node_name == "p");
+                       assert (doc.document_element.children[0].child_nodes.index_of (e2) == 2);
+                       assert (e.parent_node.contains (e0));
+                       assert (e.parent_node.contains (e1));
+                       assert (e.parent_node.contains (e2));
+                       assert (e1.parent_node.contains (e0) && e0.parent_node.contains (e1));
+                       assert (e1.compare_document_position (e0) == DomNode.DocumentPosition.PRECEDING);
+                       assert (e1.parent_node.contains (e2) && e2.parent_node.contains (e1));
+                       assert (e1.compare_document_position (e2) == DomNode.DocumentPosition.FOLLOWING);
+                       assert (e1.parent_node.contains (e1));
+                       assert (e1.compare_document_position (e1.parent_node) == 
(DomNode.DocumentPosition.CONTAINS & DomNode.DocumentPosition.FOLLOWING));
+                       assert (e1.contains (e1.child_nodes[0]));
+                       assert (e1.compare_document_position (e1.child_nodes[0]) == 
(DomNode.DocumentPosition.CONTAINED_BY & DomNode.DocumentPosition.PRECEDING));
                });
        }
 }


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