diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2011-12-21 22:18:10 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2011-12-21 22:18:10 +0000 |
commit | 83f3338663c4969eebefd8c2c43bd3fc43587fdd (patch) | |
tree | e48ba69628c5ba793533094e308c1fce9acb21aa /src/core/document.c | |
parent | 4ade8ad1c7b23e6eeeee6681acbdb43fb10cab43 (diff) | |
download | libdom-83f3338663c4969eebefd8c2c43bd3fc43587fdd.tar.gz libdom-83f3338663c4969eebefd8c2c43bd3fc43587fdd.tar.bz2 |
Merge branches/jmb/dom-alloc-purge back to trunk
svn path=/trunk/libdom/; revision=13316
Diffstat (limited to 'src/core/document.c')
-rw-r--r-- | src/core/document.c | 622 |
1 files changed, 159 insertions, 463 deletions
diff --git a/src/core/document.c b/src/core/document.c index b671c30..91b2b98 100644 --- a/src/core/document.c +++ b/src/core/document.c @@ -7,8 +7,7 @@ */ #include <assert.h> - -#include <libwapcaplet/libwapcaplet.h> +#include <stdlib.h> #include <dom/functypes.h> #include <dom/core/attr.h> @@ -36,7 +35,7 @@ * Item in list of active nodelists */ struct dom_doc_nl { - struct dom_nodelist *list; /**< Nodelist */ + dom_nodelist *list; /**< Nodelist */ struct dom_doc_nl *next; /**< Next item */ struct dom_doc_nl *prev; /**< Previous item */ @@ -45,6 +44,9 @@ struct dom_doc_nl { /* The virtual functions of this dom_document */ static struct dom_document_vtable document_vtable = { { + { + DOM_NODE_EVENT_TARGET_VTABLE + }, DOM_NODE_VTABLE }, DOM_DOCUMENT_VTABLE @@ -59,7 +61,7 @@ static struct dom_node_protect_vtable document_protect_vtable = { /* Internally used helper functions */ static dom_exception dom_document_dup_node(dom_document *doc, - struct dom_node *node, bool deep, struct dom_node **result, + dom_node *node, bool deep, dom_node **result, dom_node_operation opt); @@ -70,26 +72,20 @@ static dom_exception dom_document_dup_node(dom_document *doc, /** * Create a Document * - * \param alloc Memory (de)allocation function - * \param pw Pointer to client-specific private data * \param doc Pointer to location to receive created document * \param daf The default action fetcher - * \param daf The default action fetcher * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion. * - * ::impl will have its reference count increased. - * * The returned document will already be referenced. */ -dom_exception _dom_document_create(dom_alloc alloc, void *pw, - dom_events_default_action_fetcher daf, - struct dom_document **doc) +dom_exception _dom_document_create(dom_events_default_action_fetcher daf, + dom_document **doc) { - struct dom_document *d; + dom_document *d; dom_exception err; /* Create document */ - d = alloc(NULL, sizeof(struct dom_document), pw); + d = malloc(sizeof(dom_document)); if (d == NULL) return DOM_NO_MEM_ERR; @@ -102,10 +98,10 @@ dom_exception _dom_document_create(dom_alloc alloc, void *pw, * reaches zero. Documents own themselves (this simplifies the * rest of the code, as it doesn't need to special case Documents) */ - err = _dom_document_initialise(d, alloc, pw, daf); + err = _dom_document_initialise(d, daf); if (err != DOM_NO_ERR) { /* Clean up document */ - alloc(d, 0, pw); + free(d); return err; } @@ -115,29 +111,22 @@ dom_exception _dom_document_create(dom_alloc alloc, void *pw, } /* Initialise the document */ -dom_exception _dom_document_initialise(struct dom_document *doc, - dom_alloc alloc, void *pw, +dom_exception _dom_document_initialise(dom_document *doc, dom_events_default_action_fetcher daf) { dom_exception err; - lwc_string *name; - lwc_error lerr; + dom_string *name; - assert(alloc != NULL); - - lerr = lwc_intern_string("#document", SLEN("#document"), &name); - if (lerr != lwc_error_ok) - return _dom_exception_from_lwc_error(lerr); + err = dom_string_create((const uint8_t *) "#document", + SLEN("#document"), &name); + if (err != DOM_NO_ERR) + return err; doc->nodelists = NULL; - /* Set up document allocation context - must be first */ - doc->alloc = alloc; - doc->pw = pw; - err = _dom_node_initialise(&doc->base, doc, DOM_DOCUMENT_NODE, name, NULL, NULL, NULL); - lwc_string_unref(name); + dom_string_unref(name); list_init(&doc->pending_nodes); @@ -149,10 +138,10 @@ dom_exception _dom_document_initialise(struct dom_document *doc, /* Finalise the document */ -bool _dom_document_finalise(struct dom_document *doc) +bool _dom_document_finalise(dom_document *doc) { /* Finalise base class, delete the tree in force */ - _dom_node_finalise(doc, &doc->base); + _dom_node_finalise(&doc->base); /* Now, the first_child and last_child should be null */ doc->base.first_child = NULL; @@ -174,9 +163,7 @@ bool _dom_document_finalise(struct dom_document *doc) doc->nodelists = NULL; if (doc->id_name != NULL) - lwc_string_unref(doc->id_name); - - _dom_document_event_internal_finalise(doc, &doc->dei); + dom_string_unref(doc->id_name); _dom_document_event_internal_finalise(doc, &doc->dei); @@ -200,10 +187,10 @@ bool _dom_document_finalise(struct dom_document *doc) * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_get_doctype(struct dom_document *doc, - struct dom_document_type **result) +dom_exception _dom_document_get_doctype(dom_document *doc, + dom_document_type **result) { - struct dom_node_internal *c; + dom_node_internal *c; for (c = doc->base.first_child; c != NULL; c = c->next) { if (c->type == DOM_DOCUMENT_TYPE_NODE) @@ -213,7 +200,7 @@ dom_exception _dom_document_get_doctype(struct dom_document *doc, if (c != NULL) dom_node_ref(c); - *result = (struct dom_document_type *) c; + *result = (dom_document_type *) c; return DOM_NO_ERR; } @@ -229,7 +216,7 @@ dom_exception _dom_document_get_doctype(struct dom_document *doc, * It is the responsibility of the caller to unref the implementation once * it has finished with it. */ -dom_exception _dom_document_get_implementation(struct dom_document *doc, +dom_exception _dom_document_get_implementation(dom_document *doc, dom_implementation **result) { UNUSED(doc); @@ -250,10 +237,10 @@ dom_exception _dom_document_get_implementation(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_get_document_element(struct dom_document *doc, - struct dom_element **result) +dom_exception _dom_document_get_document_element(dom_document *doc, + dom_element **result) { - struct dom_node_internal *root; + dom_node_internal *root; /* Find the first element node in child list */ for (root = doc->base.first_child; root != NULL; root = root->next) { @@ -264,7 +251,7 @@ dom_exception _dom_document_get_document_element(struct dom_document *doc, if (root != NULL) dom_node_ref(root); - *result = (struct dom_element *) root; + *result = (dom_element *) root; return DOM_NO_ERR; } @@ -284,23 +271,13 @@ dom_exception _dom_document_get_document_element(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_element(struct dom_document *doc, - dom_string *tag_name, struct dom_element **result) +dom_exception _dom_document_create_element(dom_document *doc, + dom_string *tag_name, dom_element **result) { - lwc_string *name; - dom_exception err; - if (_dom_validate_name(tag_name) == false) return DOM_INVALID_CHARACTER_ERR; - err = _dom_string_intern(tag_name, &name); - if (err != DOM_NO_ERR) - return err; - - err = _dom_element_create(doc, name, NULL, NULL, result); - lwc_string_unref(name); - - return err; + return _dom_element_create(doc, tag_name, NULL, NULL, result); } /** @@ -314,20 +291,19 @@ dom_exception _dom_document_create_element(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_document_fragment(struct dom_document *doc, - struct dom_document_fragment **result) +dom_exception _dom_document_create_document_fragment(dom_document *doc, + dom_document_fragment **result) { - lwc_string *name; + dom_string *name; dom_exception err; - lwc_error lerr; - lerr = lwc_intern_string("#document-fragment", + err = dom_string_create((const uint8_t *) "#document-fragment", SLEN("#document-fragment"), &name); - if (lerr != lwc_error_ok) - return _dom_exception_from_lwc_error(lerr); + if (err != DOM_NO_ERR) + return err; err = _dom_document_fragment_create(doc, name, NULL, result); - lwc_string_unref(name); + dom_string_unref(name); return err; } @@ -344,19 +320,19 @@ dom_exception _dom_document_create_document_fragment(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_text_node(struct dom_document *doc, - dom_string *data, struct dom_text **result) +dom_exception _dom_document_create_text_node(dom_document *doc, + dom_string *data, dom_text **result) { - lwc_string *name; + dom_string *name; dom_exception err; - lwc_error lerr; - lerr = lwc_intern_string("#text", SLEN("#text"), &name); - if (lerr != lwc_error_ok) - return _dom_exception_from_lwc_error(lerr); + err = dom_string_create((const uint8_t *) "#text", + SLEN("#text"), &name); + if (err != DOM_NO_ERR) + return err; err = _dom_text_create(doc, name, data, result); - lwc_string_unref(name); + dom_string_unref(name); return err; } @@ -373,20 +349,19 @@ dom_exception _dom_document_create_text_node(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_comment(struct dom_document *doc, - dom_string *data, struct dom_comment **result) +dom_exception _dom_document_create_comment(dom_document *doc, + dom_string *data, dom_comment **result) { - lwc_string *name; + dom_string *name; dom_exception err; - lwc_error lerr; - lerr = lwc_intern_string("#comment", SLEN("#comment"), + err = dom_string_create((const uint8_t *) "#comment", SLEN("#comment"), &name); - if (lerr != lwc_error_ok) - return _dom_exception_from_lwc_error(lerr); + if (err != DOM_NO_ERR) + return err; err = _dom_comment_create(doc, name, data, result); - lwc_string_unref(name); + dom_string_unref(name); return err; } @@ -404,20 +379,19 @@ dom_exception _dom_document_create_comment(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_cdata_section(struct dom_document *doc, - dom_string *data, struct dom_cdata_section **result) +dom_exception _dom_document_create_cdata_section(dom_document *doc, + dom_string *data, dom_cdata_section **result) { - lwc_string *name; + dom_string *name; dom_exception err; - lwc_error lerr; - lerr = lwc_intern_string("#cdata-section", + err = dom_string_create((const uint8_t *) "#cdata-section", SLEN("#cdata-section"), &name); - if (lerr != lwc_error_ok) - return _dom_exception_from_lwc_error(lerr); + if (err != DOM_NO_ERR) + return err; err = _dom_cdata_section_create(doc, name, data, result); - lwc_string_unref(name); + dom_string_unref(name); return err; } @@ -438,24 +412,14 @@ dom_exception _dom_document_create_cdata_section(struct dom_document *doc, * finished with it. */ dom_exception _dom_document_create_processing_instruction( - struct dom_document *doc, dom_string *target, + dom_document *doc, dom_string *target, dom_string *data, - struct dom_processing_instruction **result) + dom_processing_instruction **result) { - lwc_string *name; - dom_exception err; - if (_dom_validate_name(target) == false) return DOM_INVALID_CHARACTER_ERR; - err = _dom_string_intern(target, &name); - if (err != DOM_NO_ERR) - return err; - - err = _dom_processing_instruction_create(doc, name, data, result); - lwc_string_unref(name); - - return err; + return _dom_processing_instruction_create(doc, target, data, result); } /** @@ -471,22 +435,13 @@ dom_exception _dom_document_create_processing_instruction( * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_attribute(struct dom_document *doc, - dom_string *name, struct dom_attr **result) +dom_exception _dom_document_create_attribute(dom_document *doc, + dom_string *name, dom_attr **result) { - lwc_string *n; - dom_exception err; - if (_dom_validate_name(name) == false) return DOM_INVALID_CHARACTER_ERR; - err = _dom_string_intern(name, &n); - if (err != DOM_NO_ERR) - return err; - - err = _dom_attr_create(doc, n, NULL, NULL, true, result); - lwc_string_unref(n); - return err; + return _dom_attr_create(doc, name, NULL, NULL, true, result); } /** @@ -503,23 +458,14 @@ dom_exception _dom_document_create_attribute(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_entity_reference(struct dom_document *doc, +dom_exception _dom_document_create_entity_reference(dom_document *doc, dom_string *name, - struct dom_entity_reference **result) + dom_entity_reference **result) { - lwc_string *n; - dom_exception err; - if (_dom_validate_name(name) == false) return DOM_INVALID_CHARACTER_ERR; - err = _dom_string_intern(name, &n); - if (err != DOM_NO_ERR) - return err; - - err = _dom_entity_reference_create(doc, n, NULL, result); - lwc_string_unref(n); - return err; + return _dom_entity_reference_create(doc, name, NULL, result); } /** @@ -534,22 +480,12 @@ dom_exception _dom_document_create_entity_reference(struct dom_document *doc, * the responsibility of the caller to unref the list once it has * finished with it. */ -dom_exception _dom_document_get_elements_by_tag_name(struct dom_document *doc, - dom_string *tagname, struct dom_nodelist **result) +dom_exception _dom_document_get_elements_by_tag_name(dom_document *doc, + dom_string *tagname, dom_nodelist **result) { - lwc_string *name; - dom_exception err; - - err = _dom_string_intern(tagname, &name); - if (err != DOM_NO_ERR) - return err; - - err = _dom_document_get_nodelist(doc, DOM_NODELIST_BY_NAME, - (struct dom_node_internal *) doc, name, NULL, NULL, + return _dom_document_get_nodelist(doc, DOM_NODELIST_BY_NAME, + (dom_node_internal *) doc, tagname, NULL, NULL, result); - lwc_string_unref(name); - - return err; } /** @@ -567,8 +503,8 @@ dom_exception _dom_document_get_elements_by_tag_name(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_import_node(struct dom_document *doc, - struct dom_node *node, bool deep, struct dom_node **result) +dom_exception _dom_document_import_node(dom_document *doc, + dom_node *node, bool deep, dom_node **result) { /* TODO: The DOM_INVALID_CHARACTER_ERR exception */ @@ -604,9 +540,9 @@ dom_exception _dom_document_import_node(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_element_ns(struct dom_document *doc, +dom_exception _dom_document_create_element_ns(dom_document *doc, dom_string *namespace, dom_string *qname, - struct dom_element **result) + dom_element **result) { dom_string *prefix, *localname; dom_exception err; @@ -626,56 +562,16 @@ dom_exception _dom_document_create_element_ns(struct dom_document *doc, return err; } - /* Get the interned string from the dom_string */ - lwc_string *l = NULL, *n = NULL, *p = NULL; - if (localname != NULL) { - err = _dom_string_intern(localname, &l); - if (err != DOM_NO_ERR) { - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } - if (namespace != NULL) { - err = _dom_string_intern(namespace, &n); - if (err != DOM_NO_ERR) { - lwc_string_unref(l); - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } - if (prefix != NULL) { - err = _dom_string_intern(prefix, &p); - if (err != DOM_NO_ERR) { - lwc_string_unref(l); - lwc_string_unref(n); - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } - /* Attempt to create element */ - err = _dom_element_create(doc, l, n, p, result); + err = _dom_element_create(doc, localname, namespace, prefix, result); /* Tidy up */ if (localname != NULL) { dom_string_unref(localname); - lwc_string_unref(l); } + if (prefix != NULL) { dom_string_unref(prefix); - lwc_string_unref(p); - } - if (namespace != NULL) { - lwc_string_unref(n); } return err; @@ -709,9 +605,9 @@ dom_exception _dom_document_create_element_ns(struct dom_document *doc, * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_create_attribute_ns(struct dom_document *doc, +dom_exception _dom_document_create_attribute_ns(dom_document *doc, dom_string *namespace, dom_string *qname, - struct dom_attr **result) + dom_attr **result) { dom_string *prefix, *localname; dom_exception err; @@ -731,55 +627,16 @@ dom_exception _dom_document_create_attribute_ns(struct dom_document *doc, return err; } - /* Get the interned string from the dom_string */ - lwc_string *l = NULL, *n = NULL, *p = NULL; - if (localname != NULL) { - err = _dom_string_intern(localname, &l); - if (err != DOM_NO_ERR) { - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } - if (namespace != NULL) { - err = _dom_string_intern(namespace, &n); - if (err != DOM_NO_ERR) { - lwc_string_unref(l); - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } - if (prefix != NULL) { - err = _dom_string_intern(prefix, &p); - if (err != DOM_NO_ERR) { - lwc_string_unref(l); - lwc_string_unref(n); - dom_string_unref(localname); - if (prefix != NULL) - dom_string_unref(prefix); - - return err; - } - } /* Attempt to create attribute */ - err = _dom_attr_create(doc, l, n, p, true, result); + err = _dom_attr_create(doc, localname, namespace, prefix, true, result); /* Tidy up */ if (localname != NULL) { dom_string_unref(localname); - lwc_string_unref(l); } + if (prefix != NULL) { dom_string_unref(prefix); - lwc_string_unref(p); - } - if (namespace != NULL) { - lwc_string_unref(n); } return err; @@ -799,35 +656,12 @@ dom_exception _dom_document_create_attribute_ns(struct dom_document *doc, * finished with it. */ dom_exception _dom_document_get_elements_by_tag_name_ns( - struct dom_document *doc, dom_string *namespace, - dom_string *localname, struct dom_nodelist **result) + dom_document *doc, dom_string *namespace, + dom_string *localname, dom_nodelist **result) { - dom_exception err; - lwc_string *l = NULL, *n = NULL; - - /* Get the interned string from the dom_string */ - if (localname != NULL) { - err = _dom_string_intern(localname, &l); - if (err != DOM_NO_ERR) - return err; - } - if (namespace != NULL) { - err = _dom_string_intern(namespace, &n); - if (err != DOM_NO_ERR) { - lwc_string_unref(l); - return err; - } - } - - err = _dom_document_get_nodelist(doc, DOM_NODELIST_BY_NAMESPACE, - (struct dom_node_internal *) doc, NULL, n, l, result); - - if (l != NULL) - lwc_string_unref(l); - if (n != NULL) - lwc_string_unref(n); - - return err; + return _dom_document_get_nodelist(doc, DOM_NODELIST_BY_NAMESPACE, + (dom_node_internal *) doc, NULL, namespace, localname, + result); } /** @@ -842,24 +676,19 @@ dom_exception _dom_document_get_elements_by_tag_name_ns( * the responsibility of the caller to unref the node once it has * finished with it. */ -dom_exception _dom_document_get_element_by_id(struct dom_document *doc, - dom_string *id, struct dom_element **result) +dom_exception _dom_document_get_element_by_id(dom_document *doc, + dom_string *id, dom_element **result) { - lwc_string *i; dom_node_internal *root; dom_exception err; *result = NULL; - err = _dom_string_intern(id, &i); - if (err != DOM_NO_ERR) - return err; - err = dom_document_get_document_element(doc, (void *) &root); if (err != DOM_NO_ERR) return err; - err = _dom_find_element_by_id(root, i, result); + err = _dom_find_element_by_id(root, id, result); dom_node_unref(root); return err; @@ -876,7 +705,7 @@ dom_exception _dom_document_get_element_by_id(struct dom_document *doc, * the responsibility of the caller to unref the string once it has * finished with it. */ -dom_exception _dom_document_get_input_encoding(struct dom_document *doc, +dom_exception _dom_document_get_input_encoding(dom_document *doc, dom_string **result) { UNUSED(doc); @@ -896,7 +725,7 @@ dom_exception _dom_document_get_input_encoding(struct dom_document *doc, * the responsibility of the caller to unref the string once it has * finished with it. */ -dom_exception _dom_document_get_xml_encoding(struct dom_document *doc, +dom_exception _dom_document_get_xml_encoding(dom_document *doc, dom_string **result) { UNUSED(doc); @@ -912,7 +741,7 @@ dom_exception _dom_document_get_xml_encoding(struct dom_document *doc, * \param result Pointer to location to receive result * \return DOM_NOT_SUPPORTED_ERR, we don't support this API now. */ -dom_exception _dom_document_get_xml_standalone(struct dom_document *doc, +dom_exception _dom_document_get_xml_standalone(dom_document *doc, bool *result) { UNUSED(doc); @@ -933,7 +762,7 @@ dom_exception _dom_document_get_xml_standalone(struct dom_document *doc, * We don't support this API now, so the return value is always * DOM_NOT_SUPPORTED_ERR. */ -dom_exception _dom_document_set_xml_standalone(struct dom_document *doc, +dom_exception _dom_document_set_xml_standalone(dom_document *doc, bool standalone) { UNUSED(doc); @@ -956,7 +785,7 @@ dom_exception _dom_document_set_xml_standalone(struct dom_document *doc, * We don't support this API now, so the return value is always * DOM_NOT_SUPPORTED_ERR. */ -dom_exception _dom_document_get_xml_version(struct dom_document *doc, +dom_exception _dom_document_get_xml_version(dom_document *doc, dom_string **result) { UNUSED(doc); @@ -977,7 +806,7 @@ dom_exception _dom_document_get_xml_version(struct dom_document *doc, * We don't support this API now, so the return value is always * DOM_NOT_SUPPORTED_ERR. */ -dom_exception _dom_document_set_xml_version(struct dom_document *doc, +dom_exception _dom_document_set_xml_version(dom_document *doc, dom_string *version) { UNUSED(doc); @@ -994,7 +823,7 @@ dom_exception _dom_document_set_xml_version(struct dom_document *doc, * \return DOM_NOT_SUPPORTED_ERR, we don't support this API now. */ dom_exception _dom_document_get_strict_error_checking( - struct dom_document *doc, bool *result) + dom_document *doc, bool *result) { UNUSED(doc); UNUSED(result); @@ -1010,7 +839,7 @@ dom_exception _dom_document_get_strict_error_checking( * \return DOM_NOT_SUPPORTED_ERR, we don't support this API now. */ dom_exception _dom_document_set_strict_error_checking( - struct dom_document *doc, bool strict) + dom_document *doc, bool strict) { UNUSED(doc); UNUSED(strict); @@ -1029,11 +858,10 @@ dom_exception _dom_document_set_strict_error_checking( * the responsibility of the caller to unref the string once it has * finished with it. */ -dom_exception _dom_document_get_uri(struct dom_document *doc, +dom_exception _dom_document_get_uri(dom_document *doc, dom_string **result) { - dom_string_ref(doc->uri); - *result = doc->uri; + *result = dom_string_ref(doc->uri); return DOM_NO_ERR; } @@ -1049,12 +877,12 @@ dom_exception _dom_document_get_uri(struct dom_document *doc, * the responsibility of the caller to unref the string once it has * finished with it. */ -dom_exception _dom_document_set_uri(struct dom_document *doc, +dom_exception _dom_document_set_uri(dom_document *doc, dom_string *uri) { dom_string_unref(doc->uri); - dom_string_ref(uri); - doc->uri = uri; + + doc->uri = dom_string_ref(uri); return DOM_NO_ERR; } @@ -1081,11 +909,11 @@ dom_exception _dom_document_set_uri(struct dom_document *doc, * generally, the adoptNode and importNode call the same function * dom_document_dup_node. */ -dom_exception _dom_document_adopt_node(struct dom_document *doc, - struct dom_node *node, struct dom_node **result) +dom_exception _dom_document_adopt_node(dom_document *doc, + dom_node *node, dom_node **result) { - dom_exception err; dom_node_internal *n = (dom_node_internal *) node; + dom_exception err; *result = NULL; @@ -1142,7 +970,7 @@ dom_exception _dom_document_adopt_node(struct dom_document *doc, * the responsibility of the caller to unref the object once it has * finished with it. */ -dom_exception _dom_document_get_dom_config(struct dom_document *doc, +dom_exception _dom_document_get_dom_config(dom_document *doc, struct dom_configuration **result) { UNUSED(doc); @@ -1157,7 +985,7 @@ dom_exception _dom_document_get_dom_config(struct dom_document *doc, * \param doc The document to normalize * \return DOM_NOT_SUPPORTED_ERR, we don't support this API now. */ -dom_exception _dom_document_normalize(struct dom_document *doc) +dom_exception _dom_document_normalize(dom_document *doc) { UNUSED(doc); @@ -1198,10 +1026,10 @@ dom_exception _dom_document_normalize(struct dom_document *doc) * We don't support this API now, so the return value is always * DOM_NOT_SUPPORTED_ERR. */ -dom_exception _dom_document_rename_node(struct dom_document *doc, - struct dom_node *node, +dom_exception _dom_document_rename_node(dom_document *doc, + dom_node *node, dom_string *namespace, dom_string *qname, - struct dom_node **result) + dom_node **result) { UNUSED(doc); UNUSED(node); @@ -1217,38 +1045,21 @@ dom_exception _dom_document_rename_node(struct dom_document *doc, /* Overload protectd virtual functions */ /* The virtual destroy function of this class */ -void _dom_document_destroy(struct dom_node_internal *node) +void _dom_document_destroy(dom_node_internal *node) { - struct dom_document *doc = (struct dom_document *) node; + dom_document *doc = (dom_document *) node; if (_dom_document_finalise(doc) == true) { - doc->alloc(doc, 0, doc->pw); + free(doc); } } -/* The memory allocation function of this class */ -dom_exception __dom_document_alloc(struct dom_document *doc, - struct dom_node_internal *n, struct dom_node_internal **ret) -{ - UNUSED(n); - struct dom_document *a; - - a = _dom_document_alloc(doc, NULL, sizeof(struct dom_document)); - if (a == NULL) - return DOM_NO_MEM_ERR; - - *ret = (dom_node_internal *) a; - dom_node_set_owner(*ret, doc); - - return DOM_NO_ERR; -} - /* The copy constructor function of this class */ -dom_exception _dom_document_copy(struct dom_node_internal *new, - struct dom_node_internal *old) +dom_exception _dom_document_copy(dom_node_internal *old, + dom_node_internal **copy) { - UNUSED(new); UNUSED(old); + UNUSED(copy); return DOM_NOT_SUPPORTED_ERR; } @@ -1257,125 +1068,6 @@ dom_exception _dom_document_copy(struct dom_node_internal *new, /* ----------------------------------------------------------------------- */ /* Helper functions */ -/** - * Create a DOM string, using a document's allocation context - * - * \param doc The document - * \param data Pointer to string data - * \param len Length, in bytes, of string - * \param result Pointer to location to receive result - * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion - * - * The returned string will already be referenced, so there is no need - * to explicitly reference it. - * - * The string of characters passed in will be copied for use by the - * returned DOM string. - */ -dom_exception _dom_document_create_string(struct dom_document *doc, - const uint8_t *data, size_t len, dom_string **result) -{ - return dom_string_create(doc->alloc, doc->pw, data, len, result); -} - -/** - * Create a lwc_string - * - * \param doc The document object - * \param data The raw string data - * \param len The raw string length - * \param result The resturned lwc_string - * \return DOM_NO_ERR on success, appropriate dom_exception on failure. - */ -dom_exception _dom_document_create_lwcstring(struct dom_document *doc, - const uint8_t *data, size_t len, struct lwc_string_s **result) -{ - lwc_error lerr; - - UNUSED(doc); - - lerr = lwc_intern_string((const char *) data, len, result); - - return _dom_exception_from_lwc_error(lerr); -} - -/* Unref a lwc_string created by this document */ -void _dom_document_unref_lwcstring(struct dom_document *doc, - struct lwc_string_s *str) -{ - UNUSED(doc); - - lwc_string_unref(str); -} - -/* Get the resource manager from the document */ -void _dom_document_get_resource_mgr( - struct dom_document *doc, struct dom_resource_mgr *rm) -{ - rm->alloc = doc->alloc; - rm->pw = doc->pw; -} - -/* Simple accessor for allocator data for this document */ -void _dom_document_get_allocator(struct dom_document *doc, dom_alloc *al, - void **pw) -{ - *al = doc->alloc; - *pw = doc->pw; -} -/* - * Create a dom_string from a lwc_string. - * - * \param doc The document object - * \param str The lwc_string object - * \param result The retured dom_string - * \return DOM_NO_ERR on success, appropriate dom_exception on failure. - */ -dom_exception _dom_document_create_string_from_lwcstring( - struct dom_document *doc, struct lwc_string_s *str, - dom_string **result) -{ - return _dom_string_create_from_lwcstring(doc->alloc, doc->pw, - str, result); -} - -/** - * Create a hash_table - * - * \param doc The dom_document - * \param chains The number of chains - * \param f The hash function - * \param ht The returned hash_table - * \return DOM_NO_ERR on success, appropriate dom_exception on failure. - */ -dom_exception _dom_document_create_hashtable(struct dom_document *doc, - size_t chains, dom_hash_func f, struct dom_hash_table **ht) -{ - struct dom_hash_table *ret; - - ret = _dom_hash_create(chains, f, doc->alloc, doc->pw); - if (ret == NULL) - return DOM_NO_MEM_ERR; - - *ht = ret; - return DOM_NO_ERR; -} - -/** - * (De)allocate memory with a document's context - * - * \param doc The document context to allocate from - * \param ptr Pointer to data to reallocate, or NULL to alloc new - * \param size Required size of data, or 0 to free - * \return Pointer to allocated data or NULL on failure - * - * This call (modulo ::doc) has the same semantics as realloc(). - * It is a thin veneer over the client-provided allocation function. - */ -void *_dom_document_alloc(struct dom_document *doc, void *ptr, size_t size) -{ - return doc->alloc(ptr, size, doc->pw); -} /** * Get a nodelist, creating one if necessary @@ -1393,10 +1085,10 @@ void *_dom_document_alloc(struct dom_document *doc, void *ptr, size_t size) * the responsibility of the caller to unref the list once it has * finished with it. */ -dom_exception _dom_document_get_nodelist(struct dom_document *doc, - nodelist_type type, struct dom_node_internal *root, - struct lwc_string_s *tagname, struct lwc_string_s *namespace, - struct lwc_string_s *localname, struct dom_nodelist **list) +dom_exception _dom_document_get_nodelist(dom_document *doc, + nodelist_type type, dom_node_internal *root, + dom_string *tagname, dom_string *namespace, + dom_string *localname, dom_nodelist **list) { struct dom_doc_nl *l; dom_exception err; @@ -1414,7 +1106,7 @@ dom_exception _dom_document_get_nodelist(struct dom_document *doc, /* No existing list */ /* Create active list entry */ - l = doc->alloc(NULL, sizeof(struct dom_doc_nl), doc->pw); + l = malloc(sizeof(struct dom_doc_nl)); if (l == NULL) return DOM_NO_MEM_ERR; @@ -1422,7 +1114,7 @@ dom_exception _dom_document_get_nodelist(struct dom_document *doc, err = _dom_nodelist_create(doc, type, root, tagname, namespace, localname, &l->list); if (err != DOM_NO_ERR) { - doc->alloc(l, 0, doc->pw); + free(l); return err; } @@ -1451,8 +1143,8 @@ dom_exception _dom_document_get_nodelist(struct dom_document *doc, * \param doc The document to remove the list from * \param list The list to remove */ -void _dom_document_remove_nodelist(struct dom_document *doc, - struct dom_nodelist *list) +void _dom_document_remove_nodelist(dom_document *doc, + dom_nodelist *list) { struct dom_doc_nl *l; @@ -1476,7 +1168,7 @@ void _dom_document_remove_nodelist(struct dom_document *doc, l->next->prev = l->prev; /* And free item */ - doc->alloc(l, 0, doc->pw); + free(l); } /** @@ -1488,19 +1180,25 @@ void _dom_document_remove_nodelist(struct dom_document *doc, * \return DOM_NO_ERR on success, appropriate dom_exception on failure. */ dom_exception _dom_find_element_by_id(dom_node_internal *root, - struct lwc_string_s *id, struct dom_element **result) + dom_string *id, dom_element **result) { - *result = NULL; dom_node_internal *node = root; + *result = NULL; + while (node != NULL) { if (node->type == DOM_ELEMENT_NODE) { - lwc_string *real_id; + dom_string *real_id; + _dom_element_get_id((dom_element *) node, &real_id); - if (real_id == id) { + + if (dom_string_isequal(real_id, id)) { + dom_string_unref(real_id); *result = (dom_element *) node; return DOM_NO_ERR; } + + dom_string_unref(real_id); } if (node->first_child != NULL) { @@ -1512,7 +1210,7 @@ dom_exception _dom_find_element_by_id(dom_node_internal *root, } else { /* No children or siblings. * Find first unvisited relation. */ - struct dom_node_internal *parent = node->parent; + dom_node_internal *parent = node->parent; while (parent != root && node == parent->last_child) { @@ -1537,11 +1235,12 @@ dom_exception _dom_find_element_by_id(dom_node_internal *root, * \param opt Whether this is adopt or import operation * \return DOM_NO_ERR on success, appropriate dom_exception on failure. */ -dom_exception dom_document_dup_node(dom_document *doc, struct dom_node *node, - bool deep, struct dom_node **result, dom_node_operation opt) +dom_exception dom_document_dup_node(dom_document *doc, dom_node *node, + bool deep, dom_node **result, dom_node_operation opt) { - dom_exception err; dom_node_internal *n = (dom_node_internal *) node; + dom_node_internal *ret; + dom_exception err; if (opt == DOM_NODE_ADOPTED && _dom_node_readonly(n)) return DOM_NO_MODIFICATION_ALLOWED_ERR; @@ -1550,15 +1249,9 @@ dom_exception dom_document_dup_node(dom_document *doc, struct dom_node *node, n->type == DOM_DOCUMENT_TYPE_NODE) return DOM_NOT_SUPPORTED_ERR; - err = dom_node_alloc(doc, node, result); + err = dom_node_copy(node, &ret); if (err != DOM_NO_ERR) return err; - - err = dom_node_copy(*result, node); - if (err != DOM_NO_ERR) { - _dom_document_alloc(doc, *result, 0); - return err; - } if (n->type == DOM_ATTRIBUTE_NODE) { _dom_attr_set_specified((dom_attr *) node, true); @@ -1588,13 +1281,13 @@ dom_exception dom_document_dup_node(dom_document *doc, struct dom_node *node, err = dom_document_import_node(doc, child, deep, (void *) &r); if (err != DOM_NO_ERR) { - _dom_document_alloc(doc, *result, 0); + dom_node_unref(ret); return err; } - err = dom_node_append_child(*result, r, (void *) &r); + err = dom_node_append_child(ret, r, (void *) &r); if (err != DOM_NO_ERR) { - _dom_document_alloc(doc, *result, 0); + dom_node_unref(ret); dom_node_unref(r); return err; } @@ -1608,12 +1301,15 @@ dom_exception dom_document_dup_node(dom_document *doc, struct dom_node *node, dom_user_data *ud; ud = n->user_data; while (ud != NULL) { - if (ud->handler != NULL) - ud->handler(opt, ud->key, ud->data, - node, *result); + if (ud->handler != NULL) { + ud->handler(opt, ud->key, ud->data, node, + (dom_node *) ret); + } ud = ud->next; } + *result = (dom_node *) ret; + return DOM_NO_ERR; } @@ -1628,7 +1324,7 @@ dom_exception dom_document_dup_node(dom_document *doc, struct dom_node *node, * * else, do nothing. */ -void _dom_document_try_destroy(struct dom_document *doc) +void _dom_document_try_destroy(dom_document *doc) { if (doc->base.refcnt != 0 || doc->base.parent != NULL) return; @@ -1642,10 +1338,10 @@ void _dom_document_try_destroy(struct dom_document *doc) * \param doc The document object * \param name The ID name of the elements in this document */ -void _dom_document_set_id_name(dom_document *doc, struct lwc_string_s *name) +void _dom_document_set_id_name(dom_document *doc, dom_string *name) { if (doc->id_name != NULL) - lwc_string_unref(doc->id_name); - doc->id_name = lwc_string_ref(name); + dom_string_unref(doc->id_name); + doc->id_name = dom_string_ref(name); } |