summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2015-09-20 10:35:32 +0100
committerVincent Sanders <vince@kyllikki.org>2015-09-20 10:35:32 +0100
commit46945f636221ccf0751f3f0e2e78c6e6d33cab7f (patch)
tree2b91ad943b29f66a4e08822f93dddefd55641421
parent63dba3a0618a2116ec395937b04f65740fe529f3 (diff)
downloadnetsurf-46945f636221ccf0751f3f0e2e78c6e6d33cab7f.tar.gz
netsurf-46945f636221ccf0751f3f0e2e78c6e6d33cab7f.tar.bz2
Improve location implementation to be more complete
-rw-r--r--javascript/duktape/Location.bnd223
-rw-r--r--test/js/index.html22
-rw-r--r--test/js/location-assign.html13
-rw-r--r--test/js/location-href.html13
-rw-r--r--test/js/location-replace.html13
5 files changed, 237 insertions, 47 deletions
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;
%}
diff --git a/test/js/index.html b/test/js/index.html
index 06e006ac5..38a59a6be 100644
--- a/test/js/index.html
+++ b/test/js/index.html
@@ -12,14 +12,6 @@
<li><a href="core.infinite.html">Infinite loop</a></li>
</ul>
-
-<h2>Window</h2>
-<ul>
-<li><a href="window.lately.html">location</a></li>
-<li><a href="window-enumerate.html">enumerate</a></li>
-</ul>
-
-
<h2>Document write</h2>
<ul>
<li><a href="inline-doc-write-simple.html">Simple document write</a></li>
@@ -31,6 +23,20 @@
<li><a href="inline-innerhtml.html">Inline script innerHtml test</a></li>
</ul>
+<h2>Window</h2>
+<ul>
+<li><a href="window.lately.html">lately</a></li>
+<li><a href="window-enumerate.html">enumerate</a></li>
+</ul>
+
+<h2>Location</h2>
+<ul>
+<li><a href="location-enumerate.html">Enumerate members</a></li>
+<li><a href="location-assign.html">assign</a> should navigate to enumeration with page in history</li>
+<li><a href="location-replace.html">replace</a> should navigate to enumeration without page in history.</li>
+<li><a href="location-href.html">href</a> should navigate to enumeration with page in history</li>
+</ul>
+
<h2>DOM tests</h2>
diff --git a/test/js/location-assign.html b/test/js/location-assign.html
new file mode 100644
index 000000000..5ed808cc5
--- /dev/null
+++ b/test/js/location-assign.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+<title>location interface assign</title>
+<link rel="stylesheet" type="text/css" href="tst.css">
+</head>
+<body>
+<h1>location interface assign</h1>
+
+<script>
+location.assign("location-enumerate.html");
+</script>
+</body>
+</html>
diff --git a/test/js/location-href.html b/test/js/location-href.html
new file mode 100644
index 000000000..28a9e3c02
--- /dev/null
+++ b/test/js/location-href.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+<title>location interface href</title>
+<link rel="stylesheet" type="text/css" href="tst.css">
+</head>
+<body>
+<h1>location interface href</h1>
+
+<script>
+location.href = "location-enumerate.html";
+</script>
+</body>
+</html>
diff --git a/test/js/location-replace.html b/test/js/location-replace.html
new file mode 100644
index 000000000..047cfd467
--- /dev/null
+++ b/test/js/location-replace.html
@@ -0,0 +1,13 @@
+<html>
+<head>
+<title>location interface replace</title>
+<link rel="stylesheet" type="text/css" href="tst.css">
+</head>
+<body>
+<h1>location interface replace</h1>
+
+<script>
+location.replace("location-enumerate.html");
+</script>
+</body>
+</html>