From 12111d4cdfecb5ff316501f67aa123696ade8dab Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 19 Jun 2024 13:48:39 +0200 Subject: [PATCH] dom: first draft for MutationObserver --- src/dom/dom.zig | 2 + src/dom/mutation_observer.zig | 77 +++++++++++++++++++++++++++++++++++ src/run_tests.zig | 2 + 3 files changed, 81 insertions(+) create mode 100644 src/dom/mutation_observer.zig diff --git a/src/dom/dom.zig b/src/dom/dom.zig index 168ae81a..1ec36379 100644 --- a/src/dom/dom.zig +++ b/src/dom/dom.zig @@ -25,6 +25,7 @@ const NamedNodeMap = @import("namednodemap.zig").NamedNodeMap; const DOMTokenList = @import("token_list.zig").DOMTokenList; const NodeList = @import("nodelist.zig").NodeList; const Nod = @import("node.zig"); +const MutationObserver = @import("mutation_observer.zig"); pub const Interfaces = generate.Tuple(.{ DOMException, @@ -35,4 +36,5 @@ pub const Interfaces = generate.Tuple(.{ NodeList, Nod.Node, Nod.Interfaces, + MutationObserver.Interfaces, }); diff --git a/src/dom/mutation_observer.zig b/src/dom/mutation_observer.zig new file mode 100644 index 00000000..ee378379 --- /dev/null +++ b/src/dom/mutation_observer.zig @@ -0,0 +1,77 @@ +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) +// +// Francis Bouvier +// Pierre Tachoire +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +const std = @import("std"); + +const parser = @import("netsurf"); + +const jsruntime = @import("jsruntime"); +const Callback = jsruntime.Callback; +const Case = jsruntime.test_utils.Case; +const checkCases = jsruntime.test_utils.checkCases; + +const generate = @import("../generate.zig"); + +const NodeList = @import("nodelist.zig").NodeList; + +pub const Interfaces = generate.Tuple(.{ + MutationObserver, +}); + +// WEB IDL https://dom.spec.whatwg.org/#interface-mutationobserver +pub const MutationObserver = struct { + cbk: Callback, + + pub const mem_guarantied = true; + + pub const MutationObserverInit = struct { + childList: bool = false, + attributes: bool = false, + characterData: bool = false, + subtree: bool = false, + attributeOldValue: bool = false, + characterDataOldValue: bool = false, + // TODO + // attributeFilter: [][]const u8, + }; + + pub fn constructor(cbk: Callback) !MutationObserver { + return MutationObserver{ + .cbk = cbk, + }; + } + + pub fn _observe( + _: *MutationObserver, + _: *parser.Node, + _: ?MutationObserverInit, + ) !void {} + + // TODO + pub fn _disconnect(_: *MutationObserver) !void {} +}; + +pub fn testExecFn( + _: std.mem.Allocator, + js_env: *jsruntime.Env, +) anyerror!void { + var constructor = [_]Case{ + .{ .src = "new MutationObserver(() => {}).observe(document, { childList: true });", .ex = "undefined" }, + }; + try checkCases(js_env, &constructor); +} diff --git a/src/run_tests.zig b/src/run_tests.zig index bf906733..94d46900 100644 --- a/src/run_tests.zig +++ b/src/run_tests.zig @@ -55,6 +55,7 @@ const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn; const StorageTestExecFn = storage.testExecFn; const URLTestExecFn = url.testExecFn; const HTMLElementTestExecFn = @import("html/elements.zig").testExecFn; +const MutationObserverTestExecFn = @import("dom/mutation_observer.zig").testExecFn; pub const Types = jsruntime.reflect(apiweb.Interfaces); pub const UserContext = @import("user_context.zig").UserContext; @@ -133,6 +134,7 @@ fn testsAllExecFn( StorageTestExecFn, URLTestExecFn, HTMLElementTestExecFn, + MutationObserverTestExecFn, }; inline for (testFns) |testFn| {