[gxml] Added Unit Tests for GText's implementation of DomText: BUG on libxml2



commit 4752685a9555f7b3cdf11c4d95e1c0aad69b8a20
Author: Daniel Espinosa <esodan gmail com>
Date:   Thu Jul 21 21:51:52 2016 -0500

    Added Unit Tests for GText's implementation of DomText: BUG on libxml2
    
    * libxml2 doesn't support continuos text nodes, it concatenates
      all data in just one text node when added new ones.

 gxml/DomCharacter.vala     |   24 ++++++++++++++++++++++--
 gxml/GXmlText.vala         |   22 ----------------------
 test/DomGDocumentTest.vala |   22 ++++++++++++++++++++++
 3 files changed, 44 insertions(+), 24 deletions(-)
---
diff --git a/gxml/DomCharacter.vala b/gxml/DomCharacter.vala
index f6ba13a..f617da7 100644
--- a/gxml/DomCharacter.vala
+++ b/gxml/DomCharacter.vala
@@ -62,8 +62,28 @@ public interface GXml.DomCharacterData : GLib.Object,
 }
 
 public interface GXml.DomText : GXml.DomCharacterData {
-  public abstract GXml.DomText split_text(ulong offset) throws GLib.Error;
-  public abstract string whole_text { owned get; }
+  public virtual GXml.DomText split_text (ulong offset) throws GLib.Error {
+    if (offset >= data.length)
+      throw new DomError.INDEX_SIZE_ERROR (_("Invalid offset to split text"));
+    var nt = this.owner_document.create_text_node (this.data.slice ((int)offset, this.data.length));
+    this.delete_data (offset, this.data.length);
+    var n = this.parent_node.append_child (nt) as DomText;
+    // This will not work with libxml2 library as it doesn't support
+    // continuos text nodes, then it just concatecate text nodes data
+    // as added to parent
+    return n;
+  }
+  public virtual string whole_text {
+    owned get {
+      string s = "";
+      if (this.previous_sibling is DomText)
+        s += (this.previous_sibling as DomText).whole_text;
+      s += data;
+      if (this.next_sibling is DomText)
+        s += (this.next_sibling as DomText).whole_text;
+      return s;
+    }
+  }
 }
 
 public interface GXml.DomProcessingInstruction : GXml.DomCharacterData {
diff --git a/gxml/GXmlText.vala b/gxml/GXmlText.vala
index 35701bd..34ebaff 100644
--- a/gxml/GXmlText.vala
+++ b/gxml/GXmlText.vala
@@ -36,26 +36,4 @@ public class GXml.GText : GXml.GCharacterData, GXml.Text, GXml.DomText
       return "#text".dup ();
     }
   }
-  // GXml.DomText
-  public GXml.DomText split_text(ulong offset) throws GLib.Error {
-    if (offset >= data.length)
-      throw new DomError.INDEX_SIZE_ERROR (_("Invalid offset to split text"));
-    long l = (long) offset;
-    string ns = data[0:l];
-    string nd = data[data.length - l: data.length];
-    var nt = this.owner_document.create_text_node (ns);
-    (this.parent_node.child_nodes as Gee.List<DomNode>).insert (this.parent_node.child_nodes.index_of 
(this), nt);
-    return nt;
-  }
-  public string whole_text {
-    owned get {
-      string s = "";
-      if (this.previous_sibling is DomText)
-        s += (this.previous_sibling as DomText).whole_text;
-      s += data;
-      if (this.next_sibling is DomText)
-        s += (this.next_sibling as DomText).whole_text;
-      return s;
-    }
-  }
 }
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index b5dce9d..df3495c 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -510,6 +510,28 @@ static const string XMLDOC ="<?xml version=\"1.0\"?>
                                assert (t.data == "TEXT HI");
                                t.replace_data (0, 4, "text");
                                assert (t.data == "text HI");
+                               var n = d.create_element ("node");
+                               d.append_child (n);
+                               n.append_child (t);
+                               assert (t.parent_node.child_nodes.length == 1);
+                               var t2 = (t as DomText).split_text (4);
+                               assert (t.parent_node.child_nodes.length == 1);
+                               var ntst = d.create_element ("child");
+                               n.append_child (ntst);
+                               var ct1 = d.create_text_node ("TEXT1");
+                               ntst.append_child (ct1);
+                               assert (ntst.child_nodes.length == 1);
+                               var ct2 = d.create_text_node ("TEXT2");
+                               ntst.append_child (ct2);
+                               //assert (ntst.child_nodes.length == 2);
+                               // BUG: libxml2 doesn't support continuos DomText nodes
+                               // when it is added, its data is concatecated in just text
+                               // node
+                               GLib.message ("NTST: "+(ntst as GXml.Node).to_string ());
+                               assert (ntst.child_nodes.item (0) is DomText);
+                               assert ((ntst.child_nodes.item (0) as DomText).data == "TEXT1TEXT2");
+                               // BUG: DomText.whole_text
+                               assert ((ntst.child_nodes.item(0) as DomText).whole_text == "TEXT1TEXT2");
                        } catch (GLib.Error e) {
                                GLib.message ("Error: "+ e.message);
                                assert_not_reached ();


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