Skip to content

Lightpanda: the headless browser designed for AI and automation

browser: run a javascript: URL instead of navigating to it

Navid EMADc291804c1a31
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index e616826fc..cde7102a4 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -672,6 +672,12 @@ pub fn getPinnedArena(self: *Frame, size_or_bucket: anytype, debug: []const u8)
return self._session.getPinnedArena(size_or_bucket, debug);
}
+fn sameOrigin(a: *const Frame, b: *const Frame) bool {
+ const a_origin = a.origin orelse return false;
+ const b_origin = b.origin orelse return false;
+ return std.mem.eql(u8, a_origin, b_origin);
+}
+
pub fn isSameOrigin(self: *const Frame, url: [:0]const u8) bool {
const current_origin = self.origin orelse return false;
return URL.isSameOrigin(url, current_origin);
@@ -1029,6 +1035,18 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url
.iframe => |iframe| iframe._window.?._frame, // only an frame with existing content (i.e. a window) can be navigated
};
+ // A javascript: URL is not fetched: its script runs in the target's
+ // document, and only for an initiator of the same origin.
+ // https://html.spec.whatwg.org/multipage/browsing-the-web.html#navigate
+ if (std.mem.startsWith(u8, resolved_url, "javascript:")) {
+ if (target == originator or sameOrigin(originator, target)) {
+ try user_input.runJavascriptUrl(target, resolved_url["javascript:".len..]);
+ }
+ // don't defer this, the caller is responsible for freeing it on error
+ arena.release();
+ return;
+ }
+
const session = target._session;
// Per the HTML "navigate" steps, this is a fragment navigation only when
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index e5ffa0a88..30f7a53be 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -661,7 +661,9 @@ pub fn findClickActivationTarget(target: *Node, bubbles: bool) ?*Node {
return null;
}
-fn runJavascriptUrl(frame: *Frame, source: []const u8) !void {
+// Runs a javascript: URL's script, `source` being the URL without its scheme.
+// It is percent-decoded first, as the "evaluate a javascript: URL" steps do.
+pub fn runJavascriptUrl(frame: *Frame, source: []const u8) !void {
const arena = try frame.getArena(.tiny, "javascript-url");
errdefer arena.release();
@@ -669,9 +671,7 @@ fn runJavascriptUrl(frame: *Frame, source: []const u8) !void {
task.* = .{
.frame = frame,
.arena = arena,
- // TODO: the URL body should be percent-decoded; hrefs written in
- // markup rarely are.
- .source = try arena.dupe(u8, source),
+ .source = std.Uri.percentDecodeInPlace(try arena.dupe(u8, source)),
};
try frame.js.scheduler.add(task, JavascriptUrlTask.run, 0, .{
.name = "javascript-url",
diff --git a/src/browser/tests/window/location_javascript_url.html b/src/browser/tests/window/location_javascript_url.html
new file mode 100644
index 000000000..5c8a424fd
--- /dev/null
+++ b/src/browser/tests/window/location_javascript_url.html
@@ -0,0 +1,51 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+
+<!--
+ Navigating to a javascript: URL through Location runs the URL's script in
+ this document, as a queued task, after percent-decoding it. Nothing is
+ fetched and the document stays.
+-->
+
+<script id=href type=module>
+{
+ const state = await testing.async();
+ window.__marker = 'kept';
+ window.__ran = [];
+ location.href = 'javascript:window.__ran.push("href%20ran")';
+ // Queued, not run synchronously.
+ testing.expectEqual([], window.__ran);
+ setTimeout(() => state.resolve(), 10);
+ await state.done(() => {
+ testing.expectEqual(['href ran'], window.__ran);
+ testing.expectEqual('kept', window.__marker);
+ testing.expectEqual(testing.BASE_URL + 'window/location_javascript_url.html', location.href);
+ });
+}
+</script>
+
+<script id=assign type=module>
+{
+ const state = await testing.async();
+ window.__assigned = 0;
+ location.assign('javascript:window.__assigned++');
+ setTimeout(() => state.resolve(), 10);
+ await state.done(() => {
+ testing.expectEqual(1, window.__assigned);
+ testing.expectEqual('kept', window.__marker);
+ });
+}
+</script>
+
+<script id=replace type=module>
+{
+ const state = await testing.async();
+ window.__replaced = 0;
+ location.replace(' JavaScript:void window.__replaced++');
+ setTimeout(() => state.resolve(), 10);
+ await state.done(() => {
+ testing.expectEqual(1, window.__replaced);
+ testing.expectEqual('kept', window.__marker);
+ });
+}
+</script>