summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/attr.c82
-rw-r--r--src/core/attr.h20
-rw-r--r--src/core/cdatasection.c65
-rw-r--r--src/core/cdatasection.h16
-rw-r--r--src/core/characterdata.c56
-rw-r--r--src/core/characterdata.h22
-rw-r--r--src/core/comment.c65
-rw-r--r--src/core/comment.h17
-rw-r--r--src/core/doc_fragment.c69
-rw-r--r--src/core/doc_fragment.h22
-rw-r--r--src/core/document.c622
-rw-r--r--src/core/document.h202
-rw-r--r--src/core/document_type.c130
-rw-r--r--src/core/document_type.h40
-rw-r--r--src/core/element.c622
-rw-r--r--src/core/element.h27
-rw-r--r--src/core/entity_ref.c69
-rw-r--r--src/core/entity_ref.h23
-rw-r--r--src/core/implementation.c25
-rw-r--r--src/core/namednodemap.c53
-rw-r--r--src/core/node.c646
-rw-r--r--src/core/node.h107
-rw-r--r--src/core/nodelist.c116
-rw-r--r--src/core/nodelist.h9
-rw-r--r--src/core/pi.c68
-rw-r--r--src/core/pi.h22
-rw-r--r--src/core/string.c525
-rw-r--r--src/core/string.h18
-rw-r--r--src/core/text.c100
-rw-r--r--src/core/text.h39
30 files changed, 1571 insertions, 2326 deletions
diff --git a/src/core/attr.c b/src/core/attr.c
index 937eec0..dd4dddf 100644
--- a/src/core/attr.c
+++ b/src/core/attr.c
@@ -6,9 +6,10 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
+#include <assert.h>
#include <stddef.h>
+#include <stdlib.h>
#include <string.h>
-#include <assert.h>
#include <dom/core/attr.h>
#include <dom/core/document.h>
@@ -51,6 +52,9 @@ struct dom_attr {
/* The vtable for dom_attr node */
static struct dom_attr_vtable attr_vtable = {
{
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE,
+ },
DOM_NODE_VTABLE_ATTR
},
DOM_ATTR_VTABLE
@@ -84,15 +88,15 @@ static struct dom_node_protect_vtable attr_protect_vtable = {
* The returned attribute will already be referenced.
*/
dom_exception _dom_attr_create(struct dom_document *doc,
- struct lwc_string_s *name, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix, bool specified,
+ dom_string *name, dom_string *namespace,
+ dom_string *prefix, bool specified,
struct dom_attr **result)
{
struct dom_attr *a;
dom_exception err;
/* Allocate the attribute node */
- a = _dom_document_alloc(doc, NULL, sizeof(struct dom_attr));
+ a = malloc(sizeof(struct dom_attr));
if (a == NULL)
return DOM_NO_MEM_ERR;
@@ -104,9 +108,10 @@ dom_exception _dom_attr_create(struct dom_document *doc,
err = _dom_attr_initialise(a, doc, name, namespace, prefix, specified,
result);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, a, 0);
+ free(a);
return err;
}
+
return DOM_NO_ERR;
}
@@ -123,8 +128,8 @@ dom_exception _dom_attr_create(struct dom_document *doc,
* \return DOM_NO_ERR on success, appropriate dom_exception on failure.
*/
dom_exception _dom_attr_initialise(dom_attr *a,
- struct dom_document *doc, struct lwc_string_s *name,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix,
+ struct dom_document *doc, dom_string *name,
+ dom_string *namespace, dom_string *prefix,
bool specified, struct dom_attr **result)
{
dom_exception err;
@@ -150,10 +155,9 @@ dom_exception _dom_attr_initialise(dom_attr *a,
/**
* The destructor of dom_attr
*
- * \param doc The owner document
* \param attr The attribute
*/
-void _dom_attr_finalise(dom_document *doc, dom_attr *attr)
+void _dom_attr_finalise(dom_attr *attr)
{
/* Now, clean up this node and destroy it */
@@ -161,22 +165,21 @@ void _dom_attr_finalise(dom_document *doc, dom_attr *attr)
/** \todo destroy schema type info */
}
- _dom_node_finalise(doc, &attr->base);
+ _dom_node_finalise(&attr->base);
}
/**
* Destroy an attribute node
*
- * \param doc The owning document
* \param attr The attribute to destroy
*
* The contents of ::attr will be destroyed and ::attr will be freed
*/
-void _dom_attr_destroy(struct dom_document *doc, struct dom_attr *attr)
+void _dom_attr_destroy(struct dom_attr *attr)
{
- _dom_attr_finalise(doc, attr);
+ _dom_attr_finalise(attr);
- _dom_document_alloc(doc, attr, 0);
+ free(attr);
}
/*-----------------------------------------------------------------------*/
@@ -435,8 +438,7 @@ dom_exception _dom_attr_get_value(struct dom_attr *attr,
dom_string *value, *temp;
dom_exception err;
- err = _dom_document_create_string(a->owner,
- NULL, 0, &value);
+ err = dom_string_create(NULL, 0, &value);
if (err != DOM_NO_ERR) {
return err;
}
@@ -749,51 +751,43 @@ dom_exception _dom_attr_lookup_namespace(dom_node_internal *node,
/* The virtual destroy function of this class */
void __dom_attr_destroy(dom_node_internal *node)
{
- dom_document *doc = node->owner;
-
- assert(doc != NULL);
- _dom_attr_destroy(doc, (dom_attr *) node);
+ _dom_attr_destroy((dom_attr *) node);
}
/* The memory allocator of this class */
-dom_exception _dom_attr_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+dom_exception _dom_attr_copy(dom_node_internal *n, dom_node_internal **copy)
{
- UNUSED(n);
+ dom_attr *old = (dom_attr *) n;
dom_attr *a;
+ dom_exception err;
- a = _dom_document_alloc(doc, NULL, sizeof(struct dom_attr));
+ a = malloc(sizeof(struct dom_attr));
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 of this class */
-dom_exception _dom_attr_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- dom_attr *na = (dom_attr *) new;
- dom_attr *oa = (dom_attr *) old;
-
- na->specified = oa->specified;
+ err = dom_node_copy_internal(n, a);
+ if (err != DOM_NO_ERR) {
+ free(a);
+ return err;
+ }
+
+ a->specified = old->specified;
/* TODO: deal with dom_type_info, it get no definition ! */
- na->schema_type_info = NULL;
+ a->schema_type_info = NULL;
- na->is_id = oa->is_id;
+ a->is_id = old->is_id;
- na->type = oa->type;
+ a->type = old->type;
- na->value = oa->value;
+ a->value = old->value;
/* TODO: is this correct? */
- na->read_only = false;
+ a->read_only = false;
- return _dom_node_copy(new, old);
+ *copy = (dom_node_internal *) a;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/attr.h b/src/core/attr.h
index 0d4621a..2558f65 100644
--- a/src/core/attr.h
+++ b/src/core/attr.h
@@ -12,18 +12,17 @@
struct dom_document;
struct dom_type_info;
-struct lwc_string_s;
dom_exception _dom_attr_create(struct dom_document *doc,
- struct lwc_string_s *name, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix, bool specified,
+ dom_string *name, dom_string *namespace,
+ dom_string *prefix, bool specified,
struct dom_attr **result);
-void _dom_attr_destroy(struct dom_document *doc, struct dom_attr *attr);
+void _dom_attr_destroy(struct dom_attr *attr);
dom_exception _dom_attr_initialise(struct dom_attr *a,
- struct dom_document *doc, struct lwc_string_s *name,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix,
+ struct dom_document *doc, dom_string *name,
+ dom_string *namespace, dom_string *prefix,
bool specified, struct dom_attr **result);
-void _dom_attr_finalise(struct dom_document *doc, struct dom_attr *attr);
+void _dom_attr_finalise(struct dom_attr *attr);
/* Virtual functions for dom_attr */
dom_exception _dom_attr_get_name(struct dom_attr *attr,
@@ -103,14 +102,11 @@ dom_exception _dom_attr_lookup_namespace(dom_node_internal *node,
/* The protected virtual functions */
void __dom_attr_destroy(dom_node_internal *node);
-dom_exception _dom_attr_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_attr_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+dom_exception _dom_attr_copy(dom_node_internal *old,
+ dom_node_internal **copy);
#define DOM_ATTR_PROTECT_VTABLE \
__dom_attr_destroy, \
- _dom_attr_alloc, \
_dom_attr_copy
diff --git a/src/core/cdatasection.c b/src/core/cdatasection.c
index 7417ebf..fd10c3a 100644
--- a/src/core/cdatasection.c
+++ b/src/core/cdatasection.c
@@ -6,6 +6,8 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
+#include <stdlib.h>
+
#include "core/cdatasection.h"
#include "core/document.h"
#include "core/text.h"
@@ -15,7 +17,7 @@
* A DOM CDATA section
*/
struct dom_cdata_section {
- struct dom_text base; /**< Base node */
+ dom_text base; /**< Base node */
};
static struct dom_node_protect_vtable cdata_section_protect_vtable = {
@@ -36,15 +38,15 @@ static struct dom_node_protect_vtable cdata_section_protect_vtable = {
*
* The returned node will already be referenced.
*/
-dom_exception _dom_cdata_section_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_cdata_section **result)
+dom_exception _dom_cdata_section_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_cdata_section **result)
{
- struct dom_cdata_section *c;
+ dom_cdata_section *c;
dom_exception err;
/* Allocate the comment node */
- c = _dom_document_alloc(doc, NULL, sizeof(struct dom_cdata_section));
+ c = malloc(sizeof(dom_cdata_section));
if (c == NULL)
return DOM_NO_MEM_ERR;
@@ -56,7 +58,7 @@ dom_exception _dom_cdata_section_create(struct dom_document *doc,
err = _dom_cdata_section_initialise(&c->base, doc,
DOM_CDATA_SECTION_NODE, name, value);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, c, 0);
+ free(c);
return err;
}
@@ -68,19 +70,17 @@ dom_exception _dom_cdata_section_create(struct dom_document *doc,
/**
* Destroy a CDATA section
*
- * \param doc The owning document
* \param cdata The cdata section to destroy
*
* The contents of ::cdata will be destroyed and ::cdata will be freed.
*/
-void _dom_cdata_section_destroy(struct dom_document *doc,
- struct dom_cdata_section *cdata)
+void _dom_cdata_section_destroy(dom_cdata_section *cdata)
{
/* Clean up base node contents */
- _dom_cdata_section_finalise(doc, &cdata->base);
+ _dom_cdata_section_finalise(&cdata->base);
/* Destroy the node */
- _dom_document_alloc(doc, cdata, 0);
+ free(cdata);
}
/*--------------------------------------------------------------------------*/
@@ -88,35 +88,30 @@ void _dom_cdata_section_destroy(struct dom_document *doc,
/* The protected virtual functions */
/* The virtual destroy function of this class */
-void __dom_cdata_section_destroy(struct dom_node_internal *node)
+void __dom_cdata_section_destroy(dom_node_internal *node)
{
- struct dom_document *doc;
- doc = dom_node_get_owner(node);
-
- _dom_cdata_section_destroy(doc, (struct dom_cdata_section *) node);
+ _dom_cdata_section_destroy((dom_cdata_section *) node);
}
-/* The memory allocator of this class */
-dom_exception _dom_cdata_section_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_cdata_section_copy(dom_node_internal *old,
+ dom_node_internal **copy)
{
- UNUSED(n);
- dom_cdata_section *a;
-
- a = _dom_document_alloc(doc, NULL, sizeof(struct dom_cdata_section));
- if (a == NULL)
+ dom_cdata_section *new_cdata;
+ dom_exception err;
+
+ new_cdata = malloc(sizeof(dom_cdata_section));
+ if (new_cdata == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) a;
- dom_node_set_owner(*ret, doc);
- return DOM_NO_ERR;
-}
+ err = dom_text_copy_internal(old, new_cdata);
+ if (err != DOM_NO_ERR) {
+ free(new_cdata);
+ return err;
+ }
-/* The copy constructor of this class */
-dom_exception _dom_cdata_section_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- return _dom_characterdata_copy(new, old);
+ *copy = (dom_node_internal *) new_cdata;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/cdatasection.h b/src/core/cdatasection.h
index 04a0954..8ec4755 100644
--- a/src/core/cdatasection.h
+++ b/src/core/cdatasection.h
@@ -13,30 +13,24 @@
#include <dom/core/string.h>
struct dom_node_internal;
-struct dom_cdata_section;
struct dom_document;
-struct lwc_string_s;
dom_exception _dom_cdata_section_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_cdata_section **result);
+ dom_string *name, dom_string *value,
+ dom_cdata_section **result);
-void _dom_cdata_section_destroy(struct dom_document *doc,
- struct dom_cdata_section *cdata);
+void _dom_cdata_section_destroy(dom_cdata_section *cdata);
#define _dom_cdata_section_initialise _dom_text_initialise
#define _dom_cdata_section_finalise _dom_text_finalise
/* Following comes the protected vtable */
void __dom_cdata_section_destroy(struct dom_node_internal *node);
-dom_exception _dom_cdata_section_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_cdata_section_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+dom_exception _dom_cdata_section_copy(struct dom_node_internal *old,
+ struct dom_node_internal **copy);
#define DOM_CDATA_SECTION_PROTECT_VTABLE \
__dom_cdata_section_destroy, \
- _dom_cdata_section_alloc, \
_dom_cdata_section_copy
#endif
diff --git a/src/core/characterdata.c b/src/core/characterdata.c
index 824e0dc..91a0764 100644
--- a/src/core/characterdata.c
+++ b/src/core/characterdata.c
@@ -7,6 +7,7 @@
*/
#include <assert.h>
+#include <stdlib.h>
#include <dom/core/characterdata.h>
#include <dom/core/string.h>
@@ -22,6 +23,9 @@
* public to each child class */
struct dom_characterdata_vtable characterdata_vtable = {
{
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
},
DOM_CHARACTERDATA_VTABLE
@@ -29,11 +33,9 @@ struct dom_characterdata_vtable characterdata_vtable = {
/* Create a DOM characterdata node and compose the vtable */
-dom_characterdata *_dom_characterdata_create(struct dom_document *doc)
+dom_characterdata *_dom_characterdata_create(void)
{
- dom_characterdata *cdata = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_characterdata));
-
+ dom_characterdata *cdata = malloc(sizeof(struct dom_characterdata));
if (cdata == NULL)
return NULL;
@@ -57,7 +59,7 @@ dom_characterdata *_dom_characterdata_create(struct dom_document *doc)
*/
dom_exception _dom_characterdata_initialise(struct dom_characterdata *cdata,
struct dom_document *doc, dom_node_type type,
- lwc_string *name, dom_string *value)
+ dom_string *name, dom_string *value)
{
return _dom_node_initialise(&cdata->base, doc, type,
name, value, NULL, NULL);
@@ -66,15 +68,13 @@ dom_exception _dom_characterdata_initialise(struct dom_characterdata *cdata,
/**
* Finalise a character data node
*
- * \param doc The owning document
* \param cdata The node to finalise
*
* The contents of ::cdata will be cleaned up. ::cdata will not be freed.
*/
-void _dom_characterdata_finalise(struct dom_document *doc,
- struct dom_characterdata *cdata)
+void _dom_characterdata_finalise(struct dom_characterdata *cdata)
{
- _dom_node_finalise(doc, &cdata->base);
+ _dom_node_finalise(&cdata->base);
}
@@ -451,34 +451,38 @@ dom_exception _dom_characterdata_replace_data(struct dom_characterdata *cdata,
/*----------------------------------------------------------------------*/
-/* The protected virtual functions of Node, see core/node.h for details
- *
- * @note: the three following API never be called directly from the virtual
- * functions dispatch mechanism, they are here for the code consistent.
- */
+/* The protected virtual functions of Node, see core/node.h for details */
void _dom_characterdata_destroy(struct dom_node_internal *node)
{
assert("Should never be here" == NULL);
UNUSED(node);
}
-/* The memory allocator of this class */
-dom_exception _dom_characterdata_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_characterdata_copy(dom_node_internal *old,
+ dom_node_internal **copy)
{
- assert("Should never be here" == NULL);
- UNUSED(doc);
- UNUSED(n);
- UNUSED(ret);
+ dom_characterdata *new_node;
+ dom_exception err;
+
+ new_node = malloc(sizeof(dom_characterdata));
+ if (new_node == NULL)
+ return DOM_NO_MEM_ERR;
+
+ err = dom_characterdata_copy_internal(old, new_node);
+ if (err != DOM_NO_ERR) {
+ free(new_node);
+ return err;
+ }
+
+ *copy = (dom_node_internal *) new_node;
return DOM_NO_ERR;
}
-/* The copy constructor of this class
- * The sub-class of characterdata should call this API */
-dom_exception _dom_characterdata_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
+dom_exception _dom_characterdata_copy_internal(dom_characterdata *old,
+ dom_characterdata *new)
{
- return _dom_node_copy(new, old);
+ return dom_node_copy_internal(old, new);
}
diff --git a/src/core/characterdata.h b/src/core/characterdata.h
index da8772d..7b15030 100644
--- a/src/core/characterdata.h
+++ b/src/core/characterdata.h
@@ -21,13 +21,12 @@ struct dom_characterdata {
/* The CharacterData is a intermediate node type, so the following function
* may never be used */
-dom_characterdata *_dom_characterdata_create(struct dom_document *doc);
+dom_characterdata *_dom_characterdata_create(void);
dom_exception _dom_characterdata_initialise(struct dom_characterdata *cdata,
struct dom_document *doc, dom_node_type type,
- struct lwc_string_s *name, dom_string *value);
+ dom_string *name, dom_string *value);
-void _dom_characterdata_finalise(struct dom_document *doc,
- struct dom_characterdata *cdata);
+void _dom_characterdata_finalise(struct dom_characterdata *cdata);
/* The virtual functions for dom_characterdata */
dom_exception _dom_characterdata_get_data(struct dom_characterdata *cdata,
@@ -63,17 +62,20 @@ dom_exception _dom_characterdata_replace_data(struct dom_characterdata *cdata,
*
* Only the _copy function can be used by sub-class of this.
*/
-void _dom_characterdata_destroy(struct dom_node_internal *node);
-dom_exception _dom_characterdata_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_characterdata_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_characterdata_destroy(dom_node_internal *node);
+dom_exception _dom_characterdata_copy(dom_node_internal *old,
+ dom_node_internal **copy);
#define DOM_CHARACTERDATA_PROTECT_VTABLE \
_dom_characterdata_destroy, \
- _dom_characterdata_alloc, \
_dom_characterdata_copy
extern struct dom_characterdata_vtable characterdata_vtable;
+dom_exception _dom_characterdata_copy_internal(dom_characterdata *old,
+ dom_characterdata *new);
+#define dom_characterdata_copy_internal(o, n) \
+ _dom_characterdata_copy_internal( \
+ (dom_characterdata *) (o), (dom_characterdata *) (n))
+
#endif
diff --git a/src/core/comment.c b/src/core/comment.c
index ab49a66..0697826 100644
--- a/src/core/comment.c
+++ b/src/core/comment.c
@@ -6,6 +6,8 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
+#include <stdlib.h>
+
#include "core/characterdata.h"
#include "core/comment.h"
#include "core/document.h"
@@ -16,7 +18,7 @@
* A DOM Comment node
*/
struct dom_comment {
- struct dom_characterdata base; /**< Base node */
+ dom_characterdata base; /**< Base node */
};
static struct dom_node_protect_vtable comment_protect_vtable = {
@@ -37,15 +39,15 @@ static struct dom_node_protect_vtable comment_protect_vtable = {
*
* The returned node will already be referenced.
*/
-dom_exception _dom_comment_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_comment **result)
+dom_exception _dom_comment_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_comment **result)
{
- struct dom_comment *c;
+ dom_comment *c;
dom_exception err;
/* Allocate the comment node */
- c = _dom_document_alloc(doc, NULL, sizeof(struct dom_comment));
+ c = malloc(sizeof(dom_comment));
if (c == NULL)
return DOM_NO_MEM_ERR;
@@ -57,7 +59,7 @@ dom_exception _dom_comment_create(struct dom_document *doc,
err = _dom_characterdata_initialise(&c->base, doc, DOM_COMMENT_NODE,
name, value);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, c, 0);
+ free(c);
return err;
}
@@ -69,19 +71,17 @@ dom_exception _dom_comment_create(struct dom_document *doc,
/**
* Destroy a comment node
*
- * \param doc The owning document
* \param comment The node to destroy
*
* The contents of ::comment will be destroyed and ::comment will be freed
*/
-void _dom_comment_destroy(struct dom_document *doc,
- struct dom_comment *comment)
+void _dom_comment_destroy(dom_comment *comment)
{
/* Finalise base class contents */
- _dom_characterdata_finalise(doc, &comment->base);
+ _dom_characterdata_finalise(&comment->base);
/* Free node */
- _dom_document_alloc(doc, comment, 0);
+ free(comment);
}
@@ -89,35 +89,30 @@ void _dom_comment_destroy(struct dom_document *doc,
/* The protected virtual functions */
/* The virtual destroy function */
-void __dom_comment_destroy(struct dom_node_internal *node)
+void __dom_comment_destroy(dom_node_internal *node)
{
- struct dom_document *doc;
- doc = dom_node_get_owner(node);
-
- _dom_comment_destroy(doc, (struct dom_comment *) node);
+ _dom_comment_destroy((dom_comment *) node);
}
-/* The memory allocation function of this class */
-dom_exception _dom_comment_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_comment_copy(dom_node_internal *old,
+ dom_node_internal **copy)
{
- UNUSED(n);
- dom_comment *c;
-
- c = _dom_document_alloc(doc, NULL, sizeof(struct dom_comment));
- if (c == NULL)
+ dom_comment *new_comment;
+ dom_exception err;
+
+ new_comment = malloc(sizeof(dom_comment));
+ if (new_comment == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) c;
- dom_node_set_owner(*ret, doc);
- return DOM_NO_ERR;
-}
+ err = dom_characterdata_copy_internal(old, new_comment);
+ if (err != DOM_NO_ERR) {
+ free(new_comment);
+ return err;
+ }
-/* The copy constructor of this class */
-dom_exception _dom_comment_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- return _dom_characterdata_copy(new, old);
+ *copy = (dom_node_internal *) new_comment;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/comment.h b/src/core/comment.h
index 1951732..75eace4 100644
--- a/src/core/comment.h
+++ b/src/core/comment.h
@@ -13,28 +13,23 @@
struct dom_comment;
struct dom_document;
-struct lwc_string_s;
dom_exception _dom_comment_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_comment **result);
+ dom_string *name, dom_string *value,
+ dom_comment **result);
#define _dom_comment_initialise _dom_characterdata_initialise
#define _dom_comment_finalise _dom_characterdata_finalise
-void _dom_comment_destroy(struct dom_document *doc,
- struct dom_comment *comment);
+void _dom_comment_destroy(dom_comment *comment);
/* Following comes the protected vtable */
-void __dom_comment_destroy(struct dom_node_internal *node);
-dom_exception _dom_comment_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_comment_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void __dom_comment_destroy(dom_node_internal *node);
+dom_exception _dom_comment_copy(dom_node_internal *old,
+ dom_node_internal **copy);
#define DOM_COMMENT_PROTECT_VTABLE \
__dom_comment_destroy, \
- _dom_comment_alloc, \
_dom_comment_copy
#endif
diff --git a/src/core/doc_fragment.c b/src/core/doc_fragment.c
index 1d320b5..96cc707 100644
--- a/src/core/doc_fragment.c
+++ b/src/core/doc_fragment.c
@@ -6,7 +6,7 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
-#include <libwapcaplet/libwapcaplet.h>
+#include <stdlib.h>
#include <dom/core/node.h>
@@ -19,10 +19,13 @@
* A DOM document fragment
*/
struct dom_document_fragment {
- struct dom_node_internal base; /**< Base node */
+ dom_node_internal base; /**< Base node */
};
static struct dom_node_vtable df_vtable = {
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
};
@@ -44,20 +47,17 @@ static struct dom_node_protect_vtable df_protect_vtable = {
*
* The returned node will already be referenced.
*/
-dom_exception _dom_document_fragment_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_document_fragment **result)
+dom_exception _dom_document_fragment_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_document_fragment **result)
{
- struct dom_document_fragment *f;
+ dom_document_fragment *f;
dom_exception err;
- /* Allocate the comment node */
- f = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_document_fragment));
+ f = malloc(sizeof(dom_document_fragment));
if (f == NULL)
return DOM_NO_MEM_ERR;
-
f->base.base.vtable = &df_vtable;
f->base.vtable = &df_protect_vtable;
@@ -65,7 +65,7 @@ dom_exception _dom_document_fragment_create(struct dom_document *doc,
err = _dom_document_fragment_initialise(&f->base, doc,
DOM_DOCUMENT_FRAGMENT_NODE, name, value, NULL, NULL);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, f, 0);
+ free(f);
return err;
}
@@ -77,19 +77,17 @@ dom_exception _dom_document_fragment_create(struct dom_document *doc,
/**
* Destroy a document fragment
*
- * \param doc The owning document
* \param frag The document fragment to destroy
*
* The contents of ::frag will be destroyed and ::frag will be freed.
*/
-void _dom_document_fragment_destroy(struct dom_document *doc,
- struct dom_document_fragment *frag)
+void _dom_document_fragment_destroy(dom_document_fragment *frag)
{
/* Finalise base class */
- _dom_document_fragment_finalise(doc, &frag->base);
+ _dom_document_fragment_finalise(&frag->base);
/* Destroy fragment */
- _dom_document_alloc(doc, frag, 0);
+ free(frag);
}
/*-----------------------------------------------------------------------*/
@@ -97,34 +95,29 @@ void _dom_document_fragment_destroy(struct dom_document *doc,
/* Overload protected functions */
/* The virtual destroy function of this class */
-void _dom_df_destroy(struct dom_node_internal *node)
+void _dom_df_destroy(dom_node_internal *node)
{
- _dom_document_fragment_destroy(node->owner,
- (struct dom_document_fragment *) node);
+ _dom_document_fragment_destroy((dom_document_fragment *) node);
}
-/* The memory allocator of this class */
-dom_exception _dom_df_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_df_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(n);
- struct dom_document_fragment *a;
-
- a = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_document_fragment));
- if (a == NULL)
+ dom_document_fragment *new_f;
+ dom_exception err;
+
+ new_f = malloc(sizeof(dom_document_fragment));
+ if (new_f == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) a;
- dom_node_set_owner(*ret, doc);
- return DOM_NO_ERR;
-}
+ err = dom_node_copy_internal(old, new_f);
+ if (err != DOM_NO_ERR) {
+ free(new_f);
+ return err;
+ }
-/* The copy constructor of this class */
-dom_exception _dom_df_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- return _dom_node_copy(new, old);
+ *copy = (dom_node_internal *) new_f;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/doc_fragment.h b/src/core/doc_fragment.h
index b2c3e71..944516f 100644
--- a/src/core/doc_fragment.h
+++ b/src/core/doc_fragment.h
@@ -9,32 +9,24 @@
#define dom_internal_core_documentfragment_h_
#include <dom/core/exceptions.h>
+#include <dom/core/doc_fragment.h>
-struct dom_document_fragment;
-struct dom_document;
-struct lwc_string_s;
+dom_exception _dom_document_fragment_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_document_fragment **result);
-dom_exception _dom_document_fragment_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_document_fragment **result);
-
-void _dom_document_fragment_destroy(struct dom_document *doc,
- struct dom_document_fragment *frag);
+void _dom_document_fragment_destroy(dom_document_fragment *frag);
#define _dom_document_fragment_initialise _dom_node_initialise
#define _dom_document_fragment_finalise _dom_node_finalise
/* Following comes the protected vtable */
-void _dom_df_destroy(struct dom_node_internal *node);
-dom_exception _dom_df_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_df_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_df_destroy(dom_node_internal *node);
+dom_exception _dom_df_copy(dom_node_internal *old, dom_node_internal **copy);
#define DOM_DF_PROTECT_VTABLE \
_dom_df_destroy, \
- _dom_df_alloc, \
_dom_df_copy
#endif
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);
}
diff --git a/src/core/document.h b/src/core/document.h
index 911b89f..9ff7368 100644
--- a/src/core/document.h
+++ b/src/core/document.h
@@ -12,7 +12,16 @@
#include <stddef.h>
#include <dom/core/node.h>
+#include <dom/core/attr.h>
+#include <dom/core/cdatasection.h>
+#include <dom/core/comment.h>
#include <dom/core/document.h>
+#include <dom/core/document_type.h>
+#include <dom/core/doc_fragment.h>
+#include <dom/core/element.h>
+#include <dom/core/entity_ref.h>
+#include <dom/core/pi.h>
+#include <dom/core/text.h>
#include <dom/core/implementation.h>
#include "core/string.h"
@@ -20,26 +29,10 @@
#include "core/nodelist.h"
#include "utils/hashtable.h"
-#include "utils/resource_mgr.h"
#include "utils/list.h"
#include "events/document_event.h"
-struct dom_document;
-struct dom_namednodemap;
-struct dom_node;
-struct dom_nodelist;
-struct dom_document_type;
-struct dom_element;
-struct dom_document_fragment;
-struct dom_text;
-struct dom_comment;
-struct dom_cdata_section;
-struct dom_processing_instruction;
-struct dom_attr;
-struct dom_entity_reference;
-struct dom_configuration;
-
struct dom_doc_nl;
/**
@@ -48,122 +41,102 @@ struct dom_doc_nl;
* this.
*/
struct dom_document {
- struct dom_node_internal base; /**< Base node */
+ dom_node_internal base; /**< Base node */
struct dom_doc_nl *nodelists; /**< List of active nodelists */
dom_string *uri; /**< The uri of this document */
- dom_alloc alloc; /**< Memory (de)allocation function */
- void *pw; /**< Pointer to client data */
-
struct list_entry pending_nodes;
/**< The deletion pending list */
- struct lwc_string_s *id_name; /**< The ID attribute's name */
+ dom_string *id_name; /**< The ID attribute's name */
dom_document_event_internal dei;
/**< The DocumentEVent interface */
};
/* Create a DOM document */
-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);
/* 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);
/* Finalise the document */
-bool _dom_document_finalise(struct dom_document *doc);
-
-/* Create a dom_string from C string */
-dom_exception _dom_document_create_string(struct dom_document *doc,
- const uint8_t *data, size_t len, dom_string **result);
-/* Create a lwc_string from C string */
-dom_exception _dom_document_create_lwcstring(struct dom_document *doc,
- const uint8_t *data, size_t len, struct lwc_string_s **result);
-/* Unref a lwc_string of this document */
-void _dom_document_unref_lwcstring(struct dom_document *doc,
- struct lwc_string_s *str);
-/* Create a dom_string from a lwc_string */
-dom_exception _dom_document_create_string_from_lwcstring(
- struct dom_document *doc, struct lwc_string_s *str,
- dom_string **result);
-
+bool _dom_document_finalise(dom_document *doc);
/* Begin the virtual functions */
-dom_exception _dom_document_get_doctype(struct dom_document *doc,
- struct dom_document_type **result);
-dom_exception _dom_document_get_implementation(struct dom_document *doc,
+dom_exception _dom_document_get_doctype(dom_document *doc,
+ dom_document_type **result);
+dom_exception _dom_document_get_implementation(dom_document *doc,
dom_implementation **result);
-dom_exception _dom_document_get_document_element(struct dom_document *doc,
- struct dom_element **result);
-dom_exception _dom_document_create_element(struct dom_document *doc,
- dom_string *tag_name, struct dom_element **result);
-dom_exception _dom_document_create_document_fragment(struct dom_document *doc,
- struct dom_document_fragment **result);
-dom_exception _dom_document_create_text_node(struct dom_document *doc,
- dom_string *data, struct dom_text **result);
-dom_exception _dom_document_create_comment(struct dom_document *doc,
- dom_string *data, struct dom_comment **result);
-dom_exception _dom_document_create_cdata_section(struct dom_document *doc,
- dom_string *data, struct dom_cdata_section **result);
+dom_exception _dom_document_get_document_element(dom_document *doc,
+ dom_element **result);
+dom_exception _dom_document_create_element(dom_document *doc,
+ dom_string *tag_name, dom_element **result);
+dom_exception _dom_document_create_document_fragment(dom_document *doc,
+ dom_document_fragment **result);
+dom_exception _dom_document_create_text_node(dom_document *doc,
+ dom_string *data, dom_text **result);
+dom_exception _dom_document_create_comment(dom_document *doc,
+ dom_string *data, dom_comment **result);
+dom_exception _dom_document_create_cdata_section(dom_document *doc,
+ dom_string *data, dom_cdata_section **result);
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_exception _dom_document_create_attribute(struct dom_document *doc,
- dom_string *name, struct dom_attr **result);
-dom_exception _dom_document_create_entity_reference(struct dom_document *doc,
+ dom_processing_instruction **result);
+dom_exception _dom_document_create_attribute(dom_document *doc,
+ dom_string *name, dom_attr **result);
+dom_exception _dom_document_create_entity_reference(dom_document *doc,
dom_string *name,
- struct dom_entity_reference **result);
-dom_exception _dom_document_get_elements_by_tag_name(struct dom_document *doc,
- dom_string *tagname, struct dom_nodelist **result);
-dom_exception _dom_document_import_node(struct dom_document *doc,
- struct dom_node *node, bool deep, struct dom_node **result);
-dom_exception _dom_document_create_element_ns(struct dom_document *doc,
+ dom_entity_reference **result);
+dom_exception _dom_document_get_elements_by_tag_name(dom_document *doc,
+ dom_string *tagname, dom_nodelist **result);
+dom_exception _dom_document_import_node(dom_document *doc,
+ dom_node *node, bool deep, dom_node **result);
+dom_exception _dom_document_create_element_ns(dom_document *doc,
dom_string *namespace, dom_string *qname,
- struct dom_element **result);
-dom_exception _dom_document_create_attribute_ns(struct dom_document *doc,
+ dom_element **result);
+dom_exception _dom_document_create_attribute_ns(dom_document *doc,
dom_string *namespace, dom_string *qname,
- struct dom_attr **result);
+ dom_attr **result);
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_exception _dom_document_get_element_by_id(struct dom_document *doc,
- dom_string *id, struct dom_element **result);
-dom_exception _dom_document_get_input_encoding(struct dom_document *doc,
+ dom_document *doc, dom_string *namespace,
+ dom_string *localname, dom_nodelist **result);
+dom_exception _dom_document_get_element_by_id(dom_document *doc,
+ dom_string *id, dom_element **result);
+dom_exception _dom_document_get_input_encoding(dom_document *doc,
dom_string **result);
-dom_exception _dom_document_get_xml_encoding(struct dom_document *doc,
+dom_exception _dom_document_get_xml_encoding(dom_document *doc,
dom_string **result);
-dom_exception _dom_document_get_xml_standalone(struct dom_document *doc,
+dom_exception _dom_document_get_xml_standalone(dom_document *doc,
bool *result);
-dom_exception _dom_document_set_xml_standalone(struct dom_document *doc,
+dom_exception _dom_document_set_xml_standalone(dom_document *doc,
bool standalone);
-dom_exception _dom_document_get_xml_version(struct dom_document *doc,
+dom_exception _dom_document_get_xml_version(dom_document *doc,
dom_string **result);
-dom_exception _dom_document_set_xml_version(struct dom_document *doc,
+dom_exception _dom_document_set_xml_version(dom_document *doc,
dom_string *version);
dom_exception _dom_document_get_strict_error_checking(
- struct dom_document *doc, bool *result);
+ dom_document *doc, bool *result);
dom_exception _dom_document_set_strict_error_checking(
- struct dom_document *doc, bool strict);
-dom_exception _dom_document_get_uri(struct dom_document *doc,
+ dom_document *doc, bool strict);
+dom_exception _dom_document_get_uri(dom_document *doc,
dom_string **result);
-dom_exception _dom_document_set_uri(struct dom_document *doc,
+dom_exception _dom_document_set_uri(dom_document *doc,
dom_string *uri);
-dom_exception _dom_document_adopt_node(struct dom_document *doc,
- struct dom_node *node, struct dom_node **result);
-dom_exception _dom_document_get_dom_config(struct dom_document *doc,
+dom_exception _dom_document_adopt_node(dom_document *doc,
+ dom_node *node, dom_node **result);
+dom_exception _dom_document_get_dom_config(dom_document *doc,
struct dom_configuration **result);
-dom_exception _dom_document_normalize(struct dom_document *doc);
-dom_exception _dom_document_rename_node(struct dom_document *doc,
- struct dom_node *node,
+dom_exception _dom_document_normalize(dom_document *doc);
+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);
#define DOM_DOCUMENT_VTABLE \
_dom_document_get_doctype, \
@@ -215,7 +188,7 @@ typedef struct dom_document_protected_vtable dom_document_protected_vtable;
static inline dom_exception dom_document_get_base(dom_document *doc,
dom_string **base_uri)
{
- struct dom_node_internal *node = (struct dom_node_internal *) doc;
+ dom_node_internal *node = (dom_node_internal *) doc;
return ((dom_document_protected_vtable *) node->vtable)->
dom_document_get_base(doc, base_uri);
}
@@ -223,15 +196,12 @@ static inline dom_exception dom_document_get_base(dom_document *doc,
(dom_document *) (d), (dom_string **) (b))
/* Following comes the protected vtable */
-void _dom_document_destroy(struct dom_node_internal *node);
-dom_exception __dom_document_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_document_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_document_destroy(dom_node_internal *node);
+dom_exception _dom_document_copy(dom_node_internal *old,
+ dom_node_internal **copy);
#define DOM_DOCUMENT_PROTECT_VTABLE \
_dom_document_destroy, \
- __dom_document_alloc, \
_dom_document_copy
@@ -240,40 +210,22 @@ dom_exception _dom_document_copy(struct dom_node_internal *new,
/* Try to destroy the document:
* When the refcnt is zero and the pending list is empty, we can destroy this
* document. */
-void _dom_document_try_destroy(struct dom_document *doc);
-/* (De)allocate memory */
-void *_dom_document_alloc(struct dom_document *doc, void *ptr, size_t size);
-
-/* Get the resource manager inside this document, a resource manager
- * is an object which contain the memory allocator/intern string context,
- * with which we can allocate strings or intern strings */
-void _dom_document_get_resource_mgr(
- struct dom_document *doc, struct dom_resource_mgr *rm);
-
-/* Get the internal allocator and its pointer */
-void _dom_document_get_allocator(struct dom_document *doc,
- dom_alloc *al, void **pw);
-
-/* Create a hash_table */
-dom_exception _dom_document_create_hashtable(struct dom_document *doc,
- size_t chains, dom_hash_func f, struct dom_hash_table **ht);
+void _dom_document_try_destroy(dom_document *doc);
/* Get a nodelist, creating one if necessary */
-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);
/* Remove a nodelist */
-void _dom_document_remove_nodelist(struct dom_document *doc,
- struct dom_nodelist *list);
+void _dom_document_remove_nodelist(dom_document *doc, dom_nodelist *list);
/* Find element with certain ID in the subtree rooted at root */
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);
/* Set the ID attribute name of this document */
-void _dom_document_set_id_name(struct dom_document *doc,
- struct lwc_string_s *name);
+void _dom_document_set_id_name(dom_document *doc, dom_string *name);
#define _dom_document_get_id_name(d) (d->id_name)
diff --git a/src/core/document_type.c b/src/core/document_type.c
index cfec29d..89a94f4 100644
--- a/src/core/document_type.c
+++ b/src/core/document_type.c
@@ -8,30 +8,30 @@
*/
#include <assert.h>
-
-#include <dom/core/document_type.h>
+#include <stdlib.h>
#include "core/string.h"
#include "core/document_type.h"
+#include "core/namednodemap.h"
#include "core/node.h"
#include "utils/utils.h"
#include "utils/namespace.h"
-#include "utils/resource_mgr.h"
/**
* DOM DocumentType node
*/
struct dom_document_type {
- struct dom_node_internal base; /**< Base node */
+ dom_node_internal base; /**< Base node */
dom_string *public_id; /**< Doctype public ID */
dom_string *system_id; /**< Doctype system ID */
-
- struct dom_resource_mgr res; /**< resource_mgr of this node */
};
static struct dom_document_type_vtable document_type_vtable = {
{
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
},
DOM_DOCUMENT_TYPE_VTABLE
@@ -63,14 +63,13 @@ static struct dom_node_protect_vtable dt_protect_vtable = {
*/
dom_exception _dom_document_type_create(dom_string *qname,
dom_string *public_id, dom_string *system_id,
- dom_alloc alloc, void *pw,
- struct dom_document_type **doctype)
+ dom_document_type **doctype)
{
- struct dom_document_type *result;
+ dom_document_type *result;
dom_exception err;
/* Create node */
- result = alloc(NULL, sizeof(struct dom_document_type), pw);
+ result = malloc(sizeof(dom_document_type));
if (result == NULL)
return DOM_NO_MEM_ERR;
@@ -78,8 +77,12 @@ dom_exception _dom_document_type_create(dom_string *qname,
result->base.base.vtable = &document_type_vtable;
result->base.vtable = &dt_protect_vtable;
- err = _dom_document_type_initialise(result, qname, public_id, system_id,
- alloc, pw);
+ err = _dom_document_type_initialise(result, qname,
+ public_id, system_id);
+ if (err != DOM_NO_ERR) {
+ free(result);
+ return err;
+ }
*doctype = result;
@@ -93,62 +96,37 @@ dom_exception _dom_document_type_create(dom_string *qname,
*
* The contents of ::doctype will be destroyed and ::doctype will be freed.
*/
-void _dom_document_type_destroy(struct dom_node_internal *doctypenode)
+void _dom_document_type_destroy(dom_node_internal *doctypenode)
{
- struct dom_document_type *doctype =
- (struct dom_document_type *)doctypenode;
+ dom_document_type *doctype = (dom_document_type *) doctypenode;
/* Finalise base class */
_dom_document_type_finalise(doctype);
/* Free doctype */
- doctype->res.alloc(doctype, 0, doctype->res.pw);
+ free(doctype);
}
/* Initialise this document_type */
-dom_exception _dom_document_type_initialise(struct dom_document_type *doctype,
+dom_exception _dom_document_type_initialise(dom_document_type *doctype,
dom_string *qname, dom_string *public_id,
- dom_string *system_id, dom_alloc alloc, void *pw)
+ dom_string *system_id)
{
+ dom_string *prefix, *localname;
dom_exception err;
- dom_string *prefix, *localname;
err = _dom_namespace_split_qname(qname, &prefix, &localname);
- if (err != DOM_NO_ERR) {
- alloc(doctype, 0, pw);
+ if (err != DOM_NO_ERR)
return err;
- }
-
- lwc_string *lprefix = NULL, *lname = NULL;
- if (prefix != NULL) {
- err = _dom_string_intern(prefix, &lprefix);
- if (err != DOM_NO_ERR) {
- dom_string_unref(prefix);
- dom_string_unref(localname);
- alloc(doctype, 0, pw);
- return err;
- }
- }
-
- if (localname != NULL) {
- err = _dom_string_intern(localname, &lname);
- if (err != DOM_NO_ERR) {
- dom_string_unref(prefix);
- dom_string_unref(localname);
- if (lprefix != NULL)
- lwc_string_unref(lprefix);
- alloc(doctype, 0, pw);
- return err;
- }
- }
/* TODO: I should figure out how the namespaceURI can be got */
/* Initialise base node */
- err = _dom_node_initialise_generic(&doctype->base, NULL, alloc, pw,
- DOM_DOCUMENT_TYPE_NODE, lname, NULL, NULL, lprefix);
+ err = _dom_node_initialise(&doctype->base, NULL,
+ DOM_DOCUMENT_TYPE_NODE, localname, NULL, NULL, prefix);
if (err != DOM_NO_ERR) {
- alloc(doctype, 0, pw);
+ dom_string_unref(prefix);
+ dom_string_unref(localname);
return err;
}
@@ -166,15 +144,11 @@ dom_exception _dom_document_type_initialise(struct dom_document_type *doctype,
if (localname != NULL)
dom_string_unref(localname);
- /* Fill in allocation information */
- doctype->res.alloc = alloc;
- doctype->res.pw = pw;
-
return DOM_NO_ERR;
}
/* The destructor function of dom_document_type */
-void _dom_document_type_finalise(struct dom_document_type *doctype)
+void _dom_document_type_finalise(dom_document_type *doctype)
{
if (doctype->public_id != NULL)
dom_string_unref(doctype->public_id);
@@ -183,8 +157,7 @@ void _dom_document_type_finalise(struct dom_document_type *doctype)
assert(doctype->base.owner != NULL || doctype->base.user_data == NULL);
- _dom_node_finalise_generic(&doctype->base, doctype->res.alloc,
- doctype->res.pw);
+ _dom_node_finalise(&doctype->base);
}
@@ -206,7 +179,7 @@ void _dom_document_type_finalise(struct dom_document_type *doctype)
* We don't support this API now, so this function call should always
* return DOM_NOT_SUPPORTED_ERR.
*/
-dom_exception _dom_document_type_get_name(struct dom_document_type *doc_type,
+dom_exception _dom_document_type_get_name(dom_document_type *doc_type,
dom_string **result)
{
return dom_node_get_node_name(doc_type, result);
@@ -227,8 +200,8 @@ dom_exception _dom_document_type_get_name(struct dom_document_type *doc_type,
* return DOM_NOT_SUPPORTED_ERR.
*/
dom_exception _dom_document_type_get_entities(
- struct dom_document_type *doc_type,
- struct dom_namednodemap **result)
+ dom_document_type *doc_type,
+ dom_namednodemap **result)
{
UNUSED(doc_type);
UNUSED(result);
@@ -251,8 +224,8 @@ dom_exception _dom_document_type_get_entities(
* return DOM_NOT_SUPPORTED_ERR.
*/
dom_exception _dom_document_type_get_notations(
- struct dom_document_type *doc_type,
- struct dom_namednodemap **result)
+ dom_document_type *doc_type,
+ dom_namednodemap **result)
{
UNUSED(doc_type);
UNUSED(result);
@@ -275,7 +248,7 @@ dom_exception _dom_document_type_get_notations(
* return DOM_NOT_SUPPORTED_ERR.
*/
dom_exception _dom_document_type_get_public_id(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result)
{
UNUSED(doc_type);
@@ -299,7 +272,7 @@ dom_exception _dom_document_type_get_public_id(
* return DOM_NOT_SUPPORTED_ERR.
*/
dom_exception _dom_document_type_get_system_id(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result)
{
UNUSED(doc_type);
@@ -323,7 +296,7 @@ dom_exception _dom_document_type_get_system_id(
* return DOM_NOT_SUPPORTED_ERR.
*/
dom_exception _dom_document_type_get_internal_subset(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result)
{
UNUSED(doc_type);
@@ -337,42 +310,17 @@ dom_exception _dom_document_type_get_internal_subset(
/* Overload protected virtual functions */
/* The virtual destroy function of this class */
-void _dom_dt_destroy(struct dom_node_internal *node)
+void _dom_dt_destroy(dom_node_internal *node)
{
_dom_document_type_destroy(node);
}
-/* The memory allocator of this class */
-dom_exception _dom_dt_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
-{
- UNUSED(doc);
- UNUSED(n);
- UNUSED(ret);
-
- return DOM_NOT_SUPPORTED_ERR;
-}
-
/* The copy constructor of this class */
-dom_exception _dom_dt_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
+dom_exception _dom_dt_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(new);
UNUSED(old);
+ UNUSED(copy);
return DOM_NOT_SUPPORTED_ERR;
}
-
-/*----------------------------------------------------------------------*/
-
-/* Helper functions */
-
-/* Get the resource manager of this object */
-void _dom_document_type_get_resource_mgr(
- struct dom_document_type *dt, struct dom_resource_mgr *rm)
-{
- rm->alloc = dt->res.alloc;
- rm->pw = dt->res.pw;
-}
-
diff --git a/src/core/document_type.h b/src/core/document_type.h
index 7fcac8e..e9b1dd5 100644
--- a/src/core/document_type.h
+++ b/src/core/document_type.h
@@ -8,39 +8,39 @@
#ifndef dom_internal_core_document_type_h_
#define dom_internal_core_document_type_h_
-struct dom_document_type;
-struct dom_resource_mgr;
+#include <dom/core/document_type.h>
+
+struct dom_namednodemap;
/* Create a DOM document type */
dom_exception _dom_document_type_create(dom_string *qname,
dom_string *public_id,
dom_string *system_id,
- dom_alloc alloc, void *pw,
- struct dom_document_type **doctype);
+ dom_document_type **doctype);
/* Destroy a document type */
-void _dom_document_type_destroy(struct dom_node_internal *doctypenode);
-dom_exception _dom_document_type_initialise(struct dom_document_type *doctype,
+void _dom_document_type_destroy(dom_node_internal *doctypenode);
+dom_exception _dom_document_type_initialise(dom_document_type *doctype,
dom_string *qname, dom_string *public_id,
- dom_string *system_id, dom_alloc alloc, void *pw);
-void _dom_document_type_finalise(struct dom_document_type *doctype);
+ dom_string *system_id);
+void _dom_document_type_finalise(dom_document_type *doctype);
/* The virtual functions of DocumentType */
-dom_exception _dom_document_type_get_name(struct dom_document_type *doc_type,
+dom_exception _dom_document_type_get_name(dom_document_type *doc_type,
dom_string **result);
dom_exception _dom_document_type_get_entities(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
struct dom_namednodemap **result);
dom_exception _dom_document_type_get_notations(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
struct dom_namednodemap **result);
dom_exception _dom_document_type_get_public_id(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result);
dom_exception _dom_document_type_get_system_id(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result);
dom_exception _dom_document_type_get_internal_subset(
- struct dom_document_type *doc_type,
+ dom_document_type *doc_type,
dom_string **result);
#define DOM_DOCUMENT_TYPE_VTABLE \
@@ -52,19 +52,11 @@ dom_exception _dom_document_type_get_internal_subset(
_dom_document_type_get_internal_subset
/* Following comes the protected vtable */
-void _dom_dt_destroy(struct dom_node_internal *node);
-dom_exception _dom_dt_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_dt_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_dt_destroy(dom_node_internal *node);
+dom_exception _dom_dt_copy(dom_node_internal *old, dom_node_internal **copy);
#define DOM_DT_PROTECT_VTABLE \
_dom_dt_destroy, \
- _dom_dt_alloc, \
_dom_dt_copy
-/* Helper functions */
-void _dom_document_type_get_resource_mgr(
- struct dom_document_type *dt, struct dom_resource_mgr *rm);
-
#endif
diff --git a/src/core/element.c b/src/core/element.c
index 580cb41..230087a 100644
--- a/src/core/element.c
+++ b/src/core/element.c
@@ -6,9 +6,10 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
+#include <assert.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
-#include <assert.h>
#include <dom/dom.h>
#include <dom/core/attr.h>
@@ -37,6 +38,9 @@
struct dom_element_vtable _dom_element_vtable = {
{
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE_ELEMENT
},
DOM_ELEMENT_VTABLE
@@ -110,13 +114,32 @@ static struct nnm_operation attributes_opt = {
attributes_equal
};
-static void *_key(void *key, void *key_pw, dom_alloc alloc, void *pw,
- bool clone);
-static void *_value(void *value, void *value_pw, dom_alloc alloc,
- void *pw, bool clone);
-static void *_nsattributes(void *value, void *value_pw, dom_alloc alloc,
- void *pw, bool clone);
+static uint32_t attributes_hash(void *key, void *pw);
+static void *attributes_clone_key(void *key, void *pw);
+static void attributes_destroy_key(void *key, void *pw);
+static void *attributes_clone_value(void *value, void *pw);
+static void attributes_destroy_value(void *value, void *pw);
+static void *attributes_ns_clone_value(void *value, void *pw);
+static void attributes_ns_destroy_value(void *value, void *pw);
+static bool attributes_key_isequal(void *key1, void *key2, void *pw);
+
+static const struct dom_hash_vtable attributes_vtable = {
+ attributes_hash,
+ attributes_clone_key,
+ attributes_destroy_key,
+ attributes_clone_value,
+ attributes_destroy_value,
+ attributes_key_isequal
+};
+static const struct dom_hash_vtable attributes_ns_vtable = {
+ attributes_hash,
+ attributes_clone_key,
+ attributes_destroy_key,
+ attributes_ns_clone_value,
+ attributes_ns_destroy_value,
+ attributes_key_isequal
+};
/*----------------------------------------------------------------------*/
/* Constructors and Destructors */
@@ -139,11 +162,11 @@ static void *_nsattributes(void *value, void *value_pw, dom_alloc alloc,
* The returned element will already be referenced.
*/
dom_exception _dom_element_create(struct dom_document *doc,
- struct lwc_string_s *name, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix, struct dom_element **result)
+ dom_string *name, dom_string *namespace,
+ dom_string *prefix, struct dom_element **result)
{
/* Allocate the element */
- *result = _dom_document_alloc(doc, NULL, sizeof(struct dom_element));
+ *result = malloc(sizeof(struct dom_element));
if (*result == NULL)
return DOM_NO_MEM_ERR;
@@ -174,32 +197,35 @@ dom_exception _dom_element_create(struct dom_document *doc,
* The returned element will already be referenced.
*/
dom_exception _dom_element_initialise(struct dom_document *doc,
- struct dom_element *el, struct lwc_string_s *name,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix)
+ struct dom_element *el, dom_string *name,
+ dom_string *namespace, dom_string *prefix)
{
dom_exception err;
assert(doc != NULL);
- err = _dom_document_create_hashtable(doc, CHAINS_ATTRIBUTES,
- _dom_hash_hash_lwcstring, &el->attributes);
- if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, el, 0);
- return err;
+ el->attributes = _dom_hash_create(CHAINS_ATTRIBUTES,
+ &attributes_vtable, NULL);
+ if (el->attributes == NULL) {
+ free(el);
+ return DOM_NO_MEM_ERR;;
}
- err = _dom_document_create_hashtable(doc, CHAINS_NAMESPACE,
- _dom_hash_hash_lwcstring, &el->ns_attributes);
- if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, el, 0);
- _dom_document_alloc(doc, el->attributes, 0);
- return err;
+ el->ns_attributes = _dom_hash_create(CHAINS_NAMESPACE,
+ &attributes_ns_vtable, NULL);
+ if (el->ns_attributes == NULL) {
+ free(el->attributes);
+ free(el);
+ return DOM_NO_MEM_ERR;
}
+
/* Initialise the base class */
err = _dom_node_initialise(&el->base, doc, DOM_ELEMENT_NODE,
name, NULL, namespace, prefix);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, el, 0);
+ free(el->ns_attributes);
+ free(el->attributes);
+ free(el);
return err;
}
@@ -214,20 +240,18 @@ dom_exception _dom_element_initialise(struct dom_document *doc,
/**
* Finalise a dom_element
*
- * \param doc The document
* \param ele The element
*/
-void _dom_element_finalise(struct dom_document *doc, struct dom_element *ele)
+void _dom_element_finalise(struct dom_element *ele)
{
/* Destroy attributes attached to this node */
if (ele->attributes != NULL) {
- _dom_hash_destroy(ele->attributes, _key, NULL, _value, doc);
+ _dom_hash_destroy(ele->attributes);
ele->attributes = NULL;
}
if (ele->ns_attributes != NULL) {
- _dom_hash_destroy(ele->ns_attributes, _key, NULL,
- _nsattributes, doc);
+ _dom_hash_destroy(ele->ns_attributes);
ele->ns_attributes = NULL;
}
@@ -236,24 +260,22 @@ void _dom_element_finalise(struct dom_document *doc, struct dom_element *ele)
}
/* Finalise base class */
- _dom_node_finalise(doc, &ele->base);
+ _dom_node_finalise(&ele->base);
}
/**
* Destroy an element
*
- * \param doc The owning document
* \param element The element to destroy
*
* The contents of ::element will be destroyed and ::element will be freed.
*/
-void _dom_element_destroy(struct dom_document *doc,
- struct dom_element *element)
+void _dom_element_destroy(struct dom_element *element)
{
- _dom_element_finalise(doc, element);
+ _dom_element_finalise(element);
/* Free the element */
- _dom_document_alloc(doc, element, 0);
+ free(element);
}
/*----------------------------------------------------------------------*/
@@ -410,20 +432,14 @@ dom_exception _dom_element_get_elements_by_tag_name(
struct dom_nodelist **result)
{
dom_exception err;
- lwc_string *n;
dom_node_internal *base = (dom_node_internal *) element;
assert(base->owner != NULL);
- err = _dom_string_intern(name, &n);
- if (err != DOM_NO_ERR)
- return err;
-
err = _dom_document_get_nodelist(base->owner, DOM_NODELIST_BY_NAME,
- (struct dom_node_internal *) element, n, NULL,
+ (struct dom_node_internal *) element, name, NULL,
NULL, result);
- lwc_string_unref(n);
return err;
}
@@ -448,19 +464,12 @@ dom_exception _dom_element_get_attribute_ns(struct dom_element *element,
dom_string *namespace, dom_string *localname,
dom_string **value)
{
- lwc_string *str;
- dom_exception err;
struct dom_hash_table *attrs;
if (namespace == NULL)
return _dom_element_get_attribute(element, localname, value);
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
*value = NULL;
@@ -503,7 +512,6 @@ dom_exception _dom_element_set_attribute_ns(struct dom_element *element,
dom_string *namespace, dom_string *qname,
dom_string *value)
{
- lwc_string *str;
dom_exception err;
struct dom_hash_table *attrs;
bool added;
@@ -530,26 +538,22 @@ dom_exception _dom_element_set_attribute_ns(struct dom_element *element,
return err;
}
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
dom_document *doc;
doc = dom_node_get_owner(element);
assert(doc != NULL);
- err = _dom_document_create_hashtable(doc, CHAINS_NS_ATTRIBUTES,
- _dom_hash_hash_lwcstring, &attrs);
- if (err != DOM_NO_ERR)
- return err;
+ attrs = _dom_hash_create(CHAINS_NS_ATTRIBUTES,
+ &attributes_vtable, NULL);
+ if (attrs == NULL)
+ return DOM_NO_MEM_ERR;
- added = _dom_hash_add(element->ns_attributes, str, attrs,
+ added = _dom_hash_add(element->ns_attributes, namespace, attrs,
false);
if (added == false)
return DOM_NO_MEM_ERR;
+ dom_string_ref(namespace);
}
return _dom_element_set_attr(element, attrs, localname, value);
@@ -572,19 +576,12 @@ dom_exception _dom_element_set_attribute_ns(struct dom_element *element,
dom_exception _dom_element_remove_attribute_ns(struct dom_element *element,
dom_string *namespace, dom_string *localname)
{
- lwc_string *str;
- dom_exception err;
struct dom_hash_table *attrs;
if (namespace != NULL)
return _dom_element_remove_attribute(element, localname);
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
return DOM_NO_ERR;
@@ -614,8 +611,6 @@ dom_exception _dom_element_get_attribute_node_ns(struct dom_element *element,
dom_string *namespace, dom_string *localname,
struct dom_attr **result)
{
- lwc_string *str;
- dom_exception err;
struct dom_hash_table *attrs;
if (namespace == NULL) {
@@ -623,12 +618,7 @@ dom_exception _dom_element_get_attribute_node_ns(struct dom_element *element,
result);
}
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
*result = NULL;
@@ -663,7 +653,6 @@ dom_exception _dom_element_get_attribute_node_ns(struct dom_element *element,
dom_exception _dom_element_set_attribute_node_ns(struct dom_element *element,
struct dom_attr *attr, struct dom_attr **result)
{
- lwc_string *str;
dom_exception err;
struct dom_hash_table *attrs;
bool added;
@@ -676,26 +665,22 @@ dom_exception _dom_element_set_attribute_node_ns(struct dom_element *element,
if (namespace == NULL)
return _dom_element_set_attribute_node(element, attr, result);
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
dom_document *doc;
doc = dom_node_get_owner(element);
assert(doc != NULL);
- err = _dom_document_create_hashtable(doc, CHAINS_NS_ATTRIBUTES,
- _dom_hash_hash_lwcstring, &attrs);
- if (err != DOM_NO_ERR)
- return err;
+ attrs = _dom_hash_create(CHAINS_NS_ATTRIBUTES,
+ &attributes_vtable, NULL);
+ if (attrs == NULL)
+ return DOM_NO_MEM_ERR;
- added = _dom_hash_add(element->ns_attributes, str, attrs,
+ added = _dom_hash_add(element->ns_attributes, namespace, attrs,
false);
if (added == false)
return DOM_NO_MEM_ERR;
+ dom_string_ref(namespace);
}
dom_string *localname;
@@ -734,32 +719,12 @@ dom_exception _dom_element_get_elements_by_tag_name_ns(
/** \todo ensure XML feature is supported */
- /* Get the interned string from the dom_string */
- lwc_string *l = NULL, *n = NULL;
- 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(element->base.owner,
DOM_NODELIST_BY_NAMESPACE,
- (struct dom_node_internal *) element, NULL, n, l,
+ (struct dom_node_internal *) element, NULL,
+ namespace, localname,
result);
- if (localname != NULL)
- lwc_string_unref(l);
- if (namespace != NULL)
- lwc_string_unref(n);
-
return err;
}
@@ -796,19 +761,12 @@ dom_exception _dom_element_has_attribute_ns(struct dom_element *element,
dom_string *namespace, dom_string *localname,
bool *result)
{
- lwc_string *str;
- dom_exception err;
struct dom_hash_table *attrs;
if (namespace == NULL)
return _dom_element_has_attribute(element, localname, result);
- err = _dom_node_get_intern_string(&element->base, namespace, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attrs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- str);
+ attrs = _dom_hash_get(element->ns_attributes, namespace);
/* The element has no such namespace */
if (attrs == NULL) {
*result = false;
@@ -878,24 +836,18 @@ dom_exception _dom_element_set_id_attribute_ns(struct dom_element *element,
{
struct dom_hash_table *hs;
dom_exception err;
- lwc_string *ns;
if (namespace == NULL)
return _dom_element_set_id_attribute(element, localname, is_id);
- err = _dom_node_get_intern_string(&element->base, namespace, &ns);
- if (err != DOM_NO_ERR)
- return err;
-
- hs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- ns);
+ hs = _dom_hash_get(element->ns_attributes, namespace);
assert(hs != NULL);
err = _dom_element_set_id_attr(element, hs, localname, is_id);
if (err != DOM_NO_ERR)
return err;
- element->id_ns = ns;
+ element->id_ns = dom_string_ref(namespace);
return DOM_NO_ERR;
}
@@ -916,7 +868,6 @@ dom_exception _dom_element_set_id_attribute_node(struct dom_element *element,
{
struct dom_hash_table *hs;
dom_exception err;
- lwc_string *ns;
dom_string *namespace;
dom_string *localname;
@@ -927,19 +878,14 @@ dom_exception _dom_element_set_id_attribute_node(struct dom_element *element,
if (err != DOM_NO_ERR)
return err;
- err = _dom_node_get_intern_string(&element->base, namespace, &ns);
- if (err != DOM_NO_ERR)
- return err;
-
- hs = (struct dom_hash_table *) _dom_hash_get(element->ns_attributes,
- ns);
+ hs = _dom_hash_get(element->ns_attributes, namespace);
assert(hs != NULL);
err = _dom_element_set_id_attr(element, hs, localname, is_id);
if (err != DOM_NO_ERR)
return err;
- element->id_ns = ns;
+ element->id_ns = namespace;
return DOM_NO_ERR;
@@ -1017,17 +963,11 @@ dom_exception _dom_element_is_default_namespace(dom_node_internal *node,
dom_string *namespace, bool *result)
{
struct dom_element *ele = (struct dom_element *) node;
- lwc_string *ns;
dom_string *value;
dom_exception err;
- err = _dom_node_get_intern_string(node, namespace, &ns);
- if (err != DOM_NO_ERR) {
- return err;
- }
if (node->prefix == NULL) {
- lwc_string_isequal(node->namespace, ns, result);
- lwc_string_unref(ns);
+ *result = dom_string_isequal(node->namespace, namespace);
return DOM_NO_ERR;
}
@@ -1038,23 +978,16 @@ dom_exception _dom_element_is_default_namespace(dom_node_internal *node,
return err;
if (has == true) {
- return dom_element_get_attribute(ele, xmlns, &value);
- }
+ err = dom_element_get_attribute(ele, xmlns, &value);
+ if (err != DOM_NO_ERR)
+ return err;
+
+ *result = dom_string_isequal(value, namespace);
- lwc_string *ns2;
- err = _dom_node_get_intern_string(node, value, &ns2);
- if (err != DOM_NO_ERR) {
- return err;
- }
-
- if (ns2 != NULL) {
- lwc_string_isequal(ns2, ns, result);
- lwc_string_unref(ns);
- lwc_string_unref(ns2);
dom_string_unref(value);
+
return DOM_NO_ERR;
}
-
return dom_node_is_default_namespace(node->parent, namespace, result);
}
@@ -1070,25 +1003,17 @@ dom_exception _dom_element_is_default_namespace(dom_node_internal *node,
dom_exception _dom_element_lookup_namespace(dom_node_internal *node,
dom_string *prefix, dom_string **result)
{
- lwc_string *pf;
dom_exception err;
- err = _dom_node_get_intern_string(node, prefix, &pf);
- if (err != DOM_NO_ERR)
- return err;
-
- /* To some extent, directly compare the two lwc_string pointer
- * is better */
- if (node->namespace != NULL && node->prefix == pf) {
- assert(node->owner != NULL);
- return _dom_document_create_string_from_lwcstring(node->owner,
- pf, result);
+ if (node->namespace != NULL &&
+ dom_string_isequal(node->prefix, prefix)) {
+ *result = dom_string_ref(node->namespace);
+ return DOM_NO_ERR;
}
bool has;
dom_string *xmlns = _dom_namespace_get_xmlns_prefix();
- err = dom_element_has_attribute_ns(node,
- dom_namespaces[DOM_NAMESPACE_XMLNS], prefix, &has);
+ err = dom_element_has_attribute_ns(node, xmlns, prefix, &has);
if (err != DOM_NO_ERR)
return err;
@@ -1153,26 +1078,7 @@ dom_exception _dom_element_parse_attribute(dom_element *ele,
/* The destroy virtual function of dom_element */
void __dom_element_destroy(struct dom_node_internal *node)
{
- struct dom_document *doc = dom_node_get_owner(node);
-
- _dom_element_destroy(doc, (struct dom_element *) node);
-}
-
-/* The memory allocator of this class */
-dom_exception _dom_element_alloc(dom_document *doc, struct dom_node_internal *n,
- struct dom_node_internal **ret)
-{
- dom_element *e;
- UNUSED(n);
-
- e = _dom_document_alloc(doc, NULL, sizeof(struct dom_element));
- if (e == NULL)
- return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) e;
- dom_node_set_owner(*ret, doc);
-
- return DOM_NO_ERR;
+ _dom_element_destroy((struct dom_element *) node);
}
/* TODO: How to deal with default attribue:
@@ -1183,46 +1089,42 @@ dom_exception _dom_element_alloc(dom_document *doc, struct dom_node_internal *n,
* are all specified. For the methods like importNode and adoptNode,
* this will make _dom_element_copy can be used in them.
*/
-dom_exception _dom_element_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
+dom_exception _dom_element_copy(dom_node_internal *old,
+ dom_node_internal **copy)
{
- dom_element *ne = (dom_element *) new;
- dom_element *oe = (dom_element *) old;
- dom_document *od, *nd;
+ dom_element *olde = (dom_element *) old;
+ dom_element *e;
struct dom_hash_table *ht;
dom_exception err;
+
+ e = malloc(sizeof(dom_element));
+ if (e == NULL)
+ return DOM_NO_MEM_ERR;
- err = _dom_node_copy(new, old);
- if (err != DOM_NO_ERR)
+ err = dom_node_copy_internal(old, e);
+ if (err != DOM_NO_ERR) {
+ free(e);
return err;
+ }
- od = dom_node_get_owner(old);
- nd = dom_node_get_owner(new);
- assert(od != NULL);
- assert(nd != NULL);
-
- dom_alloc alloc;
- void *pw;
- _dom_document_get_allocator(nd, &alloc, &pw);
-
/* Copy the hash tables */
- ht = _dom_hash_clone(oe->attributes, alloc, pw, _key, NULL,
- _value, nd);
+ ht = _dom_hash_clone(olde->attributes);
if (ht == NULL)
return DOM_NO_MEM_ERR;
- ne->attributes = ht;
+ e->attributes = ht;
- ht = _dom_hash_clone(oe->ns_attributes, alloc, pw, _key, NULL,
- _nsattributes, nd);
+ ht = _dom_hash_clone(olde->ns_attributes);
if (ht == NULL)
return DOM_NO_MEM_ERR;
- ne->ns_attributes = ht;
+ e->ns_attributes = ht;
- ne->id_ns = NULL;
- ne->id_name = NULL;
+ e->id_ns = NULL;
+ e->id_name = NULL;
/* TODO: deal with dom_type_info, it get no definition ! */
+ *copy = (dom_node_internal *) e;
+
return DOM_NO_ERR;
}
@@ -1247,14 +1149,10 @@ dom_exception _dom_element_get_attr(struct dom_element *element,
{
void *a;
dom_exception err = DOM_NO_ERR;
- lwc_string *str;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
+ UNUSED(element);
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
/* Fill in value */
if (a == NULL) {
@@ -1280,8 +1178,6 @@ dom_exception _dom_element_set_attr(struct dom_element *element,
dom_string *value)
{
void *a;
- dom_exception err;
- lwc_string *str;
bool added;
dom_node_internal *e = (dom_node_internal *) element;
@@ -1292,12 +1188,7 @@ dom_exception _dom_element_set_attr(struct dom_element *element,
if (_dom_node_readonly(e))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
if (a != NULL) {
/* Found an existing attribute, so replace its value */
@@ -1333,7 +1224,7 @@ dom_exception _dom_element_set_attr(struct dom_element *element,
dom_exception err;
struct dom_attr *attr;
- err = _dom_attr_create(e->owner, str, NULL, NULL, true, &attr);
+ err = _dom_attr_create(e->owner, name, NULL, NULL, true, &attr);
if (err != DOM_NO_ERR)
return err;
@@ -1360,23 +1251,22 @@ dom_exception _dom_element_set_attr(struct dom_element *element,
return err;
}
- err = _dom_dispatch_node_change_event(doc,
- (dom_event_target *) attr,
- (dom_event_target *) element,
- DOM_MUTATION_ADDITION, &success);
+ err = dom_node_dispatch_node_change_event(doc,
+ attr, element, DOM_MUTATION_ADDITION, &success);
if (err != DOM_NO_ERR) {
dom_node_set_parent(attr, NULL);
dom_node_unref(attr);
return err;
}
- added = _dom_hash_add(hs, str, attr, false);
+ added = _dom_hash_add(hs, name, attr, false);
if (added == false) {
/* If we failed at this step, there must be no memory */
dom_node_set_parent(attr, NULL);
dom_node_unref(attr);
return DOM_NO_MEM_ERR;
}
+ dom_string_ref(name);
dom_node_unref(attr);
dom_node_remove_pending(attr);
@@ -1404,34 +1294,26 @@ dom_exception _dom_element_remove_attr(struct dom_element *element,
{
void *a;
dom_exception err;
- lwc_string *str;
dom_node_internal *e = (dom_node_internal *) element;
/* Ensure element can be written to */
if (_dom_node_readonly(e))
return DOM_NO_MODIFICATION_ALLOWED_ERR;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- a = (dom_node_internal *) _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
/* Detach attr node from list */
if (a != NULL) {
/* Disptach DOMNodeRemoval event */
bool success = true;
struct dom_document *doc = dom_node_get_owner(element);
- err = _dom_dispatch_node_change_event(doc,
- (dom_event_target *) a,
- (dom_event_target *) element,
- DOM_MUTATION_REMOVAL, &success);
+ err = dom_node_dispatch_node_change_event(doc,
+ a, element, DOM_MUTATION_REMOVAL, &success);
if (err != DOM_NO_ERR)
return err;
/* Delete the attribute node */
- _dom_hash_del(hs, str);
+ _dom_hash_del(hs, name);
/* Claim a reference for later event dispatch */
dom_node_ref(a);
@@ -1485,15 +1367,10 @@ dom_exception _dom_element_get_attr_node(struct dom_element *element,
struct dom_attr **result)
{
void *a;
- dom_exception err;
- lwc_string *str;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
+ UNUSED(element);
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
/* Fill in value */
if (a == NULL) {
@@ -1528,7 +1405,6 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
struct dom_attr **result)
{
dom_exception err;
- lwc_string *str = NULL;
dom_string *name = NULL;
bool added;
dom_node_internal *e = (dom_node_internal *) element;
@@ -1552,29 +1428,21 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
if (err != DOM_NO_ERR)
return err;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
*result = NULL;
if (a != NULL) {
/* Disptach DOMNodeRemoval event */
bool success = true;
struct dom_document *doc = dom_node_get_owner(element);
- err = _dom_dispatch_node_change_event(doc,
- (dom_event_target *) a,
- (dom_event_target *) element,
- DOM_MUTATION_REMOVAL, &success);
+ err = dom_node_dispatch_node_change_event(doc,
+ a, element, DOM_MUTATION_REMOVAL, &success);
if (err != DOM_NO_ERR) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return err;
}
- _dom_hash_del(hs, str);
+ _dom_hash_del(hs, name);
dom_node_ref(a);
*result = (dom_attr *) a;
dom_node_set_parent(a, NULL);
@@ -1590,7 +1458,6 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
if (err != DOM_NO_ERR && err != DOM_NOT_SUPPORTED_ERR) {
dom_node_unref(a);
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return err;
}
err = _dom_dispatch_attr_modified_event(doc, e, old, NULL,
@@ -1599,7 +1466,6 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
dom_string_unref(old);
if (err != DOM_NO_ERR) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return err;
}
@@ -1608,18 +1474,17 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
(dom_event_target *) e, &success);
if (err != DOM_NO_ERR) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return err;
}
}
- added = _dom_hash_add(hs, str, attr, false);
+ added = _dom_hash_add(hs, name, attr, false);
if (added == false) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
/* If we failed at this step, there must be no memory */
return DOM_NO_MEM_ERR;
}
+ dom_string_ref(name);
dom_node_set_parent(attr, element);
dom_node_remove_pending(attr);
@@ -1639,14 +1504,12 @@ dom_exception _dom_element_set_attr_node(struct dom_element *element,
/* Cleanup */
dom_string_unref(new);
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
if (err != DOM_NO_ERR) {
return err;
}
- err = _dom_dispatch_node_change_event(doc, (dom_event_target *) attr,
- (dom_event_target *) element, DOM_MUTATION_ADDITION,
- &success);
+ err = dom_node_dispatch_node_change_event(doc, attr, element,
+ DOM_MUTATION_ADDITION, &success);
if (err != DOM_NO_ERR)
return err;
@@ -1680,7 +1543,6 @@ dom_exception _dom_element_remove_attr_node(struct dom_element *element,
{
void *a;
dom_exception err;
- lwc_string *str;
dom_string *name;
dom_node_internal *e = (dom_node_internal *) element;
@@ -1692,40 +1554,31 @@ dom_exception _dom_element_remove_attr_node(struct dom_element *element,
if (err != DOM_NO_ERR)
return err;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
/** \todo defaulted attribute handling */
if (a == NULL || a != (void *) attr) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return DOM_NOT_FOUND_ERR;
}
/* Dispatch a DOMNodeRemoved event */
bool success = true;
struct dom_document *doc = dom_node_get_owner(element);
- err = _dom_dispatch_node_change_event(doc, (dom_event_target *) a,
- (dom_event_target *) element, DOM_MUTATION_REMOVAL,
- &success);
+ err = dom_node_dispatch_node_change_event(doc, a, element,
+ DOM_MUTATION_REMOVAL, &success);
if (err != DOM_NO_ERR) {
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
return err;
}
/* Delete the attribute node */
- _dom_hash_del(hs, str);
+ _dom_hash_del(hs, name);
dom_node_ref(a);
- /* Now, cleaup the dom_string and lwc_string */
+ /* Now, cleaup the dom_string */
dom_string_unref(name);
- _dom_node_unref_intern_string(&element->base, str);
/* Dispatch a DOMAttrModified event */
dom_string *old = NULL;
@@ -1777,15 +1630,10 @@ dom_exception _dom_element_has_attr(struct dom_element *element,
bool *result)
{
void *a;
- dom_exception err;
- lwc_string *str;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
+ UNUSED(element);
- a = _dom_hash_get(hs, str);
+ a = _dom_hash_get(hs, name);
/* Fill in value */
if (a == NULL) {
@@ -1810,16 +1658,9 @@ dom_exception _dom_element_set_id_attr(struct dom_element *element,
struct dom_hash_table *hs, dom_string *name, bool is_id)
{
dom_attr *attr;
- lwc_string *str;
- dom_exception err;
struct dom_hash_table *oh;
- /* Looking for name */
- err = _dom_node_get_intern_string(&element->base, name, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- attr = (dom_attr *) _dom_hash_get(hs, str);
+ attr = _dom_hash_get(hs, name);
if (attr == NULL)
return DOM_NOT_FOUND_ERR;
@@ -1827,15 +1668,15 @@ dom_exception _dom_element_set_id_attr(struct dom_element *element,
/* Firstly, clear the previous id attribute if there is one */
if (element->id_ns != NULL) {
assert(element->id_name != NULL);
- oh = (struct dom_hash_table *) _dom_hash_get(
- element->ns_attributes, element->id_ns);
+ oh = _dom_hash_get(element->ns_attributes,
+ element->id_ns);
} else {
oh = element->attributes;
}
assert(oh != NULL);
if (element->id_name != NULL) {
- attr = (dom_attr *) _dom_hash_get(oh, element->id_name);
+ attr = _dom_hash_get(oh, element->id_name);
assert(attr != NULL);
_dom_attr_set_isid(attr, false);
}
@@ -1843,7 +1684,7 @@ dom_exception _dom_element_set_id_attr(struct dom_element *element,
_dom_attr_set_isid(attr, is_id);
- element->id_name = str;
+ element->id_name = dom_string_ref(name);
return DOM_NO_ERR;
}
@@ -1855,7 +1696,7 @@ dom_exception _dom_element_set_id_attr(struct dom_element *element,
* \param id The ID of this element
* \return DOM_NO_ERR on success, appropriate dom_exception on failure.
*/
-dom_exception _dom_element_get_id(struct dom_element *ele, lwc_string **id)
+dom_exception _dom_element_get_id(struct dom_element *ele, dom_string **id)
{
dom_exception err;
dom_string *ret = NULL;
@@ -1868,32 +1709,13 @@ dom_exception _dom_element_get_id(struct dom_element *ele, lwc_string **id)
doc = dom_node_get_owner(ele);
assert(doc != NULL);
- dom_string *namespace, *name;
- err = _dom_document_create_string_from_lwcstring(doc,
- ele->id_ns, &namespace);
- if (err != DOM_NO_ERR)
- return err;
-
- err = _dom_document_create_string_from_lwcstring(doc,
- ele->id_name, &name);
- if (err != DOM_NO_ERR) {
- dom_string_unref(namespace);
- return err;
- }
-
- err = _dom_element_get_attribute_ns(ele, namespace, name, &ret);
+ err = _dom_element_get_attribute_ns(ele, ele->id_ns,
+ ele->id_name, &ret);
if (err != DOM_NO_ERR) {
- dom_string_unref(namespace);
- dom_string_unref(name);
return err;
}
- dom_string_unref(namespace);
- dom_string_unref(name);
-
- err = _dom_node_get_intern_string((dom_node_internal *) ele,
- ret, id);
- dom_string_unref(ret);
+ *id = ret;
return err;
}
@@ -1903,37 +1725,24 @@ dom_exception _dom_element_get_id(struct dom_element *ele, lwc_string **id)
dom_string *name;
if (ele->id_name != NULL) {
- err = _dom_document_create_string_from_lwcstring(doc,
- ele->id_name, &name);
- if (err != DOM_NO_ERR) {
- return err;
- }
+ name = ele->id_name;
} else {
- lwc_string *id_name = _dom_document_get_id_name(doc);
- if (id_name == NULL) {
+ name = _dom_document_get_id_name(doc);
+
+ if (name == NULL) {
/* No ID attribute at all, just return NULL */
*id = NULL;
return DOM_NO_ERR;
}
- err = _dom_document_create_string_from_lwcstring(doc, id_name,
- &name);
- if (err != DOM_NO_ERR) {
- return err;
- }
}
err = _dom_element_get_attribute(ele, name, &ret);
if (err != DOM_NO_ERR) {
- dom_string_unref(name);
return err;
}
- dom_string_unref(name);
-
if (ret != NULL) {
- err = _dom_node_get_intern_string((dom_node_internal *) ele,
- ret, id);
- dom_string_unref(ret);
+ *id = ret;
} else {
*id = NULL;
}
@@ -1951,7 +1760,7 @@ dom_exception attributes_get_length(void *priv,
unsigned long *length)
{
unsigned int ret = 0;
- unsigned int c1, *c2 = NULL;
+ uintptr_t c1, *c2 = NULL;
void *key, *value;
dom_element *e = (dom_element *) priv;
@@ -2023,7 +1832,7 @@ dom_exception attributes_item(void *priv,
unsigned int len;
dom_element *e = (dom_element *) priv;
void *key, *value;
- unsigned int c1, *c2 = NULL;
+ uintptr_t c1, *c2 = NULL;
len = _dom_hash_get_length(e->attributes);
if (num <= len) {
@@ -2135,85 +1944,80 @@ bool attributes_equal(void *p1, void *p2)
}
/*------------------ End of namednodemap functions -----------------------*/
-/* The key_func of the hash table, see utils/hashtable.h for details */
-void *_key(void *key, void *key_pw, dom_alloc alloc, void *pw,
- bool clone)
+uint32_t attributes_hash(void *key, void *pw)
{
+ UNUSED(pw);
+
+ return dom_string_hash(key);
+}
+
+void *attributes_clone_key(void *key, void *pw)
+{
+ UNUSED(pw);
+
assert(key != NULL);
- UNUSED(key_pw);
- UNUSED(alloc);
+ return dom_string_ref(key);
+}
+
+void attributes_destroy_key(void *key, void *pw)
+{
UNUSED(pw);
- if (clone == false) {
- lwc_string_unref((lwc_string *) key);
- return NULL;
- } else {
- lwc_error err;
- lwc_string *ret;
- const char *data = lwc_string_data((lwc_string *) key);
- size_t len = lwc_string_length((lwc_string *) key);
- err = lwc_intern_string(data, len, &ret);
- if (err != lwc_error_ok)
- return NULL;
-
- return ret;
- }
+ assert(key != NULL);
+
+ dom_string_unref(key);
}
-/* The value_func of the hash table, see utils/hashtable.h for details */
-void *_value(void *value, void *value_pw, dom_alloc alloc,
- void *pw, bool clone)
+void *attributes_clone_value(void *value, void *pw)
{
- assert(value != NULL);
- assert(value_pw != NULL);
+ dom_exception err;
+ dom_node *node;
- UNUSED(alloc);
UNUSED(pw);
- if (clone == false) {
- dom_node_internal *a = (dom_node_internal *) value;
- a->parent = NULL;
- dom_node_try_destroy(a);
- return NULL;
- } else {
- dom_exception err;
- dom_node *node;
+ assert(value != NULL);
- err = dom_document_import_node((dom_document *) value_pw, value,
- true, &node);
- if (err != DOM_NO_ERR)
- return NULL;
+ err = dom_node_clone_node(value, true, &node);
+ if (err != DOM_NO_ERR)
+ return NULL;
- return node;
- }
+ return node;
}
-/* The value_func of the hash table, see utils/hashtable.h for details */
-void *_nsattributes(void *value, void *value_pw, dom_alloc alloc,
- void *pw, bool clone)
+void attributes_destroy_value(void *value, void *pw)
{
+ dom_node_internal *a = (dom_node_internal *) value;
+
+ UNUSED(pw);
+
assert(value != NULL);
- assert(value_pw != NULL);
- UNUSED(alloc);
+ a->parent = NULL;
+ dom_node_try_destroy(a);
+}
+
+void *attributes_ns_clone_value(void *value, void *pw)
+{
UNUSED(pw);
- if (clone == false) {
- _dom_hash_destroy((struct dom_hash_table *) value, _key,
- value_pw, _value, value_pw);
- return NULL;
- } else {
- dom_document *doc = (dom_document *) value_pw;
- dom_alloc alloc;
- void *pw;
- struct dom_hash_table *ret = NULL;
- _dom_document_get_allocator(doc, &alloc, &pw);
+ assert(value != NULL);
- ret = _dom_hash_clone((struct dom_hash_table *) value, alloc,
- pw, _key, NULL, _value, doc);
+ return _dom_hash_clone(value);
+}
- return ret;
- }
+void attributes_ns_destroy_value(void *value, void *pw)
+{
+ UNUSED(pw);
+
+ assert(value != NULL);
+
+ _dom_hash_destroy(value);
}
+bool attributes_key_isequal(void *key1, void *key2, void *pw)
+{
+ UNUSED(pw);
+
+ return dom_string_isequal(key1, key2);
+}
diff --git a/src/core/element.h b/src/core/element.h
index 66d1a50..77a040b 100644
--- a/src/core/element.h
+++ b/src/core/element.h
@@ -33,25 +33,24 @@ struct dom_element {
struct dom_hash_table *ns_attributes;
/**< Attributes with prefix */
- struct lwc_string_s *id_ns; /**< The id attribute's namespace */
+ dom_string *id_ns; /**< The id attribute's namespace */
- struct lwc_string_s *id_name; /**< The id attribute's name */
+ dom_string *id_name; /**< The id attribute's name */
struct dom_type_info *schema_type_info; /**< Type information */
};
dom_exception _dom_element_create(struct dom_document *doc,
- struct lwc_string_s *name, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix, struct dom_element **result);
+ dom_string *name, dom_string *namespace,
+ dom_string *prefix, struct dom_element **result);
dom_exception _dom_element_initialise(struct dom_document *doc,
- struct dom_element *el, struct lwc_string_s *name,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix);
+ struct dom_element *el, dom_string *name,
+ dom_string *namespace, dom_string *prefix);
-void _dom_element_finalise(struct dom_document *doc, struct dom_element *ele);
+void _dom_element_finalise(struct dom_element *ele);
-void _dom_element_destroy(struct dom_document *doc,
- struct dom_element *element);
+void _dom_element_destroy(struct dom_element *element);
/* The virtual functions of dom_element */
@@ -212,22 +211,18 @@ dom_exception _dom_element_parse_attribute(dom_element *ele,
dom_string *name, dom_string *value,
dom_string **parsed);
void __dom_element_destroy(dom_node_internal *node);
-dom_exception _dom_element_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_element_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+dom_exception _dom_element_copy(dom_node_internal *old,
+ dom_node_internal **copy);
#define DOM_ELEMENT_PROTECT_VTABLE \
_dom_element_parse_attribute
#define DOM_NODE_PROTECT_VTABLE_ELEMENT \
__dom_element_destroy, \
- _dom_element_alloc, \
_dom_element_copy
/* Helper functions*/
-dom_exception _dom_element_get_id(struct dom_element *ele,
- struct lwc_string_s **id);
+dom_exception _dom_element_get_id(struct dom_element *ele, dom_string **id);
extern struct dom_element_vtable _dom_element_vtable;
diff --git a/src/core/entity_ref.c b/src/core/entity_ref.c
index f35b306..aa32111 100644
--- a/src/core/entity_ref.c
+++ b/src/core/entity_ref.c
@@ -6,7 +6,7 @@
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
-#include <libwapcaplet/libwapcaplet.h>
+#include <stdlib.h>
#include "core/document.h"
#include "core/entity_ref.h"
@@ -17,10 +17,13 @@
* A DOM entity reference
*/
struct dom_entity_reference {
- struct dom_node_internal base; /**< Base node */
+ dom_node_internal base; /**< Base node */
};
static struct dom_node_vtable er_vtable = {
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
};
@@ -42,16 +45,15 @@ static struct dom_node_protect_vtable er_protect_vtable = {
*
* The returned node will already be referenced.
*/
-dom_exception _dom_entity_reference_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_entity_reference **result)
+dom_exception _dom_entity_reference_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_entity_reference **result)
{
- struct dom_entity_reference *e;
+ dom_entity_reference *e;
dom_exception err;
/* Allocate the comment node */
- e = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_entity_reference));
+ e = malloc(sizeof(dom_entity_reference));
if (e == NULL)
return DOM_NO_MEM_ERR;
@@ -62,7 +64,7 @@ dom_exception _dom_entity_reference_create(struct dom_document *doc,
err = _dom_entity_reference_initialise(&e->base, doc,
DOM_ENTITY_REFERENCE_NODE, name, value, NULL, NULL);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, e, 0);
+ free(e);
return err;
}
@@ -74,19 +76,17 @@ dom_exception _dom_entity_reference_create(struct dom_document *doc,
/**
* Destroy an entity reference
*
- * \param doc The owning document
* \param entity The entity reference to destroy
*
* The contents of ::entity will be destroyed and ::entity will be freed.
*/
-void _dom_entity_reference_destroy(struct dom_document *doc,
- struct dom_entity_reference *entity)
+void _dom_entity_reference_destroy(dom_entity_reference *entity)
{
/* Finalise base class */
- _dom_entity_reference_finalise(doc, &entity->base);
+ _dom_entity_reference_finalise(&entity->base);
/* Destroy fragment */
- _dom_document_alloc(doc, entity, 0);
+ free(entity);
}
/**
@@ -101,7 +101,7 @@ void _dom_entity_reference_destroy(struct dom_document *doc,
* finished with it.
*/
dom_exception _dom_entity_reference_get_textual_representation(
- struct dom_entity_reference *entity, dom_string **result)
+ dom_entity_reference *entity, dom_string **result)
{
UNUSED(entity);
UNUSED(result);
@@ -114,34 +114,29 @@ dom_exception _dom_entity_reference_get_textual_representation(
/* Following comes the protected vtable */
/* The virtual destroy function of this class */
-void _dom_er_destroy(struct dom_node_internal *node)
+void _dom_er_destroy(dom_node_internal *node)
{
- _dom_entity_reference_destroy(node->owner,
- (struct dom_entity_reference *) node);
+ _dom_entity_reference_destroy((dom_entity_reference *) node);
}
-/* The memory allocator of this class */
-dom_exception _dom_er_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_er_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(n);
- dom_entity_reference *a;
-
- a = _dom_document_alloc(doc, NULL, sizeof(struct dom_entity_reference));
- if (a == NULL)
+ dom_entity_reference *new_er;
+ dom_exception err;
+
+ new_er = malloc(sizeof(dom_entity_reference));
+ if (new_er == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) a;
- dom_node_set_owner(*ret, doc);
- return DOM_NO_ERR;
-
-}
+ err = dom_node_copy_internal(old, new_er);
+ if (err != DOM_NO_ERR) {
+ free(new_er);
+ return err;
+ }
-/* The copy constructor of this class */
-dom_exception _dom_er_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- return _dom_node_copy(new, old);
+ *copy = (dom_node_internal *) new_er;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/entity_ref.h b/src/core/entity_ref.h
index b83765c..015cef7 100644
--- a/src/core/entity_ref.h
+++ b/src/core/entity_ref.h
@@ -11,35 +11,26 @@
#include <dom/core/exceptions.h>
#include <dom/core/entity_ref.h>
-struct dom_document;
-struct dom_entity_reference;
-struct lwc_string_s;
+dom_exception _dom_entity_reference_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_entity_reference **result);
-dom_exception _dom_entity_reference_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_entity_reference **result);
-
-void _dom_entity_reference_destroy(struct dom_document *doc,
- struct dom_entity_reference *entity);
+void _dom_entity_reference_destroy(dom_entity_reference *entity);
#define _dom_entity_reference_initialise _dom_node_initialise
#define _dom_entity_reference_finalise _dom_node_finalise
/* Following comes the protected vtable */
-void _dom_er_destroy(struct dom_node_internal *node);
-dom_exception _dom_er_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_er_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_er_destroy(dom_node_internal *node);
+dom_exception _dom_er_copy(dom_node_internal *old, dom_node_internal **copy);
#define DOM_ER_PROTECT_VTABLE \
_dom_er_destroy, \
- _dom_er_alloc, \
_dom_er_copy
/* Helper functions */
dom_exception _dom_entity_reference_get_textual_representation(
- struct dom_entity_reference *entity,
+ dom_entity_reference *entity,
dom_string **result);
#endif
diff --git a/src/core/implementation.c b/src/core/implementation.c
index 848457e..ab87e6d 100644
--- a/src/core/implementation.c
+++ b/src/core/implementation.c
@@ -53,9 +53,6 @@ dom_exception dom_implementation_has_feature(
* Document does not support XML
* namespaces.
*
- * Any memory allocated by this call should be allocated using
- * the provided memory (de)allocation function.
- *
* The doctype will be referenced, so the client need not do this
* explicitly. The client must unref the doctype once it has
* finished with it.
@@ -63,7 +60,6 @@ dom_exception dom_implementation_has_feature(
dom_exception dom_implementation_create_document_type(
const char *qname, const char *public_id,
const char *system_id,
- dom_alloc alloc, void *pw,
struct dom_document_type **doctype)
{
struct dom_document_type *d;
@@ -72,7 +68,7 @@ dom_exception dom_implementation_create_document_type(
dom_exception err;
if (qname != NULL) {
- err = dom_string_create(alloc, pw, (const uint8_t *) qname,
+ err = dom_string_create((const uint8_t *) qname,
strlen(qname), &qname_s);
if (err != DOM_NO_ERR)
return err;
@@ -99,7 +95,7 @@ dom_exception dom_implementation_create_document_type(
}
if (public_id != NULL) {
- err = dom_string_create(alloc, pw, (const uint8_t *) public_id,
+ err = dom_string_create((const uint8_t *) public_id,
strlen(public_id), &public_id_s);
if (err != DOM_NO_ERR) {
dom_string_unref(lname);
@@ -110,7 +106,7 @@ dom_exception dom_implementation_create_document_type(
}
if (system_id != NULL) {
- err = dom_string_create(alloc, pw, (const uint8_t *) system_id,
+ err = dom_string_create((const uint8_t *) system_id,
strlen(system_id), &system_id_s);
if (err != DOM_NO_ERR) {
dom_string_unref(public_id_s);
@@ -122,8 +118,7 @@ dom_exception dom_implementation_create_document_type(
}
/* Create the doctype */
- err = _dom_document_type_create(qname_s, public_id_s, system_id_s,
- alloc, pw, &d);
+ err = _dom_document_type_create(qname_s, public_id_s, system_id_s, &d);
if (err == DOM_NO_ERR)
*doctype = d;
@@ -163,9 +158,6 @@ dom_exception dom_implementation_create_document_type(
* Document does not support XML
* namespaces.
*
- * Any memory allocated by this call should be allocated using
- * the provided memory (de)allocation function.
- *
* The document will be referenced, so the client need not do this
* explicitly. The client must unref the document once it has
* finished with it.
@@ -174,7 +166,6 @@ dom_exception dom_implementation_create_document(
uint32_t impl_type,
const char *namespace, const char *qname,
struct dom_document_type *doctype,
- dom_alloc alloc, void *pw,
dom_events_default_action_fetcher daf,
struct dom_document **doc)
{
@@ -183,14 +174,14 @@ dom_exception dom_implementation_create_document(
dom_exception err;
if (namespace != NULL) {
- err = dom_string_create(alloc, pw, (const uint8_t *) namespace,
+ err = dom_string_create((const uint8_t *) namespace,
strlen(namespace), &namespace_s);
if (err != DOM_NO_ERR)
return err;
}
if (qname != NULL) {
- err = dom_string_create(alloc, pw, (const uint8_t *) qname,
+ err = dom_string_create((const uint8_t *) qname,
strlen(qname), &qname_s);
if (err != DOM_NO_ERR) {
dom_string_unref(namespace_s);
@@ -226,7 +217,7 @@ dom_exception dom_implementation_create_document(
if (impl_type == DOM_IMPLEMENTATION_HTML) {
dom_html_document *html_doc;
- err = dom_html_document_create(alloc, pw, NULL, NULL,
+ err = dom_html_document_create(NULL, NULL,
daf, NULL, DOM_HTML_PARSER, &html_doc);
d = (dom_document *) html_doc;
@@ -235,7 +226,7 @@ dom_exception dom_implementation_create_document(
UNUSED(impl_type);
#endif
{
- err = _dom_document_create(alloc, pw, daf, &d);
+ err = _dom_document_create(daf, &d);
}
if (err != DOM_NO_ERR) {
diff --git a/src/core/namednodemap.c b/src/core/namednodemap.c
index 508fbb0..f68cd3b 100644
--- a/src/core/namednodemap.c
+++ b/src/core/namednodemap.c
@@ -7,6 +7,7 @@
*/
#include <assert.h>
+#include <stdlib.h>
#include <dom/core/element.h>
#include <dom/core/node.h>
@@ -23,7 +24,7 @@
* DOM named node map
*/
struct dom_namednodemap {
- struct dom_document *owner; /**< Owning document */
+ dom_document *owner; /**< Owning document */
void *priv; /**< Private data */
@@ -53,13 +54,13 @@ struct dom_namednodemap {
* explicitly reference it. The client must unref the map once it is
* finished with it.
*/
-dom_exception _dom_namednodemap_create(struct dom_document *doc,
+dom_exception _dom_namednodemap_create(dom_document *doc,
void *priv, struct nnm_operation *opt,
- struct dom_namednodemap **map)
+ dom_namednodemap **map)
{
- struct dom_namednodemap *m;
+ dom_namednodemap *m;
- m = _dom_document_alloc(doc, NULL, sizeof(struct dom_namednodemap));
+ m = malloc(sizeof(dom_namednodemap));
if (m == NULL)
return DOM_NO_MEM_ERR;
@@ -80,7 +81,7 @@ dom_exception _dom_namednodemap_create(struct dom_document *doc,
*
* \param map The map to claim a reference on
*/
-void dom_namednodemap_ref(struct dom_namednodemap *map)
+void dom_namednodemap_ref(dom_namednodemap *map)
{
assert(map != NULL);
map->refcnt++;
@@ -94,7 +95,7 @@ void dom_namednodemap_ref(struct dom_namednodemap *map)
* If the reference count reaches zero, any memory claimed by the
* map will be released
*/
-void dom_namednodemap_unref(struct dom_namednodemap *map)
+void dom_namednodemap_unref(dom_namednodemap *map)
{
if (map == NULL)
return;
@@ -104,7 +105,7 @@ void dom_namednodemap_unref(struct dom_namednodemap *map)
map->opt->namednodemap_destroy(map->priv);
/* Destroy the map object */
- _dom_document_alloc(map->owner, map, 0);
+ free(map);
}
}
@@ -115,7 +116,7 @@ void dom_namednodemap_unref(struct dom_namednodemap *map)
* \param length Pointer to location to receive length
* \return DOM_NO_ERR.
*/
-dom_exception dom_namednodemap_get_length(struct dom_namednodemap *map,
+dom_exception dom_namednodemap_get_length(dom_namednodemap *map,
unsigned long *length)
{
assert(map->opt != NULL);
@@ -133,8 +134,8 @@ dom_exception dom_namednodemap_get_length(struct dom_namednodemap *map,
* The returned node will have had its reference count increased. The client
* should unref the node once it has finished with it.
*/
-dom_exception _dom_namednodemap_get_named_item(struct dom_namednodemap *map,
- dom_string *name, struct dom_node **node)
+dom_exception _dom_namednodemap_get_named_item(dom_namednodemap *map,
+ dom_string *name, dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_get_named_item(map->priv, name, node);
@@ -164,8 +165,8 @@ dom_exception _dom_namednodemap_get_named_item(struct dom_namednodemap *map,
* The returned node will have had its reference count increased. The client
* should unref the node once it has finished with it.
*/
-dom_exception _dom_namednodemap_set_named_item(struct dom_namednodemap *map,
- struct dom_node *arg, struct dom_node **node)
+dom_exception _dom_namednodemap_set_named_item(dom_namednodemap *map,
+ dom_node *arg, dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_set_named_item(map->priv, arg, node);
@@ -186,8 +187,8 @@ dom_exception _dom_namednodemap_set_named_item(struct dom_namednodemap *map,
* should unref the node once it has finished with it.
*/
dom_exception _dom_namednodemap_remove_named_item(
- struct dom_namednodemap *map, dom_string *name,
- struct dom_node **node)
+ dom_namednodemap *map, dom_string *name,
+ dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_remove_named_item(map->priv, name, node);
@@ -207,8 +208,8 @@ dom_exception _dom_namednodemap_remove_named_item(
* The returned node will have had its reference count increased. The client
* should unref the node once it has finished with it.
*/
-dom_exception _dom_namednodemap_item(struct dom_namednodemap *map,
- unsigned long index, struct dom_node **node)
+dom_exception _dom_namednodemap_item(dom_namednodemap *map,
+ unsigned long index, dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_item(map->priv, index, node);
@@ -231,8 +232,8 @@ dom_exception _dom_namednodemap_item(struct dom_namednodemap *map,
* should unref the node once it has finished with it.
*/
dom_exception _dom_namednodemap_get_named_item_ns(
- struct dom_namednodemap *map, dom_string *namespace,
- dom_string *localname, struct dom_node **node)
+ dom_namednodemap *map, dom_string *namespace,
+ dom_string *localname, dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_get_named_item_ns(map->priv, namespace,
@@ -269,8 +270,8 @@ dom_exception _dom_namednodemap_get_named_item_ns(
* should unref the node once it has finished with it.
*/
dom_exception _dom_namednodemap_set_named_item_ns(
- struct dom_namednodemap *map, struct dom_node *arg,
- struct dom_node **node)
+ dom_namednodemap *map, dom_node *arg,
+ dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_set_named_item_ns(map->priv, arg, node);
@@ -296,8 +297,8 @@ dom_exception _dom_namednodemap_set_named_item_ns(
* should unref the node once it has finished with it.
*/
dom_exception _dom_namednodemap_remove_named_item_ns(
- struct dom_namednodemap *map, dom_string *namespace,
- dom_string *localname, struct dom_node **node)
+ dom_namednodemap *map, dom_string *namespace,
+ dom_string *localname, dom_node **node)
{
assert(map->opt != NULL);
return map->opt->namednodemap_remove_named_item_ns(map->priv, namespace,
@@ -308,8 +309,8 @@ dom_exception _dom_namednodemap_remove_named_item_ns(
* Compare whether two NamedNodeMap are equal.
*
*/
-bool _dom_namednodemap_equal(struct dom_namednodemap *m1,
- struct dom_namednodemap *m2)
+bool _dom_namednodemap_equal(dom_namednodemap *m1,
+ dom_namednodemap *m2)
{
assert(m1->opt != NULL);
return (m1->opt == m2->opt && m1->opt->namednodemap_equal(m1->priv,
@@ -322,7 +323,7 @@ bool _dom_namednodemap_equal(struct dom_namednodemap *m1,
* \param map The dom_namednodemap
* \param priv The private data to change to
*/
-void _dom_namednodemap_update(struct dom_namednodemap *map, void *priv)
+void _dom_namednodemap_update(dom_namednodemap *map, void *priv)
{
map->priv = priv;
}
diff --git a/src/core/node.c b/src/core/node.c
index 14dad4a..1c36932 100644
--- a/src/core/node.c
+++ b/src/core/node.c
@@ -9,6 +9,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
+#include <stdlib.h>
#include <dom/core/attr.h>
#include <dom/core/text.h>
@@ -33,7 +34,7 @@
#include "core/pi.h"
#include "core/text.h"
#include "utils/utils.h"
-#include "utils/resource_mgr.h"
+#include "utils/validate.h"
#include "events/mutation_event.h"
static bool _dom_node_permitted_child(const dom_node_internal *parent,
@@ -54,6 +55,9 @@ static inline void _dom_node_replace(dom_node_internal *old,
dom_node_internal *replacement);
static struct dom_node_vtable node_vtable = {
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
};
@@ -68,11 +72,9 @@ static struct dom_node_protect_vtable node_protect_vtable = {
/* The constructor and destructor of this object */
/* Create a DOM node and compose the vtable */
-dom_node_internal * _dom_node_create(struct dom_document *doc)
+dom_node_internal * _dom_node_create(void)
{
- dom_node_internal *node = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_node_internal));
-
+ dom_node_internal *node = malloc(sizeof(struct dom_node_internal));
if (node == NULL)
return NULL;
@@ -111,7 +113,7 @@ void _dom_node_destroy(struct dom_node_internal *node)
}
/* Finalise this node, this should also destroy all the child nodes. */
- _dom_node_finalise(owner, node);
+ _dom_node_finalise(node);
if (!null_owner_permitted) {
/* Release the reference we claimed on the document. If this
@@ -122,7 +124,7 @@ void _dom_node_destroy(struct dom_node_internal *node)
}
/* Release our memory */
- _dom_document_alloc(owner, node, 0);
+ free(node);
}
/**
@@ -142,59 +144,20 @@ void _dom_node_destroy(struct dom_node_internal *node)
*/
dom_exception _dom_node_initialise(dom_node_internal *node,
struct dom_document *doc, dom_node_type type,
- struct lwc_string_s *name, dom_string *value,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix)
+ dom_string *name, dom_string *value,
+ dom_string *namespace, dom_string *prefix)
{
- dom_alloc alloc;
- void *pw;
- dom_exception err;
-
- _dom_document_get_allocator(doc, &alloc, &pw);
-
- err = _dom_node_initialise_generic(node, doc, alloc, pw, type,
- name, value, namespace, prefix);
- if (err != DOM_NO_ERR)
- return err;
-
- return DOM_NO_ERR;
-}
-
-/**
- * Initialise a DOM node
- *
- * \param node The node to initialise
- * \param doc The document object
- * \param alloc The memory allocator
- * \param pw The allocator private pointer data
- * \param type The node type required
- * \param name The node (local) name, or NULL
- * \param value The node value, or NULL
- * \param namespace Namespace URI to use for node, or NULL
- * \param prefix Namespace prefix to use for node, or NULL
- * \return DOM_NO_ERR on success.
- *
- * ::name, ::value, ::namespace, and ::prefix will have their reference
- * counts increased.
- */
-dom_exception _dom_node_initialise_generic(
- struct dom_node_internal *node, struct dom_document *doc,
- dom_alloc alloc, void *pw,
- dom_node_type type, struct lwc_string_s *name,
- dom_string *value, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix)
-{
- UNUSED(alloc);
- UNUSED(pw);
-
node->owner = doc;
if (name != NULL)
- lwc_string_ref(name);
- node->name = name;
+ node->name = dom_string_ref(name);
+ else
+ node->name = NULL;
if (value != NULL)
- dom_string_ref(value);
- node->value = value;
+ node->value = dom_string_ref(value);
+ else
+ node->value = NULL;
node->type = type;
@@ -223,15 +186,15 @@ dom_exception _dom_node_initialise_generic(
* those nodes (and their sub-trees) in use by client code.
*/
- if (namespace != NULL) {
- lwc_string_ref(namespace);
- }
- node->namespace = namespace;
+ if (namespace != NULL)
+ node->namespace = dom_string_ref(namespace);
+ else
+ node->namespace = NULL;
- if (prefix != NULL) {
- lwc_string_ref(prefix);
- }
- node->prefix = prefix;
+ if (prefix != NULL)
+ node->prefix = dom_string_ref(prefix);
+ else
+ node->prefix = NULL;
node->user_data = NULL;
@@ -243,56 +206,34 @@ dom_exception _dom_node_initialise_generic(
dom_node_mark_pending(node);
}
- return _dom_event_target_internal_initialise(doc, &node->eti);
+ return _dom_event_target_internal_initialise(&node->eti);
}
/**
* Finalise a DOM node
*
- * \param doc The owning document (or NULL if it's a standalone DocumentType)
* \param node The node to finalise
*
* The contents of ::node will be cleaned up. ::node will not be freed.
* All children of ::node should have been removed prior to finalisation.
*/
-void _dom_node_finalise(struct dom_document *doc, dom_node_internal *node)
-{
- dom_alloc alloc;
- void *pw;
-
- _dom_document_get_allocator(doc, &alloc, &pw);
-
- _dom_node_finalise_generic(node, alloc, pw);
-}
-
-/**
- * Finalise a DOM node
- *
- * \param node The node to finalise
- * \param alloc The allocator
- * \param pw The allocator private data
- */
-void _dom_node_finalise_generic(dom_node_internal *node, dom_alloc alloc,
- void *pw)
+void _dom_node_finalise(dom_node_internal *node)
{
struct dom_user_data *u, *v;
- UNUSED(alloc);
- UNUSED(pw);
-
/* Destroy user data */
for (u = node->user_data; u != NULL; u = v) {
v = u->next;
dom_string_unref(u->key);
- alloc(u, 0, pw);
+ free(u);
}
node->user_data = NULL;
if (node->prefix != NULL)
- lwc_string_unref(node->prefix);
+ dom_string_unref(node->prefix);
if (node->namespace != NULL)
- lwc_string_unref(node->namespace);
+ dom_string_unref(node->namespace);
/* Destroy all the child nodes of this node */
struct dom_node_internal *p = node->first_child;
@@ -315,13 +256,13 @@ void _dom_node_finalise_generic(dom_node_internal *node, dom_alloc alloc,
dom_string_unref(node->value);
if (node->name != NULL)
- lwc_string_unref(node->name);
+ dom_string_unref(node->name);
/* If the node has no owner document, we need not to finalise its
* dom_event_target_internal structure.
*/
if (node->owner != NULL)
- _dom_event_target_internal_finalise(node->owner, &node->eti);
+ _dom_event_target_internal_finalise(&node->eti);
/* Detach from the pending list, if we are in it,
* this part of code should always be the end of this function. */
@@ -393,7 +334,6 @@ dom_exception _dom_node_get_node_name(dom_node_internal *node,
dom_string *node_name, *temp;
dom_document *doc;
dom_exception err;
- struct dom_resource_mgr rm;
doc = node->owner;
/* Document Node and DocumentType Node can have no owner */
@@ -403,72 +343,39 @@ dom_exception _dom_node_get_node_name(dom_node_internal *node,
assert(node->name != NULL);
- if (doc != NULL) {
- _dom_document_get_resource_mgr(doc, &rm);
- } else if (node->type == DOM_DOCUMENT_TYPE_NODE) {
- _dom_document_type_get_resource_mgr(
- (dom_document_type *) node, &rm);
- }
-
/* If this node was created using a namespace-aware method and
* has a defined prefix, then nodeName is a QName comprised
* of prefix:name. */
- if(node->prefix != NULL) {
+ if (node->prefix != NULL) {
dom_string *colon;
- err = _dom_resource_mgr_create_string(&rm,
- (const uint8_t *) ":", SLEN(":"), &colon);
+ err = dom_string_create((const uint8_t *) ":", SLEN(":"),
+ &colon);
if (err != DOM_NO_ERR) {
return err;
}
- /* Make a temp prefix dom_string */
- err = _dom_resource_mgr_create_string_from_lwcstring(&rm,
- node->prefix, &temp);
- if (err != DOM_NO_ERR) {
- dom_string_unref(colon);
- return err;
- }
-
/* Prefix + : */
- err = dom_string_concat(temp, colon, &node_name);
+ err = dom_string_concat(node->prefix, colon, &temp);
if (err != DOM_NO_ERR) {
- dom_string_unref(temp);
dom_string_unref(colon);
return err;
}
- /*Finished with temp*/
- dom_string_unref(temp);
/* Finished with colon */
dom_string_unref(colon);
- /* Make a temp name dom_string */
- err = _dom_resource_mgr_create_string_from_lwcstring(&rm,
- node->name, &temp);
- if (err != DOM_NO_ERR) {
- return err;
- }
/* Prefix + : + Localname */
- err = dom_string_concat(node_name, temp, &colon);
+ err = dom_string_concat(temp, node->name, &node_name);
if (err != DOM_NO_ERR) {
dom_string_unref(temp);
- dom_string_unref(node_name);
return err;
}
/* Finished with temp */
dom_string_unref(temp);
- /* Finished with intermediate node name */
- dom_string_unref(node_name);
-
- node_name = colon;
} else {
- err = _dom_resource_mgr_create_string_from_lwcstring(&rm,
- node->name, &node_name);
- if (err != DOM_NO_ERR) {
- return err;
- }
+ node_name = dom_string_ref(node->name);
}
*result = node_name;
@@ -1063,7 +970,7 @@ dom_exception _dom_node_remove_child(dom_node_internal *node,
/* Dispatch a DOMNodeRemoval event */
dom_exception err;
bool success = true;
- err = _dom_dispatch_node_change_event(node->owner, old_child, node,
+ err = dom_node_dispatch_node_change_event(node->owner, old_child, node,
DOM_MUTATION_REMOVAL, &success);
if (err != DOM_NO_ERR)
return err;
@@ -1190,13 +1097,8 @@ dom_exception _dom_node_clone_node(dom_node_internal *node, bool deep,
doc = node->owner;
assert(doc != NULL);
- err = dom_node_alloc(doc, node, &n);
- if (err != DOM_NO_ERR)
- return err;
-
- err = dom_node_copy(n, node);
+ err = dom_node_copy(node, &n);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, n, 0);
return err;
}
@@ -1325,10 +1227,11 @@ dom_exception _dom_node_get_namespace(dom_node_internal *node,
/* If there is a namespace, increase its reference count */
if (node->namespace != NULL)
- lwc_string_ref(node->namespace);
+ *result = dom_string_ref(node->namespace);
+ else
+ *result = NULL;
- return _dom_document_create_string_from_lwcstring(node->owner,
- node->namespace, result);
+ return DOM_NO_ERR;
}
/**
@@ -1349,11 +1252,11 @@ dom_exception _dom_node_get_prefix(dom_node_internal *node,
/* If there is a prefix, increase its reference count */
if (node->prefix != NULL)
- lwc_string_ref(node->prefix);
+ *result = dom_string_ref(node->prefix);
+ else
+ *result = NULL;
- return _dom_document_create_string_from_lwcstring(node->owner,
- node->prefix,
- result);
+ return DOM_NO_ERR;
}
/**
@@ -1382,9 +1285,6 @@ dom_exception _dom_node_get_prefix(dom_node_internal *node,
dom_exception _dom_node_set_prefix(dom_node_internal *node,
dom_string *prefix)
{
- dom_exception err;
- lwc_string *str;
-
/* Only Element and Attribute nodes created using
* namespace-aware methods may have a prefix */
if ((node->type != DOM_ELEMENT_NODE &&
@@ -1402,7 +1302,7 @@ dom_exception _dom_node_set_prefix(dom_node_internal *node,
/* No longer want existing prefix */
if (node->prefix != NULL) {
- lwc_string_unref(node->prefix);
+ dom_string_unref(node->prefix);
}
/* Set the prefix */
@@ -1411,11 +1311,7 @@ dom_exception _dom_node_set_prefix(dom_node_internal *node,
if (dom_string_length(prefix) == 0) {
node->prefix = NULL;
} else {
- err = _dom_node_get_intern_string(node, prefix, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- node->prefix = str;
+ node->prefix = dom_string_ref(prefix);
}
} else {
node->prefix = NULL;
@@ -1448,12 +1344,12 @@ dom_exception _dom_node_get_local_name(dom_node_internal *node,
}
/* The node may have a local name, reference it if so */
- if (node->name != NULL) {
- lwc_string_ref(node->name);
- }
+ if (node->name != NULL)
+ *result = dom_string_ref(node->name);
+ else
+ *result = NULL;
- return _dom_document_create_string_from_lwcstring(node->owner,
- node->name, result);
+ return DOM_NO_ERR;
}
/**
@@ -1725,7 +1621,7 @@ dom_exception _dom_node_is_equal(dom_node_internal *node,
if (err != DOM_NO_ERR)
return err;
- if (dom_string_cmp(s1, s2) != 0) {
+ if (dom_string_isequal(s1, s2) == false) {
*result = false;
return DOM_NO_ERR;
}
@@ -1737,7 +1633,7 @@ dom_exception _dom_node_is_equal(dom_node_internal *node,
return DOM_NO_ERR;
}
- if (dom_string_cmp(node->value, other->value) != 0) {
+ if (dom_string_isequal(node->value, other->value) == false) {
*result = false;
return DOM_NO_ERR;
}
@@ -1824,7 +1720,7 @@ dom_exception _dom_node_set_user_data(dom_node_internal *node,
/* Search for user data */
for (ud = node->user_data; ud != NULL; ud = ud->next) {
- if (dom_string_cmp(ud->key, key) == 0)
+ if (dom_string_isequal(ud->key, key))
break;
};
@@ -1841,15 +1737,14 @@ dom_exception _dom_node_set_user_data(dom_node_internal *node,
*result = ud->data;
- _dom_document_alloc(node->owner, ud, 0);
+ free(ud);
return DOM_NO_ERR;
}
/* Otherwise, create a new user data object if one wasn't found */
if (ud == NULL) {
- ud = _dom_document_alloc(node->owner, NULL,
- sizeof(struct dom_user_data));
+ ud = malloc(sizeof(struct dom_user_data));
if (ud == NULL)
return DOM_NO_MEM_ERR;
@@ -1892,7 +1787,7 @@ dom_exception _dom_node_get_user_data(dom_node_internal *node,
/* Search for user data */
for (ud = node->user_data; ud != NULL; ud = ud->next) {
- if (dom_string_cmp(ud->key, key) == 0)
+ if (dom_string_isequal(ud->key, key))
break;
};
@@ -1909,27 +1804,34 @@ dom_exception _dom_node_get_user_data(dom_node_internal *node,
/* The protected virtual functions */
-/* We should never call this pure-virtual function directly */
-dom_exception _dom_node_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* Copy the internal attributes of a Node from old to new */
+dom_exception _dom_node_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(doc);
- UNUSED(n);
- UNUSED(ret);
+ dom_node_internal *new_node;
+ dom_exception err;
- return DOM_NOT_SUPPORTED_ERR;
-}
+ new_node = malloc(sizeof(dom_node_internal));
+ if (new_node == NULL)
+ return DOM_NO_MEM_ERR;
+ err = _dom_node_copy_internal(old, new_node);
+ if (err != DOM_NO_ERR) {
+ free(new_node);
+ return err;
+ }
-/* Copy the internal attributes of a Node from old to new */
-dom_exception _dom_node_copy(dom_node_internal *new, dom_node_internal *old)
-{
- dom_exception err;
+ *copy = new_node;
+
+ return DOM_NO_ERR;
+}
+dom_exception _dom_node_copy_internal(dom_node_internal *old,
+ dom_node_internal *new)
+{
new->base.vtable = old->base.vtable;
new->vtable = old->vtable;
- new->name = lwc_string_ref(old->name);
+ new->name = dom_string_ref(old->name);
/* Value - see below */
@@ -1941,16 +1843,16 @@ dom_exception _dom_node_copy(dom_node_internal *new, dom_node_internal *old)
new->next = NULL;
assert(old->owner != NULL);
- assert(new->owner != NULL);
+
new->owner = old->owner;
if (old->namespace != NULL)
- new->namespace = lwc_string_ref(old->namespace);
+ new->namespace = dom_string_ref(old->namespace);
else
new->namespace = NULL;
if (old->prefix != NULL)
- new->prefix = lwc_string_ref(old->prefix);
+ new->prefix = dom_string_ref(old->prefix);
else
new->prefix = NULL;
@@ -1961,16 +1863,9 @@ dom_exception _dom_node_copy(dom_node_internal *new, dom_node_internal *old)
/* Value */
if (old->value != NULL) {
- dom_alloc al;
- void *pw;
+ dom_string_ref(old->value);
- _dom_document_get_allocator(new->owner, &al, &pw);
- dom_string *value;
- err = dom_string_clone(al, pw, old->value, &value);
- if (err != DOM_NO_ERR)
- return err;
-
- new->value = value;
+ new->value = old->value;
} else {
new->value = NULL;
}
@@ -1980,7 +1875,7 @@ dom_exception _dom_node_copy(dom_node_internal *new, dom_node_internal *old)
dom_node_mark_pending(new);
/* Intialise the EventTarget interface */
- return _dom_event_target_internal_initialise(new->owner, &new->eti);
+ return _dom_event_target_internal_initialise(&new->eti);
}
@@ -2157,8 +2052,8 @@ dom_exception _dom_node_attach_range(dom_node_internal *first,
for (dom_node_internal *n = first; n != last->next; n = n->next) {
n->parent = parent;
/* Dispatch a DOMNodeInserted event */
- err = _dom_dispatch_node_change_event(parent->owner, n, parent,
- DOM_MUTATION_ADDITION, &success);
+ err = dom_node_dispatch_node_change_event(parent->owner,
+ n, parent, DOM_MUTATION_ADDITION, &success);
if (err != DOM_NO_ERR)
return err;
}
@@ -2197,7 +2092,7 @@ void _dom_node_detach_range(dom_node_internal *first,
dom_node_internal *parent = first->parent;
for (dom_node_internal *n = first; n != last->next; n = n->next) {
/* Dispatch a DOMNodeRemoval event */
- _dom_dispatch_node_change_event(n->owner, n, n->parent,
+ dom_node_dispatch_node_change_event(n->owner, n, n->parent,
DOM_MUTATION_REMOVAL, &success);
n->parent = NULL;
@@ -2257,36 +2152,6 @@ void _dom_node_replace(dom_node_internal *old,
}
/**
- * Migrate one dom_string from one document to another, this function
- * may be used when we import/adopt a Node between documents.
- *
- * \param old The source document
- * \param new The new document
- * \param string The dom_string to migrate
- * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
- */
-dom_exception _redocument_domstring(dom_document *old, dom_document* new,
- dom_string **string)
-{
- dom_exception err;
- dom_string *str;
-
- UNUSED(old);
- err = _dom_document_create_string(new, NULL, 0, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- err = dom_string_dup(*string, &str);
- if (err != DOM_NO_ERR)
- return err;
-
- dom_string_unref(*string);
- *string = str;
-
- return DOM_NO_ERR;
-}
-
-/**
* Merge two adjacent text nodes into one text node.
*
* \param p The first text node
@@ -2316,78 +2181,6 @@ dom_exception _dom_merge_adjacent_text(dom_node_internal *p,
}
/**
- * Intern a dom_string
- *
- * \param node The node
- * \param str The dom_string to be interned
- * \param intern The returned interned string
- * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
- */
-dom_exception _dom_node_get_intern_string(dom_node_internal *node,
- dom_string *str, lwc_string **intern)
-{
- dom_exception err;
- lwc_string *ret;
-
- UNUSED(node);
-
- assert(str != NULL);
-
- err = dom_string_get_intern(str, &ret);
- if (err != DOM_NO_ERR)
- return err;
-
- err = _dom_string_intern(str, &ret);
- if (err != DOM_NO_ERR)
- return err;
-
- *intern = ret;
-
- return DOM_NO_ERR;
-}
-
-/**
- * Unref a lwc_string used in this node
- *
- * \param node The node
- * \param intern The lwc_string to unref
- */
-void _dom_node_unref_intern_string(dom_node_internal *node,
- struct lwc_string_s *intern)
-{
- struct dom_resource_mgr rm;
- struct dom_document *doc = node->owner;
-
- if (doc != NULL) {
- _dom_document_get_resource_mgr(doc, &rm);
- } else if (node->type == DOM_DOCUMENT_TYPE_NODE) {
- _dom_document_type_get_resource_mgr(
- (dom_document_type *) node, &rm);
- }
-
- lwc_string_unref(intern);
-}
-
-/**
- * Create a lwc_string using the node's owner's lwc_context
- *
- * \param node The node object
- * \param data The string data
- * \param len The length of the string data
- * \param str The returned lwc_string
- * \return DOM_NO_ERR on success, appropirate dom_exception on failure.
- */
-dom_exception _dom_node_create_lwcstring(dom_node_internal *node,
- const uint8_t *data, size_t len, struct lwc_string_s **str)
-{
- dom_document *doc = dom_node_get_owner(node);
-
- assert(doc != NULL);
-
- return _dom_document_create_lwcstring(doc, data, len, str);
-}
-
-/**
* Try to destroy this node.
*
* \param node The node to destroy
@@ -2454,3 +2247,262 @@ void _dom_node_remove_pending(dom_node_internal *node)
}
}
+/******************************************************************************
+ * Event Target API *
+ ******************************************************************************/
+
+dom_exception _dom_node_add_event_listener(dom_event_target *et,
+ dom_string *type, struct dom_event_listener *listener,
+ bool capture)
+{
+ dom_node_internal *node = (dom_node_internal *) et;
+
+ return _dom_event_target_add_event_listener(&node->eti, type,
+ listener, capture);
+}
+
+dom_exception _dom_node_remove_event_listener(dom_event_target *et,
+ dom_string *type, struct dom_event_listener *listener,
+ bool capture)
+{
+ dom_node_internal *node = (dom_node_internal *) et;
+
+ return _dom_event_target_remove_event_listener(&node->eti,
+ type, listener, capture);
+}
+
+dom_exception _dom_node_add_event_listener_ns(dom_event_target *et,
+ dom_string *namespace, dom_string *type,
+ struct dom_event_listener *listener, bool capture)
+{
+ dom_node_internal *node = (dom_node_internal *) et;
+
+ return _dom_event_target_add_event_listener_ns(&node->eti,
+ namespace, type, listener, capture);
+}
+
+dom_exception _dom_node_remove_event_listener_ns(dom_event_target *et,
+ dom_string *namespace, dom_string *type,
+ struct dom_event_listener *listener, bool capture)
+{
+ dom_node_internal *node = (dom_node_internal *) et;
+
+ return _dom_event_target_remove_event_listener_ns(&node->eti,
+ namespace, type, listener, capture);
+}
+
+/**
+ * Dispatch an event into the implementation's event model
+ *
+ * \param et The EventTarget object
+ * \param eti Internal EventTarget
+ * \param evt The event object
+ * \param success Indicates whether any of the listeners which handled the
+ * event called Event.preventDefault(). If
+ * Event.preventDefault() was called the returned value is
+ * false, else it is true.
+ * \return DOM_NO_ERR on success
+ * DOM_DISPATCH_REQUEST_ERR If the event is already in dispatch
+ * DOM_UNSPECIFIED_EVENT_TYPE_ERR If the type of the event is Null or
+ * empty string.
+ * DOM_NOT_SUPPORTED_ERR If the event is not created by
+ * Document.createEvent
+ * DOM_INVALID_CHARACTER_ERR If the type of this event is not a
+ * valid NCName.
+ */
+dom_exception _dom_node_dispatch_event(dom_event_target *et,
+ struct dom_event *evt, bool *success)
+{
+ dom_exception err, ret = DOM_NO_ERR;
+
+ assert(et != NULL);
+ assert(evt != NULL);
+
+ /* To test whether this event is in dispatch */
+ if (evt->in_dispatch == true) {
+ return DOM_DISPATCH_REQUEST_ERR;
+ } else {
+ evt->in_dispatch = true;
+ }
+
+ if (evt->type == NULL || dom_string_length(evt->type) == 0) {
+ return DOM_UNSPECIFIED_EVENT_TYPE_ERR;
+ }
+
+ if (evt->doc == NULL)
+ return DOM_NOT_SUPPORTED_ERR;
+
+ dom_document *doc = dom_node_get_owner(et);
+ if (doc == NULL) {
+ /* TODO: In the progress of parsing, many Nodes in the DTD has
+ * no document at all, do nothing for this kind of node */
+ return DOM_NO_ERR;
+ }
+
+ if (_dom_validate_ncname(evt->type) == false) {
+ return DOM_INVALID_CHARACTER_ERR;
+ }
+
+ dom_event_target_entry list;
+ dom_node_internal *target = (dom_node_internal *) et;
+
+ *success = true;
+
+ /* Compose the event target list */
+ list_init(&list.entry);
+ list.et = et;
+ dom_node_ref(et);
+ target = target->parent;
+
+ while (target != NULL) {
+ dom_event_target_entry *l = malloc(
+ sizeof(dom_event_target_entry));
+ if (l == NULL) {
+ ret = DOM_NO_MEM_ERR;
+ goto cleanup;
+ }
+ list_append(&list.entry, &l->entry);
+ l->et = (dom_event_target *) target;
+ dom_node_ref(target);
+ target = target->parent;
+ }
+
+ /* Fill the target of the event */
+ evt->target = et;
+ evt->phase = DOM_CAPTURING_PHASE;
+
+ /* The started callback of default action */
+ dom_document_event_internal *dei = &doc->dei;
+ void *pw = NULL;
+ if (dei->actions != NULL) {
+ dom_default_action_callback cb = dei->actions(evt->type,
+ DOM_DEFAULT_ACTION_STARTED, &pw);
+ if (cb != NULL) {
+ cb(evt, pw);
+ }
+ }
+
+ /* The capture phase */
+ struct list_entry *e = list.entry.prev;
+ for (; e != &list.entry; e = e->prev) {
+ dom_event_target_entry *l = (dom_event_target_entry *) e;
+ dom_node_internal *node = (dom_node_internal *) l->et;
+
+ err = _dom_event_target_dispatch(l->et, &node->eti, evt,
+ DOM_CAPTURING_PHASE, success);
+ if (err != DOM_NO_ERR) {
+ ret = err;
+ goto cleanup;
+ }
+ /* If the stopImmediatePropagation or stopPropagation is
+ * called, we should break */
+ if (evt->stop_now == true || evt->stop == true)
+ goto cleanup;
+ }
+
+ /* Target phase */
+ evt->phase = DOM_AT_TARGET;
+ evt->current = et;
+ err = _dom_event_target_dispatch(et, &((dom_node_internal *) et)->eti,
+ evt, DOM_AT_TARGET, success);
+ if (evt->stop_now == true || evt->stop == true)
+ goto cleanup;
+
+ /* Bubbling phase */
+ evt->phase = DOM_BUBBLING_PHASE;
+
+ e = list.entry.next;
+ for (; e != &list.entry; e = e->next) {
+ dom_event_target_entry *l = (dom_event_target_entry *) e;
+ dom_node_internal *node = (dom_node_internal *) l->et;
+ err = _dom_event_target_dispatch(l->et, &node->eti, evt,
+ DOM_BUBBLING_PHASE, success);
+ if (err != DOM_NO_ERR) {
+ ret = err;
+ goto cleanup;
+ }
+ /* If the stopImmediatePropagation or stopPropagation is
+ * called, we should break */
+ if (evt->stop_now == true || evt->stop == true)
+ goto cleanup;
+ }
+
+ if (dei->actions == NULL)
+ goto cleanup;
+
+ /* The end callback of default action */
+ if (evt->prevent_default != true) {
+ dom_default_action_callback cb = dei->actions(evt->type,
+ DOM_DEFAULT_ACTION_END, &pw);
+ if (cb != NULL) {
+ cb(evt, pw);
+ }
+ }
+
+ /* The prevented callback of default action */
+ if (evt->prevent_default != true) {
+ dom_default_action_callback cb = dei->actions(evt->type,
+ DOM_DEFAULT_ACTION_PREVENTED, &pw);
+ if (cb != NULL) {
+ cb(evt, pw);
+ }
+ }
+
+cleanup:
+ if (evt->prevent_default == true) {
+ *success = false;
+ }
+
+ while (list.entry.next != &list.entry) {
+ dom_event_target_entry *e = (dom_event_target_entry *)
+ list.entry.next;
+ dom_node_unref(e->et);
+ list_del(list.entry.next);
+ free(e);
+ }
+
+ dom_node_unref(et);
+
+ return ret;
+}
+
+dom_exception _dom_node_dispatch_node_change_event(dom_document *doc,
+ dom_node_internal *node, dom_node_internal *related,
+ dom_mutation_type change, bool *success)
+{
+ dom_node_internal *target;
+ dom_exception err;
+
+ /* Fire change event at immediate target */
+ err = _dom_dispatch_node_change_event(doc, node, related,
+ change, success);
+ if (err != DOM_NO_ERR)
+ return err;
+
+ /* Fire document change event at subtree */
+ target = node->first_child;
+ while (target != NULL) {
+ err = _dom_dispatch_node_change_document_event(doc, target,
+ change, success);
+ if (err != DOM_NO_ERR)
+ return err;
+
+ if (target->first_child != NULL) {
+ target = target->first_child;
+ } else if (target->next != NULL) {
+ target = target->next;
+ } else {
+ dom_node_internal *parent = target->parent;
+
+ while (parent != node && target == parent->last_child) {
+ target = parent;
+ parent = target->parent;
+ }
+
+ target = target->next;
+ }
+ }
+
+ return DOM_NO_ERR;
+}
+
diff --git a/src/core/node.h b/src/core/node.h
index 3134c49..22540c7 100644
--- a/src/core/node.h
+++ b/src/core/node.h
@@ -41,10 +41,7 @@ typedef struct dom_node_protect_vtable {
void (*destroy)(dom_node_internal *n);
/**< The destroy virtual function, it
* should be private to client */
- dom_exception (*alloc)(struct dom_document *doc,
- dom_node_internal *n, dom_node_internal **ret);
- /**< Allocate the memory of the new Node */
- dom_exception (*copy)(dom_node_internal *new, dom_node_internal *old);
+ dom_exception (*copy)(dom_node_internal *old, dom_node_internal **copy);
/**< Copy the old to new as well as
* all its attributes, but not its children */
} dom_node_protect_vtable;
@@ -58,10 +55,10 @@ struct dom_node_internal {
struct dom_node base; /**< The vtable base */
void *vtable; /**< The protected vtable */
- struct lwc_string_s *name; /**< Node name (this is the local part
+ dom_string *name; /**< Node name (this is the local part
* of a QName in the cases where a
* namespace exists) */
- dom_string *value; /**< Node value */
+ dom_string *value; /**< Node value */
dom_node_type type; /**< Node type */
dom_node_internal *parent; /**< Parent node */
dom_node_internal *first_child; /**< First child node */
@@ -71,8 +68,8 @@ struct dom_node_internal {
struct dom_document *owner; /**< Owning document */
- struct lwc_string_s *namespace; /**< Namespace URI */
- struct lwc_string_s *prefix; /**< Namespace prefix */
+ dom_string *namespace; /**< Namespace URI */
+ dom_string *prefix; /**< Namespace prefix */
struct dom_user_data *user_data; /**< User data list */
@@ -83,26 +80,33 @@ struct dom_node_internal {
dom_event_target_internal eti; /**< The EventTarget interface */
};
-dom_node_internal * _dom_node_create(struct dom_document *doc);
+dom_node_internal * _dom_node_create(void);
dom_exception _dom_node_initialise(struct dom_node_internal *node,
struct dom_document *doc, dom_node_type type,
- struct lwc_string_s *name, dom_string *value,
- struct lwc_string_s *namespace, struct lwc_string_s *prefix);
+ dom_string *name, dom_string *value,
+ dom_string *namespace, dom_string *prefix);
-dom_exception _dom_node_initialise_generic(
- struct dom_node_internal *node, struct dom_document *doc,
- dom_alloc alloc, void *pw,
- dom_node_type type, struct lwc_string_s *name,
- dom_string *value, struct lwc_string_s *namespace,
- struct lwc_string_s *prefix);
-
-void _dom_node_finalise(struct dom_document *doc, dom_node_internal *node);
-void _dom_node_finalise_generic(dom_node_internal *node, dom_alloc alloc,
- void *pw);
+void _dom_node_finalise(dom_node_internal *node);
bool _dom_node_readonly(const dom_node_internal *node);
+/* Event Target implementation */
+dom_exception _dom_node_add_event_listener(dom_event_target *et,
+ dom_string *type, struct dom_event_listener *listener,
+ bool capture);
+dom_exception _dom_node_remove_event_listener(dom_event_target *et,
+ dom_string *type, struct dom_event_listener *listener,
+ bool capture);
+dom_exception _dom_node_add_event_listener_ns(dom_event_target *et,
+ dom_string *namespace, dom_string *type,
+ struct dom_event_listener *listener, bool capture);
+dom_exception _dom_node_remove_event_listener_ns(dom_event_target *et,
+ dom_string *namespace, dom_string *type,
+ struct dom_event_listener *listener, bool capture);
+dom_exception _dom_node_dispatch_event(dom_event_target *et,
+ struct dom_event *evt, bool *success);
+
/* The DOM Node's vtable methods */
dom_exception _dom_node_get_node_name(dom_node_internal *node,
dom_string **result);
@@ -183,6 +187,13 @@ dom_exception _dom_node_set_user_data(dom_node_internal *node,
dom_exception _dom_node_get_user_data(dom_node_internal *node,
dom_string *key, void **result);
+#define DOM_NODE_EVENT_TARGET_VTABLE \
+ _dom_node_add_event_listener, \
+ _dom_node_remove_event_listener, \
+ _dom_node_dispatch_event, \
+ _dom_node_add_event_listener_ns, \
+ _dom_node_remove_event_listener_ns
+
#define DOM_NODE_VTABLE \
_dom_node_get_node_name, \
_dom_node_get_node_value, \
@@ -225,14 +236,11 @@ dom_exception _dom_node_get_user_data(dom_node_internal *node,
/* Following comes the protected vtable */
void _dom_node_destroy(struct dom_node_internal *node);
-dom_exception _dom_node_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_node_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+dom_exception _dom_node_copy(struct dom_node_internal *old,
+ struct dom_node_internal **copy);
#define DOM_NODE_PROTECT_VTABLE \
_dom_node_destroy, \
- _dom_node_alloc, \
_dom_node_copy
@@ -243,26 +251,21 @@ static inline void dom_node_destroy(struct dom_node_internal *node)
}
#define dom_node_destroy(n) dom_node_destroy((dom_node_internal *) (n))
-/* Allocate the Node */
-static inline dom_exception dom_node_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
-{
- return ((dom_node_protect_vtable *) n->vtable)->alloc(doc, n, ret);
-}
-#define dom_node_alloc(d,n,r) dom_node_alloc((struct dom_document *) (d), \
- (dom_node_internal *) (n), (dom_node_internal **) (r))
-
-
/* Copy the Node old to new */
-static inline dom_exception dom_node_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
+static inline dom_exception dom_node_copy(struct dom_node_internal *old,
+ struct dom_node_internal **copy)
{
- return ((dom_node_protect_vtable *) old->vtable)->copy(new, old);
+ return ((dom_node_protect_vtable *) old->vtable)->copy(old, copy);
}
-#define dom_node_copy(n,o) dom_node_copy((dom_node_internal *) (n), \
- (dom_node_internal *) (o))
+#define dom_node_copy(o,c) dom_node_copy((dom_node_internal *) (o), \
+ (dom_node_internal **) (c))
/* Following are some helper functions */
+dom_exception _dom_node_copy_internal(dom_node_internal *old,
+ dom_node_internal *new);
+#define dom_node_copy_internal(o, n) _dom_node_copy_internal( \
+ (dom_node_internal *) (o), (dom_node_internal *) (n))
+
#define dom_node_get_owner(n) ((dom_node_internal *) (n))->owner
#define dom_node_set_owner(n, d) ((dom_node_internal *) (n))->owner = \
@@ -275,19 +278,8 @@ static inline dom_exception dom_node_copy(struct dom_node_internal *new,
#define dom_node_get_refcount(n) ((dom_node_internal *) (n))->refcnt
-dom_exception _redocument_domstring(struct dom_document *old,
- struct dom_document* new, dom_string **string);
dom_exception _dom_merge_adjacent_text(dom_node_internal *p,
dom_node_internal *n);
-/* Used to extract the lwc_string from dom_string.
- * If there is no lwc_string inside the param, create one use the node->owner
- * as document */
-dom_exception _dom_node_get_intern_string(dom_node_internal *node,
- dom_string *str, struct lwc_string_s **intern);
-void _dom_node_unref_intern_string(dom_node_internal *node,
- struct lwc_string_s *inter);
-dom_exception _dom_node_create_lwcstring(dom_node_internal *node,
- const uint8_t *data, size_t len, struct lwc_string_s **str);
/* Try to destroy the node, if its refcnt is not zero, then append it to the
* owner document's pending list */
@@ -304,4 +296,15 @@ void _dom_node_remove_pending(dom_node_internal *node);
#define dom_node_remove_pending(n) _dom_node_remove_pending(\
(dom_node_internal *) (n))
+dom_exception _dom_node_dispatch_node_change_event(dom_document *doc,
+ dom_node_internal *node, dom_node_internal *related,
+ dom_mutation_type change, bool *success);
+#define dom_node_dispatch_node_change_event( \
+ doc, node, related, change, success) \
+ _dom_node_dispatch_node_change_event((dom_document *) (doc), \
+ (dom_node_internal *) (node), \
+ (dom_node_internal *) (related), \
+ (dom_mutation_type) (change), \
+ (bool *) (success))
+
#endif
diff --git a/src/core/nodelist.c b/src/core/nodelist.c
index 755438a..58a2d12 100644
--- a/src/core/nodelist.c
+++ b/src/core/nodelist.c
@@ -5,15 +5,15 @@
* Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
* Copyright 2009 Bo Yang <struggleyb.nku@gmail.com>
*/
+
#include <assert.h>
+#include <stdlib.h>
#include <dom/core/node.h>
#include <dom/core/document.h>
#include <dom/core/nodelist.h>
#include <dom/core/string.h>
-#include <libwapcaplet/libwapcaplet.h>
-
#include "core/document.h"
#include "core/node.h"
#include "core/nodelist.h"
@@ -24,24 +24,24 @@
* DOM node list
*/
struct dom_nodelist {
- struct dom_document *owner; /**< Owning document */
+ dom_document *owner; /**< Owning document */
- struct dom_node_internal *root;
+ dom_node_internal *root;
/**< Root of applicable subtree */
nodelist_type type; /**< Type of this list */
union {
struct {
- struct lwc_string_s *name;
+ dom_string *name;
/**< Tag name to match */
bool any_name; /**< The name is '*' */
} n;
struct {
bool any_namespace; /**< The namespace is '*' */
bool any_localname; /**< The localname is '*' */
- struct lwc_string_s *namespace; /**< Namespace */
- struct lwc_string_s *localname; /**< Localname */
+ dom_string *namespace; /**< Namespace */
+ dom_string *localname; /**< Localname */
} ns; /**< Data for namespace matching */
} data;
@@ -65,14 +65,14 @@ struct dom_nodelist {
* The returned list will already be referenced, so the client need not
* do so explicitly. The client must unref the list once finished with it.
*/
-dom_exception _dom_nodelist_create(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_nodelist_create(dom_document *doc, nodelist_type type,
+ dom_node_internal *root, dom_string *tagname,
+ dom_string *namespace, dom_string *localname,
+ dom_nodelist **list)
{
- struct dom_nodelist *l;
+ dom_nodelist *l;
- l = _dom_document_alloc(doc, NULL, sizeof(struct dom_nodelist));
+ l = malloc(sizeof(dom_nodelist));
if (l == NULL)
return DOM_NO_MEM_ERR;
@@ -87,35 +87,34 @@ dom_exception _dom_nodelist_create(struct dom_document *doc, nodelist_type type,
if (type == DOM_NODELIST_BY_NAME) {
assert(tagname != NULL);
l->data.n.any_name = false;
- if (lwc_string_length(tagname) == 1) {
- const char *ch = lwc_string_data(tagname);
+ if (_dom_string_byte_length(tagname) == 1) {
+ const char *ch = _dom_string_data(tagname);
if (*ch == '*') {
l->data.n.any_name = true;
}
}
- lwc_string_ref(tagname);
- l->data.n.name = tagname;
+ l->data.n.name = dom_string_ref(tagname);
} else if (type == DOM_NODELIST_BY_NAMESPACE) {
l->data.ns.any_localname = false;
l->data.ns.any_namespace = false;
if (localname != NULL) {
- if (lwc_string_length(localname) == 1) {
- const char *ch = lwc_string_data(localname);
+ if (_dom_string_byte_length(localname) == 1) {
+ const char *ch = _dom_string_data(localname);
if (*ch == '*') {
l->data.ns.any_localname = true;
}
}
- lwc_string_ref(localname);
+ dom_string_ref(localname);
}
if (namespace != NULL) {
- if (lwc_string_length(namespace) == 1) {
- const char *ch = lwc_string_data(namespace);
+ if (_dom_string_byte_length(namespace) == 1) {
+ const char *ch = _dom_string_data(namespace);
if (*ch == '*') {
l->data.ns.any_namespace = true;
}
}
- lwc_string_ref(namespace);
+ dom_string_ref(namespace);
}
l->data.ns.namespace = namespace;
@@ -134,7 +133,7 @@ dom_exception _dom_nodelist_create(struct dom_document *doc, nodelist_type type,
*
* \param list The list to claim a reference on
*/
-void dom_nodelist_ref(struct dom_nodelist *list)
+void dom_nodelist_ref(dom_nodelist *list)
{
assert(list != NULL);
list->refcnt++;
@@ -148,27 +147,26 @@ void dom_nodelist_ref(struct dom_nodelist *list)
* If the reference count reaches zero, any memory claimed by the
* list will be released
*/
-void dom_nodelist_unref(struct dom_nodelist *list)
+void dom_nodelist_unref(dom_nodelist *list)
{
if (list == NULL)
return;
if (--list->refcnt == 0) {
- struct dom_node_internal *owner =
- (struct dom_node_internal *) list->owner;
+ dom_node_internal *owner = (dom_node_internal *) list->owner;
switch (list->type) {
case DOM_NODELIST_CHILDREN:
/* Nothing to do */
break;
case DOM_NODELIST_BY_NAMESPACE:
if (list->data.ns.namespace != NULL)
- lwc_string_unref(list->data.ns.namespace);
+ dom_string_unref(list->data.ns.namespace);
if (list->data.ns.localname != NULL)
- lwc_string_unref(list->data.ns.localname);
+ dom_string_unref(list->data.ns.localname);
break;
case DOM_NODELIST_BY_NAME:
assert(list->data.n.name != NULL);
- lwc_string_unref(list->data.n.name);
+ dom_string_unref(list->data.n.name);
break;
}
@@ -178,7 +176,7 @@ void dom_nodelist_unref(struct dom_nodelist *list)
_dom_document_remove_nodelist(list->owner, list);
/* Destroy the list object */
- _dom_document_alloc(list->owner, list, 0);
+ free(list);
/* And release our reference on the owning document
* This must be last as, otherwise, it's possible that
@@ -194,10 +192,9 @@ void dom_nodelist_unref(struct dom_nodelist *list)
* \param length Pointer to location to receive length
* \return DOM_NO_ERR.
*/
-dom_exception dom_nodelist_get_length(struct dom_nodelist *list,
- unsigned long *length)
+dom_exception dom_nodelist_get_length(dom_nodelist *list, unsigned long *length)
{
- struct dom_node_internal *cur = list->root->first_child;
+ dom_node_internal *cur = list->root->first_child;
unsigned long len = 0;
/* Traverse data structure */
@@ -206,21 +203,21 @@ dom_exception dom_nodelist_get_length(struct dom_nodelist *list,
if (list->type == DOM_NODELIST_CHILDREN) {
len++;
} else if (list->type == DOM_NODELIST_BY_NAME) {
- /* Here, we compare two lwc_string pointer directly */
if (list->data.n.any_name == true || (
cur->name != NULL &&
- cur->name == list->data.n.name)) {
+ dom_string_isequal(cur->name,
+ list->data.n.name))) {
if (cur->type == DOM_ELEMENT_NODE)
len++;
}
} else {
- if (list->data.ns.any_namespace == true ||
- cur->namespace ==
- list->data.ns.namespace) {
+ if (list->data.ns.any_namespace == true ||
+ dom_string_isequal(cur->namespace,
+ list->data.ns.namespace)) {
if (list->data.ns.any_localname == true ||
(cur->name != NULL &&
- cur->name ==
- list->data.ns.localname)) {
+ dom_string_isequal(cur->name,
+ list->data.ns.localname))) {
if (cur->type == DOM_ELEMENT_NODE)
len++;
}
@@ -242,7 +239,7 @@ dom_exception dom_nodelist_get_length(struct dom_nodelist *list,
} else {
/* No children or siblings.
* Find first unvisited relation. */
- struct dom_node_internal *parent = cur->parent;
+ dom_node_internal *parent = cur->parent;
while (parent != list->root &&
cur == parent->last_child) {
@@ -274,10 +271,10 @@ dom_exception dom_nodelist_get_length(struct dom_nodelist *list,
* The returned node will have had its reference count increased. The client
* should unref the node once it has finished with it.
*/
-dom_exception _dom_nodelist_item(struct dom_nodelist *list,
- unsigned long index, struct dom_node **node)
+dom_exception _dom_nodelist_item(dom_nodelist *list,
+ unsigned long index, dom_node **node)
{
- struct dom_node_internal *cur = list->root->first_child;
+ dom_node_internal *cur = list->root->first_child;
unsigned long count = 0;
/* Traverse data structure */
@@ -288,19 +285,20 @@ dom_exception _dom_nodelist_item(struct dom_nodelist *list,
} else if (list->type == DOM_NODELIST_BY_NAME) {
if (list->data.n.any_name == true || (
cur->name != NULL &&
- cur->name == list->data.n.name)) {
+ dom_string_isequal(cur->name,
+ list->data.n.name))) {
if (cur->type == DOM_ELEMENT_NODE)
count++;
}
} else {
if (list->data.ns.any_namespace == true ||
(cur->namespace != NULL &&
- cur->namespace ==
- list->data.ns.namespace)) {
+ dom_string_isequal(cur->namespace,
+ list->data.ns.namespace))) {
if (list->data.ns.any_localname == true ||
(cur->name != NULL &&
- cur->name ==
- list->data.ns.localname)) {
+ dom_string_isequal(cur->name,
+ list->data.ns.localname))) {
if (cur->type == DOM_ELEMENT_NODE)
count++;
}
@@ -327,7 +325,7 @@ dom_exception _dom_nodelist_item(struct dom_nodelist *list,
} else {
/* No children or siblings.
* Find first unvisited relation. */
- struct dom_node_internal *parent = cur->parent;
+ dom_node_internal *parent = cur->parent;
while (parent != list->root &&
cur == parent->last_child) {
@@ -343,7 +341,7 @@ dom_exception _dom_nodelist_item(struct dom_nodelist *list,
if (cur != NULL) {
dom_node_ref(cur);
}
- *node = (struct dom_node *) cur;
+ *node = (dom_node *) cur;
return DOM_NO_ERR;
}
@@ -359,9 +357,9 @@ dom_exception _dom_nodelist_item(struct dom_nodelist *list,
* \param localname Local part of nodes in list (or NULL)
* \return true if list matches, false otherwise
*/
-bool _dom_nodelist_match(struct dom_nodelist *list, nodelist_type type,
- struct dom_node_internal *root, struct lwc_string_s *tagname,
- struct lwc_string_s *namespace, struct lwc_string_s *localname)
+bool _dom_nodelist_match(dom_nodelist *list, nodelist_type type,
+ dom_node_internal *root, dom_string *tagname,
+ dom_string *namespace, dom_string *localname)
{
if (list->root != root)
return false;
@@ -374,12 +372,12 @@ bool _dom_nodelist_match(struct dom_nodelist *list, nodelist_type type,
}
if (list->type == DOM_NODELIST_BY_NAME) {
- return (list->data.n.name == tagname);
+ return dom_string_isequal(list->data.n.name, tagname);
}
if (list->type == DOM_NODELIST_BY_NAMESPACE) {
- return (list->data.ns.namespace == namespace) &&
- (list->data.ns.localname == localname);
+ return dom_string_isequal(list->data.ns.namespace, namespace) &&
+ dom_string_isequal(list->data.ns.localname, localname);
}
return false;
@@ -392,7 +390,7 @@ bool _dom_nodelist_match(struct dom_nodelist *list, nodelist_type type,
* \param l2 The other list
* \reutrn true for equal, false otherwise.
*/
-bool _dom_nodelist_equal(struct dom_nodelist *l1, struct dom_nodelist *l2)
+bool _dom_nodelist_equal(dom_nodelist *l1, dom_nodelist *l2)
{
return _dom_nodelist_match(l1, l1->type, l2->root, l2->data.n.name,
l2->data.ns.namespace, l2->data.ns.localname);
diff --git a/src/core/nodelist.h b/src/core/nodelist.h
index ba00cbf..6bc36d7 100644
--- a/src/core/nodelist.h
+++ b/src/core/nodelist.h
@@ -15,7 +15,6 @@
struct dom_document;
struct dom_node;
struct dom_nodelist;
-struct lwc_string_s;
/**
* The NodeList type
@@ -28,14 +27,14 @@ typedef enum {
/* Create a nodelist */
dom_exception _dom_nodelist_create(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_node_internal *root, dom_string *tagname,
+ dom_string *namespace, dom_string *localname,
struct dom_nodelist **list);
/* Match a nodelist instance against a set of nodelist creation parameters */
bool _dom_nodelist_match(struct dom_nodelist *list, 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_node_internal *root, dom_string *tagname,
+ dom_string *namespace, dom_string *localname);
bool _dom_nodelist_equal(struct dom_nodelist *l1, struct dom_nodelist *l2);
#define dom_nodelist_equal(l1, l2) _dom_nodelist_equal( \
diff --git a/src/core/pi.c b/src/core/pi.c
index 1d7a508..d12f109 100644
--- a/src/core/pi.c
+++ b/src/core/pi.c
@@ -5,6 +5,8 @@
* Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org>
*/
+#include <stdlib.h>
+
#include "core/document.h"
#include "core/node.h"
#include "core/pi.h"
@@ -15,10 +17,13 @@
* A DOM processing instruction
*/
struct dom_processing_instruction {
- struct dom_node_internal base; /**< Base node */
+ dom_node_internal base; /**< Base node */
};
static struct dom_node_vtable pi_vtable = {
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE
+ },
DOM_NODE_VTABLE
};
@@ -39,16 +44,15 @@ static struct dom_node_protect_vtable pi_protect_vtable = {
*
* The returned node will already be referenced.
*/
-dom_exception _dom_processing_instruction_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_processing_instruction **result)
+dom_exception _dom_processing_instruction_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_processing_instruction **result)
{
- struct dom_processing_instruction *p;
+ dom_processing_instruction *p;
dom_exception err;
/* Allocate the comment node */
- p = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_processing_instruction));
+ p = malloc(sizeof(dom_processing_instruction));
if (p == NULL)
return DOM_NO_MEM_ERR;
@@ -60,7 +64,7 @@ dom_exception _dom_processing_instruction_create(struct dom_document *doc,
DOM_PROCESSING_INSTRUCTION_NODE,
name, value, NULL, NULL);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, p, 0);
+ free(p);
return err;
}
@@ -72,19 +76,17 @@ dom_exception _dom_processing_instruction_create(struct dom_document *doc,
/**
* Destroy a processing instruction
*
- * \param doc The owning document
* \param pi The processing instruction to destroy
*
* The contents of ::pi will be destroyed and ::pi will be freed.
*/
-void _dom_processing_instruction_destroy(struct dom_document *doc,
- struct dom_processing_instruction *pi)
+void _dom_processing_instruction_destroy(dom_processing_instruction *pi)
{
/* Finalise base class */
- _dom_processing_instruction_finalise(doc, &pi->base);
+ _dom_processing_instruction_finalise(&pi->base);
/* Free processing instruction */
- _dom_document_alloc(doc, pi, 0);
+ free(pi);
}
/*-----------------------------------------------------------------------*/
@@ -92,34 +94,30 @@ void _dom_processing_instruction_destroy(struct dom_document *doc,
/* Following comes the protected vtable */
/* The virtual destroy function of this class */
-void _dom_pi_destroy(struct dom_node_internal *node)
+void _dom_pi_destroy(dom_node_internal *node)
{
- _dom_processing_instruction_destroy(node->owner,
- (struct dom_processing_instruction *) node);
+ _dom_processing_instruction_destroy(
+ (dom_processing_instruction *) node);
}
-/* The memory allocator of this class */
-dom_exception _dom_pi_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_pi_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(n);
- struct dom_processing_instruction *a;
-
- a = _dom_document_alloc(doc, NULL,
- sizeof(struct dom_processing_instruction));
- if (a == NULL)
+ dom_processing_instruction *new_pi;
+ dom_exception err;
+
+ new_pi = malloc(sizeof(dom_processing_instruction));
+ if (new_pi == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) a;
- dom_node_set_owner(*ret, doc);
- return DOM_NO_ERR;
-}
+ err = dom_node_copy_internal(old, new_pi);
+ if (err != DOM_NO_ERR) {
+ free(new_pi);
+ return err;
+ }
-/* The copy constructor of this class */
-dom_exception _dom_pi_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
-{
- return _dom_node_copy(new, old);
+ *copy = (dom_node_internal *) copy;
+
+ return DOM_NO_ERR;
}
diff --git a/src/core/pi.h b/src/core/pi.h
index 8eca330..0690d1d 100644
--- a/src/core/pi.h
+++ b/src/core/pi.h
@@ -9,31 +9,23 @@
#define dom_internal_core_processinginstruction_h_
#include <dom/core/exceptions.h>
+#include <dom/core/pi.h>
-struct dom_document;
-struct dom_processing_instruction;
-struct lwc_string_s;
+dom_exception _dom_processing_instruction_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_processing_instruction **result);
-dom_exception _dom_processing_instruction_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_processing_instruction **result);
-
-void _dom_processing_instruction_destroy(struct dom_document *doc,
- struct dom_processing_instruction *pi);
+void _dom_processing_instruction_destroy(dom_processing_instruction *pi);
#define _dom_processing_instruction_initialise _dom_node_initialise
#define _dom_processing_instruction_finalise _dom_node_finalise
/* Following comes the protected vtable */
-void _dom_pi_destroy(struct dom_node_internal *node);
-dom_exception _dom_pi_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_pi_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+void _dom_pi_destroy(dom_node_internal *node);
+dom_exception _dom_pi_copy(dom_node_internal *old, dom_node_internal **copy);
#define DOM_PI_PROTECT_VTABLE \
_dom_pi_destroy, \
- _dom_pi_alloc, \
_dom_pi_copy
#endif
diff --git a/src/core/string.c b/src/core/string.c
index 4c104ae..dde24a8 100644
--- a/src/core/string.c
+++ b/src/core/string.c
@@ -9,6 +9,7 @@
#include <assert.h>
#include <ctype.h>
#include <inttypes.h>
+#include <stdlib.h>
#include <string.h>
#include <parserutils/charset/utf8.h>
@@ -18,43 +19,52 @@
#include "utils/utils.h"
/**
+ * Type of a DOM string
+ */
+enum dom_string_type {
+ DOM_STRING_CDATA = 0,
+ DOM_STRING_INTERNED = 1
+};
+
+/**
* A DOM string
*
* Strings are reference counted so destruction is performed correctly.
*/
struct dom_string {
- uint8_t *ptr; /**< Pointer to string data */
-
- size_t len; /**< Byte length of string */
-
- lwc_string *intern; /**< The lwc_string of this string */
-
- dom_alloc alloc; /**< Memory (de)allocation function */
- void *pw; /**< Client-specific data */
-
- uint32_t refcnt; /**< Reference count */
+ union {
+ struct {
+ uint8_t *ptr; /**< Pointer to string data */
+ size_t len; /**< Byte length of string */
+ } cdata;
+ lwc_string *intern; /**< Interned string */
+ } data;
+
+ unsigned int refcnt : 31, /**< Reference count */
+ type : 1; /**< String type */
};
-static dom_string empty_string = {
- .ptr = NULL,
- .len = 0,
- .intern = NULL,
- .alloc = NULL,
- .pw = NULL,
- .refcnt = 1
+/**
+ * Empty string, for comparisons against NULL
+ */
+static const dom_string empty_string = {
+ { { (uint8_t *) "", 0 } },
+ 0,
+ DOM_STRING_CDATA
};
-
-
/**
* Claim a reference on a DOM string
*
* \param str The string to claim a reference on
+ * \return \a str
*/
-void dom_string_ref(dom_string *str)
+dom_string *dom_string_ref(dom_string *str)
{
if (str != NULL)
str->refcnt++;
+
+ return str;
}
/**
@@ -67,25 +77,25 @@ void dom_string_ref(dom_string *str)
*/
void dom_string_unref(dom_string *str)
{
- if (str == NULL)
- return;
-
- if (--str->refcnt == 0) {
- if (str->intern != NULL) {
- lwc_string_unref(str->intern);
- str->alloc(str, 0, str->pw);
- } else if (str->alloc != NULL) {
- str->alloc(str->ptr, 0, str->pw);
- str->alloc(str, 0, str->pw);
+ if (str != NULL && --str->refcnt == 0) {
+ switch (str->type) {
+ case DOM_STRING_INTERNED:
+ if (str->data.intern != NULL) {
+ lwc_string_unref(str->data.intern);
+ }
+ break;
+ case DOM_STRING_CDATA:
+ free(str->data.cdata.ptr);
+ break;
}
+
+ free(str);
}
}
/**
* Create a DOM string from a string of characters
*
- * \param alloc Memory (de)allocation function
- * \param pw Pointer to client-specific private data
* \param ptr Pointer to string of characters
* \param len Length, in bytes, of string of characters
* \param str Pointer to location to receive result
@@ -97,85 +107,48 @@ void dom_string_unref(dom_string *str)
* The string of characters passed in will be copied for use by the
* returned DOM string.
*/
-dom_exception dom_string_create(dom_alloc alloc, void *pw,
- const uint8_t *ptr, size_t len, dom_string **str)
+dom_exception dom_string_create(const uint8_t *ptr, size_t len,
+ dom_string **str)
{
dom_string *ret;
if (ptr == NULL || len == 0) {
- dom_string_ref(&empty_string);
-
- *str = &empty_string;
-
- return DOM_NO_ERR;
+ ptr = (const uint8_t *) "";
+ len = 0;
}
- ret = alloc(NULL, sizeof(dom_string), pw);
+ ret = malloc(sizeof(dom_string));
if (ret == NULL)
return DOM_NO_MEM_ERR;
- ret->ptr = alloc(NULL, len, pw);
- if (ret->ptr == NULL) {
- alloc(ret, 0, pw);
+ ret->data.cdata.ptr = malloc(len);
+ if (ret->data.cdata.ptr == NULL) {
+ free(ret);
return DOM_NO_MEM_ERR;
}
- memcpy(ret->ptr, ptr, len);
+ memcpy(ret->data.cdata.ptr, ptr, len);
- ret->len = len;
-
- ret->alloc = alloc;
- ret->pw = pw;
-
- ret->intern = NULL;
+ ret->data.cdata.len = len;
ret->refcnt = 1;
+ ret->type = DOM_STRING_CDATA;
+
*str = ret;
return DOM_NO_ERR;
}
/**
- * Clone a dom_string if necessary. This method is used to create a new string
- * with a new allocator, but if the allocator is the same with the paramter
- * str, just ref the string.
- *
- * \param alloc The new allocator for this string
- * \param pw The new pw for this string
- * \param str The source dom_string
- * \param ret The cloned dom_string
- * \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
- *
- * @note: When both the alloc and pw are the same as the str's, we need no
- * real clone, just ref the source string is ok.
- */
-dom_exception dom_string_clone(dom_alloc alloc, void *pw,
- dom_string *str, dom_string **ret)
-{
- if (alloc == str->alloc && pw == str->pw) {
- *ret = str;
- dom_string_ref(str);
- return DOM_NO_ERR;
- }
-
- if (str->intern != NULL) {
- return _dom_string_create_from_lwcstring(alloc, pw,
- str->intern, ret);
- } else {
- return dom_string_create(alloc, pw, str->ptr, str->len, ret);
- }
-}
-
-/**
* Create a dom_string from a lwc_string
*
* \param str The lwc_string
* \param ret The new dom_string
* \return DOM_NO_ERR on success, DOM_NO_MEM_ERR on memory exhaustion
*/
-dom_exception _dom_string_create_from_lwcstring(dom_alloc alloc, void *pw,
- lwc_string *str, dom_string **ret)
+dom_exception _dom_string_create_from_lwcstring(lwc_string *str,
+ dom_string **ret)
{
dom_string *r;
@@ -184,27 +157,15 @@ dom_exception _dom_string_create_from_lwcstring(dom_alloc alloc, void *pw,
return DOM_NO_ERR;
}
- r = alloc(NULL, sizeof(dom_string), pw);
+ r = malloc(sizeof(dom_string));
if (r == NULL)
return DOM_NO_MEM_ERR;
- if (str == NULL) {
- *ret = &empty_string;
- dom_string_ref(*ret);
- return DOM_NO_ERR;
- }
-
- r->intern = str;
- r->ptr = (uint8_t *)lwc_string_data(str);
- r->len = lwc_string_length(str);
-
- r->alloc = alloc;
- r->pw = pw;
+ r->data.intern = lwc_string_ref(str);
r->refcnt = 1;
- /* Ref the lwc_string */
- lwc_string_ref(str);
+ r->type = DOM_STRING_INTERNED;
*ret = r;
return DOM_NO_ERR;
@@ -218,49 +179,28 @@ dom_exception _dom_string_create_from_lwcstring(dom_alloc alloc, void *pw,
* \param lwcstr The result lwc_string
* \return DOM_NO_ERR on success, appropriate dom_exception on failure.
*/
-dom_exception _dom_string_intern(dom_string *str,
+dom_exception dom_string_intern(dom_string *str,
struct lwc_string_s **lwcstr)
{
- lwc_string *ret;
- lwc_error lerr;
-
/* If this string is already interned, do nothing */
- if (str->intern != NULL) {
- *lwcstr = lwc_string_ref(str->intern);
- return DOM_NO_ERR;
- }
+ if (str->type != DOM_STRING_INTERNED) {
+ lwc_string *ret;
+ lwc_error lerr;
+
+ lerr = lwc_intern_string((const char *) str->data.cdata.ptr,
+ str->data.cdata.len, &ret);
+ if (lerr != lwc_error_ok) {
+ return _dom_exception_from_lwc_error(lerr);
+ }
- lerr = lwc_intern_string((const char *)str->ptr, str->len, &ret);
- if (lerr != lwc_error_ok) {
- return _dom_exception_from_lwc_error(lerr);
- }
+ free(str->data.cdata.ptr);
- str->intern = ret;
+ str->data.intern = ret;
- if (str->ptr != NULL) {
- str->alloc(str->ptr, 0, str->pw);
+ str->type = DOM_STRING_INTERNED;
}
- str->ptr = (uint8_t *) lwc_string_data(ret);
-
- *lwcstr = lwc_string_ref(ret);
- return DOM_NO_ERR;
-}
-
-/**
- * Get the internal lwc_string
- *
- * \param str The dom_string object
- * \param lwcstr The lwc_string of this dom-string
- * \return DOM_NO_ERR on success, appropriate dom_exception on failure.
- */
-dom_exception dom_string_get_intern(dom_string *str,
- struct lwc_string_s **lwcstr)
-{
- *lwcstr = str->intern;
-
- if (*lwcstr != NULL)
- lwc_string_ref(*lwcstr);
+ *lwcstr = lwc_string_ref(str->data.intern);
return DOM_NO_ERR;
}
@@ -270,33 +210,40 @@ dom_exception dom_string_get_intern(dom_string *str,
*
* \param s1 The first string to compare
* \param s2 The second string to compare
- * \return 0 if strings match, non-0 otherwise
- *
- * NULL and "" will match.
+ * \return true if strings match, false otherwise
*/
-int dom_string_cmp(dom_string *s1, dom_string *s2)
+bool dom_string_isequal(const dom_string *s1, const dom_string *s2)
{
- bool ret;
-
if (s1 == NULL)
s1 = &empty_string;
if (s2 == NULL)
s2 = &empty_string;
- if (s1->intern != NULL && s2->intern != NULL) {
- lwc_string_isequal(s1->intern, s2->intern, &ret);
- if (ret == true) {
- return 0;
- } else {
- return -1;
- }
+ if (s1->type == DOM_STRING_INTERNED &&
+ s2->type == DOM_STRING_INTERNED) {
+ bool match;
+
+ lwc_string_isequal(s1->data.intern, s2->data.intern, &match);
+
+ return match;
}
- if (s1->len != s2->len)
- return 1;
+ if (s1->data.cdata.len != s2->data.cdata.len)
+ return false;
+
+ return 0 == memcmp(s1->data.cdata.ptr, s2->data.cdata.ptr,
+ s1->data.cdata.len);
+}
- return memcmp(s1->ptr, s2->ptr, s1->len);
+/**
+ * Trivial locale-agnostic lower case convertor
+ */
+static inline uint8_t dolower(const uint8_t c)
+{
+ if ('A' <= c && c <= 'Z')
+ return c + 'a' - 'A';
+ return c;
}
/**
@@ -304,62 +251,47 @@ int dom_string_cmp(dom_string *s1, dom_string *s2)
*
* \param s1 The first string to compare
* \param s2 The second string to compare
- * \return 0 if strings match, non-0 otherwise
- *
- * NULL and "" will match.
+ * \return true if strings match, false otherwise
*/
-int dom_string_icmp(dom_string *s1, dom_string *s2)
+bool dom_string_caseless_isequal(const dom_string *s1, const dom_string *s2)
{
const uint8_t *d1 = NULL;
const uint8_t *d2 = NULL;
- size_t l1, l2;
+ size_t len;
if (s1 == NULL)
s1 = &empty_string;
+
if (s2 == NULL)
s2 = &empty_string;
- bool ret;
- if (s1->intern != NULL && s2->intern != NULL) {
- lwc_string_caseless_isequal(s1->intern, s2->intern, &ret);
- if (ret == true) {
- return 0;
- } else {
- return -1;
- }
- }
+ if (s1->type == DOM_STRING_INTERNED &&
+ s2->type == DOM_STRING_INTERNED) {
+ bool match;
- d1 = s1->ptr;
- d2 = s2->ptr;
- l1 = s1->len;
- l2 = s2->len;
+ lwc_string_caseless_isequal(s1->data.intern, s2->data.intern,
+ &match);
- while (l1 > 0 && l2 > 0) {
- uint32_t c1, c2;
- size_t cl1, cl2;
- parserutils_error err;
-
- err = parserutils_charset_utf8_to_ucs4(d1, l1, &c1, &cl1);
- if (err != PARSERUTILS_OK) {
- }
+ return match;
+ }
- err = parserutils_charset_utf8_to_ucs4(d2, l2, &c2, &cl2);
- if (err != PARSERUTILS_OK) {
- }
+ if (s1->data.cdata.len != s2->data.cdata.len)
+ return false;
- /** \todo improved lower-casing algorithm */
- if (tolower(c1) != tolower(c2)) {
- return (int)(tolower(c1) - tolower(c2));
- }
+ d1 = s1->data.cdata.ptr;
+ d2 = s2->data.cdata.ptr;
+ len = s1->data.cdata.len;
- d1 += cl1;
- d2 += cl2;
+ while (len > 0) {
+ if (dolower(*d1) != dolower(*d2))
+ return false;
- l1 -= cl1;
- l2 -= cl2;
+ d1++;
+ d2++;
+ len--;
}
- return (int)(l1 - l2);
+ return true;
}
/**
@@ -376,11 +308,8 @@ uint32_t dom_string_index(dom_string *str, uint32_t chr)
uint32_t c, index;
parserutils_error err;
- if (str == NULL)
- str = &empty_string;
-
- s = str->ptr;
- slen = str->len;
+ s = (const uint8_t *) _dom_string_data(str);
+ slen = _dom_string_byte_length(str);
index = 0;
@@ -416,11 +345,8 @@ uint32_t dom_string_rindex(dom_string *str, uint32_t chr)
uint32_t c, coff, index;
parserutils_error err;
- if (str == NULL)
- str = &empty_string;
-
- s = str->ptr;
- slen = str->len;
+ s = (const uint8_t *) _dom_string_data(str);
+ slen = _dom_string_byte_length(str);
index = dom_string_length(str);
@@ -455,13 +381,14 @@ uint32_t dom_string_rindex(dom_string *str, uint32_t chr)
*/
uint32_t dom_string_length(dom_string *str)
{
- size_t clen;
+ const uint8_t *s;
+ size_t slen, clen;
parserutils_error err;
- if (str == NULL)
- str = &empty_string;
+ s = (const uint8_t *) _dom_string_data(str);
+ slen = _dom_string_byte_length(str);
- err = parserutils_charset_utf8_length(str->ptr, str->len, &clen);
+ err = parserutils_charset_utf8_length(s, slen, &clen);
if (err != PARSERUTILS_OK) {
return 0;
}
@@ -484,11 +411,8 @@ dom_exception dom_string_at(dom_string *str, uint32_t index,
uint32_t c, i;
parserutils_error err;
- if (str == NULL)
- str = &empty_string;
-
- s = str->ptr;
- slen = str->len;
+ s = (const uint8_t *) _dom_string_data(str);
+ slen = _dom_string_byte_length(str);
i = 0;
@@ -527,59 +451,47 @@ dom_exception dom_string_at(dom_string *str, uint32_t index,
* \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 be allocated using the allocation details
- * stored in ::s1.
- *
- * The returned string will have its reference count increased. The client
+ * The returned string will be referenced. The client
* should dereference it once it has finished with it.
*/
dom_exception dom_string_concat(dom_string *s1, dom_string *s2,
dom_string **result)
{
dom_string *concat;
- dom_alloc alloc;
- void *pw;
+ const uint8_t *s1ptr, *s2ptr;
+ size_t s1len, s2len;
assert(s1 != NULL);
assert(s2 != NULL);
- if (s1->alloc != NULL) {
- alloc = s1->alloc;
- pw = s1->pw;
- } else if (s2->alloc != NULL) {
- alloc = s2->alloc;
- pw = s2->pw;
- } else {
- /* s1 == s2 == empty_string */
- *result = &empty_string;
- return DOM_NO_ERR;
- }
+ s1ptr = (const uint8_t *) _dom_string_data(s1);
+ s2ptr = (const uint8_t *) _dom_string_data(s2);
+ s1len = _dom_string_byte_length(s1);
+ s2len = _dom_string_byte_length(s2);
- concat = alloc(NULL, sizeof(dom_string), pw);
+ concat = malloc(sizeof(dom_string));
if (concat == NULL) {
return DOM_NO_MEM_ERR;
}
- concat->ptr = alloc(NULL, s1->len + s2->len, pw);
- if (concat->ptr == NULL) {
- alloc(concat, 0, pw);
+ concat->data.cdata.ptr = malloc(s1len + s2len);
+ if (concat->data.cdata.ptr == NULL) {
+ free(concat);
return DOM_NO_MEM_ERR;
}
- memcpy(concat->ptr, s1->ptr, s1->len);
-
- memcpy(concat->ptr + s1->len, s2->ptr, s2->len);
+ memcpy(concat->data.cdata.ptr, s1ptr, s1len);
- concat->len = s1->len + s2->len;
+ memcpy(concat->data.cdata.ptr + s1len, s2ptr, s2len);
- concat->alloc = alloc;
- concat->pw = pw;
- concat->intern = NULL;
+ concat->data.cdata.len = s1len + s2len;
concat->refcnt = 1;
+ concat->type = DOM_STRING_CDATA;
+
*result = concat;
return DOM_NO_ERR;
@@ -594,17 +506,14 @@ dom_exception dom_string_concat(dom_string *s1, dom_string *s2,
* \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 be allocated using the allocation details
- * stored in ::str.
- *
* The returned string will have its reference count increased. The client
* should dereference it once it has finished with it.
*/
dom_exception dom_string_substr(dom_string *str,
uint32_t i1, uint32_t i2, dom_string **result)
{
- const uint8_t *s = str->ptr;
- size_t slen = str->len;
+ const uint8_t *s = (const uint8_t *) _dom_string_data(str);
+ size_t slen = _dom_string_byte_length(str);
uint32_t b1, b2;
parserutils_error err;
@@ -637,7 +546,7 @@ dom_exception dom_string_substr(dom_string *str,
}
/* Create a string from the specified byte range */
- return dom_string_create(str->alloc, str->pw, s + b1, b2 - b1, result);
+ return dom_string_create(s + b1, b2 - b1, result);
}
/**
@@ -651,9 +560,6 @@ dom_exception dom_string_substr(dom_string *str,
* DOM_NO_MEM_ERR on memory exhaustion,
* DOM_INDEX_SIZE_ERR if ::offset > len(::target).
*
- * The returned string will be allocated using the allocation details
- * stored in ::target.
- *
* The returned string will have its reference count increased. The client
* should dereference it once it has finished with it.
*/
@@ -667,10 +573,10 @@ dom_exception dom_string_insert(dom_string *target,
uint32_t ins = 0;
parserutils_error err;
- t = target->ptr;
- tlen = target->len;
- s = source->ptr;
- slen = source->len;
+ t = (const uint8_t *) _dom_string_data(target);
+ tlen = _dom_string_byte_length(target);
+ s = (const uint8_t *) _dom_string_data(source);
+ slen = _dom_string_byte_length(source);
clen = dom_string_length(target);
@@ -696,39 +602,37 @@ dom_exception dom_string_insert(dom_string *target,
}
/* Allocate result string */
- res = target->alloc(NULL, sizeof(dom_string), target->pw);
+ res = malloc(sizeof(dom_string));
if (res == NULL) {
return DOM_NO_MEM_ERR;
}
/* Allocate data buffer for result contents */
- res->ptr = target->alloc(NULL, tlen + slen, target->pw);
- if (res->ptr == NULL) {
- target->alloc(res, 0, target->pw);
+ res->data.cdata.ptr = malloc(tlen + slen);
+ if (res->data.cdata.ptr == NULL) {
+ free(res);
return DOM_NO_MEM_ERR;
}
/* Copy initial portion of target, if any, into result */
if (ins > 0) {
- memcpy(res->ptr, t, ins);
+ memcpy(res->data.cdata.ptr, t, ins);
}
/* Copy inserted data into result */
- memcpy(res->ptr + ins, s, slen);
+ memcpy(res->data.cdata.ptr + ins, s, slen);
/* Copy remainder of target, if any, into result */
if (tlen - ins > 0) {
- memcpy(res->ptr + ins + slen, t + ins, tlen - ins);
+ memcpy(res->data.cdata.ptr + ins + slen, t + ins, tlen - ins);
}
- res->len = tlen + slen;
+ res->data.cdata.len = tlen + slen;
- res->alloc = target->alloc;
- res->pw = target->pw;
- res->intern = NULL;
-
res->refcnt = 1;
+ res->type = DOM_STRING_CDATA;
+
*result = res;
return DOM_NO_ERR;
@@ -744,9 +648,6 @@ dom_exception dom_string_insert(dom_string *target,
* \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 be allocated using the allocation details
- * stored in ::target.
- *
* The returned string will have its reference count increased. The client
* should dereference it once it has finished with it.
*/
@@ -760,13 +661,10 @@ dom_exception dom_string_replace(dom_string *target,
uint32_t b1, b2;
parserutils_error err;
- if (source == NULL)
- source = &empty_string;
-
- t = target->ptr;
- tlen = target->len;
- s = source->ptr;
- slen = source->len;
+ t = (const uint8_t *) _dom_string_data(target);
+ tlen = _dom_string_byte_length(target);
+ s = (const uint8_t *) _dom_string_data(source);
+ slen = _dom_string_byte_length(source);
/* Initialise the byte index of the start to 0 */
b1 = 0;
@@ -799,73 +697,45 @@ dom_exception dom_string_replace(dom_string *target,
}
/* Allocate result string */
- res = target->alloc(NULL, sizeof(dom_string), target->pw);
-
+ res = malloc(sizeof(dom_string));
if (res == NULL) {
return DOM_NO_MEM_ERR;
}
/* Allocate data buffer for result contents */
- res->ptr = target->alloc(NULL, tlen + slen - (b2 - b1), target->pw);
- if (res->ptr == NULL) {
- target->alloc(res, 0, target->pw);
+ res->data.cdata.ptr = malloc(tlen + slen - (b2 - b1));
+ if (res->data.cdata.ptr == NULL) {
+ free(res);
return DOM_NO_MEM_ERR;
}
/* Copy initial portion of target, if any, into result */
if (b1 > 0) {
- memcpy(res->ptr, t, b1);
+ memcpy(res->data.cdata.ptr, t, b1);
}
/* Copy replacement data into result */
if (slen > 0) {
- memcpy(res->ptr + b1, s, slen);
+ memcpy(res->data.cdata.ptr + b1, s, slen);
}
/* Copy remainder of target, if any, into result */
if (tlen - b2 > 0) {
- memcpy(res->ptr + b1 + slen, t + b2, tlen - b2);
+ memcpy(res->data.cdata.ptr + b1 + slen, t + b2, tlen - b2);
}
- res->len = tlen + slen - (b2 - b1);
-
- res->alloc = target->alloc;
- res->pw = target->pw;
- res->intern = NULL;
+ res->data.cdata.len = tlen + slen - (b2 - b1);
res->refcnt = 1;
+ res->type = DOM_STRING_CDATA;
+
*result = res;
return DOM_NO_ERR;
}
/**
- * Duplicate a dom string
- *
- * \param str The string to duplicate
- * \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 be allocated using the allocation details
- * stored in ::str.
- *
- * The returned string will have its reference count increased. The client
- * should dereference it once it has finished with it.
- */
-dom_exception dom_string_dup(dom_string *str,
- dom_string **result)
-{
- if (str->intern != NULL) {
- return _dom_string_create_from_lwcstring(str->alloc, str->pw,
- str->intern, result);
- } else {
- return dom_string_create(str->alloc, str->pw, str->ptr,
- str->len, result);
- }
-}
-
-/**
* Calculate a hash value from a dom string
*
* \param str The string to calculate a hash of
@@ -873,9 +743,9 @@ dom_exception dom_string_dup(dom_string *str,
*/
uint32_t dom_string_hash(dom_string *str)
{
- const uint8_t *s = str->ptr;
- size_t slen = str->len;
- uint32_t hash = 0x01000193;
+ const uint8_t *s = (const uint8_t *) _dom_string_data(str);
+ size_t slen = _dom_string_byte_length(str);
+ uint32_t hash = 0x811c9dc5;
while (slen > 0) {
hash *= 0x01000193;
@@ -897,15 +767,14 @@ uint32_t dom_string_hash(dom_string *str)
dom_exception _dom_exception_from_lwc_error(lwc_error err)
{
switch (err) {
- case lwc_error_ok:
- return DOM_NO_ERR;
- case lwc_error_oom:
- return DOM_NO_MEM_ERR;
- case lwc_error_range:
- return DOM_INDEX_SIZE_ERR;
+ case lwc_error_ok:
+ return DOM_NO_ERR;
+ case lwc_error_oom:
+ return DOM_NO_MEM_ERR;
+ case lwc_error_range:
+ return DOM_INDEX_SIZE_ERR;
}
- assert ("Unknow lwc_error, can't convert to dom_exception");
- /* Suppress compile errors */
+
return DOM_NO_ERR;
}
@@ -913,22 +782,30 @@ dom_exception _dom_exception_from_lwc_error(lwc_error err)
* Get the raw character data of the dom_string.
*
* \param str The dom_string object
- * \return The C string pointer
+ * \return The C string pointer
*
* @note: This function is just provided for the convenience of accessing the
* raw C string character, no change on the result string is allowed.
*/
-char *_dom_string_data(dom_string *str)
+const char *_dom_string_data(dom_string *str)
{
- return (char *) str->ptr;
+ if (str->type == DOM_STRING_CDATA) {
+ return (const char *) str->data.cdata.ptr;
+ } else {
+ return lwc_string_data(str->data.intern);
+ }
}
-/* Get the string length of this dom_string
+/* Get the byte length of this dom_string
*
* \param str The dom_string object
*/
-size_t _dom_string_length(dom_string *str)
+size_t _dom_string_byte_length(dom_string *str)
{
- return str->len;
+ if (str->type == DOM_STRING_CDATA) {
+ return str->data.cdata.len;
+ } else {
+ return lwc_string_length(str->data.intern);
+ }
}
diff --git a/src/core/string.h b/src/core/string.h
index d5a943a..3d5844a 100644
--- a/src/core/string.h
+++ b/src/core/string.h
@@ -1,3 +1,4 @@
+
/*
* This file is part of libdom.
* Licensed under the MIT License,
@@ -10,14 +11,9 @@
#include <dom/core/string.h>
-/* Create a DOM string from a lwc_string
- * This function call mainly used for create a string from lwc_string */
-dom_exception _dom_string_create_from_lwcstring(dom_alloc alloc, void *pw,
- struct lwc_string_s *str, dom_string **ret);
-
-/* Make the dom_string be interned */
-dom_exception _dom_string_intern(dom_string *str,
- struct lwc_string_s **lwcstr);
+/* Create a DOM string from a lwc_string */
+dom_exception _dom_string_create_from_lwcstring(struct lwc_string_s *str,
+ dom_string **ret);
/* Map the lwc_error to dom_exception */
dom_exception _dom_exception_from_lwc_error(lwc_error err);
@@ -27,10 +23,10 @@ dom_exception _dom_exception_from_lwc_error(lwc_error err);
* @note: This function is just provided for the convenience of accessing the
* raw C string character, no change on the result string is allowed.
*/
-char *_dom_string_data(dom_string *str);
+const char *_dom_string_data(dom_string *str);
-/* Get the string length of this dom_string */
-size_t _dom_string_length(dom_string *str);
+/* Get the byte length of this dom_string */
+size_t _dom_string_byte_length(dom_string *str);
#endif
diff --git a/src/core/text.c b/src/core/text.c
index 1cb8981..58f2c41 100644
--- a/src/core/text.c
+++ b/src/core/text.c
@@ -7,6 +7,7 @@
*/
#include <assert.h>
+#include <stdlib.h>
#include <dom/core/string.h>
#include <dom/core/text.h>
@@ -22,6 +23,9 @@
struct dom_text_vtable text_vtable = {
{
{
+ {
+ DOM_NODE_EVENT_TARGET_VTABLE,
+ },
DOM_NODE_VTABLE
},
DOM_CHARACTERDATA_VTABLE
@@ -68,28 +72,28 @@ static dom_exception walk_logic_adjacent_text(dom_text *text,
*
* The returned node will already be referenced.
*/
-dom_exception _dom_text_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_text **result)
+dom_exception _dom_text_create(dom_document *doc,
+ dom_string *name, dom_string *value,
+ dom_text **result)
{
- struct dom_text *t;
+ dom_text *t;
dom_exception err;
/* Allocate the text node */
- t = _dom_document_alloc(doc, NULL, sizeof(struct dom_text));
+ t = malloc(sizeof(dom_text));
if (t == NULL)
return DOM_NO_MEM_ERR;
/* And initialise the node */
err = _dom_text_initialise(t, doc, DOM_TEXT_NODE, name, value);
if (err != DOM_NO_ERR) {
- _dom_document_alloc(doc, t, 0);
+ free(t);
return err;
}
/* Compose the vtable */
- ((struct dom_node *) t)->vtable = &text_vtable;
- ((struct dom_node_internal *) t)->vtable = &text_protect_vtable;
+ ((dom_node *) t)->vtable = &text_vtable;
+ ((dom_node_internal *) t)->vtable = &text_protect_vtable;
*result = t;
@@ -104,13 +108,13 @@ dom_exception _dom_text_create(struct dom_document *doc,
*
* The contents of ::text will be destroyed and ::text will be freed.
*/
-void _dom_text_destroy(struct dom_document *doc, struct dom_text *text)
+void _dom_text_destroy(dom_text *text)
{
/* Finalise node */
- _dom_text_finalise(doc, text);
+ _dom_text_finalise(text);
/* Free node */
- _dom_document_alloc(doc, text, 0);
+ free(text);
}
/**
@@ -125,9 +129,9 @@ void _dom_text_destroy(struct dom_document *doc, struct dom_text *text)
*
* ::doc, ::name and ::value will have their reference counts increased.
*/
-dom_exception _dom_text_initialise(struct dom_text *text,
- struct dom_document *doc, dom_node_type type,
- struct lwc_string_s *name, dom_string *value)
+dom_exception _dom_text_initialise(dom_text *text,
+ dom_document *doc, dom_node_type type,
+ dom_string *name, dom_string *value)
{
dom_exception err;
@@ -151,9 +155,9 @@ dom_exception _dom_text_initialise(struct dom_text *text,
*
* The contents of ::text will be cleaned up. ::text will not be freed.
*/
-void _dom_text_finalise(struct dom_document *doc, struct dom_text *text)
+void _dom_text_finalise(dom_text *text)
{
- _dom_characterdata_finalise(doc, &text->base);
+ _dom_characterdata_finalise(&text->base);
}
/*----------------------------------------------------------------------*/
@@ -173,12 +177,12 @@ void _dom_text_finalise(struct dom_document *doc, struct dom_text *text)
* The returned node will be referenced. The client should unref the node
* once it has finished with it.
*/
-dom_exception _dom_text_split_text(struct dom_text *text,
- unsigned long offset, struct dom_text **result)
+dom_exception _dom_text_split_text(dom_text *text,
+ unsigned long offset, dom_text **result)
{
- struct dom_node_internal *t = (struct dom_node_internal *) text;
+ dom_node_internal *t = (dom_node_internal *) text;
dom_string *value;
- struct dom_text *res;
+ dom_text *res;
unsigned long len;
dom_exception err;
@@ -233,7 +237,7 @@ dom_exception _dom_text_split_text(struct dom_text *text,
* \return DOM_NO_ERR.
*/
dom_exception _dom_text_get_is_element_content_whitespace(
- struct dom_text *text, bool *result)
+ dom_text *text, bool *result)
{
*result = text->element_content_whitespace;
@@ -247,7 +251,7 @@ dom_exception _dom_text_get_is_element_content_whitespace(
* \param result Pointer to location to receive result
* \return DOM_NO_ERR.
*/
-dom_exception _dom_text_get_whole_text(struct dom_text *text,
+dom_exception _dom_text_get_whole_text(dom_text *text,
dom_string **result)
{
return walk_logic_adjacent_text(text, COLLECT, result);
@@ -266,8 +270,8 @@ dom_exception _dom_text_get_whole_text(struct dom_text *text,
* The returned node will be referenced. The client should unref the node
* once it has finished with it.
*/
-dom_exception _dom_text_replace_whole_text(struct dom_text *text,
- dom_string *content, struct dom_text **result)
+dom_exception _dom_text_replace_whole_text(dom_text *text,
+ dom_string *content, dom_text **result)
{
dom_exception err;
dom_string *ret;
@@ -290,41 +294,43 @@ dom_exception _dom_text_replace_whole_text(struct dom_text *text,
/* The protected virtual functions */
/* The destroy function of this class */
-void __dom_text_destroy(struct dom_node_internal *node)
+void __dom_text_destroy(dom_node_internal *node)
{
- struct dom_document *doc;
- doc = dom_node_get_owner(node);
-
- _dom_text_destroy(doc, (struct dom_text *) node);
+ _dom_text_destroy((dom_text *) node);
}
-/* The memory allocator for this class */
-dom_exception _dom_text_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret)
+/* The copy constructor of this class */
+dom_exception _dom_text_copy(dom_node_internal *old, dom_node_internal **copy)
{
- UNUSED(n);
- dom_text *a;
-
- a = _dom_document_alloc(doc, NULL, sizeof(struct dom_text));
- if (a == NULL)
+ dom_text *new_text;
+ dom_exception err;
+
+ new_text = malloc(sizeof(dom_text));
+ if (new_text == NULL)
return DOM_NO_MEM_ERR;
-
- *ret = (dom_node_internal *) a;
- dom_node_set_owner(*ret, doc);
+
+ err = dom_text_copy_internal(old, new_text);
+ if (err != DOM_NO_ERR) {
+ free(new_text);
+ return err;
+ }
+
+ *copy = (dom_node_internal *) new_text;
return DOM_NO_ERR;
}
-/* The copy constructor of this class */
-dom_exception _dom_text_copy(struct dom_node_internal *new,
- struct dom_node_internal *old)
+dom_exception _dom_text_copy_internal(dom_text *old, dom_text *new)
{
- dom_text *ot = (dom_text *) old;
- dom_text *nt = (dom_text *) new;
+ dom_exception err;
- nt->element_content_whitespace = ot->element_content_whitespace;
+ err = dom_characterdata_copy_internal(old, new);
+ if (err != DOM_NO_ERR)
+ return err;
- return _dom_characterdata_copy(new, old);
+ new->element_content_whitespace = old->element_content_whitespace;
+
+ return DOM_NO_ERR;
}
/*----------------------------------------------------------------------*/
diff --git a/src/core/text.h b/src/core/text.h
index c1d0956..1cc5b1e 100644
--- a/src/core/text.h
+++ b/src/core/text.h
@@ -11,17 +11,17 @@
#include <stdbool.h>
#include <dom/core/exceptions.h>
+#include <dom/core/text.h>
#include "core/characterdata.h"
struct dom_document;
-struct lwc_string_s;
/**
* A DOM text node
*/
struct dom_text {
- struct dom_characterdata base; /**< Base node */
+ dom_characterdata base; /**< Base node */
bool element_content_whitespace; /**< This node is element
* content whitespace */
@@ -29,27 +29,27 @@ struct dom_text {
/* Constructor and Destructor */
dom_exception _dom_text_create(struct dom_document *doc,
- struct lwc_string_s *name, dom_string *value,
- struct dom_text **result);
+ dom_string *name, dom_string *value,
+ dom_text **result);
-void _dom_text_destroy(struct dom_document *doc, struct dom_text *text);
+void _dom_text_destroy(dom_text *text);
-dom_exception _dom_text_initialise(struct dom_text *text,
+dom_exception _dom_text_initialise(dom_text *text,
struct dom_document *doc, dom_node_type type,
- struct lwc_string_s *name, dom_string *value);
+ dom_string *name, dom_string *value);
-void _dom_text_finalise(struct dom_document *doc, struct dom_text *text);
+void _dom_text_finalise(dom_text *text);
/* Virtual functions for dom_text */
-dom_exception _dom_text_split_text(struct dom_text *text,
- unsigned long offset, struct dom_text **result);
+dom_exception _dom_text_split_text(dom_text *text,
+ unsigned long offset, dom_text **result);
dom_exception _dom_text_get_is_element_content_whitespace(
- struct dom_text *text, bool *result);
-dom_exception _dom_text_get_whole_text(struct dom_text *text,
+ dom_text *text, bool *result);
+dom_exception _dom_text_get_whole_text(dom_text *text,
dom_string **result);
-dom_exception _dom_text_replace_whole_text(struct dom_text *text,
- dom_string *content, struct dom_text **result);
+dom_exception _dom_text_replace_whole_text(dom_text *text,
+ dom_string *content, dom_text **result);
#define DOM_TEXT_VTABLE \
_dom_text_split_text, \
@@ -60,17 +60,16 @@ dom_exception _dom_text_replace_whole_text(struct dom_text *text,
/* Following comes the protected vtable */
void __dom_text_destroy(struct dom_node_internal *node);
-dom_exception _dom_text_alloc(struct dom_document *doc,
- struct dom_node_internal *n, struct dom_node_internal **ret);
-dom_exception _dom_text_copy(struct dom_node_internal *new,
- struct dom_node_internal *old);
+dom_exception _dom_text_copy(dom_node_internal *old, dom_node_internal **copy);
#define DOM_TEXT_PROTECT_VTABLE \
__dom_text_destroy, \
- _dom_text_alloc, \
_dom_text_copy
-
extern struct dom_text_vtable text_vtable;
+dom_exception _dom_text_copy_internal(dom_text *old, dom_text *new);
+#define dom_text_copy_internal(o, n) \
+ _dom_text_copy_internal((dom_text *) (o), (dom_text *) (n))
+
#endif