1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

[spidermonkey] element.classList

This commit is contained in:
Witold Filipczyk 2024-06-27 16:31:27 +02:00
parent 8c40f0dbd4
commit 7b3a5ea87b
7 changed files with 419 additions and 2 deletions

View File

@ -3,6 +3,6 @@ include $(top_builddir)/Makefile.config
INCLUDES += $(SPIDERMONKEY_CFLAGS)
OBJS = attr.obj attributes.obj collection.obj console.obj css.obj customevent.obj document.obj domrect.obj element.obj event.obj form.obj forms.obj heartbeat.obj history.obj implementation.obj input.obj \
keyboard.obj localstorage.obj location.obj message.obj navigator.obj nodelist.obj nodelist2.obj screen.obj style.obj unibar.obj url.obj urlsearchparams.obj window.obj xhr.obj
keyboard.obj localstorage.obj location.obj message.obj navigator.obj nodelist.obj nodelist2.obj screen.obj style.obj tokenlist.obj unibar.obj url.obj urlsearchparams.obj window.obj xhr.obj
include $(top_srcdir)/Makefile.lib

View File

@ -41,6 +41,7 @@
#include "ecmascript/spidermonkey/nodelist.h"
#include "ecmascript/spidermonkey/nodelist2.h"
#include "ecmascript/spidermonkey/style.h"
#include "ecmascript/spidermonkey/tokenlist.h"
#include "ecmascript/spidermonkey/window.h"
#include "intl/libintl.h"
#include "main/select.h"
@ -73,6 +74,7 @@ static bool element_set_property_checked(JSContext *ctx, unsigned int argc, JS::
static bool element_get_property_children(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_childElementCount(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_childNodes(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_classList(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_get_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool element_set_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp);
//static bool element_get_property_clientHeight(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -165,6 +167,7 @@ JSPropertySpec element_props[] = {
JS_PSG("children", element_get_property_children, JSPROP_ENUMERATE),
JS_PSG("childElementCount", element_get_property_childElementCount, JSPROP_ENUMERATE),
JS_PSG("childNodes", element_get_property_childNodes, JSPROP_ENUMERATE),
JS_PSG("classList", element_get_property_classList, JSPROP_ENUMERATE),
JS_PSGS("className", element_get_property_className, element_set_property_className, JSPROP_ENUMERATE),
// JS_PSG("clientHeight", element_get_property_clientHeight, JSPROP_ENUMERATE),
// JS_PSG("clientLeft", element_get_property_clientLeft, JSPROP_ENUMERATE),
@ -546,6 +549,61 @@ element_get_property_childNodes(JSContext *ctx, unsigned int argc, JS::Value *vp
return true;
}
static bool
element_get_property_classList(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct view_state *vs;
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &element_class, NULL)) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
vs = interpreter->vs;
if (!vs) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
dom_element *el = (dom_element *)JS::GetMaybePtrFromReservedSlot<dom_node>(hobj, 0);
if (!el) {
args.rval().setNull();
return true;
}
dom_tokenlist *tl = NULL;
dom_exception exc = dom_tokenlist_create(el, corestring_dom_class, &tl);
if (exc != DOM_NO_ERR || !tl) {
args.rval().setNull();
return true;
}
JSObject *res = getTokenlist(ctx, tl);
args.rval().setObject(*res);
return true;
}
static bool
element_get_property_className(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -1,4 +1,4 @@
srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'css.cpp', 'customevent.cpp', 'document.cpp', 'domrect.cpp', 'element.cpp', 'event.cpp',
'form.cpp', 'forms.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp', 'input.cpp', 'keyboard.cpp',
'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'nodelist.cpp', 'nodelist2.cpp', 'screen.cpp', 'style.cpp', 'unibar.cpp', 'url.cpp',
'localstorage.cpp', 'location.cpp', 'message.cpp', 'navigator.cpp', 'nodelist.cpp', 'nodelist2.cpp', 'screen.cpp', 'style.cpp', 'tokenlist.cpp', 'unibar.cpp', 'url.cpp',
'urlsearchparams.cpp', 'window.cpp', 'xhr.cpp')

View File

@ -0,0 +1,326 @@
/* The SpiderMonkey html DOM tokenlist object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/spidermonkey/util.h"
#include <jsfriendapi.h>
#include "bfu/dialog.h"
#include "cache/cache.h"
#include "cookies/cookies.h"
#include "dialogs/menu.h"
#include "dialogs/status.h"
#include "document/html/frames.h"
#include "document/document.h"
#include "document/forms.h"
#include "document/libdom/corestrings.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/spidermonkey/element.h"
#include "ecmascript/spidermonkey/tokenlist.h"
#include "intl/libintl.h"
#include "main/select.h"
#include "osdep/newwin.h"
#include "osdep/sysname.h"
#include "protocol/http/http.h"
#include "protocol/uri.h"
#include "session/history.h"
#include "session/location.h"
#include "session/session.h"
#include "session/task.h"
#include "terminal/tab.h"
#include "terminal/terminal.h"
#include "util/conv.h"
#include "util/memory.h"
#include "util/string.h"
#include "viewer/text/draw.h"
#include "viewer/text/form.h"
#include "viewer/text/link.h"
#include "viewer/text/vs.h"
#include <iostream>
#include <algorithm>
#include <string>
static bool tokenlist_add(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool tokenlist_contains(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool tokenlist_remove(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool tokenlist_toggle(JSContext *ctx, unsigned int argc, JS::Value *vp);
static void tokenlist_finalize(JS::GCContext *op, JSObject *obj)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
dom_tokenlist *tl = JS::GetMaybePtrFromReservedSlot<dom_tokenlist>(obj, 0);
if (tl) {
dom_tokenlist_unref(tl);
}
}
JSClassOps tokenlist_ops = {
nullptr, // addProperty
nullptr, // deleteProperty
nullptr, // enumerate
nullptr, // newEnumerate
nullptr, // resolve
nullptr, // mayResolve
tokenlist_finalize, // finalize
nullptr, // call
nullptr, // construct
JS_GlobalObjectTraceHook
};
JSClass tokenlist_class = {
"tokenlist",
JSCLASS_HAS_RESERVED_SLOTS(1),
&tokenlist_ops
};
static const spidermonkeyFunctionSpec tokenlist_funcs[] = {
{ "add", tokenlist_add, 1 },
{ "contains", tokenlist_contains, 1 },
{ "remove", tokenlist_remove, 1 },
{ "toggle", tokenlist_toggle, 1 },
{ NULL }
};
static JSPropertySpec tokenlist_props[] = {
JS_PS_END
};
static bool
tokenlist_add(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Value val;
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedValue rval(ctx, val);
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
dom_tokenlist *tl = JS::GetMaybePtrFromReservedSlot<dom_tokenlist>(hobj, 0);
if (!tl || argc < 1) {
args.rval().setUndefined();
return true;
}
char *klass = jsval_to_string(ctx, args[0]);
if (!klass) {
args.rval().setUndefined();
return true;
}
dom_string *kl = NULL;
dom_exception exc = dom_string_create(klass, strlen(klass), &kl);
mem_free(klass);
if (exc != DOM_NO_ERR || !kl) {
args.rval().setUndefined();
return true;
}
exc = dom_tokenlist_add(tl, kl);
dom_string_unref(kl);
if (exc != DOM_NO_ERR) {
interpreter->changed = true;
}
args.rval().setUndefined();
return true;
}
static bool
tokenlist_contains(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Value val;
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedValue rval(ctx, val);
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
dom_tokenlist *tl = JS::GetMaybePtrFromReservedSlot<dom_tokenlist>(hobj, 0);
if (!tl || argc < 1) {
args.rval().setUndefined();
return true;
}
char *klass = jsval_to_string(ctx, args[0]);
if (!klass) {
args.rval().setUndefined();
return true;
}
dom_string *kl = NULL;
dom_exception exc = dom_string_create(klass, strlen(klass), &kl);
mem_free(klass);
if (exc != DOM_NO_ERR || !kl) {
args.rval().setUndefined();
return true;
}
bool res = false;
exc = dom_tokenlist_contains(tl, kl, &res);
dom_string_unref(kl);
if (exc == DOM_NO_ERR) {
args.rval().setBoolean(res);
return true;
}
args.rval().setUndefined();
return true;
}
static bool
tokenlist_remove(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Value val;
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedValue rval(ctx, val);
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
dom_tokenlist *tl = JS::GetMaybePtrFromReservedSlot<dom_tokenlist>(hobj, 0);
if (!tl || argc < 1) {
args.rval().setUndefined();
return true;
}
char *klass = jsval_to_string(ctx, args[0]);
if (!klass) {
args.rval().setUndefined();
return true;
}
dom_string *kl = NULL;
dom_exception exc = dom_string_create(klass, strlen(klass), &kl);
mem_free(klass);
if (exc != DOM_NO_ERR || !kl) {
args.rval().setUndefined();
return true;
}
exc = dom_tokenlist_remove(tl, kl);
dom_string_unref(kl);
if (exc != DOM_NO_ERR) {
interpreter->changed = true;
}
args.rval().setUndefined();
return true;
}
static bool
tokenlist_toggle(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JS::Value val;
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedValue rval(ctx, val);
JS::Realm *comp = js::GetContextRealm(ctx);
if (!comp) {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
#endif
return false;
}
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS::GetRealmPrivate(comp);
dom_tokenlist *tl = JS::GetMaybePtrFromReservedSlot<dom_tokenlist>(hobj, 0);
if (!tl || argc < 1) {
args.rval().setUndefined();
return true;
}
char *klass = jsval_to_string(ctx, args[0]);
if (!klass) {
args.rval().setUndefined();
return true;
}
dom_string *kl = NULL;
dom_exception exc = dom_string_create(klass, strlen(klass), &kl);
mem_free(klass);
if (exc != DOM_NO_ERR || !kl) {
args.rval().setUndefined();
return true;
}
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);
args.rval().setUndefined();
return true;
}
JSObject *
getTokenlist(JSContext *ctx, void *node)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSObject *el = JS_NewObject(ctx, &tokenlist_class);
if (!el) {
return NULL;
}
JS::RootedObject r_el(ctx, el);
JS_DefineProperties(ctx, r_el, (JSPropertySpec *) tokenlist_props);
spidermonkey_DefineFunctions(ctx, el, tokenlist_funcs);
JS::SetReservedSlot(el, 0, JS::PrivateValue(node));
return el;
}

View File

@ -0,0 +1,8 @@
#ifndef EL__ECMASCRIPT_SPIDERMONKEY_TOKENLIST_H
#define EL__ECMASCRIPT_SPIDERMONKEY_TOKENLIST_H
#include "ecmascript/spidermonkey/util.h"
JSObject *getTokenlist(JSContext *ctx, void *tokens);
#endif

View File

@ -0,0 +1,24 @@
<script>
function aa()
{
var div = document.createElement("div");
div.className = "foo";
// our starting state: <div class="foo"></div>
console.assert(div.outerHTML === '<div class="foo"></div>', 'class foo');
// use the classList API to remove and add classes
div.classList.remove("foo");
console.assert(div.outerHTML === '<div class=""></div>', 'empty class');
div.classList.add("anotherclass");
// <div class="anotherclass"></div>
console.assert(div.outerHTML === '<div class="anotherclass"></div>', 'anotherclass');
console.assert(div.classList.contains("anotherclass"), 'contains anotherclass');
console.assert(!div.classList.contains("foo"), 'not foo');
div.classList.toggle("foo");
console.assert(div.classList.contains("foo"), 'contains foo');
}
console.error('element.classList.html');
aa();
console.exit();
</script>

View File

@ -28,6 +28,7 @@ took = [
'element.attributes.html',
'element.checked.html',
'element.childElementCount.html',
'element.classList.html',
'element.className.html',
'element.cloneNode.html',
'element.closest.html',