[gxml] More fixes on DomNode implementation and Unit Tests
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] More fixes on DomNode implementation and Unit Tests
- Date: Tue, 19 Jul 2016 15:16:19 +0000 (UTC)
commit 8f4e1bedf724dde41a905fae2024689f694f0bb8
Author: Daniel Espinosa <esodan gmail com>
Date: Tue Jul 19 10:12:32 2016 -0500
More fixes on DomNode implementation and Unit Tests
* Implemented GListChildren.remove_at()
* GListChildren get_element() checks index boundaries
* Fixed DomNode.normalize() implementation on GNode
* Added tests for DomNode implementations
gxml/DomCharacter.vala | 7 ++++++-
gxml/DomCollections.vala | 2 +-
gxml/DomElement.vala | 4 ++--
gxml/Element.vala | 4 ++--
gxml/GXmlDomCollections.vala | 2 +-
gxml/GXmlListChildren.vala | 16 +++++++++++-----
gxml/GXmlNode.vala | 19 +++----------------
test/DomGDocumentTest.vala | 32 ++++++++++++++++++++++++++++++++
8 files changed, 58 insertions(+), 28 deletions(-)
---
diff --git a/gxml/DomCharacter.vala b/gxml/DomCharacter.vala
index 446848d..b530199 100644
--- a/gxml/DomCharacter.vala
+++ b/gxml/DomCharacter.vala
@@ -30,7 +30,12 @@ public interface GXml.DomCharacterData : GLib.Object,
*/
public abstract string data { owned get; set; }
- public virtual ulong length { get { return this.data.length; } }
+ public virtual ulong length {
+ get {
+ if (this.data == null) return 0;
+ return this.data.length;
+ }
+ }
public virtual string substring_data (ulong offset, ulong count) throws GLib.Error {
if (((int)offset) > this.data.length)
throw new DomError.INDEX_SIZE_ERROR (_("Invalid offset for substring"));
diff --git a/gxml/DomCollections.vala b/gxml/DomCollections.vala
index c0cce20..5448589 100644
--- a/gxml/DomCollections.vala
+++ b/gxml/DomCollections.vala
@@ -51,7 +51,7 @@ public interface GXml.DomNodeList : GLib.Object, Gee.BidirList<GXml.DomNode> {
}
public interface GXml.DomHTMLCollection : GLib.Object, Gee.BidirList<GXml.DomElement> {
- public abstract new GXml.DomElement get_element (int index); // FIXME: See bug #768913
+ public abstract new GXml.DomElement? get_element (int index); // FIXME: See bug #768913
public virtual new GXml.DomElement[] to_array () {
return (GXml.DomElement[]) ((Gee.Collection<GXml.Element>) this).to_array ();
}
diff --git a/gxml/DomElement.vala b/gxml/DomElement.vala
index e232f2d..fd74379 100644
--- a/gxml/DomElement.vala
+++ b/gxml/DomElement.vala
@@ -54,7 +54,7 @@ public interface GXml.DomElement : GLib.Object,
public class GXml.DomElementList : Gee.ArrayList<DomElement>, GXml.DomHTMLCollection {
// DomHTMLCollection
- public new GXml.DomElement get_element (int index) {
- return (GXml.DomElement) this.get (index);
+ public new GXml.DomElement? get_element (int index) {
+ return (GXml.DomElement?) this.get (index);
}
}
diff --git a/gxml/Element.vala b/gxml/Element.vala
index 97a2e97..b925c7a 100644
--- a/gxml/Element.vala
+++ b/gxml/Element.vala
@@ -82,7 +82,7 @@ public interface GXml.Element : Object, GXml.Node
*/
public class GXml.ElementList : ArrayList<Element>, GXml.DomHTMLCollection {
// DomHTMLCollection
- public new GXml.DomElement get_element (int index) {
- return (GXml.DomElement) this.get (index);
+ public new GXml.DomElement? get_element (int index) {
+ return (GXml.DomElement?) this.get (index);
}
}
diff --git a/gxml/GXmlDomCollections.vala b/gxml/GXmlDomCollections.vala
index 1435f4c..0d43e90 100644
--- a/gxml/GXmlDomCollections.vala
+++ b/gxml/GXmlDomCollections.vala
@@ -128,7 +128,7 @@ public class GXml.GDomHTMLCollection : Gee.ArrayList<GXml.DomElement>,
return null;
}
// DomHTMLCollection
- public new GXml.DomElement get_element (int index) {
+ public new GXml.DomElement? get_element (int index) {
return (GXml.DomElement) this.get (index);
}
}
diff --git a/gxml/GXmlListChildren.vala b/gxml/GXmlListChildren.vala
index 9c3edd7..4f6e03a 100644
--- a/gxml/GXmlListChildren.vala
+++ b/gxml/GXmlListChildren.vala
@@ -75,13 +75,16 @@ public class GXml.GListChildren : AbstractBidirList<GXml.Node>,
}
public override Gee.ListIterator<GXml.Node> list_iterator () { return new Iterator (_doc, _node); }
/**
- * Removes a node at @index
+ * Removes a node at @index. This method never returns a valid pointer.
*/
public override GXml.Node remove_at (int index) {
+ if (index > size || index < 0) return null;
var n = @get (index);
if (n == null) return null;
- (n as GXml.GNode).get_internal_node ()->unlink ();
- return n;
+ var np = (n as GXml.GNode).get_internal_node ();
+ np->unlink ();
+ delete np;
+ return null;
}
/**
* This method is ignored by default.
@@ -254,8 +257,11 @@ public class GXml.GListChildren : AbstractBidirList<GXml.Node>,
public DomNode? item (ulong index) { return (DomNode) @get ((int) index); }
public ulong length { get { return (ulong) size; } }
// DomHTMLCollection
- public new GXml.DomElement get_element (int index) {
- return (GXml.DomElement) this.get (index);
+ public new GXml.DomElement? get_element (int index) {
+ if (index > this.size || index < 0) return null;
+ var e = this.get (index);
+ if (!(e is DomNode)) return null;
+ return (GXml.DomElement) e;
}
}
diff --git a/gxml/GXmlNode.vala b/gxml/GXmlNode.vala
index 45ddd01..75d63fd 100644
--- a/gxml/GXmlNode.vala
+++ b/gxml/GXmlNode.vala
@@ -202,26 +202,13 @@ public abstract class GXml.GNode : Object,
public bool has_child_nodes () { return (children_nodes.size > 0); }
public void normalize () {
- GXml.DomText t = null;
- int[] r = {};
+ GXml.DomText t = owner_document.create_text_node (text_content);
for (int i = 0; i < children_nodes.size; i++) {
var n = children_nodes.get (i);
- if (n is GXml.DomText) {
- if ((t as GXml.DomText).length == 0) {
- r += i;
- continue;
- }
- if (t == null) {
- t = (GXml.DomText) n;
- continue;
- } else {
- t.data += n.value;
- }
+ if (n is DomText) {
+ child_nodes.remove_at (i);
}
}
- foreach (int j in r) {
- children_nodes.remove_at (j);
- }
}
public DomNode clone_node (bool deep = false) {
diff --git a/test/DomGDocumentTest.vala b/test/DomGDocumentTest.vala
index cbb965d..b9b04d4 100644
--- a/test/DomGDocumentTest.vala
+++ b/test/DomGDocumentTest.vala
@@ -118,5 +118,37 @@ static const string HTMLDOC ="
assert (lc[1].node_name == "p");
assert (lc[1].get_attribute ("class") == "black block");
});
+ Test.add_func ("/gxml/dom/node", () => {
+ GLib.message ("Doc: "+HTMLDOC);
+ GDocument doc = new GDocument.from_string (HTMLDOC);
+ assert (doc is DomDocument);
+ assert (doc.document_element.children.size == 1);
+ assert (doc.document_element.children[0] != null);
+ assert (doc.document_element.children[0].children[1] != null);
+ var e = doc.document_element.children[0].children[1];
+ assert (e.owner_document == (DomDocument) doc);
+ assert (e.parent_node != null);
+ assert (e.parent_node.node_name == "body");
+ assert (e.parent_element != null);
+ assert (e.parent_element.node_name == "body");
+ assert (e.child_nodes != null);
+ assert (e.child_nodes.size == 1);
+ assert (e.child_nodes.item (0) != null);
+ assert (e.child_nodes.item (0) is DomText);
+ assert (e.previous_sibling != null);
+ assert (e.previous_sibling is DomText);
+ assert (e.next_sibling != null);
+ assert (e.next_sibling is DomText);
+ var t = e.child_nodes.item (0);
+ assert (t.previous_sibling == null);
+ assert (t.next_sibling == null);
+ assert (t.text_content != null);
+ assert (t.text_content == "p01 p id");
+ 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);
+ });
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]