From 43fb761f450e463dbce7431d20606cf795daf04c Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Wed, 7 Nov 2012 18:47:35 +0000 Subject: add document.body, head and documentElement getters add a dom utility file and use it --- Makefile.sources | 2 +- javascript/jsapi/htmldocument.bnd | 61 ++++++++++++++++++++++++++++++++++-- render/html.c | 42 ++----------------------- utils/domutils.c | 66 +++++++++++++++++++++++++++++++++++++++ utils/domutils.h | 24 ++++++++++++++ 5 files changed, 152 insertions(+), 43 deletions(-) create mode 100644 utils/domutils.c create mode 100644 utils/domutils.h diff --git a/Makefile.sources b/Makefile.sources index 32db5e666..50dec8e5a 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 + utf8.c utils.c useragent.c domutils.c S_HTTP := challenge.c generics.c primitives.c parameter.c \ content-disposition.c content-type.c www-authenticate.c diff --git a/javascript/jsapi/htmldocument.bnd b/javascript/jsapi/htmldocument.bnd index 7574512dd..1432ce5da 100644 --- a/javascript/jsapi/htmldocument.bnd +++ b/javascript/jsapi/htmldocument.bnd @@ -12,6 +12,8 @@ preamble %{ #include "utils/config.h" #include "utils/log.h" +#include "utils/corestrings.h" +#include "utils/domutils.h" #include "content/urldb.h" @@ -23,13 +25,13 @@ preamble %{ binding document { type js_libdom; /* the binding type */ + interface Document; /* Web IDL interface to generate */ + /* parameters to constructor value stored in private * context structure. */ private "dom_document *" node; private "struct html_content *" htmlc; - - interface Document; /* Web IDL interface to generate */ } api finalise %{ @@ -49,6 +51,61 @@ getter cookie %{ } %} +getter documentElement %{ + dom_exception exc; + dom_element *element; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, (void *)&element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, element, private->htmlc); + } +%} + +getter head %{ + dom_node *element; + dom_node *head; + dom_exception exc; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, &element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + head = find_first_named_dom_element(element, corestring_lwc_head) ; + if (head != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)head, private->htmlc); + } + dom_node_unref(element); + } +%} + +getter body %{ + dom_node *element; + dom_node *body; + dom_exception exc; + + /* document (html) element */ + exc = dom_document_get_document_element(private->node, &element); + if (exc != DOM_NO_ERR) { + return JS_FALSE; + } + + if (element != NULL) { + body = find_first_named_dom_element(element, corestring_lwc_body) ; + if (body != NULL) { + jsret = jsapi_new_HTMLElement(cx, NULL, NULL, (dom_element *)body, private->htmlc); + } + dom_node_unref(element); + } +%} + operation getElementById %{ dom_string *elementId_dom; dom_element *element; diff --git a/render/html.c b/render/html.c index 58e7254ac..616ae7b2a 100644 --- a/render/html.c +++ b/render/html.c @@ -53,6 +53,7 @@ #include "utils/url.h" #include "utils/utf8.h" #include "utils/utils.h" +#include "utils/domutils.h" #define CHUNK 4096 @@ -2031,46 +2032,7 @@ html_begin_conversion(html_content *htmlc) } dom_string_unref(node_name); - /* ensure the head element is found */ - exc = dom_node_get_first_child(html, &head); - if ((exc != DOM_NO_ERR) || (head == NULL)) { - head = NULL; - LOG(("head element not found")); - } else { - dom_node_type node_type; - dom_node *next_node; - - /* find first node thats a element */ - do { - exc = dom_node_get_node_type(head, &node_type); - - if ((exc != DOM_NO_ERR) || - (node_type == DOM_ELEMENT_NODE)) - break; - - exc = dom_node_get_next_sibling(head, &next_node); - dom_node_unref(head); - if (exc == DOM_NO_ERR) { - head = next_node; - } else { - head = NULL; - } - } while (head != NULL); - - if (head != NULL) { - exc = dom_node_get_node_name(head, &node_name); - if ((exc == DOM_NO_ERR) || (node_name != NULL)) { - if (!dom_string_caseless_lwc_isequal(node_name, - corestring_lwc_head)) { - dom_node_unref(head); - LOG(("head element not found")); - head = NULL; - } - dom_string_unref(node_name); - } - } - } - + head = find_first_named_dom_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 new file mode 100644 index 000000000..4d32a67d8 --- /dev/null +++ b/utils/domutils.c @@ -0,0 +1,66 @@ +/* + * 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 "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; +} diff --git a/utils/domutils.h b/utils/domutils.h new file mode 100644 index 000000000..ecdf2bc4a --- /dev/null +++ b/utils/domutils.h @@ -0,0 +1,24 @@ +/* + * 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_ + +dom_node *find_first_named_dom_element(dom_node *parent, lwc_string *element_name); + +#endif -- cgit v1.2.3