Add initial readonly location implementation

This commit is contained in:
Vincent Sanders 2015-09-07 13:52:57 +01:00
parent 1f07c45740
commit 1a43dd21b3
3 changed files with 227 additions and 0 deletions

View File

@ -0,0 +1,206 @@
/* Location binding for browser using duktape and libdom
*
* Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
* Copyright 2015 Daniel Silverstone <dsilvers@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* Released under the terms of the MIT License,
* http://www.opensource.org/licenses/mit-license
*/
class Location {
private "nsurl *" url;
}
init Location("nsurl *" url)
%{
priv->url = url;
nsurl_ref(url);
%}
fini Location()
%{
nsurl_unref(priv->url);
%}
getter Location::href()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_COMPLETE, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::protocol()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_SCHEME, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::username()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_USERNAME, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::password()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_PASSWORD, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::host()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_HOST, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::hostname()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_COMPLETE, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::port()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_PORT, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::pathname()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_PATH, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::search()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_QUERY, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}
getter Location::hash()
%{
char *url_s = NULL;
size_t url_l;
nsurl_get(priv->url, NSURL_FRAGMENT, &url_s, &url_l);
if (url_s == NULL) {
return 0;
}
duk_push_lstring(ctx, url_s, url_l);
free(url_s);
return 1;
%}

View File

@ -76,3 +76,22 @@ getter Window::console()
}
return 1;
%}
getter Window::location()
%{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, MAGIC(Location));
if (duk_is_undefined(ctx, -1)) {
duk_pop(ctx);
duk_push_pointer(ctx, llcache_handle_get_url(priv->htmlc->base.llcache));
if (dukky_create_object(ctx, PROTO_NAME(LOCATION), 1) != DUK_EXEC_SUCCESS) {
duk_error(ctx, DUK_ERR_ERROR, "Unable to create location object");
return 0;
}
duk_dup(ctx, -1);
duk_put_prop_string(ctx, -3, MAGIC(Location));
}
return 1;
%}

View File

@ -27,6 +27,7 @@ binding duk_libdom {
#include <dom/dom.h>
#include "utils/log.h"
#include "utils/nsurl.h"
#include "javascript/duktape/duktape.h"
@ -58,6 +59,7 @@ struct dom_html_br_element;
#include "NodeList.bnd"
#include "Element.bnd"
#include "HTMLCollection.bnd"
#include "Location.bnd"
/* specialisations of html_element */
init HTMLUnknownElement("struct dom_html_element *" html_unknown_element::html_element);