Skip to content

Commit 12111d4

Browse files
committed
dom: first draft for MutationObserver
1 parent 00e8f13 commit 12111d4

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/dom/dom.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const NamedNodeMap = @import("namednodemap.zig").NamedNodeMap;
2525
const DOMTokenList = @import("token_list.zig").DOMTokenList;
2626
const NodeList = @import("nodelist.zig").NodeList;
2727
const Nod = @import("node.zig");
28+
const MutationObserver = @import("mutation_observer.zig");
2829

2930
pub const Interfaces = generate.Tuple(.{
3031
DOMException,
@@ -35,4 +36,5 @@ pub const Interfaces = generate.Tuple(.{
3536
NodeList,
3637
Nod.Node,
3738
Nod.Interfaces,
39+
MutationObserver.Interfaces,
3840
});

src/dom/mutation_observer.zig

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <francis@lightpanda.io>
4+
// Pierre Tachoire <pierre@lightpanda.io>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
21+
const parser = @import("netsurf");
22+
23+
const jsruntime = @import("jsruntime");
24+
const Callback = jsruntime.Callback;
25+
const Case = jsruntime.test_utils.Case;
26+
const checkCases = jsruntime.test_utils.checkCases;
27+
28+
const generate = @import("../generate.zig");
29+
30+
const NodeList = @import("nodelist.zig").NodeList;
31+
32+
pub const Interfaces = generate.Tuple(.{
33+
MutationObserver,
34+
});
35+
36+
// WEB IDL https://dom.spec.whatwg.org/#interface-mutationobserver
37+
pub const MutationObserver = struct {
38+
cbk: Callback,
39+
40+
pub const mem_guarantied = true;
41+
42+
pub const MutationObserverInit = struct {
43+
childList: bool = false,
44+
attributes: bool = false,
45+
characterData: bool = false,
46+
subtree: bool = false,
47+
attributeOldValue: bool = false,
48+
characterDataOldValue: bool = false,
49+
// TODO
50+
// attributeFilter: [][]const u8,
51+
};
52+
53+
pub fn constructor(cbk: Callback) !MutationObserver {
54+
return MutationObserver{
55+
.cbk = cbk,
56+
};
57+
}
58+
59+
pub fn _observe(
60+
_: *MutationObserver,
61+
_: *parser.Node,
62+
_: ?MutationObserverInit,
63+
) !void {}
64+
65+
// TODO
66+
pub fn _disconnect(_: *MutationObserver) !void {}
67+
};
68+
69+
pub fn testExecFn(
70+
_: std.mem.Allocator,
71+
js_env: *jsruntime.Env,
72+
) anyerror!void {
73+
var constructor = [_]Case{
74+
.{ .src = "new MutationObserver(() => {}).observe(document, { childList: true });", .ex = "undefined" },
75+
};
76+
try checkCases(js_env, &constructor);
77+
}

src/run_tests.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
5555
const StorageTestExecFn = storage.testExecFn;
5656
const URLTestExecFn = url.testExecFn;
5757
const HTMLElementTestExecFn = @import("html/elements.zig").testExecFn;
58+
const MutationObserverTestExecFn = @import("dom/mutation_observer.zig").testExecFn;
5859

5960
pub const Types = jsruntime.reflect(apiweb.Interfaces);
6061
pub const UserContext = @import("user_context.zig").UserContext;
@@ -133,6 +134,7 @@ fn testsAllExecFn(
133134
StorageTestExecFn,
134135
URLTestExecFn,
135136
HTMLElementTestExecFn,
137+
MutationObserverTestExecFn,
136138
};
137139

138140
inline for (testFns) |testFn| {

0 commit comments

Comments
 (0)