|
|
@@ -448,6 +448,79 @@ static void listener_phase(js_State *state, unsigned int target,
|
|
|
|
|
|
/* supplied_idx: 0 creates a fresh object, otherwise it is the argument
|
|
|
* index of an Event supplied by JavaScript. Leaves a boolean result. */
|
|
|
+static void inline_attr_decode(const char *s, char *d, unsigned int cap)
|
|
|
+{
|
|
|
+ unsigned int i = 0, n = 0;
|
|
|
+
|
|
|
+ while (s && s[i] && n + 1 < cap) {
|
|
|
+ if (s[i] == '&') {
|
|
|
+ if (!strncmp(s + i, """, 6)) {
|
|
|
+ d[n++] = '"'; i += 6; continue;
|
|
|
+ }
|
|
|
+ if (!strncmp(s + i, "'", 5)) {
|
|
|
+ d[n++] = '\''; i += 5; continue;
|
|
|
+ }
|
|
|
+ if (!strncmp(s + i, "&", 5)) {
|
|
|
+ d[n++] = '&'; i += 5; continue;
|
|
|
+ }
|
|
|
+ if (!strncmp(s + i, "<", 4)) {
|
|
|
+ d[n++] = '<'; i += 4; continue;
|
|
|
+ }
|
|
|
+ if (!strncmp(s + i, ">", 4)) {
|
|
|
+ d[n++] = '>'; i += 4; continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ d[n++] = s[i++];
|
|
|
+ }
|
|
|
+ d[n] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+/* Inline on<event> attributes run at the target and while bubbling, after
|
|
|
+ * the registered listeners for that node. The handler gets the event and
|
|
|
+ * the element as `this`; a false return prevents the default action. */
|
|
|
+static void run_inline_handler(js_State *state, unsigned int node,
|
|
|
+ const char *type, int slot, int *prevented)
|
|
|
+{
|
|
|
+ char name[40], body[512], src[640];
|
|
|
+ const char *attr;
|
|
|
+ int truthy;
|
|
|
+
|
|
|
+ if (!D || node >= D->nnode || !events[slot].objref[0])
|
|
|
+ return;
|
|
|
+ snprintf(name, sizeof(name), "on%s", type);
|
|
|
+ attr = br_find_attr(D, node, name);
|
|
|
+ if (!attr || !attr[0])
|
|
|
+ return;
|
|
|
+ inline_attr_decode(attr, body, sizeof(body));
|
|
|
+ snprintf(src, sizeof(src), "(function(event){ %s\n})", body);
|
|
|
+ if (js_try(state)) {
|
|
|
+ js_pop(state, 1);
|
|
|
+ js_endtry(state);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ js_loadstring(state, "[inline]", src);
|
|
|
+ /* The script evaluates the function expression; run it first, then
|
|
|
+ * call the resulting function with the element as `this`. */
|
|
|
+ js_pushundefined(state);
|
|
|
+ if (js_pcall(state, 0) || !js_iscallable(state, -1)) {
|
|
|
+ js_pop(state, 1);
|
|
|
+ js_endtry(state);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ push_node(state, node + 1);
|
|
|
+ js_getregistry(state, events[slot].objref);
|
|
|
+ if (js_pcall(state, 1)) {
|
|
|
+ js_pop(state, 1);
|
|
|
+ js_endtry(state);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ truthy = js_toboolean(state, -1);
|
|
|
+ js_pop(state, 1);
|
|
|
+ js_endtry(state);
|
|
|
+ if (!truthy && prevented)
|
|
|
+ *prevented = 1;
|
|
|
+}
|
|
|
+
|
|
|
static int dispatch_object(js_State *state, unsigned int target,
|
|
|
int supplied_idx, const char *fallback,
|
|
|
int def_bubbles, int def_cancelable)
|
|
|
@@ -502,13 +575,23 @@ static int dispatch_object(js_State *state, unsigned int target,
|
|
|
listener_phase(state, path[i], type, 1, slot);
|
|
|
}
|
|
|
/* Target phase: capture and bubble listeners in registration order. */
|
|
|
- if (!events[slot].stopped)
|
|
|
+ if (!events[slot].stopped) {
|
|
|
listener_phase(state, path[0], type, 0, slot);
|
|
|
+ if (!events[slot].stopped)
|
|
|
+ run_inline_handler(state, path[0], type, slot,
|
|
|
+ &events[slot].prevented);
|
|
|
+ }
|
|
|
/* Bubble only for events that ask for it; stopPropagation stops the
|
|
|
* remaining ancestors. */
|
|
|
if (ev_get_bool(state, slot, "bubbles", def_bubbles)) {
|
|
|
- for (i = 1; i < np && !events[slot].stopped; i++)
|
|
|
+ for (i = 1; i < np && !events[slot].stopped; i++) {
|
|
|
listener_phase(state, path[i], type, 2, slot);
|
|
|
+ if (!events[slot].stopped &&
|
|
|
+ path[i] != JS_TARGET_WINDOW &&
|
|
|
+ path[i] != JS_TARGET_DOCUMENT)
|
|
|
+ run_inline_handler(state, path[i], type, slot,
|
|
|
+ &events[slot].prevented);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
prevented = events[slot].prevented;
|