Skip to content

Commit 0e3f183

Browse files
committed
add set_hash to Location
1 parent 47ceabc commit 0e3f183

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/browser/html/location.zig

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
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 Uri = @import("std").Uri;
19+
const std = @import("std");
20+
const Uri = std.Uri;
2021

2122
const Page = @import("../page.zig").Page;
2223
const URL = @import("../url/url.zig").URL;
@@ -42,6 +43,20 @@ pub const Location = struct {
4243
return page.navigateFromWebAPI(href, .{ .reason = .script }, .{ .push = null });
4344
}
4445

46+
pub fn set_hash(_: *const Location, hash: []const u8, page: *Page) !void {
47+
const current_url = page.url.raw;
48+
49+
const base_without_hash = if (std.mem.indexOfScalar(u8, current_url, '#')) |pos|
50+
current_url[0..pos]
51+
else
52+
current_url;
53+
54+
const normalized_hash = std.mem.trimStart(u8, hash, "#");
55+
const new_url = try std.fmt.allocPrint(page.arena, "{s}#{s}", .{ base_without_hash, normalized_hash });
56+
57+
return page.navigateFromWebAPI(new_url, .{ .reason = .script }, .replace);
58+
}
59+
4560
pub fn get_protocol(self: *Location) []const u8 {
4661
return self.url.get_protocol();
4762
}

0 commit comments

Comments
 (0)