|
|
@@ -72,6 +72,12 @@ static int node_delete(js_State *state, void *p, const char *name);
|
|
|
static void push_element_url(js_State *state, unsigned int node,
|
|
|
const char *name);
|
|
|
static void rellist_supports(js_State *state);
|
|
|
+static void push_query_array(js_State *state, unsigned int root,
|
|
|
+ const char *selector);
|
|
|
+static void node_push_text(js_State *state, unsigned int node);
|
|
|
+static void url_resolve(const char *base, const char *rel,
|
|
|
+ char *out, unsigned int cap);
|
|
|
+static int hash_pending;
|
|
|
|
|
|
/* One wrapper per DOM node: the same element must have the same object
|
|
|
* identity on every access, or library caches (jQuery expando data, Sizzle)
|
|
|
@@ -571,7 +577,8 @@ static int dispatch_object(js_State *state, unsigned int target,
|
|
|
}
|
|
|
if (target != JS_TARGET_WINDOW)
|
|
|
path[np++] = JS_TARGET_DOCUMENT;
|
|
|
- if (np < (int)(sizeof(path) / sizeof(path[0])))
|
|
|
+ if (target != JS_TARGET_WINDOW &&
|
|
|
+ np < (int)(sizeof(path) / sizeof(path[0])))
|
|
|
path[np++] = JS_TARGET_WINDOW;
|
|
|
|
|
|
/* Capture from the outermost ancestor down to the parent. */
|
|
|
@@ -1926,6 +1933,22 @@ static int node_has(js_State *state, void *p, const char *name)
|
|
|
}
|
|
|
if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
|
|
|
if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
|
|
|
+ if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_SELECT) {
|
|
|
+ if (!strcmp(name, "options")) {
|
|
|
+ push_query_array(state, node, "option");
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ if (!strcmp(name, "multiple")) {
|
|
|
+ js_pushboolean(state, node_attr(node, "multiple") != 0);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /* An <option> without a value attribute has its text as value. */
|
|
|
+ if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_OPTION &&
|
|
|
+ !strcmp(name, "value") && !node_attr(node, "value")) {
|
|
|
+ node_push_text(state, node);
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
if (!strcmp(name, "value")) {
|
|
|
char buf[128];
|
|
|
|
|
|
@@ -2369,22 +2392,78 @@ static void location_get(js_State *state)
|
|
|
js_pushstring(state, current_url ? current_url : "");
|
|
|
}
|
|
|
|
|
|
-static void location_set(js_State *state)
|
|
|
+/* Length of a URL without its #fragment. */
|
|
|
+static unsigned int url_doc_len(const char *u)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ while (u[n] && u[n] != '#')
|
|
|
+ n++;
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+/* Navigate, or, when only the #fragment changes, stay on the page: the
|
|
|
+ * host updates the URL (location.href follows it) and a hashchange event is
|
|
|
+ * queued. Hash-routed apps (the Cloudron dashboard) navigate this way; a
|
|
|
+ * reload would restart the whole app. */
|
|
|
+static void location_go(const char *url)
|
|
|
{
|
|
|
- const char *url = js_tostring(state, 1);
|
|
|
+ char abs[1024];
|
|
|
+ unsigned int n;
|
|
|
|
|
|
+ if (!current_url || !url) {
|
|
|
+ if (host.navigate && url)
|
|
|
+ host.navigate(url);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ url_resolve(current_url, url, abs, sizeof(abs));
|
|
|
+ n = url_doc_len(abs);
|
|
|
+ if (host.set_hash && abs[n] == '#' && n == url_doc_len(current_url) &&
|
|
|
+ !strncmp(abs, current_url, n)) {
|
|
|
+ if (strcmp(abs, current_url) && host.set_hash(abs) == 0)
|
|
|
+ hash_pending = 1;
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (host.navigate)
|
|
|
host.navigate(url);
|
|
|
}
|
|
|
|
|
|
+static void location_set(js_State *state)
|
|
|
+{
|
|
|
+ location_go(js_tostring(state, 1));
|
|
|
+}
|
|
|
+
|
|
|
/* assign()/replace(): navigate (the history entry is not replaced yet). */
|
|
|
static void location_assign(js_State *state)
|
|
|
{
|
|
|
- if (host.navigate)
|
|
|
- host.navigate(js_tostring(state, 1));
|
|
|
+ location_go(js_tostring(state, 1));
|
|
|
js_pushundefined(state);
|
|
|
}
|
|
|
|
|
|
+static void location_hash_get(js_State *state)
|
|
|
+{
|
|
|
+ const char *h = current_url ? strchr(current_url, '#') : 0;
|
|
|
+
|
|
|
+ js_pushstring(state, h && h[1] ? h : "");
|
|
|
+}
|
|
|
+
|
|
|
+static void location_hash_set(js_State *state)
|
|
|
+{
|
|
|
+ const char *v = js_tostring(state, 1);
|
|
|
+ char url[1024];
|
|
|
+ unsigned int n;
|
|
|
+
|
|
|
+ if (!current_url)
|
|
|
+ return;
|
|
|
+ n = url_doc_len(current_url);
|
|
|
+ if (n + 2 >= sizeof(url))
|
|
|
+ return;
|
|
|
+ memcpy(url, current_url, n);
|
|
|
+ url[n] = '#';
|
|
|
+ snprintf(url + n + 1, sizeof(url) - n - 1, "%s", v[0] == '#' ? v + 1 : v);
|
|
|
+ location_go(url);
|
|
|
+}
|
|
|
+
|
|
|
static void location_reload(js_State *state)
|
|
|
{
|
|
|
if (host.navigate && current_url)
|
|
|
@@ -2493,10 +2572,12 @@ static void push_location(js_State *state)
|
|
|
p.path ? (int)p.path_len : 0);
|
|
|
js_setproperty(state, -2, "pathname");
|
|
|
LOC_FIELD(search);
|
|
|
- LOC_FIELD(hash);
|
|
|
LOC_FIELD(origin);
|
|
|
#undef LOC_FIELD
|
|
|
|
|
|
+ js_newcfunction(state, location_hash_get, "get hash", 0);
|
|
|
+ js_newcfunction(state, location_hash_set, "set hash", 1);
|
|
|
+ js_defaccessor(state, -3, "hash", 0);
|
|
|
js_newcfunction(state, location_reload, "reload", 0);
|
|
|
js_setproperty(state, -2, "reload");
|
|
|
js_newcfunction(state, location_assign, "assign", 1);
|
|
|
@@ -3196,6 +3277,11 @@ static void report_top(js_State *state)
|
|
|
js_pop(state, 1);
|
|
|
}
|
|
|
|
|
|
+void js_browser_hashchange(void)
|
|
|
+{
|
|
|
+ hash_pending = 1;
|
|
|
+}
|
|
|
+
|
|
|
void js_browser_tick(void)
|
|
|
{
|
|
|
unsigned int now;
|
|
|
@@ -3203,6 +3289,12 @@ void js_browser_tick(void)
|
|
|
|
|
|
if (!J)
|
|
|
return;
|
|
|
+ if (hash_pending) {
|
|
|
+ hash_pending = 0;
|
|
|
+ js_browser_event(JS_TARGET_WINDOW, "hashchange", 0, 0);
|
|
|
+ if (!J)
|
|
|
+ return;
|
|
|
+ }
|
|
|
now = u_ticks_ms();
|
|
|
for (i = 0; i < JS_TIMERS; i++) {
|
|
|
if (!timers[i].used ||
|