Explorar el Código

Add browser CustomEvent and MouseEvent constructors

The constructors build the same event objects as Event and carry detail, client/screen coordinates, buttons and modifier keys through dispatchEvent. Adds ladder 054.
Johannes Zellner hace 1 semana
padre
commit
930bfdcc59

+ 48 - 0
js_browser.c

@@ -700,6 +700,50 @@ static void doc_createevent(js_State *state)
 	event_build_object(state, type, 0, 0, 1);
 }
 
+static void custom_event_call(js_State *state)
+{
+	event_call(state);
+	if (js_isobject(state, 2))
+		js_getproperty(state, 2, "detail");
+	else
+		js_pushnull(state);
+	js_setproperty(state, -2, "detail");
+}
+
+static void mouse_event_call(js_State *state)
+{
+	static const struct {
+		const char *name;
+		int is_number;
+	} props[] = {
+		{ "clientX", 1 }, { "clientY", 1 }, { "screenX", 1 },
+		{ "screenY", 1 }, { "button", 1 }, { "buttons", 1 },
+		{ "ctrlKey", 0 }, { "shiftKey", 0 }, { "altKey", 0 },
+		{ "metaKey", 0 }
+	};
+	unsigned int i;
+
+	event_call(state);
+	if (!js_isobject(state, 2))
+		return;
+	for (i = 0; i < sizeof(props) / sizeof(props[0]); i++) {
+		js_getproperty(state, 2, props[i].name);
+		if (props[i].is_number) {
+			double v = js_isnumber(state, -1) ?
+				js_tonumber(state, -1) : 0;
+
+			js_pop(state, 1);
+			js_pushnumber(state, v);
+		} else {
+			int v = js_toboolean(state, -1);
+
+			js_pop(state, 1);
+			js_pushboolean(state, v);
+		}
+		js_setproperty(state, -2, props[i].name);
+	}
+}
+
 int js_browser_event(unsigned int node, const char *type, int bubbles,
 		     int cancelable)
 {
@@ -2697,6 +2741,10 @@ static void install_globals(js_State *state)
 
 	js_newcfunction(state, event_call, "Event", 1);
 	js_defglobal(state, "Event", JS_DONTENUM);
+	js_newcfunction(state, custom_event_call, "CustomEvent", 1);
+	js_defglobal(state, "CustomEvent", JS_DONTENUM);
+	js_newcfunction(state, mouse_event_call, "MouseEvent", 1);
+	js_defglobal(state, "MouseEvent", JS_DONTENUM);
 
 	push_location(state);
 	js_defglobal(state, "location", JS_DONTENUM);

+ 60 - 0
testdata/browser/compat/054-event-constructors.html

@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+<meta charset="utf-8">
+<title>054 CustomEvent and MouseEvent</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>054 &mdash; CustomEvent and MouseEvent</h1>
+<p>The constructors must carry <code>detail</code> and the mouse fields into
+   <code>dispatchEvent</code>, and <code>bubbles</code> must reach the parent.</p>
+<div id="parent"><button id="child">event target</button></div>
+<div id="result">checking...</div>
+<script>
+setTimeout(function () {
+  var parent = document.getElementById('parent');
+  var child = document.getElementById('child');
+  var got = [];
+
+  parent.addEventListener('custom', function (e) {
+    got.push('parent:' + e.detail);
+  });
+  child.addEventListener('custom', function (e) {
+    got.push('child:' + e.detail);
+  });
+  child.dispatchEvent(new CustomEvent('custom', {
+    detail: 42, bubbles: true
+  }));
+
+  child.addEventListener('mouse', function (e) {
+    got.push('mouse:' + e.clientX + ',' + e.clientY + ',' + e.button);
+  });
+  child.dispatchEvent(new MouseEvent('mouse', {
+    bubbles: true, clientX: 12, clientY: 34, button: 2
+  }));
+
+  child.addEventListener('cancel', function (e) {
+    e.preventDefault();
+  });
+  var notCancelled = child.dispatchEvent(new CustomEvent('cancel', {
+    cancelable: true
+  }));
+
+  var ok = got.join('|') === 'child:42|parent:42|mouse:12,34,2' &&
+           notCancelled === false;
+  var r = document.getElementById('result');
+  if (r) {
+    r.style.background = ok ? '#c8f0c8' : '#ffc8c8';
+    r.textContent = (ok ? 'EVENTS-OK ' : 'EVENTS-FAIL ') +
+                    got.join('|') + ' 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-053 and 030a pass.  036a needs the POST echo server:
+Status: 010-054 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

@@ -76,6 +76,7 @@ a { color: #0645ad; }
 <tr><td>051</td><td><a href="051-list-decoration.html">list styles and text decoration</a></td><td>disc/circle/square/alpha/roman markers, line-through and overline</td><td class="pass">PASS</td></tr>
 <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>
 </table>
 </body>
 </html>