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

[js] location.port getter

This commit is contained in:
Witold Filipczyk 2021-05-06 15:06:35 +02:00
parent b07d39364c
commit 07dba347ac
2 changed files with 74 additions and 0 deletions

View File

@ -157,6 +157,8 @@ history_go(JSContext *ctx, unsigned int argc, JS::Value *rval)
static bool location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_get_property_port(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_set_property_port(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_get_property_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_set_property_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp);
@ -182,6 +184,7 @@ enum location_prop {
};
JSPropertySpec location_props[] = {
JS_PSGS("href", location_get_property_href, location_set_property_href, JSPROP_ENUMERATE),
JS_PSGS("port", location_get_property_port, location_set_property_port, JSPROP_ENUMERATE),
JS_PSGS("protocol", location_get_property_protocol, location_set_property_protocol, JSPROP_ENUMERATE),
JS_PS_END
};
@ -225,6 +228,44 @@ location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
location_get_property_port(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, &location_class, NULL))
return false;
vs = interpreter->vs;
if (!vs) {
return false;
}
struct string port;
init_string(&port);
if (vs->uri->port && vs->uri->portlen) {
add_bytes_to_string(&port, vs->uri->port, vs->uri->portlen);
}
args.rval().setString(JS_NewStringCopyZ(ctx, port.source));
done_string(&port);
return true;
}
static bool
location_get_property_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -300,6 +341,38 @@ location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
location_set_property_port(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;
struct document_view *doc_view;
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, &location_class, NULL))
return false;
vs = interpreter->vs;
if (!vs) {
return false;
}
doc_view = vs->doc_view;
// location_goto(doc_view, JS_EncodeString(ctx, args[0].toString()));
return true;
}
static bool
location_set_property_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp)
{

View File

@ -0,0 +1 @@
<p><button onclick="javascript:{ alert(location.port); }">Display location.port.</button></p>