[gxml] StreamReader: can read children's children



commit 2c32669ad2d424ca9ba1f1afd8f965ce67ecff0c
Author: Daniel Espinosa <esodan gmail com>
Date:   Mon Jul 22 14:44:33 2019 -0500

    StreamReader: can read children's children

 gxml/StreamReader.vala     |  9 ++++-----
 test/StreamReaderTest.vala | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+), 5 deletions(-)
---
diff --git a/gxml/StreamReader.vala b/gxml/StreamReader.vala
index 5df379b..b4f8226 100644
--- a/gxml/StreamReader.vala
+++ b/gxml/StreamReader.vala
@@ -171,18 +171,17 @@ public class GXml.StreamReader : GLib.Object {
       atts += (string) buf;
       buf[0] = (char) stream.read_byte (cancellable);
     }
+    var e = document.create_element (name);
+    parent.append_child (e);
     message ("Element's attributes found: %s", atts);
     str = "<"+name+atts;
+    str += ">";
     if (atts[atts.length - 1] == '/') {
-     str += "/>";
+      (e as Element).unparsed = str;
      return str;
-    } else {
-      str += ">";
     }
     message ("Element's declaration head: %s", str);
     message ("Current: %s", (string) buf);
-    var e = document.create_element (name);
-    parent.append_child (e);
     while (true) {
       string content = "";
       buf[0] = (char) stream.read_byte (cancellable);
diff --git a/test/StreamReaderTest.vala b/test/StreamReaderTest.vala
index 480398d..13cb95f 100644
--- a/test/StreamReaderTest.vala
+++ b/test/StreamReaderTest.vala
@@ -54,6 +54,27 @@ class GXmlTest {
                                warning ("Error: %s", e.message);
                        }
                });
+               Test.add_func ("/gxml/stream-reader/child-multiple", () => {
+                       string str = """<root p1="a" p2="b" ><child k="p" y="9"><code/><code 
u="3">TestC</code><Tek/><Tex y="456"/></child></root>""";
+                       var istream = new MemoryInputStream.from_data (str.data, null);
+                       var sr = new StreamReader (istream);
+                       try {
+                               var doc = sr.read ();
+                               message (doc.write_string ());
+                               message ((doc.document_element as GXml.Element).unparsed);
+                               message ((doc.document_element.child_nodes.item (0) as 
GXml.Element).unparsed);
+                               assert ((doc.document_element as GXml.Element).unparsed == """<root p1="a" 
p2="b" ></root>""");
+                               assert ((doc.document_element.child_nodes.item(0) as GXml.Element).unparsed 
== """<child k="p" y="9"><code/><code u="3">TestC</code><Tek/><Tex y="456"/></child>""");
+                               var cchilds = doc.document_element.child_nodes.item(0).child_nodes;
+                               message ("Element childs: %d", cchilds.length);
+                               assert ((cchilds.item (0) as GXml.Element).unparsed == null);
+                               assert ((cchilds.item (1) as GXml.Element).unparsed == """<code 
u="3">TestC</code>""");
+                               assert ((cchilds.item (2) as GXml.Element).unparsed == null);
+                               assert ((cchilds.item (3) as GXml.Element).unparsed == """<Tex y="456"/>""");
+                       } catch (GLib.Error e) {
+                               warning ("Error: %s", e.message);
+                       }
+               });
                Test.run ();
 
                return 0;


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