Skip to content

Commit 758f7de

Browse files
authored
Merge pull request #1155 from lightpanda-io/composition_event
add CompositionEvent
2 parents c52dce1 + c69adcb commit 758f7de

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

src/browser/dom/tree_walker.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ pub const TreeWalker = struct {
227227
continue;
228228
};
229229

230-
231230
if (!result.should_descend) {
232231
// This is an .accept node - return it
233232
self.current_node = result.node;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (C) 2023-2025 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.zig");
22+
23+
// https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent
24+
pub const CompositionEvent = struct {
25+
data: []const u8,
26+
proto: parser.Event,
27+
28+
pub const union_make_copy = true;
29+
pub const prototype = *parser.Event;
30+
31+
pub const ConstructorOptions = struct {
32+
data: []const u8 = "",
33+
};
34+
35+
pub fn constructor(event_type: []const u8, options_: ?ConstructorOptions) !CompositionEvent {
36+
const options: ConstructorOptions = options_ orelse .{};
37+
38+
const event = try parser.eventCreate();
39+
defer parser.eventDestroy(event);
40+
try parser.eventInit(event, event_type, .{});
41+
parser.eventSetInternalType(event, .composition_event);
42+
43+
return .{
44+
.proto = event.*,
45+
.data = options.data,
46+
};
47+
}
48+
49+
pub fn get_data(self: *const CompositionEvent) []const u8 {
50+
return self.data;
51+
}
52+
};
53+
54+
const testing = @import("../../testing.zig");
55+
test "Browser: Events.Composition" {
56+
try testing.htmlRunner("events/composition.html");
57+
}

src/browser/events/event.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const KeyboardEvent = @import("keyboard_event.zig").KeyboardEvent;
3737
const ErrorEvent = @import("../html/error_event.zig").ErrorEvent;
3838
const MessageEvent = @import("../dom/MessageChannel.zig").MessageEvent;
3939
const PopStateEvent = @import("../html/History.zig").PopStateEvent;
40+
const CompositionEvent = @import("composition_event.zig").CompositionEvent;
4041

4142
// Event interfaces
4243
pub const Interfaces = .{
@@ -48,6 +49,7 @@ pub const Interfaces = .{
4849
ErrorEvent,
4950
MessageEvent,
5051
PopStateEvent,
52+
CompositionEvent,
5153
};
5254

5355
pub const Union = generate.Union(Interfaces);
@@ -76,6 +78,7 @@ pub const Event = struct {
7678
.message_event => .{ .MessageEvent = @as(*MessageEvent, @ptrCast(evt)).* },
7779
.keyboard_event => .{ .KeyboardEvent = @as(*parser.KeyboardEvent, @ptrCast(evt)) },
7880
.pop_state => .{ .PopStateEvent = @as(*PopStateEvent, @ptrCast(evt)).* },
81+
.composition_event => .{ .CompositionEvent = (@as(*CompositionEvent, @fieldParentPtr("proto", evt))).* },
7982
};
8083
}
8184

src/browser/netsurf.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ pub const EventType = enum(u8) {
559559
message_event = 7,
560560
keyboard_event = 8,
561561
pop_state = 9,
562+
composition_event = 10,
562563
};
563564

564565
pub const MutationEvent = c.dom_mutation_event;

src/tests/events/composition.html

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<!DOCTYPE html>
2+
<script src="../testing.js"></script>
3+
4+
<script id=noNata>
5+
{
6+
let event = new CompositionEvent("test", {});
7+
testing.expectEqual(true, event instanceof CompositionEvent);
8+
testing.expectEqual(true, event instanceof Event);
9+
10+
testing.expectEqual("test", event.type);
11+
testing.expectEqual("", event.data);
12+
}
13+
</script>
14+
15+
<script id=withData>
16+
{
17+
let event = new CompositionEvent("test2", {data: "over 9000!"});
18+
testing.expectEqual("test2", event.type);
19+
testing.expectEqual("over 9000!", event.data);
20+
}
21+
</script>
22+
23+
<script id=dispatch>
24+
{
25+
let called = 0;
26+
document.addEventListener('CE', (e) => {
27+
testing.expectEqual('test-data', e.data);
28+
testing.expectEqual(true, e instanceof CompositionEvent);
29+
called += 1
30+
});
31+
32+
document.dispatchEvent(new CompositionEvent('CE', {data: 'test-data'}));
33+
testing.expectEqual(1, called);
34+
}
35+
</script>
36+

0 commit comments

Comments
 (0)