From 16e7c0841dc425c2ce51eb07413a6377d4dc99a1 Mon Sep 17 00:00:00 2001 From: Muki Kiboigo Date: Mon, 10 Nov 2025 06:52:14 -0800 Subject: [PATCH] handle empty hashes in Location --- src/browser/html/location.zig | 19 +++++++++++++------ src/tests/html/location.html | 8 ++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/browser/html/location.zig b/src/browser/html/location.zig index 27dcd335..45817845 100644 --- a/src/browser/html/location.zig +++ b/src/browser/html/location.zig @@ -17,8 +17,6 @@ // along with this program. If not, see . const std = @import("std"); -const Uri = std.Uri; - const Page = @import("../page.zig").Page; const URL = @import("../url/url.zig").URL; @@ -44,10 +42,19 @@ pub const Location = struct { } pub fn set_hash(_: *const Location, hash: []const u8, page: *Page) !void { - const normalized_hash = if (hash[0] == '#') - hash - else - try std.fmt.allocPrint(page.arena, "#{s}", .{hash}); + const normalized_hash = blk: { + if (hash.len == 0) { + const old_url = page.url.raw; + + break :blk if (std.mem.indexOfScalar(u8, old_url, '#')) |index| + old_url[0..index] + else + old_url; + } else if (hash[0] == '#') + break :blk hash + else + break :blk try std.fmt.allocPrint(page.arena, "#{s}", .{hash}); + }; return page.navigateFromWebAPI(normalized_hash, .{ .reason = .script }, .replace); } diff --git a/src/tests/html/location.html b/src/tests/html/location.html index 863ae658..a5de3ba8 100644 --- a/src/tests/html/location.html +++ b/src/tests/html/location.html @@ -15,6 +15,10 @@