Lightpanda: the headless browser designed for AI and automation
webapi: resolve Location URLs against the calling document
Navid EMAD5632314b4ded
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex 8e3287a28..cad80ab7f 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -580,6 +580,22 @@ pub fn base(self: *const Frame) [:0]const u8 {return self.base_url orelse self.url;}+// The base a relative navigation URL resolves against: this frame's base,+// unless it's "about:blank", in which case we have to walk up the parents and+// find a real base.+pub fn navigationBase(self: *const Frame) [:0]const u8 {+ var maybe_not_blank_frame = self;+ while (true) {+ const maybe_base = maybe_not_blank_frame.base();+ if (std.mem.eql(u8, maybe_base, "about:blank") == false) {+ return maybe_base;+ }+ // The orelse here is probably an invalid case, but there isn't+ // anything we can do about it. It should never happen?+ maybe_not_blank_frame = maybe_not_blank_frame.parent orelse return "";+ }+}+pub fn referrerSource(self: *const Frame) [:0]const u8 {var frame = self;while (std.mem.startsWith(u8, frame.url, "about:")) {@@ -973,24 +989,10 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url}// request_url isn't a "complete" URL, so it has to be resolved with the- // originator's base. Unless, originator's base is "about:blank", in which- // case we have to walk up the parents and find a real base.- const frame_base = base_blk: {- var maybe_not_blank_frame = originator;- while (true) {- const maybe_base = maybe_not_blank_frame.base();- if (std.mem.eql(u8, maybe_base, "about:blank") == false) {- break :base_blk maybe_base;- }- // The orelse here is probably an invalid case, but there isn't- // anything we can do about it. It should never happen?- maybe_not_blank_frame = maybe_not_blank_frame.parent orelse break :base_blk "";- }- };-+ // originator's base.const u = try URL.resolve(arena.allocator(),- frame_base,+ originator.navigationBase(),request_url,.{ .encoding = originator.charset },);diff --git a/src/browser/tests/frames/location_relative_url.html b/src/browser/tests/frames/location_relative_url.htmlnew file mode 100644index 000000000..88de408b1--- /dev/null+++ b/src/browser/tests/frames/location_relative_url.html@@ -0,0 +1,93 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- The Location href setter, assign() and replace() parse the URL relative+ to the entry settings object: the calling script's document, not the+ document of the window being navigated.+ https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-location-href+ The frames start at support/nested/start.html, so "support/page.html"+ resolves to support/page.html from this document ("a-page") and to+ support/nested/support/page.html from theirs ("nested-page"). -->++<script>+ function loadFrame(name) {+ 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.endsWith('/nested/start.html')) return;+ frame.onload = null;+ resolve(frame);+ };+ frame.src = 'support/nested/start.html';+ document.body.appendChild(frame);+ });+ }++ async function navigateFrame(name, navigate) {+ const frame = await loadFrame(name);+ const loaded = new Promise((resolve) => frame.onload = resolve);+ navigate(frame);+ await loaded;+ return frame.contentDocument.body.textContent.trim();+ }+</script>++<script id=href_setter type=module>+ const state = await testing.async();+ const text = await navigateFrame('href_frame', (frame) => {+ frame.contentWindow.location.href = 'support/page.html';+ });+ state.resolve();+ await state.done(() => {+ testing.expectEqual('a-page', text);+ });+</script>++<script id=assign type=module>+ const state = await testing.async();+ const text = await navigateFrame('assign_frame', (frame) => {+ frame.contentWindow.location.assign('support/page.html');+ });+ state.resolve();+ await state.done(() => {+ testing.expectEqual('a-page', text);+ });+</script>++<script id=replace type=module>+ const state = await testing.async();+ const text = await navigateFrame('replace_frame', (frame) => {+ frame.contentWindow.location.replace('support/page.html');+ });+ state.resolve();+ await state.done(() => {+ testing.expectEqual('a-page', text);+ });+</script>++<script id=window_location_setter type=module>+ const state = await testing.async();+ const text = await navigateFrame('window_frame', (frame) => {+ frame.contentWindow.location = 'support/page.html';+ });+ state.resolve();+ await state.done(() => {+ testing.expectEqual('a-page', text);+ });+</script>++<script id=own_script type=module>+ // The frame's own script is the entry: its document's base applies.+ const state = await testing.async();+ const text = await navigateFrame('own_frame', (frame) => {+ const script = frame.contentDocument.createElement('script');+ script.textContent = "location.href = 'support/page.html'";+ frame.contentDocument.body.appendChild(script);+ });+ state.resolve();+ await state.done(() => {+ testing.expectEqual('nested-page', text);+ });+</script>diff --git a/src/browser/tests/frames/support/nested/start.html b/src/browser/tests/frames/support/nested/start.htmlnew file mode 100644index 000000000..97aef00e7--- /dev/null+++ b/src/browser/tests/frames/support/nested/start.html@@ -0,0 +1,2 @@+<!DOCTYPE html>+nested-startdiff --git a/src/browser/tests/frames/support/nested/support/page.html b/src/browser/tests/frames/support/nested/support/page.htmlnew file mode 100644index 000000000..d5dae0d89--- /dev/null+++ b/src/browser/tests/frames/support/nested/support/page.html@@ -0,0 +1 @@+nested-pagediff --git a/src/browser/webapi/Location.zig b/src/browser/webapi/Location.zigindex e9443cdad..19e00e522 100644--- a/src/browser/webapi/Location.zig+++ b/src/browser/webapi/Location.zig@@ -132,12 +132,27 @@ fn setHash(_: *const Location, hash: []const u8, frame: *Frame) !void {}, .{ .script = frame });}-fn assign(_: *const Location, url: [:0]const u8, frame: *Frame) !void {- return frame.scheduleNavigation(url, .{ .reason = .script, .kind = .{ .push = null } }, .{ .script = frame });+// The href setter, assign() and replace() parse the URL relative to the entry+// settings object: the calling script's document. It isn't this location's+// document when a script navigates another same-origin window, as in+// iframe.contentWindow.location.href = "page.html". V8 only exposes the+// incumbent context, which is the entry one for a call made by a script.+fn parseFromCaller(url: [:0]const u8, frame: *Frame) ![]const u8 {+ const caller = frame.js.getIncumbent();+ if (caller == frame) {+ return url;+ }+ return U.resolve(frame.call_arena, caller.navigationBase(), url, .{ .encoding = caller.charset });+}++pub fn assign(_: *const Location, url: [:0]const u8, frame: *Frame) !void {+ const target_url = try parseFromCaller(url, frame);+ return frame.scheduleNavigation(target_url, .{ .reason = .script, .kind = .{ .push = null } }, .{ .script = frame });}pub fn replace(_: *const Location, url: [:0]const u8, frame: *Frame) !void {- return frame.scheduleNavigation(url, .{ .reason = .script, .kind = .{ .replace = null } }, .{ .script = frame });+ const target_url = try parseFromCaller(url, frame);+ return frame.scheduleNavigation(target_url, .{ .reason = .script, .kind = .{ .replace = null } }, .{ .script = frame });}pub fn reload(_: *const Location, frame: *Frame) !void {diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zigindex f464c3c62..bb7a0cde5 100644--- a/src/browser/webapi/Window.zig+++ b/src/browser/webapi/Window.zig@@ -356,8 +356,9 @@ pub fn getLocation(self: *const Window) *Location {return self._location;}+// [PutForwards=href]: window.location = url is location.href = url.fn setLocation(self: *Window, url: [:0]const u8, frame: *Frame) !void {- return frame.scheduleNavigation(url, .{ .reason = .script, .kind = .{ .push = null } }, .{ .script = self._frame });+ return self._location.assign(url, frame);}pub fn getHistory(_: *Window, frame: *Frame) *History {