[lasem] dom_node_list: implementation.



commit 72a582d96643f6304990edad48844af961f51755
Author: Emmanuel Pacaud <emmanuel gnome org>
Date:   Sun Nov 14 19:07:43 2010 +0100

    dom_node_list: implementation.

 docs/reference/lasem/lasem-docs.xml     |    1 +
 docs/reference/lasem/lasem-sections.txt |   18 ++++++
 src/lsmdomnode.c                        |   11 ++--
 src/lsmdomnode.h                        |   34 ++++------
 src/lsmdomnodelist.c                    |  101 ++++++++++++++++++++++++++++++-
 src/lsmdomnodelist.h                    |   32 +++++++++-
 src/lsmdomtypes.h                       |    1 +
 tests/dom.c                             |   22 +++++++
 8 files changed, 190 insertions(+), 30 deletions(-)
---
diff --git a/docs/reference/lasem/lasem-docs.xml b/docs/reference/lasem/lasem-docs.xml
index 8540190..b080ef8 100644
--- a/docs/reference/lasem/lasem-docs.xml
+++ b/docs/reference/lasem/lasem-docs.xml
@@ -39,6 +39,7 @@
 		<chapter>
 			<title>DOM</title>
 			<xi:include href="xml/lsmdomnode.xml"/>
+			<xi:include href="xml/lsmdomnodelist.xml"/>
 			<xi:include href="xml/lsmdomdocument.xml"/>
 			<xi:include href="xml/lsmdomelement.xml"/>
 			<xi:include href="xml/lsmdomcharacterdata.xml"/>
diff --git a/docs/reference/lasem/lasem-sections.txt b/docs/reference/lasem/lasem-sections.txt
index 770b933..cad8e43 100644
--- a/docs/reference/lasem/lasem-sections.txt
+++ b/docs/reference/lasem/lasem-sections.txt
@@ -51,6 +51,24 @@ lsm_dom_node_write_to_stream
 </SECTION>
 
 <SECTION>
+<FILE>lsmdomnodelist</FILE>
+<TITLE>LsmDomNodeList</TITLE>
+lsm_dom_node_list_get_item
+lsm_dom_node_list_get_length
+lsm_dom_node_list_get_type
+lsm_dom_node_list_new
+<SUBSECTION Standard>
+LsmDomNodeList
+LsmDomNodeListClass
+LSM_DOM_NODE_LIST
+LSM_DOM_NODE_LIST_CLASS
+LSM_DOM_NODE_LIST_GET_CLASS
+LSM_IS_DOM_NODE_LIST
+LSM_IS_DOM_NODE_LIST_CLASS
+LSM_TYPE_DOM_NODE_LIST
+</SECTION>
+
+<SECTION>
 <FILE>lsmdomdocument</FILE>
 <TITLE>LsmDomDocument</TITLE>
 LSM_DOM_DOCUMENT_DEFAULT_RESOLUTION
diff --git a/src/lsmdomnode.c b/src/lsmdomnode.c
index 673f942..9f3eee4 100644
--- a/src/lsmdomnode.c
+++ b/src/lsmdomnode.c
@@ -27,6 +27,7 @@
  */
 
 #include <lsmdomnode.h>
+#include <lsmdomnodelist.h>
 #include <lsmdomdocument.h>
 #include <lsmdebug.h>
 #include <glib/gprintf.h>
@@ -111,11 +112,11 @@ lsm_dom_node_get_parent_node (LsmDomNode* self)
 	return self->parent_node;
 }
 
-/*LsmDomNodeList**/
-/*lsm_dom_node_get_child_nodes (LsmDomNode* self)*/
-/*{*/
-/*        return LSM_DOM_NODE_GET_CLASS (self)->get_child_nodes (self);*/
-/*}*/
+LsmDomNodeList*
+lsm_dom_node_get_child_nodes (LsmDomNode* self)
+{
+	return lsm_dom_node_list_new (self);
+}
 
 LsmDomNode*
 lsm_dom_node_get_first_child (LsmDomNode* self)
diff --git a/src/lsmdomnode.h b/src/lsmdomnode.h
index e8c5bf4..1e4498d 100644
--- a/src/lsmdomnode.h
+++ b/src/lsmdomnode.h
@@ -72,10 +72,6 @@ struct _LsmDomNodeClass {
 	const char* 		(*get_node_value) 	(LsmDomNode* self);
 	void 			(*set_node_value) 	(LsmDomNode* self, const char* new_value);
 	LsmDomNodeType 		(*get_node_type) 	(LsmDomNode* self);
-#if 0
-	LsmDomNodeList* 	(*get_child_nodes) (LsmDomNode* self);
-	LsmDomNamedNodeMap* 	(*get_attributes) (LsmDomNode* self);
-#endif
 
 	/* Validation virtuals */
 
@@ -93,30 +89,28 @@ struct _LsmDomNodeClass {
 
 GType lsm_dom_node_get_type (void);
 
-const char* 		lsm_dom_node_get_node_name 		(LsmDomNode* self);
-const char* 		lsm_dom_node_get_node_value 		(LsmDomNode* self);
+const char * 		lsm_dom_node_get_node_name 		(LsmDomNode* self);
+const char * 		lsm_dom_node_get_node_value 		(LsmDomNode* self);
 void 			lsm_dom_node_set_node_value 		(LsmDomNode* self, const char* new_value);
 LsmDomNodeType 		lsm_dom_node_get_node_type 		(LsmDomNode* self);
-LsmDomNode* 		lsm_dom_node_get_parent_node 		(LsmDomNode* self);
-#if 0
-LsmDomNodeList* 		lsm_dom_node_get_child_nodes 	(LsmDomNode* self);
-#endif
-LsmDomNode* 		lsm_dom_node_get_first_child 		(LsmDomNode* self);
-LsmDomNode* 		lsm_dom_node_get_last_child 		(LsmDomNode* self);
-LsmDomNode* 		lsm_dom_node_get_previous_sibling 	(LsmDomNode* self);
-LsmDomNode* 		lsm_dom_node_get_next_sibling 		(LsmDomNode* self);
+LsmDomNode * 		lsm_dom_node_get_parent_node 		(LsmDomNode* self);
+LsmDomNodeList *	lsm_dom_node_get_child_nodes 		(LsmDomNode* self);
+LsmDomNode * 		lsm_dom_node_get_first_child 		(LsmDomNode* self);
+LsmDomNode * 		lsm_dom_node_get_last_child 		(LsmDomNode* self);
+LsmDomNode * 		lsm_dom_node_get_previous_sibling 	(LsmDomNode* self);
+LsmDomNode * 		lsm_dom_node_get_next_sibling 		(LsmDomNode* self);
 #if 0
-LsmDomNamedNodeMap* 	lsm_dom_node_get_attributes 	(LsmDomNode* self);
+LsmDomNamedNodeMap * 	lsm_dom_node_get_attributes 		(LsmDomNode* self);
 #endif
-LsmDomNode* 		lsm_dom_node_insert_before		(LsmDomNode* self, LsmDomNode* new_child, LsmDomNode* ref_child);
-LsmDomNode* 		lsm_dom_node_replace_child 		(LsmDomNode* self, LsmDomNode* new_child, LsmDomNode* old_child);
-LsmDomNode* 		lsm_dom_node_append_child 		(LsmDomNode* self, LsmDomNode* new_child);
-LsmDomNode* 		lsm_dom_node_remove_child 		(LsmDomNode* self, LsmDomNode* old_child);
+LsmDomNode * 		lsm_dom_node_insert_before		(LsmDomNode* self, LsmDomNode* new_child, LsmDomNode* ref_child);
+LsmDomNode * 		lsm_dom_node_replace_child 		(LsmDomNode* self, LsmDomNode* new_child, LsmDomNode* old_child);
+LsmDomNode * 		lsm_dom_node_append_child 		(LsmDomNode* self, LsmDomNode* new_child);
+LsmDomNode * 		lsm_dom_node_remove_child 		(LsmDomNode* self, LsmDomNode* old_child);
 gboolean 		lsm_dom_node_has_child_nodes 		(LsmDomNode* self);
 
 void 			lsm_dom_node_changed 			(LsmDomNode *self);
 
-LsmDomDocument*		lsm_dom_node_get_owner_document 	(LsmDomNode* self);
+LsmDomDocument *	lsm_dom_node_get_owner_document 	(LsmDomNode* self);
 
 void			lsm_dom_node_write_to_stream		(LsmDomNode *self, GOutputStream *stream,
 								 GError **error);
diff --git a/src/lsmdomnodelist.c b/src/lsmdomnodelist.c
index b6f4117..c8a012a 100644
--- a/src/lsmdomnodelist.c
+++ b/src/lsmdomnodelist.c
@@ -1,6 +1,6 @@
-/* lsmdomnodelist.c
+/* Lasem - SVG and Mathml library
  *
- * Copyright © 2007-2008  Emmanuel Pacaud
+ * Copyright © 2010 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -22,3 +22,100 @@
  */
 
 #include <lsmdomnodelist.h>
+#include <lsmdomnode.h>
+
+static GObjectClass *parent_class;
+
+/* LsmDomNodeList implementation */
+
+static void
+lsm_dom_node_list_weak_notify_cb (void *user_data, GObject *object)
+{
+	LsmDomNodeList *list = user_data;
+
+	list->parent_node = NULL;
+}
+
+LsmDomNodeList *
+lsm_dom_node_list_new (LsmDomNode *parent_node)
+{
+	LsmDomNodeList *list;
+
+	g_return_val_if_fail (LSM_IS_DOM_NODE (parent_node), NULL);
+
+	list = g_object_new (LSM_TYPE_DOM_NODE_LIST, NULL);
+	list->parent_node = parent_node;
+
+	g_object_weak_ref (G_OBJECT (parent_node), lsm_dom_node_list_weak_notify_cb, list);
+
+	return list;
+}
+
+LsmDomNode *
+lsm_dom_node_list_get_item (LsmDomNodeList *list, unsigned int index)
+{
+	LsmDomNode *iter;
+	unsigned int i = 0;
+
+	g_return_val_if_fail (LSM_IS_DOM_NODE_LIST (list), NULL);
+
+	if (list->parent_node == NULL)
+		return NULL;
+
+	for (iter = list->parent_node->first_child; iter != NULL; iter = iter->next_sibling) {
+		if (i == index)
+			return iter;
+		i++;
+	}
+
+	return NULL;
+}
+
+unsigned int
+lsm_dom_node_list_get_length (LsmDomNodeList *list)
+{
+	LsmDomNode *iter;
+	unsigned int length = 0;
+
+	g_return_val_if_fail (LSM_IS_DOM_NODE_LIST (list), 0);
+
+	if (list->parent_node == NULL)
+		return 0;
+
+	for (iter = list->parent_node->first_child; iter != NULL; iter = iter->next_sibling)
+		length++;
+
+	return length;
+}
+
+static void
+lsm_dom_node_list_init (LsmDomNodeList *list)
+{
+}
+
+static void
+lsm_dom_node_list_finalize (GObject *object)
+{
+	LsmDomNodeList *list = LSM_DOM_NODE_LIST (object);
+
+	if (list->parent_node != NULL) {
+		g_object_weak_unref (G_OBJECT (list->parent_node), lsm_dom_node_list_weak_notify_cb, list);
+		list->parent_node = NULL;
+	}
+
+	parent_class->finalize (object);
+}
+
+/* LsmDomNodeList class */
+
+static void
+lsm_dom_node_list_class_init (LsmDomNodeListClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+	parent_class = g_type_class_peek_parent (klass);
+
+	object_class->finalize = lsm_dom_node_list_finalize;
+}
+
+G_DEFINE_TYPE (LsmDomNodeList, lsm_dom_node_list, G_TYPE_OBJECT)
diff --git a/src/lsmdomnodelist.h b/src/lsmdomnodelist.h
index da5116d..462ed9e 100644
--- a/src/lsmdomnodelist.h
+++ b/src/lsmdomnodelist.h
@@ -1,6 +1,6 @@
-/* lsmdomnodelist.h
+/* Lasem - SVG and Mathml library
  *
- * Copyright © 2007-2008  Emmanuel Pacaud
+ * Copyright © 2010 Emmanuel Pacaud
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,10 +24,36 @@
 #ifndef LSM_DOM_NODE_LIST_H
 #define LSM_DOM_NODE_LIST_H
 
-#include <lsmdom.h>
+#include <lsmdomtypes.h>
 
 G_BEGIN_DECLS
 
+#define LSM_TYPE_DOM_NODE_LIST             (lsm_dom_node_list_get_type ())
+#define LSM_DOM_NODE_LIST(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), LSM_TYPE_DOM_NODE_LIST, LsmDomNodeList))
+#define LSM_DOM_NODE_LIST_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), LSM_TYPE_DOM_NODE_LIST, LsmDomNodeListClass))
+#define LSM_IS_DOM_NODE_LIST(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), LSM_TYPE_DOM_NODE_LIST))
+#define LSM_IS_DOM_NODE_LIST_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), LSM_TYPE_DOM_NODE_LIST))
+#define LSM_DOM_NODE_LIST_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS((obj), LSM_TYPE_DOM_NODE_LIST, LsmDomNodeListClass))
+
+typedef struct _LsmDomNodeListClass LsmDomNodeListClass;
+
+struct _LsmDomNodeList {
+	GObject	object;
+
+	LsmDomNode *parent_node;
+};
+
+struct _LsmDomNodeListClass {
+	GObjectClass parent_class;
+};
+
+GType lsm_dom_node_list_get_type (void);
+
+LsmDomNodeList *	lsm_dom_node_list_new			(LsmDomNode *parent_node);
+
+LsmDomNode *		lsm_dom_node_list_get_item 		(LsmDomNodeList *list, unsigned int index);
+unsigned int		lsm_dom_node_list_get_length		(LsmDomNodeList *list);
+
 G_END_DECLS
 
 #endif
diff --git a/src/lsmdomtypes.h b/src/lsmdomtypes.h
index b52efae..f922751 100644
--- a/src/lsmdomtypes.h
+++ b/src/lsmdomtypes.h
@@ -29,6 +29,7 @@
 G_BEGIN_DECLS
 
 typedef struct _LsmDomNode LsmDomNode;
+typedef struct _LsmDomNodeList LsmDomNodeList;
 typedef struct _LsmDomElement LsmDomElement;
 typedef struct _LsmDomDocument LsmDomDocument;
 typedef struct _LsmDomCharacterData LsmDomCharacterData;
diff --git a/tests/dom.c b/tests/dom.c
index 929609f..8e05685 100644
--- a/tests/dom.c
+++ b/tests/dom.c
@@ -78,6 +78,27 @@ add_remove_element_test (void)
 	g_object_unref (document);
 }
 
+static void
+node_list_test (void)
+{
+	LsmDomDocument *document;
+	LsmDomElement *element;
+	LsmDomNodeList *childs;
+
+	document = lsm_dom_implementation_create_document ("svg");
+	element = lsm_dom_document_create_element (document, "svg");
+	lsm_dom_node_append_child (LSM_DOM_NODE (document), LSM_DOM_NODE (element));
+
+	childs = lsm_dom_node_get_child_nodes (LSM_DOM_NODE (document));
+	g_assert (LSM_IS_DOM_NODE_LIST (childs));
+	g_assert (lsm_dom_node_list_get_length (childs) == 1);
+	g_assert (lsm_dom_node_list_get_item (childs, 0) == LSM_DOM_NODE (element));
+	g_assert (lsm_dom_node_list_get_item (childs, 1) == NULL);
+
+	g_object_unref (childs);
+	g_object_unref (document);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -88,6 +109,7 @@ main (int argc, char *argv[])
 	g_test_add_func ("/dom/create-document", create_document_test);
 	g_test_add_func ("/dom/create-element", create_document_test);
 	g_test_add_func ("/dom/add-remove-element", create_document_test);
+	g_test_add_func ("/dom/node-list", node_list_test);
 
 	g_type_init ();
 



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