Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/browser/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ pub const Document = struct {
state.active_element = @ptrCast(e);
}

pub fn _createRange(_: *parser.Document) Range {
return Range.constructor();
pub fn _createRange(_: *parser.Document, page: *Page) Range {
return Range.constructor(page);
}
};

Expand Down
28 changes: 21 additions & 7 deletions src/browser/dom/range.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const std = @import("std");
const parser = @import("../netsurf.zig");
const Page = @import("../page.zig").Page;

const NodeUnion = @import("node.zig").Union;
const Node = @import("node.zig").Node;

pub const Interfaces = .{
AbstractRange,
Range,
Expand All @@ -42,16 +45,16 @@ pub const AbstractRange = struct {
return self.collapsed;
}

pub fn get_endContainer(self: *const AbstractRange) *parser.Node {
return self.end_container;
pub fn get_endContainer(self: *const AbstractRange) !NodeUnion {
return Node.toInterface(self.end_container);
}

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

pub fn get_startContainer(self: *const AbstractRange) *parser.Node {
return self.start_container;
pub fn get_startContainer(self: *const AbstractRange) !NodeUnion {
return Node.toInterface(self.start_container);
}

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

proto: AbstractRange,

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

Expand Down Expand Up @@ -120,6 +126,12 @@ pub const Range = struct {

self.proto.updateCollapsed();
}

// The Range.detach() method does nothing. It used to disable the Range
// object and enable the browser to release associated resources. The
// method has been kept for compatibility.
// https://developer.mozilla.org/en-US/docs/Web/API/Range/detach
pub fn _detach(_: *Range) void {}
};

const testing = @import("../../testing.zig");
Expand All @@ -137,6 +149,8 @@ test "Browser.Range" {
.{ "range.collapsed", "true" },
.{ "range.startOffset", "0" },
.{ "range.endOffset", "0" },
.{ "range.startContainer instanceof HTMLDocument", "true" },
.{ "range.endContainer instanceof HTMLDocument", "true" },

// Test document.createRange()
.{ "let docRange = document.createRange()", "undefined" },
Expand Down