diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-02-21 18:40:35 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2020-02-21 18:40:35 +0000 |
commit | d0e775e901cc7ba88c7173c5c797ad1844745a69 (patch) | |
tree | 2f6a0246f6c2b33fe0e88ae47bb9636bb2ddb38e /content | |
parent | b23063bb5200bb1fc6a64e9e811ab1191d114fd9 (diff) | |
download | netsurf-d0e775e901cc7ba88c7173c5c797ad1844745a69.tar.gz netsurf-d0e775e901cc7ba88c7173c5c797ad1844745a69.tar.bz2 |
JS: Add Element::attributes and NamedNodeMap to handle it
This is a *very* rudimentary implementation lacking most of the
functionality of NamedNodeMap but it's enough to get jQuery 1.12.4 up.
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'content')
-rw-r--r-- | content/handlers/javascript/duktape/Element.bnd | 28 | ||||
-rw-r--r-- | content/handlers/javascript/duktape/NamedNodeMap.bnd | 74 | ||||
-rw-r--r-- | content/handlers/javascript/duktape/netsurf.bnd | 1 |
3 files changed, 103 insertions, 0 deletions
diff --git a/content/handlers/javascript/duktape/Element.bnd b/content/handlers/javascript/duktape/Element.bnd index 35157d5cd..a965a6ceb 100644 --- a/content/handlers/javascript/duktape/Element.bnd +++ b/content/handlers/javascript/duktape/Element.bnd @@ -514,3 +514,31 @@ out: } return 0; %} + +getter Element::attributes() +%{ + dom_exception exc; + dom_namednodemap *nmap = NULL; + duk_set_top(ctx, 0); + duk_push_this(ctx); + duk_get_prop_string(ctx, 0, MAGIC(attributes)); + if (duk_is_undefined(ctx, -1)) { + duk_pop(ctx); + exc = dom_node_get_attributes(priv->parent.node, &nmap); + if (exc != DOM_NO_ERR) return 0; + dukky_push_generics(ctx, "makeNodeMapProxy"); + duk_push_pointer(ctx, nmap); + if (dukky_create_object(ctx, PROTO_NAME(NAMEDNODEMAP), 1) != DUK_EXEC_SUCCESS) { + dom_namednodemap_unref(nmap); + return 0; + } + dom_namednodemap_unref(nmap); + if (dukky_pcall(ctx, 1, false) != 0) { + NSLOG(dukky, DEBUG, "Unable to construct nodelist?"); + return 0; /* coerced to undefined */ + } + duk_dup(ctx, -1); + duk_put_prop_string(ctx, 0, MAGIC(attributes)); + } + return 1; +%} diff --git a/content/handlers/javascript/duktape/NamedNodeMap.bnd b/content/handlers/javascript/duktape/NamedNodeMap.bnd new file mode 100644 index 000000000..c81993c92 --- /dev/null +++ b/content/handlers/javascript/duktape/NamedNodeMap.bnd @@ -0,0 +1,74 @@ +/* NamedNodeMap binding for browser using duktape and libdom + * + * Copyright 2020 Daniel Silverstone <dsilvers@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 NamedNodeMap { + private struct dom_namednodemap *map; +}; + +init NamedNodeMap(struct dom_namednodemap *map) +%{ + priv->map = map; + dom_namednodemap_ref(map); +%} + +fini NamedNodeMap() +%{ + dom_namednodemap_unref(priv->map); +%} + +getter NamedNodeMap::length() +%{ + dom_exception err; + uint32_t len; + + err = dom_namednodemap_get_length(priv->map, &len); + + if (err != DOM_NO_ERR) return 0; /* coerced to undefined */ + + duk_push_uint(ctx, (duk_uint_t)len); + + return 1; +%} + +method NamedNodeMap::item() +%{ + unsigned long i = duk_to_uint(ctx, 0); + dom_exception err; + dom_node *node; + + err = dom_namednodemap_item(priv->map, i, &node); + + if (err != DOM_NO_ERR) return 0; /* coerced to undefined */ + + dukky_push_node(ctx, node); + dom_node_unref(node); + + return 1; +%} + +method NamedNodeMap::getNamedItem() +%{ + duk_size_t size; + const char *s = duk_safe_to_lstring(ctx, 0, &size); + dom_exception exc; + dom_node *attr = NULL; + dom_string *name = NULL; + + exc = dom_string_create((const uint8_t*)s, size, &name); + if (exc != DOM_NO_ERR) return 0; /* coerced to undefined */ + + exc = dom_namednodemap_get_named_item(priv->map, name, &attr); + dom_string_unref(name); + if (exc != DOM_NO_ERR) return 0; /* coerced to undefined */ + + dukky_push_node(ctx, attr); + dom_node_unref(attr); + return 1; +%} diff --git a/content/handlers/javascript/duktape/netsurf.bnd b/content/handlers/javascript/duktape/netsurf.bnd index 30b3d4a14..6dbdb70a9 100644 --- a/content/handlers/javascript/duktape/netsurf.bnd +++ b/content/handlers/javascript/duktape/netsurf.bnd @@ -60,6 +60,7 @@ struct dom_html_br_element; #include "Document.bnd" #include "Node.bnd" #include "NodeList.bnd" +#include "NamedNodeMap.bnd" #include "Element.bnd" #include "HTMLCollection.bnd" #include "Location.bnd" |