summaryrefslogtreecommitdiff
path: root/src/core/namednodemap.c
diff options
context:
space:
mode:
authorJohn Mark Bell <jmb@netsurf-browser.org>2007-09-25 00:02:07 +0000
committerJohn Mark Bell <jmb@netsurf-browser.org>2007-09-25 00:02:07 +0000
commitba89caf25cfe662a29c4fa866fc0d440908d5545 (patch)
tree68a7efb856946deb63fb8e881078a04340b8ebd8 /src/core/namednodemap.c
parent7259d8fa11f3fc45adbfa9634c145ba58f7f6257 (diff)
downloadlibdom-ba89caf25cfe662a29c4fa866fc0d440908d5545.tar.gz
libdom-ba89caf25cfe662a29c4fa866fc0d440908d5545.tar.bz2
Add library-internal accessor to the head of an Element's attribute list (dom_element_get_first_attribute())
Implement dom_namednodemap_get_length() Implement dom_namednodemap_get_named_item() Implement dom_namednodemap_set_named_item() Implement dom_namednodemap_remove_named_item() Implement dom_namednodemap_item() All dom_namednodemap_*() still require support for maps of Entity and Notation nodes to be added (this requires DocumentType to gain support for these nodes). svn path=/trunk/dom/; revision=3591
Diffstat (limited to 'src/core/namednodemap.c')
-rw-r--r--src/core/namednodemap.c175
1 files changed, 156 insertions, 19 deletions
diff --git a/src/core/namednodemap.c b/src/core/namednodemap.c
index c049a6c..2d1fbc8 100644
--- a/src/core/namednodemap.c
+++ b/src/core/namednodemap.c
@@ -5,10 +5,14 @@
* Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
*/
+#include <dom/core/element.h>
#include <dom/core/node.h>
+#include <dom/core/string.h>
#include "core/document.h"
+#include "core/element.h"
#include "core/namednodemap.h"
+#include "core/node.h"
#include "utils/utils.h"
@@ -118,10 +122,29 @@ void dom_namednodemap_unref(struct dom_namednodemap *map)
dom_exception dom_namednodemap_get_length(struct dom_namednodemap *map,
unsigned long *length)
{
- UNUSED(map);
- UNUSED(length);
+ struct dom_node *cur;
+ unsigned long len = 0;
+
+ switch (map->type) {
+ case DOM_ATTRIBUTE_NODE:
+ cur = dom_element_get_first_attribute(
+ (struct dom_element *) map->head);
+ break;
+ case DOM_NOTATION_NODE:
+ case DOM_ENTITY_NODE:
+ /** \todo handle notation and entity nodes */
+ default:
+ return DOM_NOT_SUPPORTED_ERR;
+ break;
+ }
- return DOM_NOT_SUPPORTED_ERR;
+ for (; cur != NULL; cur = cur->next) {
+ len++;
+ }
+
+ *length = len;
+
+ return DOM_NO_ERR;
}
/**
@@ -138,11 +161,33 @@ dom_exception dom_namednodemap_get_length(struct dom_namednodemap *map,
dom_exception dom_namednodemap_get_named_item(struct dom_namednodemap *map,
struct dom_string *name, struct dom_node **node)
{
- UNUSED(map);
- UNUSED(name);
- UNUSED(node);
+ struct dom_node *cur;
+
+ switch (map->type) {
+ case DOM_ATTRIBUTE_NODE:
+ cur = dom_element_get_first_attribute(
+ (struct dom_element *) map->head);
+ break;
+ case DOM_NOTATION_NODE:
+ case DOM_ENTITY_NODE:
+ /** \todo handle notation and entity nodes */
+ default:
+ return DOM_NOT_SUPPORTED_ERR;
+ break;
+ }
- return DOM_NOT_SUPPORTED_ERR;
+ for (; cur != NULL; cur = cur->next) {
+ if (dom_string_cmp(cur->name, name) == 0) {
+ break;
+ }
+ }
+
+ if (cur != NULL) {
+ dom_node_ref(cur);
+ }
+ *node = cur;
+
+ return DOM_NO_ERR;
}
/**
@@ -172,11 +217,45 @@ dom_exception dom_namednodemap_get_named_item(struct dom_namednodemap *map,
dom_exception dom_namednodemap_set_named_item(struct dom_namednodemap *map,
struct dom_node *arg, struct dom_node **node)
{
- UNUSED(map);
- UNUSED(arg);
- UNUSED(node);
+ dom_exception err;
+
+ /* Ensure arg and map belong to the same document */
+ if (arg->owner != map->owner)
+ return DOM_WRONG_DOCUMENT_ERR;
+
+ /* Ensure map is writable */
+ if (_dom_node_readonly(map->head))
+ return DOM_NO_MODIFICATION_ALLOWED_ERR;
+
+ /* Ensure arg isn't attached to another element */
+ if (arg->type == DOM_ATTRIBUTE_NODE && arg->parent != NULL &&
+ arg->parent != map->head)
+ return DOM_INUSE_ATTRIBUTE_ERR;
+
+ /* Ensure arg is permitted in the map */
+ if (arg->type != map->type)
+ return DOM_HIERARCHY_REQUEST_ERR;
+
+ /* Now delegate to the container-specific function.
+ * NamedNodeMaps are live, so this is fine. */
+ switch (map->type) {
+ case DOM_ATTRIBUTE_NODE:
+ err = dom_element_set_attribute_node(
+ (struct dom_element *) map->head,
+ (struct dom_attr *) arg,
+ (struct dom_attr **) node);
+ break;
+ case DOM_NOTATION_NODE:
+ case DOM_ENTITY_NODE:
+ /** \todo handle notation and entity nodes */
+ default:
+ err = DOM_NOT_SUPPORTED_ERR;
+ break;
+ }
- return DOM_NOT_SUPPORTED_ERR;
+ /* Reference counting is handled by the container-specific call */
+
+ return err;
}
/**
@@ -197,11 +276,44 @@ dom_exception dom_namednodemap_remove_named_item(
struct dom_namednodemap *map, struct dom_string *name,
struct dom_node **node)
{
- UNUSED(map);
- UNUSED(name);
- UNUSED(node);
+ dom_exception err;
+
+ /* Ensure map is writable */
+ if (_dom_node_readonly(map->head))
+ return DOM_NO_MODIFICATION_ALLOWED_ERR;
+
+ /* Now delegate to the container-specific function.
+ * NamedNodeMaps are live, so this is fine. */
+ switch (map->type) {
+ case DOM_ATTRIBUTE_NODE:
+ {
+ struct dom_attr *attr;
+
+ err = dom_element_get_attribute_node(
+ (struct dom_element *) map->head,
+ name, &attr);
+ if (err == DOM_NO_ERR) {
+ err = dom_element_remove_attribute_node(
+ (struct dom_element *) map->head,
+ attr, (struct dom_attr **) node);
+ if (err == DOM_NO_ERR) {
+ /* No longer want attr */
+ dom_node_unref((struct dom_node *) attr);
+ }
+ }
+ }
+ break;
+ case DOM_NOTATION_NODE:
+ case DOM_ENTITY_NODE:
+ /** \todo handle notation and entity nodes */
+ default:
+ err = DOM_NOT_SUPPORTED_ERR;
+ break;
+ }
- return DOM_NOT_SUPPORTED_ERR;
+ /* Reference counting is handled by the container-specific call */
+
+ return err;
}
/**
@@ -221,11 +333,36 @@ dom_exception dom_namednodemap_remove_named_item(
dom_exception dom_namednodemap_item(struct dom_namednodemap *map,
unsigned long index, struct dom_node **node)
{
- UNUSED(map);
- UNUSED(index);
- UNUSED(node);
+ struct dom_node *cur;
+ unsigned long count = 0;
+
+ switch (map->type) {
+ case DOM_ATTRIBUTE_NODE:
+ cur = dom_element_get_first_attribute(
+ (struct dom_element *) map->head);
+ break;
+ case DOM_NOTATION_NODE:
+ case DOM_ENTITY_NODE:
+ /** \todo handle notation and entity nodes */
+ default:
+ return DOM_NOT_SUPPORTED_ERR;
+ break;
+ }
- return DOM_NOT_SUPPORTED_ERR;
+ for (; cur != NULL; cur = cur->next) {
+ count++;
+
+ if ((index + 1) == count) {
+ break;
+ }
+ }
+
+ if (cur != NULL) {
+ dom_node_ref(cur);
+ }
+ *node = cur;
+
+ return DOM_NO_ERR;
}
/**