Location changes regarding to changes in URL

This commit is contained in:
Halil Durak
2025-10-21 16:44:29 +03:00
parent c568a75599
commit 69884b9d8d
5 changed files with 20 additions and 18 deletions

View File

@@ -42,12 +42,12 @@ pub const HTMLDocument = struct {
// JS funcs // JS funcs
// -------- // --------
pub fn get_domain(self: *parser.DocumentHTML, page: *Page) ![]const u8 { pub fn get_domain(self: *parser.DocumentHTML) ![]const u8 {
// libdom's document_html get_domain always returns null, this is // libdom's document_html get_domain always returns null, this is
// the way MDN recommends getting the domain anyways, since document.domain // the way MDN recommends getting the domain anyways, since document.domain
// is deprecated. // is deprecated.
const location = try parser.documentHTMLGetLocation(Location, self) orelse return ""; const location = try parser.documentHTMLGetLocation(Location, self) orelse return "";
return location.get_host(page); return location.get_host();
} }
pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 { pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 {

View File

@@ -25,13 +25,14 @@ const URL = @import("../url/url.zig").URL;
pub const Location = struct { pub const Location = struct {
url: URL, url: URL,
/// Initializes the `Location` to be used in `Window`.
/// Browsers give such initial values when user not navigated yet: /// Browsers give such initial values when user not navigated yet:
/// Chrome -> chrome://new-tab-page/ /// Chrome -> chrome://new-tab-page/
/// Firefox -> about:newtab /// Firefox -> about:newtab
/// Safari -> favorites:// /// Safari -> favorites://
pub const default = Location{ pub fn init(url: []const u8) !Location {
.url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable), return .{ .url = try .initForLocation(url) };
}; }
pub fn get_href(self: *Location, page: *Page) ![]const u8 { pub fn get_href(self: *Location, page: *Page) ![]const u8 {
return self.url.get_href(page); return self.url.get_href(page);
@@ -45,16 +46,16 @@ pub const Location = struct {
return self.url.get_protocol(); return self.url.get_protocol();
} }
pub fn get_host(self: *Location, page: *Page) ![]const u8 { pub fn get_host(self: *Location) []const u8 {
return self.url.get_host(page); return self.url.get_host();
} }
pub fn get_hostname(self: *Location) []const u8 { pub fn get_hostname(self: *Location) []const u8 {
return self.url.get_hostname(); return self.url.get_hostname();
} }
pub fn get_port(self: *Location, page: *Page) ![]const u8 { pub fn get_port(self: *Location) []const u8 {
return self.url.get_port(page); return self.url.get_port();
} }
pub fn get_pathname(self: *Location) []const u8 { pub fn get_pathname(self: *Location) []const u8 {
@@ -65,8 +66,8 @@ pub const Location = struct {
return self.url.get_search(page); return self.url.get_search(page);
} }
pub fn get_hash(self: *Location, page: *Page) ![]const u8 { pub fn get_hash(self: *Location) []const u8 {
return self.url.get_hash(page); return self.url.get_hash();
} }
pub fn get_origin(self: *Location, page: *Page) ![]const u8 { pub fn get_origin(self: *Location, page: *Page) ![]const u8 {
@@ -86,7 +87,7 @@ pub const Location = struct {
} }
pub fn _toString(self: *Location, page: *Page) ![]const u8 { pub fn _toString(self: *Location, page: *Page) ![]const u8 {
return try self.get_href(page); return self.get_href(page);
} }
}; };

View File

@@ -53,7 +53,7 @@ pub const Window = struct {
document: *parser.DocumentHTML, document: *parser.DocumentHTML,
target: []const u8 = "", target: []const u8 = "",
location: Location = .default, location: Location,
storage_shelf: ?*storage.Shelf = null, storage_shelf: ?*storage.Shelf = null,
// counter for having unique timer ids // counter for having unique timer ids
@@ -79,6 +79,7 @@ pub const Window = struct {
return .{ return .{
.document = html_doc, .document = html_doc,
.target = target orelse "", .target = target orelse "",
.location = try .init("about:blank"),
.navigator = navigator orelse .{}, .navigator = navigator orelse .{},
.performance = Performance.init(), .performance = Performance.init(),
}; };
@@ -89,6 +90,10 @@ pub const Window = struct {
try parser.documentHTMLSetLocation(Location, self.document, &self.location); try parser.documentHTMLSetLocation(Location, self.document, &self.location);
} }
pub fn changeLocation(self: *Window, new_url: []const u8, page: *Page) !void {
return self.location.url.reinit(new_url, page);
}
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void { pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
self.performance.reset(); // When to reset see: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin self.performance.reset(); // When to reset see: https://developer.mozilla.org/en-US/docs/Web/API/Performance/timeOrigin
self.document = doc; self.document = doc;

View File

@@ -859,7 +859,7 @@ pub const Page = struct {
self.window.setStorageShelf( self.window.setStorageShelf(
try self.session.storage_shed.getOrPut(try self.origin(self.arena)), try self.session.storage_shed.getOrPut(try self.origin(self.arena)),
); );
try self.window.replaceLocation(.{ .url = try self.url.toWebApi(self.arena) }); try self.window.changeLocation(self.url.raw, self);
} }
pub const MouseEvent = struct { pub const MouseEvent = struct {

View File

@@ -75,10 +75,6 @@ pub const URL = struct {
return writer.writeAll(self.raw); return writer.writeAll(self.raw);
} }
pub fn toWebApi(self: *const URL, allocator: Allocator) !WebApiURL {
return WebApiURL.init(allocator, self.uri);
}
/// Properly stitches two URL fragments together. /// Properly stitches two URL fragments together.
/// ///
/// For URLs with a path, it will replace the last entry with the src. /// For URLs with a path, it will replace the last entry with the src.