From 46945f636221ccf0751f3f0e2e78c6e6d33cab7f Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Sun, 20 Sep 2015 10:35:32 +0100 Subject: Improve location implementation to be more complete --- javascript/duktape/Location.bnd | 223 +++++++++++++++++++++++++++++++++------- 1 file changed, 184 insertions(+), 39 deletions(-) (limited to 'javascript/duktape/Location.bnd') diff --git a/javascript/duktape/Location.bnd b/javascript/duktape/Location.bnd index 5acbde2da..8c79cd460 100644 --- a/javascript/duktape/Location.bnd +++ b/javascript/duktape/Location.bnd @@ -10,7 +10,10 @@ */ class Location { - private "nsurl *" url; + private "nsurl *" url; + prologue %{ +#include "desktop/browser.h" +%}; } init Location("nsurl *" url) @@ -24,6 +27,80 @@ fini Location() nsurl_unref(priv->url); %} +method Location::reload() +%{ + /* retrieve the private data from the root object (window) */ + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, PRIVATE_MAGIC); + window_private_t *priv_win = duk_get_pointer(ctx, -1); + duk_pop(ctx); + + if (priv_win->win != NULL) { + browser_window_reload(priv_win->win, false); + } else { + LOG("failed to get browser context"); + } + return 0; +%} + +method Location::assign() +%{ + /* retrieve the private data from the root object (window) */ + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, PRIVATE_MAGIC); + window_private_t *priv_win = duk_get_pointer(ctx, -1); + duk_pop(ctx); + + if (priv_win == NULL || priv_win->win == NULL) { + LOG("failed to get browser context"); + return 0; + } + + nsurl *joined; + duk_size_t slen; + const char *url = duk_safe_to_lstring(ctx, 0, &slen); + + nsurl_join(priv->url, url, &joined); + browser_window_navigate(priv_win->win, + joined, + NULL, + BW_NAVIGATE_HISTORY, + NULL, + NULL, + NULL); + nsurl_unref(joined); + return 0; +%} + +method Location::replace() +%{ + /* retrieve the private data from the root object (window) */ + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, PRIVATE_MAGIC); + window_private_t *priv_win = duk_get_pointer(ctx, -1); + duk_pop(ctx); + + if (priv_win == NULL || priv_win->win == NULL) { + LOG("failed to get browser context"); + return 0; + } + + nsurl *joined; + duk_size_t slen; + const char *url = duk_safe_to_lstring(ctx, 0, &slen); + + nsurl_join(priv->url, url, &joined); + browser_window_navigate(priv_win->win, + joined, + NULL, + BW_NAVIGATE_NONE, + NULL, + NULL, + NULL); + nsurl_unref(joined); + return 0; +%} + getter Location::href() %{ char *url_s = NULL; @@ -36,11 +113,60 @@ getter Location::href() duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} +setter Location::href() +%{ + /* retrieve the private data from the root object (window) */ + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, PRIVATE_MAGIC); + window_private_t *priv_win = duk_get_pointer(ctx, -1); + duk_pop(ctx); + + if (priv_win == NULL || priv_win->win == NULL) { + LOG("failed to get browser context"); + return 0; + } + + nsurl *joined; + duk_size_t slen; + const char *url = duk_safe_to_lstring(ctx, 0, &slen); + + nsurl_join(priv->url, url, &joined); + browser_window_navigate(priv_win->win, + joined, + NULL, + BW_NAVIGATE_HISTORY, + NULL, + NULL, + NULL); + nsurl_unref(joined); + return 0; +%} + +getter Location::origin() +%{ + char *url_s = NULL; + size_t url_l; + + nsurl_get(priv->url, NSURL_SCHEME | NSURL_HOST | NSURL_PORT, &url_s, &url_l); + + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ + duk_push_lstring(ctx, url_s, url_l); + + if (url_s != NULL) { + free(url_s); + } + + return 1; +%} getter Location::protocol() %{ @@ -48,13 +174,15 @@ getter Location::protocol() size_t url_l; nsurl_get(priv->url, NSURL_SCHEME, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -66,13 +194,15 @@ getter Location::username() size_t url_l; nsurl_get(priv->url, NSURL_USERNAME, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -84,13 +214,15 @@ getter Location::password() size_t url_l; nsurl_get(priv->url, NSURL_PASSWORD, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -102,13 +234,15 @@ getter Location::host() size_t url_l; nsurl_get(priv->url, NSURL_HOST, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -119,32 +253,37 @@ 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; - } + nsurl_get(priv->url, NSURL_HOST, &url_s, &url_l); + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + 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; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -156,13 +295,15 @@ getter Location::pathname() size_t url_l; nsurl_get(priv->url, NSURL_PATH, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -174,13 +315,15 @@ getter Location::search() size_t url_l; nsurl_get(priv->url, NSURL_QUERY, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} @@ -192,13 +335,15 @@ getter Location::hash() size_t url_l; nsurl_get(priv->url, NSURL_FRAGMENT, &url_s, &url_l); - if (url_s == NULL) { - return 0; - } + /* if url_s is NULL duk_push_lstring pushes an empty string + * which is correct for this API + */ duk_push_lstring(ctx, url_s, url_l); - free(url_s); + if (url_s != NULL) { + free(url_s); + } return 1; %} -- cgit v1.2.3