1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[mujs] element.classList getter

This commit is contained in:
Witold Filipczyk 2024-06-27 17:54:30 +02:00
parent 63e65bb4fc
commit fbbba38b8a
5 changed files with 253 additions and 2 deletions

View File

@ -2,6 +2,6 @@ top_builddir=../../..
include $(top_builddir)/Makefile.config 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 \ 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 include $(top_srcdir)/Makefile.lib

View File

@ -43,6 +43,7 @@
#include "ecmascript/mujs/nodelist.h" #include "ecmascript/mujs/nodelist.h"
#include "ecmascript/mujs/nodelist2.h" #include "ecmascript/mujs/nodelist2.h"
#include "ecmascript/mujs/style.h" #include "ecmascript/mujs/style.h"
#include "ecmascript/mujs/tokenlist.h"
#include "ecmascript/mujs/window.h" #include "ecmascript/mujs/window.h"
#include "intl/libintl.h" #include "intl/libintl.h"
#include "main/select.h" #include "main/select.h"
@ -251,6 +252,30 @@ mjs_element_get_property_childNodes(js_State *J)
mjs_push_nodelist(J, nodes); 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 static void
mjs_element_get_property_className(js_State *J) 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, "children", mjs_element_get_property_children, NULL);
addproperty(J, "childElementCount", mjs_element_get_property_childElementCount, NULL); addproperty(J, "childElementCount", mjs_element_get_property_childElementCount, NULL);
addproperty(J, "childNodes", mjs_element_get_property_childNodes, 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, "className", mjs_element_get_property_className, mjs_element_set_property_className);
// addproperty(J, "clientHeight", mjs_element_get_property_clientHeight, NULL); // addproperty(J, "clientHeight", mjs_element_get_property_clientHeight, NULL);
// addproperty(J, "clientLeft", mjs_element_get_property_clientLeft, NULL); // addproperty(J, "clientLeft", mjs_element_get_property_clientLeft, NULL);

View File

@ -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', 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')

View File

@ -0,0 +1,209 @@
/* The MuJS DOM tokenlist object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef CONFIG_LIBDOM
#include <dom/dom.h>
#include <dom/bindings/hubbub/parser.h>
#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);
}
}

View File

@ -0,0 +1,16 @@
#ifndef EL__ECMASCRIPT_MUJS_TOKENLIST_H
#define EL__ECMASCRIPT_MUJS_TOKENLIST_H
#include <mujs.h>
#ifdef __cplusplus
extern "C" {
#endif
void mjs_push_tokenlist(js_State *J, void *node);
#ifdef __cplusplus
}
#endif
#endif