Skip to content

Commit 27c9e18

Browse files
authored
Merge pull request #1134 from lightpanda-io/nikneym/default-location
Location: prefer `about:blank` when not navigated yet
2 parents b53c2bf + 0b38b7d commit 27c9e18

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/browser/html/location.zig

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,57 +16,61 @@
1616
// You should have received a copy of the GNU Affero General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19-
const Page = @import("../page.zig").Page;
19+
const Uri = @import("std").Uri;
2020

21+
const Page = @import("../page.zig").Page;
2122
const URL = @import("../url/url.zig").URL;
2223

2324
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#the-location-interface
2425
pub const Location = struct {
25-
url: ?URL = null,
26+
url: URL,
27+
28+
/// Browsers give such initial values when user not navigated yet:
29+
/// Chrome -> chrome://new-tab-page/
30+
/// Firefox -> about:newtab
31+
/// Safari -> favorites://
32+
pub const default = Location{
33+
.url = .initWithoutSearchParams(Uri.parse("about:blank") catch unreachable),
34+
};
2635

2736
pub fn get_href(self: *Location, page: *Page) ![]const u8 {
28-
if (self.url) |*u| return u.get_href(page);
29-
return "";
37+
return self.url.get_href(page);
38+
}
39+
40+
pub fn set_href(_: *const Location, href: []const u8, page: *Page) !void {
41+
return page.navigateFromWebAPI(href, .{ .reason = .script });
3042
}
3143

3244
pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
33-
if (self.url) |*u| return u.get_protocol(page);
34-
return "";
45+
return self.url.get_protocol(page);
3546
}
3647

3748
pub fn get_host(self: *Location, page: *Page) ![]const u8 {
38-
if (self.url) |*u| return u.get_host(page);
39-
return "";
49+
return self.url.get_host(page);
4050
}
4151

4252
pub fn get_hostname(self: *Location) []const u8 {
43-
if (self.url) |*u| return u.get_hostname();
44-
return "";
53+
return self.url.get_hostname();
4554
}
4655

4756
pub fn get_port(self: *Location, page: *Page) ![]const u8 {
48-
if (self.url) |*u| return u.get_port(page);
49-
return "";
57+
return self.url.get_port(page);
5058
}
5159

5260
pub fn get_pathname(self: *Location) []const u8 {
53-
if (self.url) |*u| return u.get_pathname();
54-
return "";
61+
return self.url.get_pathname();
5562
}
5663

5764
pub fn get_search(self: *Location, page: *Page) ![]const u8 {
58-
if (self.url) |*u| return u.get_search(page);
59-
return "";
65+
return self.url.get_search(page);
6066
}
6167

6268
pub fn get_hash(self: *Location, page: *Page) ![]const u8 {
63-
if (self.url) |*u| return u.get_hash(page);
64-
return "";
69+
return self.url.get_hash(page);
6570
}
6671

6772
pub fn get_origin(self: *Location, page: *Page) ![]const u8 {
68-
if (self.url) |*u| return u.get_origin(page);
69-
return "";
73+
return self.url.get_origin(page);
7074
}
7175

7276
pub fn _assign(_: *const Location, url: []const u8, page: *Page) !void {

src/browser/html/window.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub const Window = struct {
5252

5353
document: *parser.DocumentHTML,
5454
target: []const u8 = "",
55-
location: Location = .{},
55+
location: Location = .default,
5656
storage_shelf: ?*storage.Shelf = null,
5757

5858
// counter for having unique timer ids

src/browser/url/url.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ pub const URL = struct {
112112
};
113113
}
114114

115+
pub fn initWithoutSearchParams(uri: std.Uri) URL {
116+
return .{ .uri = uri, .search_params = .{} };
117+
}
118+
115119
pub fn get_origin(self: *URL, page: *Page) ![]const u8 {
116120
var aw = std.Io.Writer.Allocating.init(page.arena);
117121
try self.uri.writeToStream(&aw.writer, .{

0 commit comments

Comments
 (0)