add document.body, head and documentElement getters

add a dom utility file and use it
This commit is contained in:
Vincent Sanders 2012-11-07 18:47:35 +00:00
parent ecd8364fe1
commit 43fb761f45
5 changed files with 152 additions and 43 deletions

View File

@ -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

View File

@ -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;

View File

@ -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");

66
utils/domutils.c Normal file
View File

@ -0,0 +1,66 @@
/*
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <dom/dom.h>
#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;
}

24
utils/domutils.h Normal file
View File

@ -0,0 +1,24 @@
/*
* Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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