Skip to content

The headless browser for AI agents and web scraping

Expose window.event while dispatching events

Lucas Brito414e649a8c2b
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.js
index c77d5201..af3b08d2 100644
--- a/crates/obscura-js/js/bootstrap.js
+++ b/crates/obscura-js/js/bootstrap.js
@@ -2068,7 +2068,35 @@ function _eventTargetDispatch(target, event) {
}
}
+// HTML Standard "current event": `window.event` exposes the event being
+// dispatched, and is restored once the invocation returns so nested dispatches
+// and code outside dispatch see the right value. Legacy handlers rely on it,
+// e.g. `onclick="go()"` with `function go() { event.preventDefault(); }`.
+let _currentWindowEvent;
+Object.defineProperty(globalThis, "event", {
+ get() { return _currentWindowEvent; },
+ // [Replaceable]: assigning replaces the accessor with a plain data property.
+ set(value) {
+ Object.defineProperty(globalThis, "event", {
+ value, writable: true, enumerable: true, configurable: true,
+ });
+ },
+ enumerable: true,
+ configurable: true,
+});
+
function _domEventInvoke(target, event, capture, phase) {
+ const previousEvent = _currentWindowEvent;
+ // The spec leaves the current event untouched for targets in a shadow tree.
+ if (!event._inShadowTree) _currentWindowEvent = event;
+ try {
+ _domEventInvokeHandlers(target, event, capture, phase);
+ } finally {
+ _currentWindowEvent = previousEvent;
+ }
+}
+
+function _domEventInvokeHandlers(target, event, capture, phase) {
event.currentTarget = target;
event.eventPhase = phase;
@@ -2130,6 +2158,10 @@ function _domEventDispatch(target, event) {
path.push(ancestor);
ancestor = ancestor.parentNode || null;
}
+ // The parentNode-based path never crosses a shadow boundary, so the tree
+ // root (last path entry, or the target itself) tells which tree every node
+ // on the path belongs to.
+ event._inShadowTree = (path.length ? path[path.length - 1] : target) instanceof ShadowRoot;
// Detached nodes have no event parent. Connected paths reach Window through
// Document, except for load, whose Document parent is null by specification.
const reachesDocument = target === document || path[path.length - 1] === document;
diff --git a/crates/obscura-js/tests/window_event.rs b/crates/obscura-js/tests/window_event.rs
new file mode 100644
index 00000000..c33eb458
--- /dev/null
+++ b/crates/obscura-js/tests/window_event.rs
@@ -0,0 +1,120 @@
+use obscura_js::runtime::ObscuraJsRuntime;
+use serde_json::json;
+
+fn runtime() -> ObscuraJsRuntime {
+ let mut rt = ObscuraJsRuntime::new();
+ rt.set_dom(obscura_dom::parse_html(
+ r#"<body><button id="b" onclick="probe()">x</button></body>"#,
+ ));
+ rt.run_page_init();
+ rt
+}
+
+#[test]
+fn global_event_is_visible_to_functions_called_from_inline_handlers_and_listeners() {
+ let mut rt = runtime();
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const out = {};
+ globalThis.probe = function () {
+ out.free = [typeof event, event.type, event === window.event];
+ };
+ const b = document.getElementById('b');
+ b.addEventListener('click', e => { out.listener = window.event === e; });
+ b.click();
+ out.after = typeof window.event;
+ return out;
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(
+ result,
+ json!({"free": ["object", "click", true], "listener": true, "after": "undefined"})
+ );
+}
+
+#[test]
+fn nested_dispatch_restores_the_outer_event() {
+ let mut rt = runtime();
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const b = document.getElementById('b'), seen = [];
+ b.addEventListener('ping', () => {
+ seen.push(window.event.type);
+ b.dispatchEvent(new Event('pong'));
+ seen.push(window.event.type);
+ });
+ b.addEventListener('pong', () => seen.push(window.event.type));
+ b.dispatchEvent(new Event('ping'));
+ seen.push(typeof window.event);
+ return seen;
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(result, json!(["ping", "pong", "ping", "undefined"]));
+}
+
+#[test]
+fn event_is_restored_when_a_listener_throws() {
+ let mut rt = runtime();
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const b = document.getElementById('b');
+ let during;
+ b.addEventListener('boom', () => {
+ during = typeof window.event;
+ throw new Error('listener failure');
+ });
+ b.dispatchEvent(new Event('boom'));
+ return [during, typeof window.event];
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(result, json!(["object", "undefined"]));
+}
+
+#[test]
+fn window_event_is_replaceable() {
+ let mut rt = runtime();
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const b = document.getElementById('b');
+ let during;
+ b.addEventListener('x', () => { during = [typeof window.event, window.event === 7]; });
+ b.dispatchEvent(new Event('x'));
+ const before = during;
+ window.event = 7;
+ b.dispatchEvent(new Event('x'));
+ return [before, during, window.event];
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(result, json!([["object", false], ["number", true], 7]));
+}
+
+#[test]
+fn event_is_not_exposed_to_listeners_inside_a_shadow_tree() {
+ let mut rt = runtime();
+ let result = rt
+ .evaluate(
+ r#"(() => {
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const root = host.attachShadow({ mode: 'open' });
+ const inner = document.createElement('span');
+ root.appendChild(inner);
+ const seen = {};
+ inner.addEventListener('x', () => { seen.shadow = typeof window.event; });
+ host.addEventListener('x', () => { seen.light = typeof window.event; });
+ inner.dispatchEvent(new Event('x'));
+ host.dispatchEvent(new Event('x'));
+ return seen;
+ })()"#,
+ )
+ .unwrap();
+ assert_eq!(result, json!({"shadow": "undefined", "light": "object"}));
+}