2021-10-30 13:19:39 -04:00
|
|
|
/* The QuickJS domimplementation object. */
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "ecmascript/ecmascript.h"
|
2021-11-20 11:29:00 -05:00
|
|
|
#include "ecmascript/quickjs.h"
|
2021-11-04 14:58:57 -04:00
|
|
|
#include "ecmascript/quickjs/document.h"
|
2021-10-30 13:19:39 -04:00
|
|
|
#include "ecmascript/quickjs/implementation.h"
|
|
|
|
#include "util/conv.h"
|
|
|
|
|
|
|
|
#include <libxml/HTMLparser.h>
|
|
|
|
#include <libxml++/libxml++.h>
|
|
|
|
|
|
|
|
#define countof(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
|
|
|
|
static JSClassID js_implementation_class_id;
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_implementation_createHTMLDocument(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
if (argc != 1) {
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__);
|
|
|
|
#endif
|
|
|
|
return JS_UNDEFINED;
|
|
|
|
}
|
|
|
|
size_t len;
|
|
|
|
const char *title = JS_ToCStringLen(ctx, &len, argv[0]);
|
|
|
|
|
|
|
|
if (!title) {
|
|
|
|
return JS_EXCEPTION;
|
|
|
|
}
|
|
|
|
struct string str;
|
|
|
|
|
|
|
|
if (!init_string(&str)) {
|
|
|
|
JS_FreeCString(ctx, title);
|
|
|
|
|
|
|
|
return JS_NULL;
|
|
|
|
}
|
|
|
|
add_to_string(&str, "<!doctype html>\n<html><head><title>");
|
|
|
|
add_html_to_string(&str, title, len);
|
|
|
|
add_to_string(&str, "</title></head><body></body></html>");
|
|
|
|
|
|
|
|
// Parse HTML and create a DOM tree
|
|
|
|
xmlDoc* doc = htmlReadDoc((xmlChar*)str.source, NULL, "utf-8",
|
|
|
|
HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
|
|
|
|
// Encapsulate raw libxml document in a libxml++ wrapper
|
2021-11-16 11:10:35 -05:00
|
|
|
xmlpp::Document *docu = new(std::nothrow) xmlpp::Document(doc);
|
2021-10-30 13:19:39 -04:00
|
|
|
done_string(&str);
|
|
|
|
JS_FreeCString(ctx, title);
|
|
|
|
|
|
|
|
return getDocument(ctx, docu);
|
|
|
|
}
|
|
|
|
|
2021-12-08 07:22:35 -05:00
|
|
|
static JSValue
|
|
|
|
js_implementation_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, "[implementation object]");
|
|
|
|
}
|
|
|
|
|
2021-10-30 13:19:39 -04:00
|
|
|
static const JSCFunctionListEntry js_implementation_proto_funcs[] = {
|
|
|
|
JS_CFUNC_DEF("createHTMLDocument", 1, js_implementation_createHTMLDocument),
|
2021-12-08 07:22:35 -05:00
|
|
|
JS_CFUNC_DEF("toString", 0, js_implementation_toString)
|
2021-10-30 13:19:39 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSClassDef js_implementation_class = {
|
|
|
|
"implementation",
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSValue
|
|
|
|
js_implementation_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *argv)
|
|
|
|
{
|
|
|
|
JSValue obj = JS_UNDEFINED;
|
|
|
|
JSValue proto;
|
|
|
|
/* using new_target to get the prototype is necessary when the
|
|
|
|
class is extended. */
|
|
|
|
proto = JS_GetPropertyStr(ctx, new_target, "prototype");
|
|
|
|
|
|
|
|
if (JS_IsException(proto)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
obj = JS_NewObjectProtoClass(ctx, proto, js_implementation_class_id);
|
|
|
|
JS_FreeValue(ctx, proto);
|
|
|
|
|
|
|
|
if (JS_IsException(obj)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
2021-11-20 11:29:00 -05:00
|
|
|
RETURN_JS(obj);
|
2021-10-30 13:19:39 -04:00
|
|
|
|
|
|
|
fail:
|
|
|
|
JS_FreeValue(ctx, obj);
|
|
|
|
return JS_EXCEPTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
js_implementation_init(JSContext *ctx, JSValue global_obj)
|
|
|
|
{
|
|
|
|
JSValue implementation_proto, implementation_class;
|
|
|
|
|
|
|
|
/* create the implementation class */
|
|
|
|
JS_NewClassID(&js_implementation_class_id);
|
|
|
|
JS_NewClass(JS_GetRuntime(ctx), js_implementation_class_id, &js_implementation_class);
|
|
|
|
|
|
|
|
implementation_proto = JS_NewObject(ctx);
|
|
|
|
JS_SetPropertyFunctionList(ctx, implementation_proto, js_implementation_proto_funcs, countof(js_implementation_proto_funcs));
|
|
|
|
|
|
|
|
implementation_class = JS_NewCFunction2(ctx, js_implementation_ctor, "implementation", 0, JS_CFUNC_constructor, 0);
|
|
|
|
/* set proto.constructor and ctor.prototype */
|
|
|
|
JS_SetConstructor(ctx, implementation_class, implementation_proto);
|
|
|
|
JS_SetClassProto(ctx, js_implementation_class_id, implementation_proto);
|
|
|
|
|
|
|
|
JS_SetPropertyStr(ctx, global_obj, "implementation", implementation_proto);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValue
|
|
|
|
getImplementation(JSContext *ctx)
|
|
|
|
{
|
|
|
|
#ifdef ECMASCRIPT_DEBUG
|
|
|
|
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
|
|
|
|
#endif
|
|
|
|
JSValue implementation_obj = JS_NewObject(ctx);
|
|
|
|
JS_SetPropertyFunctionList(ctx, implementation_obj, js_implementation_proto_funcs, countof(js_implementation_proto_funcs));
|
|
|
|
// implementation_class = JS_NewCFunction2(ctx, js_implementation_ctor, "implementation", 0, JS_CFUNC_constructor, 0);
|
|
|
|
// JS_SetConstructor(ctx, implementation_class, implementation_obj);
|
|
|
|
JS_SetClassProto(ctx, js_implementation_class_id, implementation_obj);
|
|
|
|
|
2021-11-20 11:29:00 -05:00
|
|
|
RETURN_JS(implementation_obj);
|
2021-10-30 13:19:39 -04:00
|
|
|
}
|