Skip to content

Commit 947d01a

Browse files
krichprollschsjorsdonkers
authored andcommitted
range starts and ends with the global document by default
1 parent 81fb71b commit 947d01a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/browser/dom/document.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,8 @@ pub const Document = struct {
292292
state.active_element = @ptrCast(e);
293293
}
294294

295-
pub fn _createRange(_: *parser.Document) Range {
296-
return Range.constructor();
295+
pub fn _createRange(_: *parser.Document, page: *Page) Range {
296+
return Range.constructor(page);
297297
}
298298
};
299299

src/browser/dom/range.zig

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const std = @import("std");
2121
const parser = @import("../netsurf.zig");
2222
const Page = @import("../page.zig").Page;
2323

24+
const NodeUnion = @import("node.zig").Union;
25+
const Node = @import("node.zig").Node;
26+
2427
pub const Interfaces = .{
2528
AbstractRange,
2629
Range,
@@ -42,16 +45,16 @@ pub const AbstractRange = struct {
4245
return self.collapsed;
4346
}
4447

45-
pub fn get_endContainer(self: *const AbstractRange) *parser.Node {
46-
return self.end_container;
48+
pub fn get_endContainer(self: *const AbstractRange) !NodeUnion {
49+
return Node.toInterface(self.end_container);
4750
}
4851

4952
pub fn get_endOffset(self: *const AbstractRange) i32 {
5053
return self.end_offset;
5154
}
5255

53-
pub fn get_startContainer(self: *const AbstractRange) *parser.Node {
54-
return self.start_container;
56+
pub fn get_startContainer(self: *const AbstractRange) !NodeUnion {
57+
return Node.toInterface(self.start_container);
5558
}
5659

5760
pub fn get_startOffset(self: *const AbstractRange) i32 {
@@ -64,12 +67,15 @@ pub const Range = struct {
6467

6568
proto: AbstractRange,
6669

67-
pub fn constructor() Range {
70+
// The Range() constructor returns a newly created Range object whose start
71+
// and end is the global Document object.
72+
// https://developer.mozilla.org/en-US/docs/Web/API/Range/Range
73+
pub fn constructor(page: *Page) Range {
6874
const proto: AbstractRange = .{
6975
.collapsed = true,
70-
.end_container = undefined,
76+
.end_container = parser.documentHTMLToNode(page.window.document),
7177
.end_offset = 0,
72-
.start_container = undefined,
78+
.start_container = parser.documentHTMLToNode(page.window.document),
7379
.start_offset = 0,
7480
};
7581

@@ -137,6 +143,8 @@ test "Browser.Range" {
137143
.{ "range.collapsed", "true" },
138144
.{ "range.startOffset", "0" },
139145
.{ "range.endOffset", "0" },
146+
.{ "range.startContainer instanceof HTMLDocument", "true" },
147+
.{ "range.endContainer instanceof HTMLDocument", "true" },
140148

141149
// Test document.createRange()
142150
.{ "let docRange = document.createRange()", "undefined" },

0 commit comments

Comments
 (0)