diff options
Diffstat (limited to 'src/utils/namespace.c')
-rw-r--r-- | src/utils/namespace.c | 71 |
1 files changed, 63 insertions, 8 deletions
diff --git a/src/utils/namespace.c b/src/utils/namespace.c index ca5b01d..8d109ae 100644 --- a/src/utils/namespace.c +++ b/src/utils/namespace.c @@ -1,8 +1,9 @@ /* * This file is part of libdom. * Licensed under the MIT License, - * http://www.opensource.org/licenses/mit-license.php + * http://www.opensource.org/licenses/mit-license.php * Copyright 2007 John-Mark Bell <jmb@netsurf-browser.org> + * Copyright 2009 Bo Yang <struggleyb.nku@gmail.com> */ #include <string.h> @@ -10,6 +11,7 @@ #include <dom/dom.h> #include "utils/namespace.h" +#include "utils/validate.h" #include "utils/utils.h" @@ -18,7 +20,7 @@ static struct dom_string *xml; /** XMLNS prefix */ static struct dom_string *xmlns; -/** The namespace strings */ +/* The namespace strings */ static const char *namespaces[DOM_NAMESPACE_COUNT] = { NULL, "http://www.w3.org/1999/xhtml", @@ -37,7 +39,7 @@ struct dom_string *dom_namespaces[DOM_NAMESPACE_COUNT] = { * Initialise the namespace component * * \param alloc Pointer to memory (de)allocation function - * \param pw Pointer to client-specific private data + * \param pw Pointer to client-specific private data * \return DOM_NO_ERR on success. */ dom_exception _dom_namespace_initialise(dom_alloc alloc, void *pw) @@ -130,10 +132,19 @@ dom_exception _dom_namespace_finalise(void) dom_exception _dom_namespace_validate_qname(struct dom_string *qname, struct dom_string *namespace) { - uint32_t colon; + uint32_t colon, len; + + if (qname == NULL){ + if (namespace != NULL) + return DOM_NAMESPACE_ERR; + if (namespace == NULL) + return DOM_NO_ERR; + } + + if (_dom_validate_name(qname) == false) + return DOM_NAMESPACE_ERR; - /** \todo search qname for invalid characters */ - /** \todo ensure qname is not malformed */ + len = dom_string_length(qname); /* Find colon */ colon = dom_string_index(qname, ':'); @@ -147,9 +158,14 @@ dom_exception _dom_namespace_validate_qname(struct dom_string *qname, dom_string_cmp(qname, xmlns) != 0) { return DOM_NAMESPACE_ERR; } + } else if (colon == 0) { + /* Some name like ":name" */ + if (namespace != NULL) + return DOM_NAMESPACE_ERR; } else { /* Prefix */ struct dom_string *prefix; + struct dom_string *lname; dom_exception err; /* Ensure there is a namespace URI */ @@ -157,11 +173,21 @@ dom_exception _dom_namespace_validate_qname(struct dom_string *qname, return DOM_NAMESPACE_ERR; } - err = dom_string_substr(qname, 0, colon - 1, &prefix); + err = dom_string_substr(qname, 0, colon, &prefix); if (err != DOM_NO_ERR) { return err; } + err = dom_string_substr(qname, colon + 1, len, &lname); + if (err != DOM_NO_ERR) { + return err; + } + + if (_dom_validate_ncname(prefix) == false || + _dom_validate_ncname(lname) == false) { + return DOM_NAMESPACE_ERR; + } + /* Test for invalid XML namespace */ if (dom_string_cmp(prefix, xml) == 0 && dom_string_cmp(namespace, @@ -223,7 +249,7 @@ dom_exception _dom_namespace_split_qname(struct dom_string *qname, } } else { /* Found one => prefix */ - err = dom_string_substr(qname, 0, colon - 1, prefix); + err = dom_string_substr(qname, 0, colon, prefix); if (err != DOM_NO_ERR) { return err; } @@ -240,3 +266,32 @@ dom_exception _dom_namespace_split_qname(struct dom_string *qname, return DOM_NO_ERR; } +/** + * Get the XML prefix dom_string + * + * \return the xml prefix dom_string. + * + * Note: The client of this function may or may not call the dom_string_ref + * on the returned dom_string, because this string will only be destroyed when + * the dom_finalise is called. But if the client call dom_string_ref, it must + * call dom_string_unref to maintain a correct ref count of the dom_string. + */ +dom_string *_dom_namespace_get_xml_prefix(void) +{ + return xml; +} + +/** + * Get the XMLNS prefix dom_string. + * + * \return the xmlns prefix dom_string + * + * Note: The client of this function may or may not call the dom_string_ref + * on the returned dom_string, because this string will only be destroyed when + * the dom_finalise is called. But if the client call dom_string_ref, it must + * call dom_string_unref to maintain a correct ref count of the dom_string. + */ +dom_string *_dom_namespace_get_xmlns_prefix(void) +{ + return xmlns; +} |