[gxml] GChildNode next/previouse element sibling using libxml2
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] GChildNode next/previouse element sibling using libxml2
- Date: Mon, 4 Sep 2017 04:33:50 +0000 (UTC)
commit 2449699c6537956c287255f1727293824f61e3c5
Author: Daniel Espinosa <esodan gmail com>
Date: Sat Sep 2 22:27:33 2017 -0500
GChildNode next/previouse element sibling using libxml2
libxml2 has previous/next_element_sibling methods, so
using them instead.
One of more Fixes for Bug #785279
gxml/GHtml.vala | 1 +
gxml/GXmlChildNode.vala | 30 ++++++++----------------------
test/GElementTest.vala | 31 +++++++++++++++++++++++++++++++
3 files changed, 40 insertions(+), 22 deletions(-)
---
diff --git a/gxml/GHtml.vala b/gxml/GHtml.vala
index d315ea7..810c3c1 100644
--- a/gxml/GHtml.vala
+++ b/gxml/GHtml.vala
@@ -23,6 +23,7 @@
*/
using Gee;
+using Xml;
namespace GXml {
/**
diff --git a/gxml/GXmlChildNode.vala b/gxml/GXmlChildNode.vala
index 19de0e3..efef54f 100644
--- a/gxml/GXmlChildNode.vala
+++ b/gxml/GXmlChildNode.vala
@@ -42,32 +42,18 @@ public class GXml.GNonDocumentChildNode : GXml.GChildNode,
// DomNonDocumentTypeChildNode
public DomElement? previous_element_sibling {
owned get {
- if (parent_node != null) {
- var i = parent_node.child_nodes.index_of (this);
- if (i == 0)
- return null;
- for (var j = i; j >= 1; j--) {
- var n = parent_node.child_nodes.item (j - 1);
- if (n is DomElement)
- return n as DomElement;
- }
- }
- return null;
+ if (_node == null) return null;
+ var n = _node->previous_element_sibling ();
+ if (n == null) return null;
+ return new GElement (owner_document as GDocument, n);
}
}
public DomElement? next_element_sibling {
owned get {
- if (parent_node != null) {
- var i = parent_node.child_nodes.index_of (this);
- if (i == parent_node.child_nodes.length - 1)
- return null;
- for (var j = i; j < parent_node.child_nodes.length - 1; j--) {
- var n = parent_node.child_nodes.item (j + 1);
- if (n is DomElement)
- return (DomElement) n;
- }
- }
- return null;
+ if (_node == null) return null;
+ var n = _node->next_element_sibling ();
+ if (n == null) return null;
+ return new GElement (owner_document as GDocument, n);
}
}
}
diff --git a/test/GElementTest.vala b/test/GElementTest.vala
index 139510e..0a9be27 100644
--- a/test/GElementTest.vala
+++ b/test/GElementTest.vala
@@ -157,5 +157,36 @@ class GElementTest : GXmlTest {
assert_not_reached ();
}
});
+ Test.add_func ("/gxml/gelement/previous_element_sibling", () => {
+ try {
+ var doc = new GDocument.from_string ("<root> <child/> <child/></root>");
+ assert (doc.document_element != null);
+ assert (doc.document_element.parent_node is GXml.Node);
+ assert (doc.document_element.parent_node is GXml.Document);
+ assert (doc.document_element.child_nodes[0] != null);
+ assert (doc.document_element.child_nodes[0].parent_node != null);
+ assert (doc.document_element.child_nodes[0].parent_node.node_name == "root");
+ assert (doc.document_element.child_nodes[0] is DomText);
+ assert (doc.document_element.child_nodes[1] != null);
+ assert (doc.document_element.child_nodes[1] is DomElement);
+ assert (doc.document_element.child_nodes[1].node_name == "child");
+ assert ((doc.document_element.child_nodes[1] as
DomElement).next_element_sibling != null);
+ assert ((doc.document_element.child_nodes[1] as
DomElement).next_element_sibling is DomElement);
+ assert ((doc.document_element.child_nodes[1] as
DomElement).next_element_sibling.node_name == "child");
+ assert (doc.document_element.child_nodes[2] != null);
+ assert (doc.document_element.child_nodes[2].parent_node != null);
+ assert (doc.document_element.child_nodes[2].parent_node.node_name == "root");
+ assert (doc.document_element.child_nodes[2] is DomText);
+ assert (doc.document_element.child_nodes[3] != null);
+ assert (doc.document_element.child_nodes[3] is DomElement);
+ assert (doc.document_element.child_nodes[3].node_name == "child");
+ assert ((doc.document_element.child_nodes[3] as
DomElement).previous_element_sibling != null);
+ assert ((doc.document_element.child_nodes[3] as
DomElement).previous_element_sibling is DomElement);
+ assert ((doc.document_element.child_nodes[3] as
DomElement).previous_element_sibling.node_name == "child");
+ } catch (GLib.Error e) {
+ Test.message (e.message);
+ assert_not_reached ();
+ }
+ });
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]