mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
EventTarget constructor
This commit is contained in:
@@ -35,6 +35,9 @@ pub const EventTarget = struct {
|
||||
pub const Self = parser.EventTarget;
|
||||
pub const Exception = DOMException;
|
||||
|
||||
// Extend libdom event target for pure zig struct.
|
||||
base: parser.EventTargetTBase = parser.EventTargetTBase{},
|
||||
|
||||
pub fn toInterface(e: *parser.Event, et: *parser.EventTarget, page: *Page) !Union {
|
||||
// libdom assumes that all event targets are libdom nodes. They are not.
|
||||
|
||||
@@ -63,6 +66,11 @@ pub const EventTarget = struct {
|
||||
|
||||
// JS funcs
|
||||
// --------
|
||||
pub fn constructor(page: *Page) !*parser.EventTarget {
|
||||
const et = try page.arena.create(EventTarget);
|
||||
return @ptrCast(&et.base);
|
||||
}
|
||||
|
||||
pub fn _addEventListener(
|
||||
self: *parser.EventTarget,
|
||||
typ: []const u8,
|
||||
@@ -128,6 +136,10 @@ test "Browser.DOM.EventTarget" {
|
||||
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
|
||||
defer runner.deinit();
|
||||
|
||||
try runner.testCases(&.{
|
||||
.{ "new EventTarget()", "[object EventTarget]" },
|
||||
}, .{});
|
||||
|
||||
try runner.testCases(&.{
|
||||
.{ "let content = document.getElementById('content')", "undefined" },
|
||||
.{ "let para = document.getElementById('para')", "undefined" },
|
||||
|
||||
@@ -2131,7 +2131,6 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
|
||||
const info = v8.FunctionCallbackInfo.initFromV8(raw_info);
|
||||
var caller = Caller(JsContext, State).init(info);
|
||||
defer caller.deinit();
|
||||
|
||||
// See comment above. We generateConstructor on all types
|
||||
// in order to create the FunctionTemplate, but there might
|
||||
// not be an actual "constructor" function. So if someone
|
||||
@@ -2139,6 +2138,7 @@ pub fn Env(comptime State: type, comptime WebApis: type) type {
|
||||
// a constructor function, we'll return an error.
|
||||
if (@hasDecl(Struct, "constructor") == false) {
|
||||
const iso = caller.isolate;
|
||||
log.warn(.js, "Illegal constructor call", .{ .name = @typeName(Struct) });
|
||||
const js_exception = iso.throwException(createException(iso, "Illegal Constructor"));
|
||||
info.getReturnValue().set(js_exception);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user