Skip to content

Commit 69884b9

Browse files
committed
Location changes regarding to changes in URL
1 parent c568a75 commit 69884b9

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

src/browser/html/document.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ pub const HTMLDocument = struct {
4242
// JS funcs
4343
// --------
4444

45-
pub fn get_domain(self: *parser.DocumentHTML, page: *Page) ![]const u8 {
45+
pub fn get_domain(self: *parser.DocumentHTML) ![]const u8 {
4646
// libdom's document_html get_domain always returns null, this is
4747
// the way MDN recommends getting the domain anyways, since document.domain
4848
// is deprecated.
4949
const location = try parser.documentHTMLGetLocation(Location, self) orelse return "";
50-
return location.get_host(page);
50+
return location.get_host();
5151
}
5252

5353
pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 {

src/browser/html/location.zig

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ const URL = @import("../url/url.zig").URL;
2525
pub const Location = struct {
2626
url: URL,
2727

28+
/// Initializes the `Location` to be used in `Window`.
2829
/// Browsers give such initial values when user not navigated yet:
2930
/// Chrome -> chrome://new-tab-page/
3031
/// Firefox -> about:newtab
3132
/// Safari -> favorites://
32-
pub const default = Location{
33-
.url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable),
34-
};
33+
pub fn init(url: []const u8) !Location {
34+
return .{ .url = try .initForLocation(url) };
35+
}
3536

3637
pub fn get_href(self: *Location, page: *Page) ![]const u8 {
3738
return self.url.get_href(page);
@@ -45,16 +46,16 @@ pub const Location = struct {
4546
return self.url.get_protocol();
4647
}
4748

48-
pub fn get_host(self: *Location, page: *Page) ![]const u8 {
49-
return self.url.get_host(page);
49+
pub fn get_host(self: *Location) []const u8 {
50+
return self.url.get_host();
5051
}
5152

5253
pub fn get_hostname(self: *Location) []const u8 {
5354
return self.url.get_hostname();
5455
}
5556

56-
pub fn get_port(self: *Location, page: *Page) ![]const u8 {
57-
return self.url.get_port(page);
57+
pub fn get_port(self: *Location) []const u8 {
58+
return self.url.get_port();
5859
}
5960

6061
pub fn get_pathname(self: *Location) []const u8 {
@@ -65,8 +66,8 @@ pub const Location = struct {
6566
return self.url.get_search(page);
6667
}
6768

68-
pub fn get_hash(self: *Location, page: *Page) ![]const u8 {
69-
return self.url.get_hash(page);
69+
pub fn get_hash(self: *Location) []const u8 {
70+
return self.url.get_hash();
7071
}
7172

7273
pub fn get_origin(self: *Location, page: *Page) ![]const u8 {
@@ -86,7 +87,7 @@ pub const Location = struct {
8687
}
8788

8889
pub fn _toString(self: *Location, page: *Page) ![]const u8 {
89-
return try self.get_href(page);
90+
return self.get_href(page);
9091
}
9192
};
9293

src/browser/html/window.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub const Window = struct {
5353

5454
document: *parser.DocumentHTML,
5555
target: []const u8 = "",
56-
location: Location = .default,
56+
location: Location,
5757
storage_shelf: ?*storage.Shelf = null,
5858

5959
// counter for having unique timer ids
@@ -79,6 +79,7 @@ pub const Window = struct {
7979
return .{
8080
.document = html_doc,
8181
.target = target orelse "",
82+
.location = try .init("about:blank"),
8283
.navigator = navigator orelse .{},
8384
.performance = Performance.init(),
8485
};
@@ -89,6 +90,10 @@ pub const Window = struct {
8990
try parser.documentHTMLSetLocation(Location, self.document, &self.location);
9091
}
9192

93+
pub fn changeLocation(self: *Window, new_url: []const u8, page: *Page) !void {
94+
return self.location.url.reinit(new_url, page);
95+
}
96+
9297
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
9398
self.performance.reset(); // When to reset see: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin
9499
self.document = doc;

src/browser/page.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ pub const Page = struct {
859859
self.window.setStorageShelf(
860860
try self.session.storage_shed.getOrPut(try self.origin(self.arena)),
861861
);
862-
try self.window.replaceLocation(.{ .url = try self.url.toWebApi(self.arena) });
862+
try self.window.changeLocation(self.url.raw, self);
863863
}
864864

865865
pub const MouseEvent = struct {

src/url.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ pub const URL = struct {
7575
return writer.writeAll(self.raw);
7676
}
7777

78-
pub fn toWebApi(self: *const URL, allocator: Allocator) !WebApiURL {
79-
return WebApiURL.init(allocator, self.uri);
80-
}
81-
8278
/// Properly stitches two URL fragments together.
8379
///
8480
/// For URLs with a path, it will replace the last entry with the src.

0 commit comments

Comments
 (0)