mutation: very partial childList implementation

This commit is contained in:
Pierre Tachoire
2024-06-21 17:56:28 +02:00
parent c9cfb1ecba
commit d8fe029f80
2 changed files with 42 additions and 2 deletions

View File

@@ -284,6 +284,9 @@ const EventHandler = struct {
const muevt = parser.eventToMutationEvent(evt.?);
// TODO get the allocator by another way?
const alloc = data.cbk.nat_ctx.alloc;
if (std.mem.eql(u8, t, "DOMAttrModified")) {
mrs.first = .{
.type = "attributes",
@@ -305,12 +308,41 @@ const EventHandler = struct {
if (o.options.characterDataOldValue) {
mrs.first.?.oldValue = parser.mutationEventPrevValue(muevt) catch null;
}
} else if (std.mem.eql(u8, t, "DOMNodeInserted")) {
mrs.first = .{
.type = "childList",
.target = o.node,
.addedNodes = NodeList.init(),
.removedNodes = NodeList.init(),
};
const rn = parser.mutationEventRelatedNode(muevt) catch null;
if (rn) |n| {
mrs.first.?.addedNodes.append(alloc, n) catch |e| {
log.err("mutation event handler error: {any}", .{e});
return;
};
}
} else if (std.mem.eql(u8, t, "DOMNodeRemoved")) {
mrs.first = .{
.type = "childList",
.target = o.node,
.addedNodes = NodeList.init(),
.removedNodes = NodeList.init(),
};
const rn = parser.mutationEventRelatedNode(muevt) catch null;
if (rn) |n| {
mrs.first.?.removedNodes.append(alloc, n) catch |e| {
log.err("mutation event handler error: {any}", .{e});
return;
};
}
} else {
return;
}
// TODO get the allocator by another way?
var res = CallbackResult.init(data.cbk.nat_ctx.alloc);
var res = CallbackResult.init(alloc);
defer res.deinit();
// TODO pass MutationRecords and MutationObserver

View File

@@ -543,6 +543,14 @@ pub fn mutationEventPrevValue(evt: *MutationEvent) !?[]const u8 {
return strToData(s.?);
}
pub fn mutationEventRelatedNode(evt: *MutationEvent) !?*Node {
var n: NodeExternal = undefined;
const err = c._dom_mutation_event_get_related_node(evt, &n);
try DOMErr(err);
if (n == null) return null;
return @as(*Node, @ptrCast(n));
}
// EventListener
pub const EventListener = c.dom_event_listener;
const EventListenerEntry = c.listener_entry;