summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2015-10-25 21:47:35 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2015-10-25 21:47:35 +0100
commit36bf691ceea67bb0938e6fbdd2802423c53cc6a9 (patch)
tree80154e1bb18296ba2d1229c86a599b4064acb292
parent0423704eb1a5930d63a5c75add26ccb5e9eb1ff2 (diff)
downloadnetsurf-36bf691ceea67bb0938e6fbdd2802423c53cc6a9.tar.gz
netsurf-36bf691ceea67bb0938e6fbdd2802423c53cc6a9.tar.bz2
Add .id and .className support to Element along with a test
-rw-r--r--javascript/duktape/Element.bnd76
-rw-r--r--test/js/dom-element-reflection.html21
-rw-r--r--test/js/index.html1
3 files changed, 98 insertions, 0 deletions
diff --git a/javascript/duktape/Element.bnd b/javascript/duktape/Element.bnd
index 07a059fbe..9cd8a4c9f 100644
--- a/javascript/duktape/Element.bnd
+++ b/javascript/duktape/Element.bnd
@@ -10,6 +10,7 @@
class Element {
prologue %{
+#include <utils/corestrings.h>
%};
};
@@ -209,3 +210,78 @@ method Element::getElementsByTagName ()
return 1;
%}
+
+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;
+%}
+
+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;
+%}
+
diff --git a/test/js/dom-element-reflection.html b/test/js/dom-element-reflection.html
new file mode 100644
index 000000000..2ec9d2666
--- /dev/null
+++ b/test/js/dom-element-reflection.html
@@ -0,0 +1,21 @@
+<html>
+<head>
+<title>DOM element reflection reference</title>
+<link rel="stylesheet" type="text/css" href="tst.css">
+</head>
+<body>
+<h1 id="header" class="normal">DOM firstElementChild reference (#header .normal)</h1>
+<p><b>body.firstElementChild.id:</b> <script>document.write(document.body.firstElementChild.id);</script></p>
+<p><b>body.firstElementChild.className:</b> <script>document.write(document.body.firstElementChild.className);</script></p>
+
+<script>
+ document.body.firstElementChild.id = "replacedid"
+ document.body.firstElementChild.className = "replacedclass"
+</script>
+
+<h1>DOM firstElementChild reference (#replacedid .replacedclass)</h1>
+<p><b>body.firstElementChild.id:</b> <script>document.write(document.body.firstElementChild.id);</script></p>
+<p><b>body.firstElementChild.className:</b> <script>document.write(document.body.firstElementChild.className);</script></p>
+
+</body>
+</html>
diff --git a/test/js/index.html b/test/js/index.html
index c12f32360..a1bc4c4de 100644
--- a/test/js/index.html
+++ b/test/js/index.html
@@ -54,6 +54,7 @@
<li><a href="dom-element-childElementCount.html">childElementCount</a></li>
<li><a href="doc-dom2.html">getElementById</a></li>
<li><a href="dom-getElementsByTagName.html">getElementsByTagName</a></li>
+<li><a href="dom-element-reflection.html">reflection</a></li>
</ul>
<h3>Enumeration tests</h3>