1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-15 23:35:34 +00:00

[js] location.protocol (read)

This commit is contained in:
Witold Filipczyk 2021-05-04 20:55:06 +02:00
parent 1e8f86628d
commit 9b738edb18
2 changed files with 82 additions and 1 deletions

View File

@ -157,6 +157,9 @@ 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_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool location_set_property_protocol(JSContext *ctx, unsigned int argc, JS::Value *vp);
JSClassOps location_ops = {
JS_PropertyStub, nullptr,
@ -179,6 +182,7 @@ enum location_prop {
};
JSPropertySpec location_props[] = {
JS_PSGS("href", location_get_property_href, location_set_property_href, JSPROP_ENUMERATE),
JS_PSGS("protocol", location_get_property_protocol, location_set_property_protocol, JSPROP_ENUMERATE),
JS_PS_END
};
@ -221,6 +225,49 @@ location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
location_get_property_protocol(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 proto;
init_string(&proto);
/* Custom or unknown keep the URI untouched. */
if (vs->uri->protocol == PROTOCOL_UNKNOWN) {
add_to_string(&proto, struri(vs->uri));
} else {
add_bytes_to_string(&proto, vs->uri->string, vs->uri->protocollen);
add_char_to_string(&proto, ':');
}
args.rval().setString(JS_NewStringCopyZ(ctx, proto.source));
done_string(&proto);
return true;
}
static bool
location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
@ -245,7 +292,7 @@ location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
vs = interpreter->vs;
if (!vs) {
return;
return false;
}
doc_view = vs->doc_view;
location_goto(doc_view, JS_EncodeString(ctx, args[0].toString()));
@ -253,6 +300,38 @@ location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
return true;
}
static bool
location_set_property_protocol(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_toString(JSContext *ctx, unsigned int argc, JS::Value *rval);

View File

@ -3,3 +3,5 @@
<p><a href="javascript:document.url='http://elinks.or.cz';">Change the document.url</a> (which is impossible because it's read-only, but the return value of the statement is used as the link target so it magically still works).</p>
<p><a href="javascript:{ document.title = 'This is a test'; document.title += ', this document URL is '+document.url; 'http://elinks.or.cz' }">Change the document.title and go to elinks.or.cz (return value of statement).</a></p>
<p><button onclick="javascript:{ document.title = 'This is a test'; document.title += ', this location protocol is '+ location.protocol }">Change the document.title to location.protocol.</button></p>