From b0ee34bdd508f90b8575a4077102b139593c12c1 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Tue, 4 May 2021 16:24:56 +0200 Subject: [PATCH] [js] dir (read) --- src/ecmascript/spidermonkey/element.c | 80 ++++++++++++++++++++++++++- test/ecmascript/dir.html | 20 +++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/ecmascript/dir.html diff --git a/src/ecmascript/spidermonkey/element.c b/src/ecmascript/spidermonkey/element.c index 44b210806..6e218f67c 100644 --- a/src/ecmascript/spidermonkey/element.c +++ b/src/ecmascript/spidermonkey/element.c @@ -50,9 +50,10 @@ #include #include - using namespace htmlcxx; +static bool element_get_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp); +static bool element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_set_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool element_get_property_innerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp); @@ -78,6 +79,7 @@ JSClass element_class = { }; JSPropertySpec element_props[] = { + JS_PSGS("dir", element_get_property_dir, element_set_property_dir, JSPROP_ENUMERATE), JS_PSGS("id", element_get_property_id, element_set_property_id, JSPROP_ENUMERATE), JS_PSGS("innerHTML", element_get_property_innerHtml, element_set_property_innerHtml, JSPROP_ENUMERATE), JS_PSGS("lang", element_get_property_lang, element_set_property_lang, JSPROP_ENUMERATE), @@ -87,6 +89,53 @@ JSPropertySpec element_props[] = { JS_PS_END }; +static bool +element_get_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + + struct view_state *vs; + JSCompartment *comp = js::GetContextCompartment(ctx); + + if (!comp) { + return false; + } + + struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(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)) + return false; + + vs = interpreter->vs; + if (!vs) { + return false; + } + + tree *el = JS_GetPrivate(hobj); + + if (!el) { + args.rval().setNull(); + return true; + } + + tree::iterator it = el->begin(); + it->parseAttributes(); + std::string v = it->attribute("dir").second; + + if (v != "auto" && v != "ltr" && v != "rtl") { + v = ""; + } + + args.rval().setString(JS_NewStringCopyZ(ctx, v.c_str())); + + return true; +} + + static bool element_get_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp) { @@ -412,6 +461,35 @@ element_get_property_outerHtml(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +element_set_property_dir(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, vp); + JS::RootedObject hobj(ctx, &args.thisv().toObject()); + + JSCompartment *comp = js::GetContextCompartment(ctx); + + if (!comp) { + return false; + } + + struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(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)) + return false; + + struct view_state *vs = interpreter->vs; + if (!vs) { + return true; + } + + return true; +} + + static bool element_set_property_id(JSContext *ctx, unsigned int argc, JS::Value *vp) { diff --git a/test/ecmascript/dir.html b/test/ecmascript/dir.html new file mode 100644 index 000000000..4fee33424 --- /dev/null +++ b/test/ecmascript/dir.html @@ -0,0 +1,20 @@ + + +BBB +bbb + +AAAUUUAAAAAAA + +BB + + + +