Lightpanda: the headless browser designed for AI and automation
location: build hash setter URL from the current URL, not the base
Pierre Tachoired87dd653732f
diff --git a/src/browser/tests/window/location_hash_base.html b/src/browser/tests/window/location_hash_base.htmlnew file mode 100644index 000000000..29744f937--- /dev/null+++ b/src/browser/tests/window/location_hash_base.html@@ -0,0 +1,49 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- The Location hash setter changes the fragment of a copy of the current+ URL. The document base URL plays no part: a <base> element must not turn+ `location.hash = x` into a navigation to another document.+ https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-hash -->++<script id=location_hash_ignores_base type=module>+ {+ const state = await testing.async();+ // Built from the current URL, so the test works whatever the server root.+ const page_url = location.href.split('#')[0];+ testing.expectEqual('', location.hash);++ // Same origin, other path: if the setter resolved against the base, the+ // fragment navigation would become a real navigation to this directory.+ const base_url = new URL('support/', page_url).href;+ // Insert first, then set href: Lightpanda only updates the base URL from+ // the href setter of a connected <base>.+ const base = document.createElement('base');+ document.head.appendChild(base);+ base.href = base_url;+ testing.expectEqual(base_url, document.baseURI);++ // Each change below is a fragment navigation, so each one fires its own+ // hashchange. A real navigation would fire none.+ const new_urls = [];+ window.onhashchange = (e) => {+ new_urls.push(e.newURL);+ if (new_urls.length === 3) state.resolve();+ };++ location.hash = 'foo';+ testing.expectEqual(page_url + '#foo', location.href);+ testing.expectEqual('#foo', location.hash);++ location.hash = '#bar';+ testing.expectEqual(page_url + '#bar', location.href);++ location.hash = '';+ testing.expectEqual(page_url + '#', location.href);++ await state.done(() => {+ testing.expectEqual([page_url + '#foo', page_url + '#bar', page_url + '#'], new_urls);+ window.onhashchange = null;+ });+ }+</script>diff --git a/src/browser/webapi/Location.zig b/src/browser/webapi/Location.zigindex b0769717a..6d4c91e8d 100644--- a/src/browser/webapi/Location.zig+++ b/src/browser/webapi/Location.zig@@ -111,9 +111,14 @@ fn setHash(self: *const Location, hash: []const u8, frame: *Frame) !void {// Includes the leading '#'; empty when the URL has no fragment.const old_fragment = old_url[base_end..];+ // Clearing the hash on an URL w/ no fragment does nothing.+ if (old_fragment.len == 0 and (hash.len == 0 or std.mem.eql(u8, hash, "#"))) {+ return;+ }+const normalized_hash: []const u8 = blk: {if (hash.len == 0) {- break :blk "";+ break :blk "#";} else if (hash[0] == '#') {break :blk hash;}@@ -122,19 +127,12 @@ fn setHash(self: *const Location, hash: []const u8, frame: *Frame) !void {break :blk try frame.local_arena.print("#{s}", .{hash});};- // Per the Location hash setter, when the fragment doesn't change no- // navigation happens at all — in particular `location.hash = ""` on a- // fragment-less URL must not turn into a same-URL reload.+ // No navigation when the fragment doesn't change.if (std.mem.eql(u8, old_fragment, normalized_hash)) {return;}- // Clearing the hash sets an empty, not a null, fragment: /x#a -> /x#,- // which stays a fragment navigation. Dropping the '#' would reload.- const target_url = if (normalized_hash.len == 0)- try std.fmt.allocPrint(frame.local_arena, "{s}#", .{old_url[0..base_end]})- else- normalized_hash;+ const target_url = try frame.local_arena.print("{s}{s}", .{ old_url[0..base_end], normalized_hash });return target.scheduleNavigation(target_url, .{.reason = .script,