Lightpanda: the headless browser designed for AI and automation
browser: replace the action's query on a GET form submission
Jay Hemnani63130c36e863
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 941bc863d..752f64ea0 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -4105,7 +4105,7 @@ pub fn submitForm(self: *Frame, submitter_: ?*Element, form_: ?*Element.Html.For};} else if (URL.isJavascriptUrl(action) == false) {// A javascript: action is navigated to as is, without the entries.- action = try URL.concatQueryString(arena.allocator(), action, buf.written());+ action = try URL.setQuery(arena.allocator(), action, buf.written());}// Opened only once the submission goes ahead, so an aborted one leavesdiff --git a/src/browser/URL.zig b/src/browser/URL.zigindex 43345d981..eea555915 100644--- a/src/browser/URL.zig+++ b/src/browser/URL.zig@@ -624,28 +624,11 @@ fn buildUrlWithUserInfo(}}-pub fn concatQueryString(arena: Allocator, url: []const u8, query_string: []const u8) ![:0]const u8 {- if (query_string.len == 0) {- return arena.dupeSentinel(u8, url, 0);- }-- var buf: std.ArrayList(u8) = .empty;-- // the most space well need is the url + ('?' or '&') + the query_string + null terminator- try buf.ensureTotalCapacityPrecise(arena, url.len + 2 + query_string.len);- buf.appendSliceAssumeCapacity(url);-- if (std.mem.findScalar(u8, url, '?')) |index| {- const last_index = url.len - 1;- if (index != last_index and url[last_index] != '&') {- buf.appendAssumeCapacity('&');- }- } else {- buf.appendAssumeCapacity('?');- }- buf.appendSliceAssumeCapacity(query_string);- buf.appendAssumeCapacity(0);- return buf.items[0 .. buf.items.len - 1 :0];+/// Replaces the query of `url` with `query` and keeps its fragment.+pub fn setQuery(arena: Allocator, url: []const u8, query: []const u8) ![:0]const u8 {+ const hash_start = std.mem.findScalar(u8, url, '#') orelse url.len;+ const query_start = std.mem.findScalar(u8, url[0..hash_start], '?') orelse hash_start;+ return arena.printSentinel("{s}?{s}{s}", .{ url[0..query_start], query, url[hash_start..] }, 0);}pub fn getRobotsUrl(arena: Allocator, url: [:0]const u8) ![:0]const u8 {@@ -1361,32 +1344,32 @@ test "URL: eqlDocument" {}}-test "URL: concatQueryString" {+test "URL: setQuery" {const arena = testing.arena_allocator;{- const url = try concatQueryString(arena, "https://www.lightpanda.io/", "");- try testing.expectEqual("https://www.lightpanda.io/", url);+ const url = try setQuery(arena, "https://www.lightpanda.io/", "");+ try testing.expectEqual("https://www.lightpanda.io/?", url);}{- const url = try concatQueryString(arena, "https://www.lightpanda.io/index?", "");- try testing.expectEqual("https://www.lightpanda.io/index?", url);+ const url = try setQuery(arena, "https://www.lightpanda.io/index?", "a=b");+ try testing.expectEqual("https://www.lightpanda.io/index?a=b", url);}{- const url = try concatQueryString(arena, "https://www.lightpanda.io/index?", "a=b");+ const url = try setQuery(arena, "https://www.lightpanda.io/index?1=2", "a=b");try testing.expectEqual("https://www.lightpanda.io/index?a=b", url);}{- const url = try concatQueryString(arena, "https://www.lightpanda.io/index?1=2", "a=b");- try testing.expectEqual("https://www.lightpanda.io/index?1=2&a=b", url);+ const url = try setQuery(arena, "https://www.lightpanda.io/index#top", "a=b");+ try testing.expectEqual("https://www.lightpanda.io/index?a=b#top", url);}{- const url = try concatQueryString(arena, "https://www.lightpanda.io/index?1=2&", "a=b");- try testing.expectEqual("https://www.lightpanda.io/index?1=2&a=b", url);+ const url = try setQuery(arena, "https://www.lightpanda.io/index?1=2#top?x", "a=b");+ try testing.expectEqual("https://www.lightpanda.io/index?a=b#top?x", url);}}diff --git a/src/browser/tests/frames/form_get.html b/src/browser/tests/frames/form_get.htmlnew file mode 100644index 000000000..9b0be94aa--- /dev/null+++ b/src/browser/tests/frames/form_get.html@@ -0,0 +1,67 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- A GET submission sets the action URL's query to the form data: any query+ already in the action is replaced, and its fragment is kept after it.+ https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#submit-mutate-action -->++<script>+ function loadFrame(name, src) {+ return new Promise((resolve) => {+ const frame = document.createElement('iframe');+ frame.name = name;+ frame.onload = () => {+ if (frame.contentWindow.location.pathname.endsWith('/support/page.html') === false) return;+ frame.onload = null;+ resolve(frame);+ };+ frame.src = src;+ document.body.appendChild(frame);+ });+ }+</script>++<script id=get_replaces_action_query type=module>+ {+ const state = await testing.async();+ const frame = await loadFrame('get_query_frame', 'support/page.html?sort=name');++ const form = document.createElement('form');+ form.action = 'support/page.html?sort=name';+ form.target = 'get_query_frame';+ form.innerHTML = '<input name=sort value=price>';+ document.body.appendChild(form);++ frame.onload = () => state.resolve();+ form.submit();++ await state.done(() => {+ testing.expectEqual('?sort=price', frame.contentWindow.location.search);+ });+ }+</script>++<script id=get_keeps_fragment_after_query type=module>+ {+ // Appending the entries after the fragment would turn this into a+ // fragment navigation that never reaches the server.+ const state = await testing.async();+ const frame = await loadFrame('get_fragment_frame', 'support/page.html?sort=name#top');+ const initial_document = frame.contentDocument;++ const form = document.createElement('form');+ form.action = 'support/page.html?sort=name#top';+ form.target = 'get_fragment_frame';+ form.innerHTML = '<input name=sort value=price>';+ document.body.appendChild(form);++ frame.onload = () => state.resolve();+ form.submit();++ await state.done(() => {+ testing.expectEqual(false, frame.contentDocument === initial_document);+ testing.expectEqual('?sort=price', frame.contentWindow.location.search);+ testing.expectEqual('#top', frame.contentWindow.location.hash);+ });+ }+</script>