diff --git a/src/ecmascript/quickjs.c b/src/ecmascript/quickjs.c index dc8130e29..07bc181a5 100644 --- a/src/ecmascript/quickjs.c +++ b/src/ecmascript/quickjs.c @@ -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); diff --git a/src/ecmascript/quickjs/Makefile b/src/ecmascript/quickjs/Makefile index 64e82e430..b284cc902 100644 --- a/src/ecmascript/quickjs/Makefile +++ b/src/ecmascript/quickjs/Makefile @@ -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 diff --git a/src/ecmascript/quickjs/document.c b/src/ecmascript/quickjs/document.c index 1419d26e7..be5be4dc4 100644 --- a/src/ecmascript/quickjs/document.c +++ b/src/ecmascript/quickjs/document.c @@ -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; diff --git a/src/ecmascript/quickjs/domparser.c b/src/ecmascript/quickjs/domparser.c new file mode 100644 index 000000000..5c6e746fa --- /dev/null +++ b/src/ecmascript/quickjs/domparser.c @@ -0,0 +1,124 @@ +/* The QuickJS DOMParser implementation. */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include + +#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; +} diff --git a/src/ecmascript/quickjs/domparser.h b/src/ecmascript/quickjs/domparser.h new file mode 100644 index 000000000..d3b96a4f4 --- /dev/null +++ b/src/ecmascript/quickjs/domparser.h @@ -0,0 +1,17 @@ +#ifndef EL__ECMASCRIPT_QUICKJS_DOMPARSER_H +#define EL__ECMASCRIPT_QUICKJS_DOMPARSER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +int js_domparser_init(JSContext *ctx); +extern JSClassID js_domparser_class_id; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/ecmascript/quickjs/meson.build b/src/ecmascript/quickjs/meson.build index f0f9a0a2f..28ccc98fc 100644 --- a/src/ecmascript/quickjs/meson.build +++ b/src/ecmascript/quickjs/meson.build @@ -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')