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
44 changes: 24 additions & 20 deletions src/browser/html/location.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,57 +16,61 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

const Page = @import("../page.zig").Page;
const Uri = @import("std").Uri;

const Page = @import("../page.zig").Page;
const URL = @import("../url/url.zig").URL;

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface
pub const Location = struct {
url: ?URL = null,
url: URL,

/// Browsers give such initial values when user not navigated yet:
/// Chrome -> chrome://new-tab-page/
/// Firefox -> about:newtab
/// Safari -> favorites://
pub const default = Location{
.url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable),
};

pub fn get_href(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_href(page);
return "";
return self.url.get_href(page);
}

pub fn set_href(_: *const Location, href: []const u8, page: *Page) !void {
return page.navigateFromWebAPI(href, .{ .reason = .script });
}

pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_protocol(page);
return "";
return self.url.get_protocol(page);
}

pub fn get_host(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_host(page);
return "";
return self.url.get_host(page);
}

pub fn get_hostname(self: *Location) []const u8 {
if (self.url) |*u| return u.get_hostname();
return "";
return self.url.get_hostname();
}

pub fn get_port(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_port(page);
return "";
return self.url.get_port(page);
}

pub fn get_pathname(self: *Location) []const u8 {
if (self.url) |*u| return u.get_pathname();
return "";
return self.url.get_pathname();
}

pub fn get_search(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_search(page);
return "";
return self.url.get_search(page);
}

pub fn get_hash(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_hash(page);
return "";
return self.url.get_hash(page);
}

pub fn get_origin(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_origin(page);
return "";
return self.url.get_origin(page);
}

pub fn _assign(_: *const Location, url: []const u8, page: *Page) !void {
Expand Down
2 changes: 1 addition & 1 deletion src/browser/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub const Window = struct {

document: *parser.DocumentHTML,
target: []const u8 = "",
location: Location = .{},
location: Location = .default,
storage_shelf: ?*storage.Shelf = null,

// counter for having unique timer ids
Expand Down
4 changes: 4 additions & 0 deletions src/browser/url/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ pub const URL = struct {
};
}

pub fn initWithoutSearchParams(uri: std.Uri) URL {
return .{ .uri = uri, .search_params = .{} };
}

pub fn get_origin(self: *URL, page: *Page) ![]const u8 {
var aw = std.Io.Writer.Allocating.init(page.arena);
try self.uri.writeToStream(&aw.writer, .{
Expand Down