Skip to content

Lightpanda: the headless browser designed for AI and automation

browser: ignore a '?' inside the fragment when reading the URL search

Jay Hemnanid34644088179
diff --git a/src/browser/URL.zig b/src/browser/URL.zig
index 39f10d6d1..ea682b6f9 100644
--- a/src/browser/URL.zig
+++ b/src/browser/URL.zig
@@ -305,14 +305,9 @@ fn findPortSeparator(host: []const u8) ?usize {
}
pub fn getSearch(raw: [:0]const u8) []const u8 {
- const pos = std.mem.findScalarPos(u8, raw, 0, '?') orelse return "";
- const query_part = raw[pos..];
-
- if (std.mem.findScalarPos(u8, query_part, 0, '#')) |fragment_start| {
- return query_part[0..fragment_start];
- }
-
- return query_part;
+ const end = std.mem.findScalar(u8, raw, '#') orelse raw.len;
+ const pos = std.mem.findScalar(u8, raw[0..end], '?') orelse return "";
+ return raw[pos..end];
}
pub fn getHash(raw: [:0]const u8) []const u8 {
@@ -1541,6 +1536,13 @@ test "URL: getPort" {
try testing.expectEqualSlices(u8, "", getPort("https://[2001:db8::1]/"));
}
+test "URL: getSearch ignores a '?' in the fragment" {
+ try testing.expectEqualSlices(u8, "", getSearch("https://example.com/app#/item?id=5"));
+ try testing.expectEqualSlices(u8, "?q=1", getSearch("https://example.com/app?q=1#/item?id=5"));
+ try testing.expectEqualSlices(u8, "?q=1", getSearch("https://example.com/app?q=1"));
+ try testing.expectEqualSlices(u8, "", getSearch("https://example.com/app"));
+}
+
test "URL: setPathname percent-encodes" {
// Use arena allocator to match production usage (setPathname makes intermediate allocations)
var arena = std.heap.ArenaAllocator.init(testing.allocator);
diff --git a/src/browser/tests/element/html/anchor.html b/src/browser/tests/element/html/anchor.html
index ccabf3ee9..679305664 100644
--- a/src/browser/tests/element/html/anchor.html
+++ b/src/browser/tests/element/html/anchor.html
@@ -188,6 +188,24 @@
}
</script>
+<script id=anchor_question_mark_in_fragment>
+{
+ let a = document.createElement('a');
+ a.href = 'https://example.com/app#/item?id=5';
+ testing.expectEqual('', a.search);
+ testing.expectEqual('#/item?id=5', a.hash);
+
+ a.pathname = '/other';
+ testing.expectEqual('https://example.com/other#/item?id=5', a.href);
+
+ a.hash = '#top';
+ testing.expectEqual('https://example.com/other#top', a.href);
+
+ a.href = 'https://example.com/app?q=1#/item?id=5';
+ testing.expectEqual('?q=1', a.search);
+}
+</script>
+
<script id=anchor_name_attribute>
{
let a = document.createElement('a');