ソースを参照

Add browser inline event attributes

on<event> attributes run after the registered listeners at the target and while bubbling, receive the event object, use the element as this, decode HTML entities and honour a false return as preventDefault. Adds ladder 055.
Johannes Zellner 1 週間 前
親
コミット
e664de18ae

+ 85 - 2
js_browser.c

@@ -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, "&quot;", 6)) {
+				d[n++] = '"'; i += 6; continue;
+			}
+			if (!strncmp(s + i, "&#39;", 5)) {
+				d[n++] = '\''; i += 5; continue;
+			}
+			if (!strncmp(s + i, "&amp;", 5)) {
+				d[n++] = '&'; i += 5; continue;
+			}
+			if (!strncmp(s + i, "&lt;", 4)) {
+				d[n++] = '<'; i += 4; continue;
+			}
+			if (!strncmp(s + i, "&gt;", 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;

+ 59 - 0
testdata/browser/compat/055-inline-events.html

@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>055 inline event attributes</title>
+<style>
+  body { background: #ffffff; color: #202020; font-family: sans-serif;
+         margin: 12px; font-size: 18px; }
+  #result { background: #ffc8c8; color: #000000; padding: 8px;
+            margin-top: 12px; }
+</style>
+</head>
+<body>
+<h1>055 &mdash; inline event attributes</h1>
+<p><code>onclick</code> must run with the element as <code>this</code>, bubble
+   to an ancestor handler, and <code>return false</code> must prevent the
+   default action.</p>
+
+<button id="button" onclick="this.setAttribute(&quot;data-x&quot;, &quot;1&quot;); this.textContent = 'clicked'; window.__inline = (window.__inline || 0) + 1;">press me</button>
+
+<div id="parent" onclick="window.__bubbled = (window.__bubbled || 0) + 1;">
+  <span id="child">bubble source</span>
+</div>
+
+<a id="cancel" href="#somewhere" onclick="return false;">cancel link</a>
+
+<div id="result">checking...</div>
+<script>
+setTimeout(function () {
+  var b = document.getElementById('button');
+  var p = document.getElementById('parent');
+  var c = document.getElementById('child');
+  var a = document.getElementById('cancel');
+
+  b.dispatchEvent(new MouseEvent('click', { bubbles: true }));
+  c.dispatchEvent(new MouseEvent('click', { bubbles: true }));
+  var notCancelled = a.dispatchEvent(new MouseEvent('click', {
+    bubbles: true, cancelable: true
+  }));
+
+  var ok = b.textContent === 'clicked' &&
+           b.getAttribute('data-x') === '1' &&
+           window.__inline === 1 &&
+           window.__bubbled === 1 &&
+           notCancelled === false;
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'INLINE-OK ' : 'INLINE-FAIL ') +
+                    'text=' + b.textContent +
+                    ' x=' + b.getAttribute('data-x') +
+                    ' inline=' + window.__inline +
+                    ' bubble=' + window.__bubbled +
+                    ' dispatch=' + notCancelled;
+  }
+}, 300);
+</script>
+</body>
+</html>

+ 1 - 1
testdata/browser/compat/README

@@ -17,7 +17,7 @@ Serve this directory from the host and open it on the board:
   browser http://<host>:8000/            # board
   browser http://10.0.2.2:8000/         # QEMU with -netdev user
 
-Status: 010-054 and 030a pass.  036a needs the POST echo server:
+Status: 010-055 and 030a pass.  036a needs the POST echo server:
 
   python3 testdata/browser/compat/post_server.py
   browser /floppy1/036a.htm      # action is http://10.0.2.2:8765/echo

+ 1 - 0
testdata/browser/compat/index.html

@@ -77,6 +77,7 @@ a { color: #0645ad; }
 <tr><td>052</td><td><a href="052-border-styles.html">border styles</a></td><td>solid, dashed, dotted and per-side borders</td><td class="pass">PASS</td></tr>
 <tr><td>053</td><td><a href="053-background-shorthand.html">background shorthand</a></td><td>position before the slash and size after it</td><td class="pass">PASS</td></tr>
 <tr><td>054</td><td><a href="054-event-constructors.html">event constructors</a></td><td>CustomEvent detail/bubbles, MouseEvent fields, preventDefault</td><td class="pass">PASS</td></tr>
+<tr><td>055</td><td><a href="055-inline-events.html">inline events</a></td><td>onclick with this, bubbling, return false and entity-decoded attributes</td><td class="pass">PASS</td></tr>
 </table>
 </body>
 </html>