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

[quickjs] DOMParser

Not tested.
This commit is contained in:
Witold Filipczyk 2024-07-06 09:01:21 +02:00
parent 7c249117f9
commit 56c11ef07a
6 changed files with 149 additions and 2 deletions

View File

@ -28,6 +28,7 @@
#include "ecmascript/quickjs/console.h"
#include "ecmascript/quickjs/customevent.h"
#include "ecmascript/quickjs/document.h"
#include "ecmascript/quickjs/domparser.h"
#include "ecmascript/quickjs/element.h"
#include "ecmascript/quickjs/event.h"
#include "ecmascript/quickjs/heartbeat.h"
@ -191,6 +192,7 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter)
js_customEvent_init(ctx);
js_url_init(ctx);
js_urlSearchParams_init(ctx);
js_domparser_init(ctx);
interpreter->document_obj = getDocument(ctx, document->dom);

View File

@ -1,7 +1,7 @@
top_builddir=../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o console.o css.o customevent.o dataset.o document.o domrect.o element.o event.o form.o forms.o heartbeat.o history.o implementation.o input.o \
OBJS = attr.o attributes.o collection.o console.o css.o customevent.o dataset.o document.o domparser.o domrect.o element.o event.o form.o forms.o heartbeat.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 tokenlist.o unibar.o url.o urlsearchparams.o window.o xhr.o
include $(top_srcdir)/Makefile.lib

View File

@ -1977,6 +1977,10 @@ getDocument(JSContext *ctx, void *doc)
return JS_NULL;
}
init_list(doc_private->listeners);
if (doc) {
dom_node_ref((dom_node *)doc);
}
doc_private->node = doc;
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
doc_private->interpreter = interpreter;

View File

@ -0,0 +1,124 @@
/* The QuickJS DOMParser implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "document/libdom/doc.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/libdom/dom.h"
#include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/document.h"
#include "ecmascript/quickjs/domparser.h"
#include "intl/charsets.h"
#include "terminal/event.h"
#define countof(x) (sizeof(x) / sizeof((x)[0]))
JSClassID js_domparser_class_id;
static JSValue js_domparser_parseFromString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static JSClassDef js_domparser_class = {
"DOMParser",
};
static const JSCFunctionListEntry js_domparser_proto_funcs[] = {
JS_CFUNC_DEF("parseFromString", 2, js_domparser_parseFromString),
};
static JSValue
js_domparser_parseFromString(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(this_val);
size_t len;
const char *str = JS_ToCStringLen(ctx, &len, argv[0]);
if (!str) {
return JS_NULL;
}
dom_html_document *doc = document_parse_text("utf-8", str, len);
JS_FreeCString(ctx, str);
if (!doc) {
return JS_NULL;
}
return getDocument(ctx, doc);
}
static JSValue
js_domparser_constructor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
REF_JS(new_target);
JSValue obj = JS_NewObjectClass(ctx, js_domparser_class_id);
REF_JS(obj);
return obj;
}
static void
JS_NewGlobalCConstructor2(JSContext *ctx, JSValue func_obj, const char *name, JSValueConst proto)
{
REF_JS(func_obj);
REF_JS(proto);
JSValue global_object = JS_GetGlobalObject(ctx);
JS_DefinePropertyValueStr(ctx, global_object, name,
JS_DupValue(ctx, func_obj), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
JS_SetConstructor(ctx, func_obj, proto);
JS_FreeValue(ctx, func_obj);
JS_FreeValue(ctx, global_object);
}
static JSValueConst
JS_NewGlobalCConstructor(JSContext *ctx, const char *name, JSCFunction *func, int length, JSValueConst proto)
{
JSValue func_obj;
func_obj = JS_NewCFunction2(ctx, func, name, length, JS_CFUNC_constructor_or_func, 0);
REF_JS(func_obj);
REF_JS(proto);
JS_NewGlobalCConstructor2(ctx, func_obj, name, proto);
return func_obj;
}
int
js_domparser_init(JSContext *ctx)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
JSValue proto, obj;
/* DOMParser class */
JS_NewClassID(&js_domparser_class_id);
JS_NewClass(JS_GetRuntime(ctx), js_domparser_class_id, &js_domparser_class);
proto = JS_NewObject(ctx);
REF_JS(proto);
JS_SetPropertyFunctionList(ctx, proto, js_domparser_proto_funcs, countof(js_domparser_proto_funcs));
JS_SetClassProto(ctx, js_domparser_class_id, proto);
/* DOMParser object */
obj = JS_NewGlobalCConstructor(ctx, "DOMParser", js_domparser_constructor, 1, proto);
REF_JS(obj);
return 0;
}

View File

@ -0,0 +1,17 @@
#ifndef EL__ECMASCRIPT_QUICKJS_DOMPARSER_H
#define EL__ECMASCRIPT_QUICKJS_DOMPARSER_H
#include <quickjs/quickjs.h>
#ifdef __cplusplus
extern "C" {
#endif
int js_domparser_init(JSContext *ctx);
extern JSClassID js_domparser_class_id;
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,3 +1,3 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'customevent.c', 'dataset.c', 'document.c', 'domrect.c', 'element.c', 'event.c', 'form.c', 'forms.c', 'heartbeat.c', 'history.c',
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'css.c', 'customevent.c', 'dataset.c', 'document.c', 'domparser.c', 'domrect.c', 'element.c', 'event.c', 'form.c', 'forms.c', 'heartbeat.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', 'tokenlist.c', 'unibar.c', 'url.c', 'urlsearchparams.c', 'window.c', 'xhr.c')