add set_hash to Location

This commit is contained in:
Muki Kiboigo
2025-11-02 21:04:11 -08:00
parent 47ceabc43f
commit 0e3f18367a

View File

@@ -16,7 +16,8 @@
// 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 Uri = @import("std").Uri;
const std = @import("std");
const Uri = std.Uri;
const Page = @import("../page.zig").Page;
const URL = @import("../url/url.zig").URL;
@@ -42,6 +43,20 @@ pub const Location = struct {
return page.navigateFromWebAPI(href, .{ .reason = .script }, .{ .push = null });
}
pub fn set_hash(_: *const Location, hash: []const u8, page: *Page) !void {
const current_url = page.url.raw;
const base_without_hash = if (std.mem.indexOfScalar(u8, current_url, '#')) |pos|
current_url[0..pos]
else
current_url;
const normalized_hash = std.mem.trimStart(u8, hash, "#");
const new_url = try std.fmt.allocPrint(page.arena, "{s}#{s}", .{ base_without_hash, normalized_hash });
return page.navigateFromWebAPI(new_url, .{ .reason = .script }, .replace);
}
pub fn get_protocol(self: *Location) []const u8 {
return self.url.get_protocol();
}