mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
handle empty hashes in Location
This commit is contained in:
@@ -17,8 +17,6 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Uri = std.Uri;
|
|
||||||
|
|
||||||
const Page = @import("../page.zig").Page;
|
const Page = @import("../page.zig").Page;
|
||||||
const URL = @import("../url/url.zig").URL;
|
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 {
|
pub fn set_hash(_: *const Location, hash: []const u8, page: *Page) !void {
|
||||||
const normalized_hash = if (hash[0] == '#')
|
const normalized_hash = blk: {
|
||||||
hash
|
if (hash.len == 0) {
|
||||||
else
|
const old_url = page.url.raw;
|
||||||
try std.fmt.allocPrint(page.arena, "#{s}", .{hash});
|
|
||||||
|
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);
|
return page.navigateFromWebAPI(normalized_hash, .{ .reason = .script }, .replace);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,10 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<script id=location_hash>
|
<script id=location_hash>
|
||||||
|
location.hash = "";
|
||||||
|
testing.expectEqual("", location.hash);
|
||||||
|
testing.expectEqual('http://localhost:9582/src/tests/html/location.html', location.href);
|
||||||
|
|
||||||
location.hash = "#abcdef";
|
location.hash = "#abcdef";
|
||||||
testing.expectEqual("#abcdef", location.hash);
|
testing.expectEqual("#abcdef", location.hash);
|
||||||
testing.expectEqual('http://localhost:9582/src/tests/html/location.html#abcdef', location.href);
|
testing.expectEqual('http://localhost:9582/src/tests/html/location.html#abcdef', location.href);
|
||||||
@@ -22,4 +26,8 @@
|
|||||||
location.hash = "xyzxyz";
|
location.hash = "xyzxyz";
|
||||||
testing.expectEqual("#xyzxyz", location.hash);
|
testing.expectEqual("#xyzxyz", location.hash);
|
||||||
testing.expectEqual('http://localhost:9582/src/tests/html/location.html#xyzxyz', location.href);
|
testing.expectEqual('http://localhost:9582/src/tests/html/location.html#xyzxyz', location.href);
|
||||||
|
|
||||||
|
location.hash = "";
|
||||||
|
testing.expectEqual("", location.hash);
|
||||||
|
testing.expectEqual('http://localhost:9582/src/tests/html/location.html', location.href);
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user