diff --git a/src/ecmascript/mujs/Makefile b/src/ecmascript/mujs/Makefile index 0b730c8cf..17a6c4507 100644 --- a/src/ecmascript/mujs/Makefile +++ b/src/ecmascript/mujs/Makefile @@ -2,6 +2,6 @@ top_builddir=../../.. include $(top_builddir)/Makefile.config OBJS = attr.o attributes.o collection.o console.o css.o customevent.o document.o domrect.o element.o event.o form.o forms.o history.o implementation.o input.o \ - keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o nodelist2.o screen.o style.o unibar.o url.o window.o xhr.o + keyboard.o localstorage.o location.o mapa.o message.o navigator.o nodelist.o nodelist2.o screen.o style.o tokenlist.o unibar.o url.o window.o xhr.o include $(top_srcdir)/Makefile.lib diff --git a/src/ecmascript/mujs/element.c b/src/ecmascript/mujs/element.c index 764c84072..0aae281f2 100644 --- a/src/ecmascript/mujs/element.c +++ b/src/ecmascript/mujs/element.c @@ -43,6 +43,7 @@ #include "ecmascript/mujs/nodelist.h" #include "ecmascript/mujs/nodelist2.h" #include "ecmascript/mujs/style.h" +#include "ecmascript/mujs/tokenlist.h" #include "ecmascript/mujs/window.h" #include "intl/libintl.h" #include "main/select.h" @@ -251,6 +252,30 @@ mjs_element_get_property_childNodes(js_State *J) mjs_push_nodelist(J, nodes); } +static void +mjs_element_get_property_classList(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + dom_element *el = (dom_element *)(mjs_getprivate(J, 0)); + + if (!el) { + js_pushnull(J); + return; + } + dom_node_ref(el); + dom_tokenlist *tl = NULL; + dom_exception exc = dom_tokenlist_create(el, corestring_dom_class, &tl); + dom_node_unref(el); + + if (exc != DOM_NO_ERR || !tl) { + js_pushnull(J); + return; + } + mjs_push_tokenlist(J, tl); +} + static void mjs_element_get_property_className(js_State *J) { @@ -3221,6 +3246,7 @@ mjs_push_element(js_State *J, void *node) addproperty(J, "children", mjs_element_get_property_children, NULL); addproperty(J, "childElementCount", mjs_element_get_property_childElementCount, NULL); addproperty(J, "childNodes", mjs_element_get_property_childNodes, NULL); + addproperty(J, "classList", mjs_element_get_property_classList, NULL); addproperty(J, "className", mjs_element_get_property_className, mjs_element_set_property_className); // addproperty(J, "clientHeight", mjs_element_get_property_clientHeight, NULL); // addproperty(J, "clientLeft", mjs_element_get_property_clientLeft, NULL); diff --git a/src/ecmascript/mujs/meson.build b/src/ecmascript/mujs/meson.build index d8a6c1514..4ee661dc0 100644 --- a/src/ecmascript/mujs/meson.build +++ b/src/ecmascript/mujs/meson.build @@ -1,2 +1,2 @@ srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'customevent.c', 'document.c', 'domrect.c', 'element.c', 'event.c', 'form.c', 'forms.c', 'history.c', 'implementation.c', 'input.c', 'keyboard.c', -'localstorage.c', 'location.c', 'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'nodelist2.c', 'screen.c', 'style.c', 'unibar.c', 'url.c', 'window.c', 'xhr.c') +'localstorage.c', 'location.c', 'mapa.c', 'message.c', 'navigator.c', 'nodelist.c', 'nodelist2.c', 'screen.c', 'style.c', 'tokenlist.c', 'unibar.c', 'url.c', 'window.c', 'xhr.c') diff --git a/src/ecmascript/mujs/tokenlist.c b/src/ecmascript/mujs/tokenlist.c new file mode 100644 index 000000000..705510a2e --- /dev/null +++ b/src/ecmascript/mujs/tokenlist.c @@ -0,0 +1,209 @@ +/* The MuJS DOM tokenlist object implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#ifdef CONFIG_LIBDOM +#include +#include +#endif + +#include "elinks.h" + +#include "document/libdom/corestrings.h" +#include "ecmascript/ecmascript.h" +#include "ecmascript/mujs/mapa.h" +#include "ecmascript/mujs.h" +#include "ecmascript/mujs/element.h" +#include "ecmascript/mujs/tokenlist.h" + +static void +mjs_tokenlist_add(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + dom_tokenlist *tl = (dom_tokenlist *)(js_touserdata(J, 0, "tokenlist")); + + if (!tl) { + js_pushundefined(J); + return; + } + const char *klass = js_tostring(J, 1); + + if (!klass) { + js_pushundefined(J); + return; + } + dom_string *kl = NULL; + dom_exception exc = dom_string_create(klass, strlen(klass), &kl); + + if (exc != DOM_NO_ERR || !kl) { + js_pushundefined(J); + return; + } + exc = dom_tokenlist_add(tl, kl); + dom_string_unref(kl); + + if (exc == DOM_NO_ERR) { + interpreter->changed = true; + } + js_pushundefined(J); +} + +static void +mjs_tokenlist_contains(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + dom_tokenlist *tl = (dom_tokenlist *)(js_touserdata(J, 0, "tokenlist")); + + if (!tl) { + js_pushundefined(J); + return; + } + const char *klass = js_tostring(J, 1); + + if (!klass) { + js_pushundefined(J); + return; + } + dom_string *kl = NULL; + dom_exception exc = dom_string_create(klass, strlen(klass), &kl); + + if (exc != DOM_NO_ERR || !kl) { + js_pushundefined(J); + return; + } + bool res = false; + exc = dom_tokenlist_contains(tl, kl, &res); + dom_string_unref(kl); + + if (exc == DOM_NO_ERR) { + js_pushboolean(J, res); + return; + } + js_pushundefined(J); +} + +static void +mjs_tokenlist_remove(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + dom_tokenlist *tl = (dom_tokenlist *)(js_touserdata(J, 0, "tokenlist")); + + if (!tl) { + js_pushundefined(J); + return; + } + const char *klass = js_tostring(J, 1); + + if (!klass) { + js_pushundefined(J); + return; + } + dom_string *kl = NULL; + dom_exception exc = dom_string_create(klass, strlen(klass), &kl); + + if (exc != DOM_NO_ERR || !kl) { + js_pushundefined(J); + return; + } + exc = dom_tokenlist_remove(tl, kl); + dom_string_unref(kl); + + if (exc == DOM_NO_ERR) { + interpreter->changed = true; + } + js_pushundefined(J); +} + +static void +mjs_tokenlist_toggle(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J); + dom_tokenlist *tl = (dom_tokenlist *)(js_touserdata(J, 0, "tokenlist")); + + if (!tl) { + js_pushundefined(J); + return; + } + const char *klass = js_tostring(J, 1); + + if (!klass) { + js_pushundefined(J); + return; + } + dom_string *kl = NULL; + dom_exception exc = dom_string_create(klass, strlen(klass), &kl); + + if (exc != DOM_NO_ERR || !kl) { + js_pushundefined(J); + return; + } + bool res = false; + exc = dom_tokenlist_contains(tl, kl, &res); + + if (exc == DOM_NO_ERR) { + if (res) { + exc = dom_tokenlist_remove(tl, kl); + } else { + exc = dom_tokenlist_add(tl, kl); + } + if (exc == DOM_NO_ERR) { + interpreter->changed = true; + } + } + dom_string_unref(kl); + js_pushundefined(J); +} + +static void +mjs_tokenlist_toString(js_State *J) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_pushstring(J, "[tokenlist object]"); +} + +static void +mjs_tokenlist_finalizer(js_State *J, void *node) +{ + dom_tokenlist *tl = (dom_tokenlist *)(node); + + if (tl) { + dom_tokenlist_unref(tl); + } +} + +void +mjs_push_tokenlist(js_State *J, void *node) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + js_newobject(J); + { + js_newuserdata(J, "tokenlist", node, mjs_tokenlist_finalizer); + addmethod(J, "add", mjs_tokenlist_add, 1); + addmethod(J, "contains", mjs_tokenlist_contains, 1); + addmethod(J, "remove", mjs_tokenlist_remove, 1); + addmethod(J, "toggle", mjs_tokenlist_toggle, 1); + addmethod(J, "toString", mjs_tokenlist_toString, 0); + } +} diff --git a/src/ecmascript/mujs/tokenlist.h b/src/ecmascript/mujs/tokenlist.h new file mode 100644 index 000000000..67e2fc2e8 --- /dev/null +++ b/src/ecmascript/mujs/tokenlist.h @@ -0,0 +1,16 @@ +#ifndef EL__ECMASCRIPT_MUJS_TOKENLIST_H +#define EL__ECMASCRIPT_MUJS_TOKENLIST_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void mjs_push_tokenlist(js_State *J, void *node); + +#ifdef __cplusplus +} +#endif + +#endif