2015-08-09 14:26:41 +03:00
|
|
|
/* document binding for browser using duktape and libdom
|
|
|
|
*
|
|
|
|
* Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* This file is part of NetSurf, http://www.netsurf-browser.org/
|
|
|
|
*
|
|
|
|
* Released under the terms of the MIT License,
|
|
|
|
* http://www.opensource.org/licenses/mit-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Element {
|
|
|
|
prologue %{
|
2015-10-25 23:47:35 +03:00
|
|
|
#include <utils/corestrings.h>
|
2015-08-09 14:26:41 +03:00
|
|
|
%};
|
2015-09-28 10:56:23 +03:00
|
|
|
};
|
2015-08-09 14:26:41 +03:00
|
|
|
|
2015-09-28 10:56:23 +03:00
|
|
|
init Element(struct dom_element *element::node);
|
2015-08-09 14:26:41 +03:00
|
|
|
|
|
|
|
getter Element::firstElementChild()
|
|
|
|
%{
|
|
|
|
dom_node *element;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_node_type node_type;
|
|
|
|
dom_node *next_node;
|
|
|
|
|
|
|
|
exc = dom_node_get_first_child(((node_private_t*)priv)->node, &element);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element != NULL) {
|
|
|
|
exc = dom_node_get_node_type(element, &node_type);
|
|
|
|
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
|
|
|
|
/* found it */
|
2015-10-21 19:46:43 +03:00
|
|
|
break;
|
2015-08-09 14:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_node_get_next_sibling(element, &next_node);
|
|
|
|
dom_node_unref(element);
|
|
|
|
if (exc == DOM_NO_ERR) {
|
|
|
|
element = next_node;
|
|
|
|
} else {
|
|
|
|
element = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:46:43 +03:00
|
|
|
if (dukky_push_node(ctx, (dom_node *)element) == false) {
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 1;
|
2015-08-09 14:26:41 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
getter Element::lastElementChild()
|
|
|
|
%{
|
|
|
|
dom_node *element;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_node_type node_type;
|
|
|
|
dom_node *next_node;
|
|
|
|
|
|
|
|
exc = dom_node_get_last_child(((node_private_t*)priv)->node, &element);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element != NULL) {
|
|
|
|
exc = dom_node_get_node_type(element, &node_type);
|
|
|
|
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
|
|
|
|
/* found it */
|
2015-10-21 19:46:43 +03:00
|
|
|
break;
|
2015-08-09 14:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_node_get_previous_sibling(element, &next_node);
|
|
|
|
dom_node_unref(element);
|
|
|
|
if (exc == DOM_NO_ERR) {
|
|
|
|
element = next_node;
|
|
|
|
} else {
|
|
|
|
element = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:46:43 +03:00
|
|
|
if (dukky_push_node(ctx, (dom_node *)element) == false) {
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 1;
|
2015-08-09 14:26:41 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
getter Element::previousElementSibling()
|
|
|
|
%{
|
|
|
|
dom_node *element;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_node_type node_type;
|
|
|
|
dom_node *sib_node;
|
|
|
|
|
|
|
|
exc = dom_node_get_previous_sibling(((node_private_t *)priv)->node, &element);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element != NULL) {
|
|
|
|
exc = dom_node_get_node_type(element, &node_type);
|
|
|
|
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
|
|
|
|
/* found it */
|
2015-10-21 19:46:43 +03:00
|
|
|
break;
|
2015-08-09 14:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_node_get_previous_sibling(element, &sib_node);
|
|
|
|
dom_node_unref(element);
|
|
|
|
if (exc == DOM_NO_ERR) {
|
|
|
|
element = sib_node;
|
|
|
|
} else {
|
|
|
|
element = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:46:43 +03:00
|
|
|
if (dukky_push_node(ctx, (dom_node *)element) == false) {
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 1;
|
2015-08-09 14:26:41 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
getter Element::nextElementSibling()
|
|
|
|
%{
|
|
|
|
dom_node *element;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_node_type node_type;
|
|
|
|
dom_node *sib_node;
|
|
|
|
|
|
|
|
exc = dom_node_get_next_sibling(((node_private_t *)priv)->node, &element);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element != NULL) {
|
|
|
|
exc = dom_node_get_node_type(element, &node_type);
|
|
|
|
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
|
|
|
|
/* found it */
|
2015-10-21 19:46:43 +03:00
|
|
|
break;
|
2015-08-09 14:26:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_node_get_next_sibling(element, &sib_node);
|
|
|
|
dom_node_unref(element);
|
|
|
|
if (exc == DOM_NO_ERR) {
|
|
|
|
element = sib_node;
|
|
|
|
} else {
|
|
|
|
element = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-10-21 19:46:43 +03:00
|
|
|
if (dukky_push_node(ctx, (dom_node *)element) == false) {
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
dom_node_unref(element);
|
|
|
|
return 1;
|
2015-08-09 14:26:41 +03:00
|
|
|
%}
|
|
|
|
|
|
|
|
getter Element::childElementCount()
|
|
|
|
%{
|
|
|
|
dom_node *element;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_node_type node_type;
|
|
|
|
dom_node *next_node;
|
|
|
|
duk_uint_t jsret = 0;
|
|
|
|
|
|
|
|
exc = dom_node_get_first_child(((node_private_t *)priv)->node, &element);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (element != NULL) {
|
|
|
|
exc = dom_node_get_node_type(element, &node_type);
|
|
|
|
if ((exc == DOM_NO_ERR) && (node_type == DOM_ELEMENT_NODE)) {
|
|
|
|
jsret += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_node_get_next_sibling(element, &next_node);
|
|
|
|
dom_node_unref(element);
|
|
|
|
if (exc == DOM_NO_ERR) {
|
|
|
|
element = next_node;
|
|
|
|
} else {
|
|
|
|
element = NULL;
|
|
|
|
}
|
|
|
|
}
|
2017-09-04 01:19:11 +03:00
|
|
|
NSLOG(netsurf, INFO, "I found %u of them", jsret);
|
2015-08-09 14:26:41 +03:00
|
|
|
duk_push_uint(ctx, jsret);
|
|
|
|
return 1;
|
|
|
|
%}
|
2015-08-11 16:13:14 +03:00
|
|
|
|
|
|
|
method Element::getElementsByTagName ()
|
|
|
|
%{
|
|
|
|
dom_nodelist *nlist = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
dom_string *tagname;
|
|
|
|
duk_size_t len;
|
|
|
|
const char *str = duk_to_lstring(ctx, 0, &len);
|
|
|
|
|
2015-08-11 22:44:52 +03:00
|
|
|
exc = dom_string_create((const uint8_t *)str, len, &tagname);
|
2015-08-11 16:13:14 +03:00
|
|
|
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
|
|
|
|
exc = dom_element_get_elements_by_tag_name(priv->parent.node,
|
|
|
|
tagname, &nlist);
|
|
|
|
dom_string_unref(tagname);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
2019-05-05 01:06:14 +03:00
|
|
|
|
2019-05-19 17:26:49 +03:00
|
|
|
dukky_push_generics(ctx, "makeListProxy");
|
2019-05-05 01:06:14 +03:00
|
|
|
|
2015-08-11 16:13:14 +03:00
|
|
|
duk_push_pointer(ctx, nlist);
|
|
|
|
dukky_create_object(ctx, PROTO_NAME(NODELIST), 1);
|
|
|
|
dom_nodelist_unref(nlist);
|
|
|
|
|
2019-05-05 01:06:14 +03:00
|
|
|
if (dukky_pcall(ctx, 1, false) != 0) {
|
|
|
|
NSLOG(dukky, DEBUG, "Unable to construct nodelist?");
|
|
|
|
return 0; /* coerced to undefined */
|
|
|
|
}
|
|
|
|
|
2015-08-11 16:13:14 +03:00
|
|
|
return 1;
|
|
|
|
%}
|
2015-10-25 23:47:35 +03:00
|
|
|
|
|
|
|
getter Element::id ()
|
|
|
|
%{
|
|
|
|
dom_string *idstr = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
|
|
|
|
exc = dom_element_get_attribute(priv->parent.node,
|
|
|
|
corestring_dom_id,
|
|
|
|
&idstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
if (idstr == NULL) {
|
|
|
|
duk_push_lstring(ctx, "", 0);
|
|
|
|
} else {
|
|
|
|
duk_push_lstring(ctx, dom_string_data(idstr),
|
|
|
|
dom_string_length(idstr));
|
|
|
|
dom_string_unref(idstr);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
%}
|
|
|
|
|
|
|
|
setter Element::id ()
|
|
|
|
%{
|
|
|
|
dom_string *idstr = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
duk_size_t slen;
|
|
|
|
const char *s = duk_safe_to_lstring(ctx, 0, &slen);
|
|
|
|
|
|
|
|
exc = dom_string_create((const uint8_t *)s, slen, &idstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
|
|
|
|
exc = dom_element_set_attribute(priv->parent.node,
|
|
|
|
corestring_dom_id,
|
|
|
|
idstr);
|
|
|
|
dom_string_unref(idstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
return 0;
|
|
|
|
%}
|
|
|
|
|
2015-10-29 19:51:57 +03:00
|
|
|
|
|
|
|
method Element::removeAttribute()
|
|
|
|
%{
|
|
|
|
dom_string *attr = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
duk_size_t slen;
|
|
|
|
const char *s = duk_safe_to_lstring(ctx, 0, &slen);
|
|
|
|
|
|
|
|
exc = dom_string_create((const uint8_t *)s, slen, &attr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
|
|
|
|
exc = dom_element_remove_attribute(priv->parent.node, attr);
|
|
|
|
dom_string_unref(attr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
return 0;
|
|
|
|
%}
|
|
|
|
|
2015-11-01 02:23:38 +03:00
|
|
|
method Element::getAttribute()
|
|
|
|
%{
|
|
|
|
dom_string *attr_name = NULL;
|
|
|
|
dom_string *attr_value = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
duk_size_t slen;
|
|
|
|
|
|
|
|
const char *s = duk_safe_to_lstring(ctx, 0, &slen);
|
|
|
|
exc = dom_string_create((const uint8_t *)s, slen, &attr_name);
|
|
|
|
duk_pop(ctx);
|
|
|
|
|
|
|
|
exc = dom_element_get_attribute(priv->parent.node,
|
|
|
|
attr_name, &attr_value);
|
|
|
|
dom_string_unref(attr_name);
|
2015-11-01 12:17:04 +03:00
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-11-01 02:23:38 +03:00
|
|
|
if (attr_value == NULL) {
|
2015-11-01 12:17:04 +03:00
|
|
|
duk_push_null(ctx);
|
2015-11-01 02:23:38 +03:00
|
|
|
} else {
|
|
|
|
duk_push_lstring(ctx, dom_string_data(attr_value),
|
|
|
|
dom_string_length(attr_value));
|
|
|
|
dom_string_unref(attr_value);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
%}
|
|
|
|
|
2015-10-29 17:13:49 +03:00
|
|
|
method Element::setAttribute()
|
|
|
|
%{
|
|
|
|
dom_exception exc;
|
|
|
|
dom_string *attr_str, *value_str;
|
|
|
|
duk_size_t attr_len, value_len;
|
|
|
|
const char *attr = duk_safe_to_lstring(ctx, 0, &attr_len);
|
|
|
|
const char *value = duk_safe_to_lstring(ctx, 1, &value_len);
|
|
|
|
|
|
|
|
exc = dom_string_create((const uint8_t *)attr, attr_len, &attr_str);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
|
|
|
|
exc = dom_string_create((const uint8_t *)value, value_len, &value_str);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
dom_string_unref(attr_str);
|
2015-10-29 17:27:21 +03:00
|
|
|
return 0;
|
2015-10-29 17:13:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exc = dom_element_set_attribute(priv->parent.node,
|
|
|
|
attr_str, value_str);
|
|
|
|
dom_string_unref(attr_str);
|
|
|
|
dom_string_unref(value_str);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
return 0;
|
|
|
|
%}
|
|
|
|
|
2015-11-01 12:28:15 +03:00
|
|
|
method Element::hasAttribute()
|
|
|
|
%{
|
|
|
|
dom_string *attr_name = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
duk_size_t slen;
|
|
|
|
bool res;
|
|
|
|
|
|
|
|
const char *s = duk_safe_to_lstring(ctx, 0, &slen);
|
|
|
|
exc = dom_string_create((const uint8_t *)s, slen, &attr_name);
|
|
|
|
duk_pop(ctx);
|
|
|
|
|
|
|
|
exc = dom_element_has_attribute(priv->parent.node,
|
|
|
|
attr_name, &res);
|
|
|
|
dom_string_unref(attr_name);
|
|
|
|
if (exc != DOM_NO_ERR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
duk_push_boolean(ctx, res);
|
|
|
|
return 1;
|
|
|
|
%}
|
|
|
|
|
2015-10-25 23:47:35 +03:00
|
|
|
getter Element::className ()
|
|
|
|
%{
|
|
|
|
dom_string *classstr = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
|
|
|
|
exc = dom_element_get_attribute(priv->parent.node,
|
|
|
|
corestring_dom_class,
|
|
|
|
&classstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
if (classstr == NULL) {
|
|
|
|
duk_push_lstring(ctx, "", 0);
|
|
|
|
} else {
|
|
|
|
duk_push_lstring(ctx, dom_string_data(classstr),
|
|
|
|
dom_string_length(classstr));
|
|
|
|
dom_string_unref(classstr);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
%}
|
|
|
|
|
|
|
|
setter Element::className ()
|
|
|
|
%{
|
|
|
|
dom_string *classstr = NULL;
|
|
|
|
dom_exception exc;
|
|
|
|
duk_size_t slen;
|
|
|
|
const char *s = duk_safe_to_lstring(ctx, 0, &slen);
|
|
|
|
|
|
|
|
exc = dom_string_create((const uint8_t *)s, slen, &classstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
|
|
|
|
exc = dom_element_set_attribute(priv->parent.node,
|
|
|
|
corestring_dom_class,
|
|
|
|
classstr);
|
|
|
|
dom_string_unref(classstr);
|
|
|
|
if (exc != DOM_NO_ERR) return 0;
|
|
|
|
return 0;
|
|
|
|
%}
|
|
|
|
|