Merge pull request #214 from lightpanda-io/event-callback

Event callback
This commit is contained in:
Pierre Tachoire
2024-05-02 15:30:08 +02:00
committed by GitHub
3 changed files with 18 additions and 1 deletions

View File

@@ -622,6 +622,11 @@ pub fn eventTargetAddEventListener(
const cbk_ptr = try alloc.create(Callback);
cbk_ptr.* = cbk;
// When a function is used as an event handler, its this parameter is bound
// to the DOM element on which the listener is placed.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this#this_in_dom_event_handlers
try cbk_ptr.setThisArg(et);
const ctx = @as(*anyopaque, @ptrCast(cbk_ptr));
var listener: ?*EventListener = undefined;
const errLst = c.dom_event_listener_create(event_handler, ctx, &listener);

View File

@@ -837,4 +837,16 @@ pub fn testExecFn(
.{ .src = "req4.responseText.length > 64", .ex = "true" },
};
try checkCases(js_env, &post);
var cbk = [_]Case{
.{ .src = "const req5 = new XMLHttpRequest()", .ex = "undefined" },
.{ .src = "req5.open('GET', 'http://httpbin.io/json')", .ex = "undefined" },
.{ .src = "var status = 0; req5.onload = function () { status = this.status };", .ex = "function () { status = this.status }" },
.{ .src = "req5.send()", .ex = "undefined" },
// Each case executed waits for all loop callaback calls.
// So the url has been retrieved.
.{ .src = "status", .ex = "200" },
};
try checkCases(js_env, &cbk);
}