From a79e0988174c51b44e9e549d8026e38ad4839e1a Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sat, 10 Nov 2012 11:20:31 +0000 Subject: remove redundant domutils and put functionality in existing utils/libdom --- Makefile.sources | 2 +- desktop/tree_url_node.c | 14 ++-- javascript/jsapi/htmldocument.bnd | 6 +- javascript/jsapi/htmlelement.bnd | 14 +++- render/html.c | 3 +- utils/domutils.c | 160 -------------------------------------- utils/domutils.h | 37 --------- utils/libdom.c | 144 +++++++++++++++++++++++++++++++++- utils/libdom.h | 16 ++++ 9 files changed, 184 insertions(+), 212 deletions(-) delete mode 100644 utils/domutils.c delete mode 100644 utils/domutils.h diff --git a/Makefile.sources b/Makefile.sources index 50dec8e5a..32db5e666 100644 --- a/Makefile.sources +++ b/Makefile.sources @@ -19,7 +19,7 @@ S_RENDER := box.c box_construct.c box_normalise.c \ S_UTILS := base64.c corestrings.c filename.c filepath.c hashtable.c \ libdom.c locale.c log.c messages.c nsurl.c talloc.c url.c \ - utf8.c utils.c useragent.c domutils.c + utf8.c utils.c useragent.c S_HTTP := challenge.c generics.c primitives.c parameter.c \ content-disposition.c content-type.c www-authenticate.c diff --git a/desktop/tree_url_node.c b/desktop/tree_url_node.c index 3bff10272..8cc3e0546 100644 --- a/desktop/tree_url_node.c +++ b/desktop/tree_url_node.c @@ -35,7 +35,7 @@ #include "desktop/options.h" #include "desktop/tree_url_node.h" #include "utils/corestrings.h" -#include "utils/domutils.h" +#include "utils/libdom.h" #include "utils/log.h" #include "utils/messages.h" #include "utils/url.h" @@ -495,7 +495,7 @@ static void tree_url_load_entry(dom_node *li, tree_url_load_ctx *ctx) nserror error; /* The li must contain an "a" element */ - a = find_first_named_dom_element(li, corestring_lwc_a); + a = libdom_find_first_element(li, corestring_lwc_a); if (a == NULL) { warn_user("TreeLoadError", "(Missing in
  • )"); return; @@ -710,7 +710,7 @@ static void tree_url_load_directory(dom_node *ul, tree_url_load_ctx *ctx) assert(ctx != NULL); assert(ctx->directory != NULL); - domutils_iterate_child_elements(ul, tree_url_load_directory_cb, ctx); + libdom_iterate_child_elements(ul, tree_url_load_directory_cb, ctx); } /** @@ -732,13 +732,13 @@ bool tree_urlfile_load(const char *filename, struct tree *tree, return false; } - document = domutils_parse_file(filename, "iso-8859-1"); + document = libdom_parse_file(filename, "iso-8859-1"); if (document == NULL) { warn_user("TreeLoadError", messages_get("ParsingFail")); return false; } - html = find_first_named_dom_element((dom_node *) document, + html = libdom_find_first_element((dom_node *) document, corestring_lwc_html); if (html == NULL) { dom_node_unref(document); @@ -746,7 +746,7 @@ bool tree_urlfile_load(const char *filename, struct tree *tree, return false; } - body = find_first_named_dom_element(html, corestring_lwc_body); + body = libdom_find_first_element(html, corestring_lwc_body); if (body == NULL) { dom_node_unref(html); dom_node_unref(document); @@ -754,7 +754,7 @@ bool tree_urlfile_load(const char *filename, struct tree *tree, return false; } - ul = find_first_named_dom_element(body, corestring_lwc_ul); + ul = libdom_find_first_element(body, corestring_lwc_ul); if (ul == NULL) { dom_node_unref(body); dom_node_unref(html); diff --git a/javascript/jsapi/htmldocument.bnd b/javascript/jsapi/htmldocument.bnd index a45489245..80a30bb4e 100644 --- a/javascript/jsapi/htmldocument.bnd +++ b/javascript/jsapi/htmldocument.bnd @@ -13,7 +13,7 @@ preamble %{ #include "utils/config.h" #include "utils/log.h" #include "utils/corestrings.h" -#include "utils/domutils.h" +#include "utils/libdom.h" #include "content/urldb.h" @@ -78,7 +78,7 @@ getter head %{ } if (element != NULL) { - head = find_first_named_dom_element(element, corestring_lwc_head) ; + head = libdom_find_first_element(element, corestring_lwc_head) ; if (head != NULL) { jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)head, private->htmlc); } @@ -100,7 +100,7 @@ getter body %{ } if (element != NULL) { - body = find_first_named_dom_element(element, corestring_lwc_body) ; + body = libdom_find_first_element(element, corestring_lwc_body) ; if (body != NULL) { jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)body, private->htmlc); } diff --git a/javascript/jsapi/htmlelement.bnd b/javascript/jsapi/htmlelement.bnd index 83941c1b7..18e343f68 100644 --- a/javascript/jsapi/htmlelement.bnd +++ b/javascript/jsapi/htmlelement.bnd @@ -42,4 +42,16 @@ api finalise %{ if (private != NULL) { dom_node_unref(private->node); } -%} \ No newline at end of file +%} + +getter firstElementChild %{ + %} + +getter lastElementChild %{ + %} + +getter previousElementSibling %{ + %} + +getter nextElementSibling %{ + %} diff --git a/render/html.c b/render/html.c index 616ae7b2a..92f1ad6a1 100644 --- a/render/html.c +++ b/render/html.c @@ -53,7 +53,6 @@ #include "utils/url.h" #include "utils/utf8.h" #include "utils/utils.h" -#include "utils/domutils.h" #define CHUNK 4096 @@ -2032,7 +2031,7 @@ html_begin_conversion(html_content *htmlc) } dom_string_unref(node_name); - head = find_first_named_dom_element(html, corestring_lwc_head); + head = libdom_find_first_element(html, corestring_lwc_head); if (head != NULL) { if (html_head(htmlc, head) == false) { msg_data.error = messages_get("NoMemory"); diff --git a/utils/domutils.c b/utils/domutils.c deleted file mode 100644 index 741148913..000000000 --- a/utils/domutils.c +++ /dev/null @@ -1,160 +0,0 @@ -/* - * Copyright 2012 Vincent Sanders - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include -#include - -#include "utils/config.h" -#include "utils/log.h" - -#include "domutils.h" - -/* search children of a node for first named element */ -dom_node *find_first_named_dom_element(dom_node *parent, lwc_string *element_name) -{ - dom_node *element; - dom_exception exc; - dom_string *node_name = NULL; - dom_node_type node_type; - dom_node *next_node; - - exc = dom_node_get_first_child(parent, &element); - if ((exc != DOM_NO_ERR) || (element == NULL)) { - return NULL; - } - - /* find first node thats a element */ - do { - exc = dom_node_get_node_type(element, &node_type); - - if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) { - exc = dom_node_get_node_name(element, &node_name); - if ((exc == DOM_NO_ERR) && (node_name != NULL)) { - if (dom_string_caseless_lwc_isequal(node_name, - element_name)) { - dom_string_unref(node_name); - break; - } - dom_string_unref(node_name); - } - } - - exc = dom_node_get_next_sibling(element, &next_node); - dom_node_unref(element); - if (exc == DOM_NO_ERR) { - element = next_node; - } else { - element = NULL; - } - } while (element != NULL); - - return element; -} - -void domutils_iterate_child_elements(dom_node *parent, - domutils_iterate_cb cb, void *ctx) -{ - dom_nodelist *children; - uint32_t index, num_children; - dom_exception error; - - error = dom_node_get_child_nodes(parent, &children); - if (error != DOM_NO_ERR || children == NULL) - return; - - error = dom_nodelist_get_length(children, &num_children); - if (error != DOM_NO_ERR) { - dom_nodelist_unref(children); - return; - } - - for (index = 0; index < num_children; index++) { - dom_node *child; - dom_node_type type; - - error = dom_nodelist_item(children, index, &child); - if (error != DOM_NO_ERR) { - dom_nodelist_unref(children); - return; - } - - error = dom_node_get_node_type(child, &type); - if (error == DOM_NO_ERR && type == DOM_ELEMENT_NODE) { - if (cb(child, ctx) == false) { - dom_node_unref(child); - dom_nodelist_unref(children); - return; - } - } - - dom_node_unref(child); - } - - dom_nodelist_unref(children); -} - -static void ignore_dom_msg(uint32_t severity, void *ctx, const char *msg, ...) -{ -} - -dom_document *domutils_parse_file(const char *filename, const char *encoding) -{ - dom_hubbub_error error; - dom_hubbub_parser *parser; - dom_document *document; - FILE *fp = NULL; -#define BUF_SIZE 512 - uint8_t buf[BUF_SIZE]; - - fp = fopen(filename, "r"); - if (fp == NULL) { - return NULL; - } - - parser = dom_hubbub_parser_create(encoding, false, false, - ignore_dom_msg, NULL, NULL, &document); - if (parser == NULL) { - fclose(fp); - return NULL; - } - - while (feof(fp) == 0) { - size_t read = fread(buf, sizeof(buf[0]), BUF_SIZE, fp); - - error = dom_hubbub_parser_parse_chunk(parser, buf, read); - if (error != DOM_HUBBUB_OK) { - dom_node_unref(document); - dom_hubbub_parser_destroy(parser); - fclose(fp); - return NULL; - } - } - - error = dom_hubbub_parser_completed(parser); - if (error != DOM_HUBBUB_OK) { - dom_node_unref(document); - dom_hubbub_parser_destroy(parser); - fclose(fp); - return NULL; - } - - dom_hubbub_parser_destroy(parser); - - return document; -} - diff --git a/utils/domutils.h b/utils/domutils.h deleted file mode 100644 index 6dd62fc8a..000000000 --- a/utils/domutils.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2012 Vincent Sanders - * - * This file is part of NetSurf, http://www.netsurf-browser.org/ - * - * NetSurf is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. - * - * NetSurf is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef _NETSURF_UTILS_DOMUTILS_H_ -#define _NETSURF_UTILS_DOMUTILS_H_ - -#include - -#include - -typedef bool (*domutils_iterate_cb)(dom_node *node, void *ctx); - -dom_node *find_first_named_dom_element(dom_node *parent, - lwc_string *element_name); - -void domutils_iterate_child_elements(dom_node *parent, - domutils_iterate_cb cb, void *ctx); - -dom_document *domutils_parse_file(const char *filename, - const char *encoding); - -#endif diff --git a/utils/libdom.c b/utils/libdom.c index 95f7254f9..756465a02 100644 --- a/utils/libdom.c +++ b/utils/libdom.c @@ -21,9 +21,13 @@ */ #include +#include +#include -#include "utils/libdom.h" +#include "utils/config.h" +#include "utils/log.h" +#include "utils/libdom.h" /* exported interface documented in libdom.h */ bool libdom_treewalk(dom_node *root, @@ -129,6 +133,7 @@ struct find_element_ctx { lwc_string *search; dom_node *found; }; + /* libdom_treewalk callback for libdom_find_element */ static bool libdom_find_element_callback(dom_node *node, dom_string *name, void *ctx) @@ -162,3 +167,140 @@ dom_node *libdom_find_element(dom_node *node, lwc_string *element_name) return data.found; } + + +/* exported interface documented in libdom.h */ +dom_node *libdom_find_first_element(dom_node *parent, lwc_string *element_name) +{ + dom_node *element; + dom_exception exc; + dom_string *node_name = NULL; + dom_node_type node_type; + dom_node *next_node; + + exc = dom_node_get_first_child(parent, &element); + if ((exc != DOM_NO_ERR) || (element == NULL)) { + return NULL; + } + + /* find first node thats a element */ + do { + exc = dom_node_get_node_type(element, &node_type); + + if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) { + exc = dom_node_get_node_name(element, &node_name); + if ((exc == DOM_NO_ERR) && (node_name != NULL)) { + if (dom_string_caseless_lwc_isequal(node_name, + element_name)) { + dom_string_unref(node_name); + break; + } + dom_string_unref(node_name); + } + } + + exc = dom_node_get_next_sibling(element, &next_node); + dom_node_unref(element); + if (exc == DOM_NO_ERR) { + element = next_node; + } else { + element = NULL; + } + } while (element != NULL); + + return element; +} + +/* exported interface documented in libdom.h */ +void libdom_iterate_child_elements(dom_node *parent, + libdom_iterate_cb cb, void *ctx) +{ + dom_nodelist *children; + uint32_t index, num_children; + dom_exception error; + + error = dom_node_get_child_nodes(parent, &children); + if (error != DOM_NO_ERR || children == NULL) + return; + + error = dom_nodelist_get_length(children, &num_children); + if (error != DOM_NO_ERR) { + dom_nodelist_unref(children); + return; + } + + for (index = 0; index < num_children; index++) { + dom_node *child; + dom_node_type type; + + error = dom_nodelist_item(children, index, &child); + if (error != DOM_NO_ERR) { + dom_nodelist_unref(children); + return; + } + + error = dom_node_get_node_type(child, &type); + if (error == DOM_NO_ERR && type == DOM_ELEMENT_NODE) { + if (cb(child, ctx) == false) { + dom_node_unref(child); + dom_nodelist_unref(children); + return; + } + } + + dom_node_unref(child); + } + + dom_nodelist_unref(children); +} + +static void ignore_dom_msg(uint32_t severity, void *ctx, const char *msg, ...) +{ +} + +/* exported interface documented in libdom.h */ +dom_document *libdom_parse_file(const char *filename, const char *encoding) +{ + dom_hubbub_error error; + dom_hubbub_parser *parser; + dom_document *document; + FILE *fp = NULL; +#define BUF_SIZE 512 + uint8_t buf[BUF_SIZE]; + + fp = fopen(filename, "r"); + if (fp == NULL) { + return NULL; + } + + parser = dom_hubbub_parser_create(encoding, false, false, + ignore_dom_msg, NULL, NULL, &document); + if (parser == NULL) { + fclose(fp); + return NULL; + } + + while (feof(fp) == 0) { + size_t read = fread(buf, sizeof(buf[0]), BUF_SIZE, fp); + + error = dom_hubbub_parser_parse_chunk(parser, buf, read); + if (error != DOM_HUBBUB_OK) { + dom_node_unref(document); + dom_hubbub_parser_destroy(parser); + fclose(fp); + return NULL; + } + } + + error = dom_hubbub_parser_completed(parser); + if (error != DOM_HUBBUB_OK) { + dom_node_unref(document); + dom_hubbub_parser_destroy(parser); + fclose(fp); + return NULL; + } + + dom_hubbub_parser_destroy(parser); + + return document; +} diff --git a/utils/libdom.h b/utils/libdom.h index e5a7c20c1..f2da40570 100644 --- a/utils/libdom.h +++ b/utils/libdom.h @@ -48,4 +48,20 @@ bool libdom_treewalk(dom_node *root, */ dom_node *libdom_find_element(dom_node *node, lwc_string *element_name); +/** + * Search children of a node for first named element + * \param parent dom_node to search children of, or NULL + * \param element_name name of element to find + * \return first child of node which is an element and matches name, or + * NULL if not found or parameter node is NULL + */ +dom_node *libdom_find_first_element(dom_node *parent, lwc_string *element_name); + +typedef bool (*libdom_iterate_cb)(dom_node *node, void *ctx); + +void libdom_iterate_child_elements(dom_node *parent, + libdom_iterate_cb cb, void *ctx); + +dom_document *libdom_parse_file(const char *filename, const char *encoding); + #endif -- cgit v1.2.3