diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2007-09-24 23:26:04 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2007-09-24 23:26:04 +0000 |
commit | 7259d8fa11f3fc45adbfa9634c145ba58f7f6257 (patch) | |
tree | 341664962cc99a7ac74430bb57e8e9ad20016c20 /src | |
parent | c224138caa7b6e392a14a023d74cd4683503a60e (diff) | |
download | libdom-7259d8fa11f3fc45adbfa9634c145ba58f7f6257.tar.gz libdom-7259d8fa11f3fc45adbfa9634c145ba58f7f6257.tar.bz2 |
Move attributes field to struct dom_element as it doesn't apply to other node types.
svn path=/trunk/dom/; revision=3590
Diffstat (limited to 'src')
-rw-r--r-- | src/core/element.c | 68 | ||||
-rw-r--r-- | src/core/element.h | 8 | ||||
-rw-r--r-- | src/core/node.c | 13 | ||||
-rw-r--r-- | src/core/node.h | 2 |
4 files changed, 69 insertions, 22 deletions
diff --git a/src/core/element.c b/src/core/element.c index 0d23347..a0f0b51 100644 --- a/src/core/element.c +++ b/src/core/element.c @@ -23,6 +23,8 @@ struct dom_element { struct dom_node base; /**< Base node */ + struct dom_attr *attributes; /**< Element attributes */ + struct dom_type_info *schema_type_info; /**< Type information */ }; @@ -62,6 +64,7 @@ dom_exception dom_element_create(struct dom_document *doc, } /* Perform our type-specific initialisation */ + el->attributes = NULL; el->schema_type_info = NULL; *result = el; @@ -105,7 +108,7 @@ void dom_element_destroy(struct dom_document *doc, } /* Destroy attributes attached to this node */ - for (c = (struct dom_node *) element->base.attributes; + for (c = (struct dom_node *) element->attributes; c != NULL; c = d) { d = c->next; @@ -205,7 +208,7 @@ dom_exception dom_element_get_tag_name(struct dom_element *element, dom_exception dom_element_get_attribute(struct dom_element *element, struct dom_string *name, struct dom_string **value) { - struct dom_node *a = (struct dom_node *) element->base.attributes; + struct dom_node *a = (struct dom_node *) element->attributes; /* Search attributes, looking for name */ for (; a != NULL; a = a->next) { @@ -237,7 +240,7 @@ dom_exception dom_element_set_attribute(struct dom_element *element, struct dom_string *name, struct dom_string *value) { struct dom_node *e = (struct dom_node *) element; - struct dom_node *a = (struct dom_node *) e->attributes; + struct dom_node *a = (struct dom_node *) element->attributes; /** \todo validate name */ @@ -278,12 +281,12 @@ dom_exception dom_element_set_attribute(struct dom_element *element, /* And insert it into the element */ a->previous = NULL; - a->next = (struct dom_node *) e->attributes; + a->next = (struct dom_node *) element->attributes; if (a->next != NULL) a->next->previous = a; - e->attributes = attr; + element->attributes = attr; } return DOM_NO_ERR; @@ -301,7 +304,7 @@ dom_exception dom_element_remove_attribute(struct dom_element *element, struct dom_string *name) { struct dom_node *e = (struct dom_node *) element; - struct dom_node *a = (struct dom_node *) e->attributes; + struct dom_node *a = (struct dom_node *) element->attributes; /* Ensure element can be written to */ if (_dom_node_readonly(e)) @@ -318,7 +321,7 @@ dom_exception dom_element_remove_attribute(struct dom_element *element, if (a->previous != NULL) a->previous->next = a->next; else - e->attributes = (struct dom_attr *) a->next; + element->attributes = (struct dom_attr *) a->next; if (a->next != NULL) a->next->previous = a->previous; @@ -349,7 +352,7 @@ dom_exception dom_element_remove_attribute(struct dom_element *element, dom_exception dom_element_get_attribute_node(struct dom_element *element, struct dom_string *name, struct dom_attr **result) { - struct dom_node *a = (struct dom_node *) element->base.attributes; + struct dom_node *a = (struct dom_node *) element->attributes; /* Search attributes, looking for name */ for (; a != NULL; a = a->next) { @@ -404,7 +407,7 @@ dom_exception dom_element_set_attribute_node(struct dom_element *element, if (a->parent == NULL) { /* Search for existing attribute with same name */ - prev = e->attributes; + prev = element->attributes; while (prev != NULL) { struct dom_node *p = (struct dom_node *) prev; @@ -426,7 +429,7 @@ dom_exception dom_element_set_attribute_node(struct dom_element *element, if (a->previous != NULL) a->previous->next = a; else - e->attributes = attr; + element->attributes = attr; if (a->next != NULL) a->next->previous = a; @@ -438,12 +441,12 @@ dom_exception dom_element_set_attribute_node(struct dom_element *element, } else { /* No existing attribute, so insert at front of list */ a->previous = NULL; - a->next = (struct dom_node *) e->attributes; + a->next = (struct dom_node *) element->attributes; if (a->next != NULL) a->next->previous = a; - e->attributes = attr; + element->attributes = attr; } } @@ -488,7 +491,7 @@ dom_exception dom_element_remove_attribute_node(struct dom_element *element, if (a->previous != NULL) a->previous->next = a->next; else - e->attributes = (struct dom_attr *) a->next; + element->attributes = (struct dom_attr *) a->next; if (a->next != NULL) a->next->previous = a->previous; @@ -718,7 +721,7 @@ dom_exception dom_element_get_elements_by_tag_name_ns( dom_exception dom_element_has_attribute(struct dom_element *element, struct dom_string *name, bool *result) { - struct dom_node *a = (struct dom_node *) element->base.attributes; + struct dom_node *a = (struct dom_node *) element->attributes; /* Search attributes, looking for name */ for (; a != NULL; a = a->next) { @@ -843,3 +846,40 @@ dom_exception dom_element_set_id_attribute_node(struct dom_element *element, return DOM_NOT_SUPPORTED_ERR; } +/* */ +/*----------------------------------------------------------------------------*/ +/* */ + +/** + * Retrieve a map of attributes associated with an Element + * + * \param element The element to retrieve the attributes of + * \param result Pointer to location to receive attribute map + * \return DOM_NO_ERR. + * + * The returned NamedNodeMap will be referenced. It is the responsibility + * of the caller to unref the map once it has finished with it. + */ +dom_exception dom_element_get_attributes(struct dom_element *element, + struct dom_namednodemap **result) +{ + return dom_document_get_namednodemap(element->base.owner, + (struct dom_node *) element, DOM_ATTRIBUTE_NODE, + result); +} + +/** + * Determine if an element has any attributes + * + * \param element Element to inspect + * \param result Pointer to location to receive result + * \return DOM_NO_ERR. + */ +dom_exception dom_element_has_attributes(struct dom_element *element, + bool *result) +{ + *result = (element->attributes != NULL); + + return DOM_NO_ERR; +} + diff --git a/src/core/element.h b/src/core/element.h index 2a79997..f51b9ae 100644 --- a/src/core/element.h +++ b/src/core/element.h @@ -8,10 +8,13 @@ #ifndef dom_internal_core_element_h_ #define dom_internal_core_element_h_ +#include <stdbool.h> + #include <dom/core/exceptions.h> struct dom_document; struct dom_element; +struct dom_namednodemap; struct dom_string; dom_exception dom_element_create(struct dom_document *doc, @@ -20,4 +23,9 @@ dom_exception dom_element_create(struct dom_document *doc, void dom_element_destroy(struct dom_document *doc, struct dom_element *element); +dom_exception dom_element_get_attributes(struct dom_element *element, + struct dom_namednodemap **result); + +dom_exception dom_element_has_attributes(struct dom_element *element, + bool *result); #endif diff --git a/src/core/node.c b/src/core/node.c index e8ebc34..ea957e4 100644 --- a/src/core/node.c +++ b/src/core/node.c @@ -157,7 +157,6 @@ dom_exception dom_node_initialise(struct dom_node *node, node->last_child = NULL; node->previous = NULL; node->next = NULL; - node->attributes = NULL; /* Note: nodes do not reference the document to which they belong, * as this would result in the document never being destroyed once @@ -232,7 +231,6 @@ void dom_node_finalise(struct dom_document *doc, struct dom_node *node) node->owner = NULL; /* Paranoia */ - node->attributes = NULL; node->next = NULL; node->previous = NULL; node->last_child = NULL; @@ -565,8 +563,7 @@ dom_exception dom_node_get_attributes(struct dom_node *node, return DOM_NO_ERR; } - return dom_document_get_namednodemap(node->owner, node, - DOM_ATTRIBUTE_NODE, result); + return dom_element_get_attributes((struct dom_element *) node, result); } /** @@ -1105,9 +1102,13 @@ dom_exception dom_node_get_local_name(struct dom_node *node, */ dom_exception dom_node_has_attributes(struct dom_node *node, bool *result) { - *result = node->attributes != NULL; + if (node->type != DOM_ELEMENT_NODE) { + *result = false; - return DOM_NO_ERR; + return DOM_NO_ERR; + } + + return dom_element_has_attributes((struct dom_element *) node, result); } /** diff --git a/src/core/node.h b/src/core/node.h index bba00f5..2297ca5 100644 --- a/src/core/node.h +++ b/src/core/node.h @@ -12,7 +12,6 @@ #include <dom/core/node.h> -struct dom_attr; struct dom_string; /** @@ -41,7 +40,6 @@ struct dom_node { struct dom_node *last_child; /**< Last child node */ struct dom_node *previous; /**< Previous sibling */ struct dom_node *next; /**< Next sibling */ - struct dom_attr *attributes; /**< Node attributes */ struct dom_document *owner; /**< Owning document */ |