Lightpanda: the headless browser designed for AI and automation
browser: don't fragment-navigate POSTs or fragment-less URLs
Navid EMAD2385eb26b92a
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex cad80ab7f..82ad47f7a 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -1007,24 +1007,27 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_urlconst session = target._session;- // Re-navigating to the exact current URL is only a reload when the URL- // has no fragment. With a fragment it's a fragment navigation per the- // HTML "navigate" steps (url equals the document's URL excluding- // fragments and url's fragment is non-null): no reload, and since the- // fragment didn't change, no hashchange and no new history entry either.- if (!opts.force and+ // Per the HTML "navigate" steps, this is a fragment navigation only when+ // there's no document resource (no POST body), url equals the document's+ // URL excluding fragments and url's fragment is non-null. Anything else+ // is a real navigation: a form POST to the current URL, or dropping the+ // fragment (/x#a -> /x), must hit the network.+ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate+ const is_fragment_navigation = !opts.force andopts.kind != .reload and- std.mem.eql(u8, target.url, resolved_url) and- std.mem.findScalar(u8, resolved_url, '#') != null)- {- arena.release();- return;- }+ opts.method == .GET and+ opts.body == null and+ std.mem.indexOfScalar(u8, resolved_url, '#') != null and+ URL.eqlDocument(target.url, resolved_url);++ if (is_fragment_navigation) {+ // Re-navigating to the exact current URL: since the fragment didn't+ // change, no hashchange and no new history entry either.+ if (std.mem.eql(u8, target.url, resolved_url)) {+ arena.release();+ return;+ }- // Short-circuit only true fragment-only navigations (same path/query, different- // fragment). Identical URLs fall through and trigger a real reload.- const is_fragment_navigation = !std.mem.eql(u8, target.url, resolved_url) and URL.eqlDocument(target.url, resolved_url);- if (!opts.force and is_fragment_navigation) {const old_url = target.url;target.url = try target.arena.dupeSentinel(u8, resolved_url, 0);diff --git a/src/browser/tests/frames/fragment_navigation.html b/src/browser/tests/frames/fragment_navigation.htmlnew file mode 100644index 000000000..3d2d50556--- /dev/null+++ b/src/browser/tests/frames/fragment_navigation.html@@ -0,0 +1,135 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- Per the HTML "navigate" algorithm, a navigation only short-circuits to a+ fragment navigation when it has no document resource (no POST body), the+ URL equals the current URL excluding fragments AND the URL's fragment is+ non-null. Anything else is a real navigation, even when only the current+ URL's fragment differs.+ https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate -->++<script>+ // Loads /echo_method (which reports the request method) in a fresh named+ // iframe, at a URL with a fragment. In the wild the fragment usually comes+ // from a page script tracking a UI step (history.pushState(null, null,+ // "#select-spot")); the navigation sees the same current URL either way.+ function loadFrame(name, src) {+ return new Promise((resolve) => {+ const frame = document.createElement('iframe');+ frame.name = name;+ frame.onload = () => {+ // Skip the load of the initial about:blank document.+ if (frame.contentWindow.location.pathname !== '/echo_method') return;+ frame.onload = null;+ resolve(frame);+ };+ frame.src = src;+ document.body.appendChild(frame);+ });+ }+</script>++<script id=post_same_url_without_fragment type=module>+ {+ const state = await testing.async();+ const frame = await loadFrame('post_frame', '/echo_method#select-spot');+ testing.expectEqual('#select-spot', frame.contentWindow.location.hash);+ const initial_document = frame.contentDocument;++ const form = document.createElement('form');+ form.method = 'post';+ form.action = '/echo_method';+ form.target = 'post_frame';+ form.innerHTML = '<input name=spot value=42>';+ document.body.appendChild(form);++ let submit_events = 0;+ form.addEventListener('submit', () => submit_events += 1);+ frame.onload = () => state.resolve();+ form.requestSubmit();++ await state.done(() => {+ testing.expectEqual(1, submit_events);+ testing.expectEqual(false, frame.contentDocument === initial_document);+ testing.expectEqual('method=POST', frame.contentDocument.body.textContent);+ testing.expectEqual('', frame.contentWindow.location.hash);+ });+ }+</script>++<script id=post_exact_current_url type=module>+ {+ // A POST to the exact current URL, fragment included, still has a+ // document resource: it must be sent, not dropped as a no-op.+ const state = await testing.async();+ const frame = await loadFrame('post_exact_frame', '/echo_method#select-spot');+ testing.expectEqual('#select-spot', frame.contentWindow.location.hash);+ const initial_document = frame.contentDocument;++ const form = document.createElement('form');+ form.method = 'post';+ form.action = '/echo_method#select-spot';+ form.target = 'post_exact_frame';+ document.body.appendChild(form);++ frame.onload = () => state.resolve();+ form.submit();++ await state.done(() => {+ testing.expectEqual(false, frame.contentDocument === initial_document);+ testing.expectEqual('method=POST', frame.contentDocument.body.textContent);+ });+ }+</script>++<script id=get_same_url_without_fragment type=module>+ {+ // Same URL with the fragment dropped: the target URL's fragment is null,+ // so this reloads the document.+ const state = await testing.async();+ const frame = await loadFrame('get_frame', '/echo_method#a');+ testing.expectEqual('#a', frame.contentWindow.location.hash);+ const initial_document = frame.contentDocument;++ const link = document.createElement('a');+ link.href = '/echo_method';+ link.target = 'get_frame';+ document.body.appendChild(link);++ frame.onload = () => state.resolve();+ link.click();++ await state.done(() => {+ testing.expectEqual(false, frame.contentDocument === initial_document);+ testing.expectEqual('method=GET', frame.contentDocument.body.textContent);+ testing.expectEqual('', frame.contentWindow.location.hash);+ });+ }+</script>++<script id=get_other_fragment type=module>+ {+ // Same URL with another fragment: a fragment navigation, the document+ // stays and hashchange fires.+ const state = await testing.async();+ const frame = await loadFrame('fragment_frame', '/echo_method#a');+ testing.expectEqual('#a', frame.contentWindow.location.hash);+ const initial_document = frame.contentDocument;++ let loads = 0;+ frame.onload = () => loads += 1;+ frame.contentWindow.onhashchange = () => state.resolve();++ const link = document.createElement('a');+ link.href = '/echo_method#b';+ link.target = 'fragment_frame';+ document.body.appendChild(link);+ link.click();++ await state.done(() => {+ testing.expectEqual(0, loads);+ testing.expectEqual(true, frame.contentDocument === initial_document);+ testing.expectEqual('#b', frame.contentWindow.location.hash);+ });+ }+</script>diff --git a/src/browser/tests/window/location.html b/src/browser/tests/window/location.htmlindex 17e38a748..a25701c10 100644--- a/src/browser/tests/window/location.html+++ b/src/browser/tests/window/location.html@@ -11,9 +11,11 @@testing.expectEqual("#abcdef", location.hash);testing.expectEqual(testing.BASE_URL + 'window/location.html#abcdef', location.href);+ // Clearing the hash leaves an empty fragment, not a null one: the '#'+ // stays and the document isn't reloaded.location.hash = "";testing.expectEqual("", location.hash);- testing.expectEqual(testing.BASE_URL + 'window/location.html', location.href);+ testing.expectEqual(testing.BASE_URL + 'window/location.html#', location.href);location.hash = "xyzxyz";testing.expectEqual("#xyzxyz", location.hash);@@ -21,7 +23,7 @@location.hash = "";testing.expectEqual("", location.hash);- testing.expectEqual(testing.BASE_URL + 'window/location.html', location.href);+ testing.expectEqual(testing.BASE_URL + 'window/location.html#', location.href);</script><script id=location_pathname_setter>@@ -74,11 +76,19 @@// fragment must not navigate at all: re-assigning "" on a fragment-less URL// must not schedule a same-URL reload, and re-assigning the current// fragment must be a no-op too.+ history.replaceState(null, '', location.pathname);const stable_href = location.href;location.hash = "";- location.hash = "";testing.expectEqual(stable_href, location.href);+ // Clearing an existing fragment gives an empty one; clearing it again+ // is a no-op.+ location.hash = "#gone";+ location.hash = "";+ testing.expectEqual(stable_href + '#', location.href);+ location.hash = "";+ testing.expectEqual(stable_href + '#', location.href);+location.hash = "#same";const with_fragment = location.href;location.hash = "#same";@@ -87,5 +97,5 @@testing.expectEqual("#same", location.hash);location.hash = "";- testing.expectEqual(stable_href, location.href);+ testing.expectEqual(stable_href + '#', location.href);</script>diff --git a/src/browser/webapi/Location.zig b/src/browser/webapi/Location.zigindex 0e821e765..b0769717a 100644--- a/src/browser/webapi/Location.zig+++ b/src/browser/webapi/Location.zig@@ -129,7 +129,12 @@ fn setHash(self: *const Location, hash: []const u8, frame: *Frame) !void {return;}- const target_url = if (normalized_hash.len == 0) old_url[0..base_end] else normalized_hash;+ // 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;return target.scheduleNavigation(target_url, .{.reason = .script,