2021-10-26 12:15:08 -04:00
|
|
|
/* The QuickJS attributes object implementation. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.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/view.h"
|
|
|
|
#include "ecmascript/ecmascript.h"
|
2021-11-20 11:29:00 -05:00
|
|
|
#include "ecmascript/quickjs.h"
|
2021-10-26 12:15:08 -04:00
|
|
|
#include "ecmascript/quickjs/attr.h"
|
|
|
|
#include "ecmascript/quickjs/attributes.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 <libxml/tree.h>
|
|
|
|
#include <libxml/HTMLparser.h>
|
|
|
|
#include <libxml++/libxml++.h>
|
|
|
|
#include <libxml++/attributenode.h>
|
|
|
|
#include <libxml++/parsers/domparser.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#define countof(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
|
2021-11-25 10:21:25 -05:00
|
|
|
static std::map<void *, JSValueConst> map_attributes;
|
|
|
|
static std::map<JSValueConst, void *> map_rev_attributes;
|
|
|
|
|
|
|
|
static void *
|
|
|
|
js_attributes_GetOpaque(JSValueConst this_val)
|
|
|
|
{
|
|
|
|
return map_rev_attributes[this_val];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
js_attributes_SetOpaque(JSValueConst this_val, void *node)
|
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
map_rev_attributes.erase(this_val);
|
|
|
|
} else {
|
|
|
|
map_rev_attributes[this_val] = node;
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
js_attributes_set_items(JSContext *ctx, JSValue this_val, void *node)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
|
|
|
|
assert(interpreter);
|
|
|
|
|
2022-01-30 05:04:50 -05:00
|
|
|
xmlpp::Element::AttributeList *al = static_cast<xmlpp::Element::AttributeList *>(node);
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
if (!al) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = al->begin();
|
|
|
|
auto end = al->end();
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (;it != end; ++it, ++i) {
|
|
|
|
xmlpp::Attribute *attr = *it;
|
|
|
|
|
|
|
|
if (!attr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue obj = getAttr(ctx, attr);
|
|
|
|
JS_SetPropertyUint32(ctx, this_val, i, obj);
|
|
|
|
|
|
|
|
xmlpp::ustring name = attr->get_name();
|
|
|
|
|
|
|
|
if (name != "" && name != "item" && name != "namedItem") {
|
|
|
|
JS_DefinePropertyValueStr(ctx, this_val, name.c_str(), obj, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_attributes_get_property_length(JSContext *ctx, JSValueConst this_val)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
|
|
|
|
struct view_state *vs = interpreter->vs;
|
|
|
|
|
|
|
|
if (!vs) {
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
|
|
|
#endif
|
|
|
|
return JS_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
2022-01-30 05:04:50 -05:00
|
|
|
xmlpp::Element::AttributeList *al = static_cast<xmlpp::Element::AttributeList *>(js_attributes_GetOpaque(this_val));
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
if (!al) {
|
|
|
|
return JS_NewInt32(ctx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_NewInt32(ctx, al->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_attributes_item2(JSContext *ctx, JSValueConst this_val, int idx)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2022-01-30 05:04:50 -05:00
|
|
|
xmlpp::Element::AttributeList *al = static_cast<xmlpp::Element::AttributeList *>(js_attributes_GetOpaque(this_val));
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
if (!al) {
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = al->begin();
|
|
|
|
auto end = al->end();
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (;it != end; it++, i++) {
|
|
|
|
if (i != idx) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
xmlpp::Attribute *attr = *it;
|
|
|
|
|
|
|
|
return getAttr(ctx, attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_attributes_item(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
if (argc != 1) {
|
2021-10-27 12:10:04 -04:00
|
|
|
return JS_UNDEFINED;
|
2021-10-26 12:15:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int index;
|
|
|
|
JS_ToInt32(ctx, &index, argv[0]);
|
|
|
|
|
|
|
|
return js_attributes_item2(ctx, this_val, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_attributes_namedItem2(JSContext *ctx, JSValueConst this_val, const char *str)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
2022-01-30 05:04:50 -05:00
|
|
|
xmlpp::Element::AttributeList *al = static_cast<xmlpp::Element::AttributeList *>(js_attributes_GetOpaque(this_val));
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
if (!al) {
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlpp::ustring name = str;
|
|
|
|
|
|
|
|
auto it = al->begin();
|
|
|
|
auto end = al->end();
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
2022-01-30 05:12:39 -05:00
|
|
|
auto attr = dynamic_cast<xmlpp::AttributeNode*>(*it);
|
2021-10-26 12:15:08 -04:00
|
|
|
|
|
|
|
if (!attr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name == attr->get_name()) {
|
|
|
|
JSValue obj = getAttr(ctx, attr);
|
2021-11-20 11:29:00 -05:00
|
|
|
RETURN_JS(obj);
|
2021-10-26 12:15:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_attributes_getNamedItem(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
if (argc != 1) {
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *str;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
str = JS_ToCStringLen(ctx, &len, argv[0]);
|
|
|
|
|
|
|
|
if (!str) {
|
|
|
|
return JS_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue ret = js_attributes_namedItem2(ctx, this_val, str);
|
|
|
|
JS_FreeCString(ctx, str);
|
|
|
|
|
2021-11-20 11:29:00 -05:00
|
|
|
RETURN_JS(ret);
|
2021-10-26 12:15:08 -04:00
|
|
|
}
|
|
|
|
|
2021-12-08 07:22:35 -05:00
|
|
|
static JSValue
|
|
|
|
js_attributes_toString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
return JS_NewString(ctx, "[attributes object]");
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:15:08 -04:00
|
|
|
static const JSCFunctionListEntry js_attributes_proto_funcs[] = {
|
|
|
|
JS_CGETSET_DEF("length", js_attributes_get_property_length, nullptr),
|
|
|
|
JS_CFUNC_DEF("item", 1, js_attributes_item),
|
|
|
|
JS_CFUNC_DEF("getNamedItem", 1, js_attributes_getNamedItem),
|
2021-12-08 07:22:35 -05:00
|
|
|
JS_CFUNC_DEF("toString", 0, js_attributes_toString)
|
2021-10-26 12:15:08 -04:00
|
|
|
};
|
|
|
|
|
2022-01-30 05:04:50 -05:00
|
|
|
#if 0
|
2021-11-25 10:21:25 -05:00
|
|
|
static void
|
|
|
|
js_attributes_finalizer(JSRuntime *rt, JSValue val)
|
2021-10-26 12:15:08 -04:00
|
|
|
{
|
2021-11-25 10:21:25 -05:00
|
|
|
void *node = js_attributes_GetOpaque(val);
|
2021-10-26 12:15:08 -04:00
|
|
|
|
2021-11-25 10:21:25 -05:00
|
|
|
js_attributes_SetOpaque(val, nullptr);
|
|
|
|
map_attributes.erase(node);
|
2021-10-26 12:15:08 -04:00
|
|
|
}
|
|
|
|
|
2021-11-25 10:21:25 -05:00
|
|
|
static JSClassDef js_attributes_class = {
|
|
|
|
"attributes",
|
|
|
|
js_attributes_finalizer
|
|
|
|
};
|
2021-10-26 12:15:08 -04:00
|
|
|
|
2022-01-30 05:04:50 -05:00
|
|
|
#endif
|
|
|
|
|
2021-10-26 12:15:08 -04:00
|
|
|
JSValue
|
|
|
|
getAttributes(JSContext *ctx, void *node)
|
|
|
|
{
|
2021-11-25 10:21:25 -05:00
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
auto node_find = map_attributes.find(node);
|
|
|
|
|
|
|
|
if (node_find != map_attributes.end()) {
|
|
|
|
JSValue r = JS_DupValue(ctx, node_find->second);
|
|
|
|
RETURN_JS(r);
|
|
|
|
}
|
|
|
|
JSValue attributes_obj = JS_NewArray(ctx);
|
2021-10-26 12:15:08 -04:00
|
|
|
JS_SetPropertyFunctionList(ctx, attributes_obj, js_attributes_proto_funcs, countof(js_attributes_proto_funcs));
|
|
|
|
|
2021-11-25 10:21:25 -05:00
|
|
|
js_attributes_SetOpaque(attributes_obj, node);
|
2021-10-26 12:15:08 -04:00
|
|
|
js_attributes_set_items(ctx, attributes_obj, node);
|
2021-11-25 10:21:25 -05:00
|
|
|
map_attributes[node] = attributes_obj;
|
2021-10-26 12:15:08 -04:00
|
|
|
|
2021-11-25 10:21:25 -05:00
|
|
|
JSValue rr = JS_DupValue(ctx, attributes_obj);
|
|
|
|
RETURN_JS(rr);
|
2021-10-26 12:15:08 -04:00
|
|
|
}
|