Lightpanda: the headless browser designed for AI and automation
webapi: add support for iframe srcdoc
Karl Seguincfb2fc5e9c15
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zigindex cdc75676d..7d6aa65bd 100644--- a/src/browser/Frame.zig+++ b/src/browser/Frame.zig@@ -578,6 +578,16 @@ pub fn base(self: *const Frame) [:0]const u8 {return self.base_url orelse self.url;}+fn referrerSource(self: *const Frame) [:0]const u8 {+ var frame = self;+ while (std.mem.startsWith(u8, frame.url, "about:")) {+ // about:blank and about:srcdoc documents aren't valid referrer sources,+ // use the parents+ frame = frame.parent orelse return frame.url;+ }+ return frame.url;+}+pub fn getTitle(self: *Frame) !?[]const u8 {if (self.window._document.is(Document.HTMLDocument)) |html_doc| {return try html_doc.getTitle(self);@@ -603,7 +613,7 @@ pub fn httpMetadata(self: *const Frame) HttpMetadata {// * refererpub fn headersForRequest(self: *Frame, transfer: *HttpClient.Transfer) !void {const arena = transfer.arena.allocator();- if (try referrer.compute(arena, self.referrer_policy, self.url, transfer.req.url)) |ref| {+ if (try referrer.compute(arena, self.referrer_policy, self.referrerSource(), transfer.req.url)) |ref| {try transfer.addHeader("Referer", ref, .{});transfer.req.referrer_policy = self.referrer_policy;}@@ -646,11 +656,12 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !voconst http_client = &session.browser.http_client;- // Handle synthetic navigations: about:blank and blob: URLs+ // Handle synthetic navigations: about:blank, about:srcdoc and blob: URLsconst is_about_blank = std.mem.eql(u8, "about:blank", request_url);- const is_blob = !is_about_blank and std.mem.startsWith(u8, request_url, "blob:");+ const is_srcdoc = !is_about_blank and std.mem.eql(u8, "about:srcdoc", request_url);+ const is_blob = !is_about_blank and !is_srcdoc and std.mem.startsWith(u8, request_url, "blob:");- if (is_about_blank or is_blob) {+ if (is_about_blank or is_srcdoc or is_blob) {if (is_blob) {if (!Blob.urlBelongsToOrigin(request_url, opts.initiator_origin)) {log.warn(.js, "invalid blob", .{ .url = request_url });@@ -658,7 +669,12 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !vo}}- self.url = if (is_about_blank) "about:blank" else try self.arena.dupeZ(u8, request_url);+ self.url = if (is_about_blank)+ "about:blank"+ else if (is_srcdoc)+ "about:srcdoc"+ else+ try self.arena.dupeZ(u8, request_url);// even though about:blank navigations may share the same _data_, we// have to do this to make sure window.location is at a unique _address_.@@ -675,13 +691,17 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !voself.origin = try URL.getOrigin(self.arena, request_url[5.. :0]);} else if (self.parent) |parent| {self.origin = parent.origin;- if (is_about_blank) {+ if (is_about_blank or is_srcdoc) {self.base_url = parent.base();+ // about:blank and about:srcdoc documents inherit their+ // creator's policy container, including the referrer policy+ self.referrer_policy = parent.referrer_policy;}} else if (self.window._opener) |opener| {self.origin = opener._frame.origin;if (is_about_blank) {self.base_url = opener._frame.base();+ self.referrer_policy = opener._frame.referrer_policy;}} else {self.origin = null;@@ -706,6 +726,30 @@ pub fn navigate(self: *Frame, request_url: [:0]const u8, opts: NavigateOpts) !voconst html = try parse_arena.dupe(u8, blob._slice);var parser = Parser.init(parse_arena.allocator(), self.document.asNode(), self, .{ .allow_declarative_shadow = true });parser.parse(html);+ } else if (is_srcdoc) {+ // The "response body" is the iframe's srcdoc attribute. Only an+ // iframe can navigate here (e.g. location = 'about:srcdoc' on a+ // root frame ends up with an empty document, like Chrome).+ const content = blk: {+ const iframe = self.iframe orelse break :blk "";+ break :blk iframe.asElement().getAttributeSafe(comptime .wrap("srcdoc")) orelse "";+ };+ if (content.len == 0) {+ // the parser emits nothing for an empty input; commit the+ // same html/head/body scaffolding an empty srcdoc implies+ self.document.injectBlank(self) catch |err| {+ log.err(.browser, "inject blank", .{ .err = err });+ return error.InjectBlankFailed;+ };+ } else {+ const parse_arena = try self.getArena(content.len, "Frame.parseSrcdoc");+ defer parse_arena.release();+ // A script executed mid-parse can rewrite the srcdoc attribute,+ // freeing the value under the parser; parse a copy.+ const html = try parse_arena.dupe(u8, content);+ var parser = Parser.init(parse_arena.allocator(), self.document.asNode(), self, .{ .allow_declarative_shadow = true });+ parser.parse(html);+ }} else {self.document.injectBlank(self) catch |err| {log.err(.browser, "inject blank", .{ .err = err });@@ -875,7 +919,7 @@ pub fn scheduleNavigation(self: *Frame, request_url: []const u8, opts: NavigateO// might change inside the function. So the code should be explicit about the// frame that it's acting on.fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url: []const u8, opts: NavigateOpts, nt: Navigation) !void {- const resolved_url, const is_about_blank = blk: {+ const resolved_url, const is_about_something = blk: {if (URL.isCompleteHTTPUrl(request_url)) {break :blk .{ try arena.dupeZ(u8, request_url), false };}@@ -885,6 +929,11 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_urlbreak :blk .{ "about:blank", true };}+ if (std.mem.eql(u8, request_url, "about:srcdoc")) {+ // like about:blank, a synchronous navigation handled by navigate+ break :blk .{ "about:srcdoc", true };+ }+// 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.@@ -970,13 +1019,14 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url// runs (processRootQueuedNavigation rebuilds the Page in-place), so// allocate from the QueuedNavigation arena which outlives that tear-down.var nav_opts = opts;- if (std.mem.startsWith(u8, originator.url, "http")) {+ const referrer_source = originator.referrerSource();+ if (std.mem.startsWith(u8, referrer_source, "http")) {if (nav_opts.referer == null) {- nav_opts.referer = try referrer.compute(arena.allocator(), originator.referrer_policy, originator.url, resolved_url);+ nav_opts.referer = try referrer.compute(arena.allocator(), originator.referrer_policy, referrer_source, resolved_url);nav_opts.referrer_policy = originator.referrer_policy;}if (nav_opts.initiator_url == null) {- nav_opts.initiator_url = try arena.dupeZ(u8, originator.url);+ nav_opts.initiator_url = try arena.dupeZ(u8, referrer_source);}}if (nav_opts.initiator_origin == null) {@@ -990,7 +1040,7 @@ fn scheduleNavigationWithArena(originator: *Frame, arena: *lp.Arena, request_url.opts = nav_opts,.arena = arena,.url = resolved_url,- .is_about_blank = is_about_blank,+ .is_about_something = is_about_something,.navigation_type = std.meta.activeTag(nt),};@@ -1117,7 +1167,7 @@ pub fn iframeCompletedLoading(self: *Frame, iframe: *IFrame, delays_load: bool).html => true,else => false,};- if (parsing_html and iframe._src.len > 0) {+ if (parsing_html and (iframe._src.len > 0 or iframe.hasSrcdoc())) {self.queueElementEvent(Factory.protoOf(iframe), .load) catch |err| {log.err(.frame, "iframe queue load", .{ .err = err, .url = iframe._src });};@@ -1797,15 +1847,23 @@ pub fn iframeAddedCallback(self: *Frame, iframe: *IFrame) !void {return;}- var src = iframe.asElement().getAttributeSafe(comptime .wrap("src")) orelse "";- if (src.len == 0) {- src = "about:blank";- }+ const src = blk: {+ if (iframe.hasSrcdoc()) {+ // srcdoc takes precedence over src, even when empty+ break :blk "about:srcdoc";+ }- if (URL.isCompleteHTTPUrl(src) and !URL.canParse(src, null)) {- // per spec, if we can't parse the URL, we should load about:blank- src = "about:blank";- }+ var src = iframe.asElement().getAttributeSafe(comptime .wrap("src")) orelse "";+ if (src.len == 0) {+ src = "about:blank";+ }++ if (URL.isCompleteHTTPUrl(src) and !URL.canParse(src, null)) {+ // per spec, if we can't parse the URL, we should load about:blank+ src = "about:blank";+ }+ break :blk src;+ };if (iframe._window != null) {// This frame is being re-navigated. We need to do this through a@@ -1849,6 +1907,9 @@ pub fn iframeAddedCallback(self: *Frame, iframe: *IFrame) !void {if (std.mem.eql(u8, src, "about:blank")) {break :blk "about:blank"; // navigate will handle this special case}+ if (std.mem.eql(u8, src, "about:srcdoc")) {+ break :blk "about:srcdoc"; // navigate will handle this special case+ }break :blk try URL.resolve(self.call_arena, // ok to use, frame.navigate dupes thisself.base(),@@ -1869,12 +1930,14 @@ pub fn iframeAddedCallback(self: *Frame, iframe: *IFrame) !void {// Iframe's initial src request carries the parent's URL as Referer// (subject to the parent's Referrer-Policy) and as the SameSite- // initiator. Parent frame outlives this navigate() call, so the slice- // is safe; navigate dupes what it keeps.- const parent_url: ?[:0]const u8 = if (std.mem.startsWith(u8, self.url, "http")) self.url else null;+ // initiator. When this frame is itself an about: document, the nearest+ // ancestor's URL is the referrer source. Parent frame outlives this+ // navigate() call, so the slice is safe; navigate dupes what it keeps.+ const referrer_source = self.referrerSource();+ const parent_url: ?[:0]const u8 = if (std.mem.startsWith(u8, referrer_source, "http")) referrer_source else null;new_frame.navigate(url, .{.reason = .initialFrameNavigation,- .referer = try referrer.compute(self.call_arena, self.referrer_policy, self.url, url),+ .referer = try referrer.compute(self.call_arena, self.referrer_policy, referrer_source, url),.referrer_policy = self.referrer_policy,.initiator_url = parent_url,.initiator_origin = self.origin,@@ -3174,7 +3237,7 @@ pub const QueuedNavigation = struct {arena: *lp.Arena,url: [:0]const u8,opts: NavigateOpts,- is_about_blank: bool,+ is_about_something: bool, // about:blank or about:srcdocnavigation_type: NavigationType,};diff --git a/src/browser/Session.zig b/src/browser/Session.zigindex bc1ee1a92..f24c58fc8 100644--- a/src/browser/Session.zig+++ b/src/browser/Session.zig@@ -598,8 +598,8 @@ fn processPageQueuedNavigation(self: *Session, page: *Page) !void {continue;};- if (qn.is_about_blank) {- // Defer about:blank to second pass+ if (qn.is_about_something) {+ // Defer about:blank or about:srcdoc to second passtry about_blank_queue.append(self.arena.allocator(), frame);continue;}@@ -637,7 +637,7 @@ fn processPageQueuedNavigation(self: *Session, page: *Page) !void {while (i < new_navigations.items.len) {const frame = new_navigations.items[i];if (frame._queued_navigation) |qn| {- if (qn.is_about_blank) {+ if (qn.is_about_something) {log.warn(.frame, "recursive about blank", .{});_ = page.queued_navigation.swapRemove(i);continue;@@ -783,7 +783,7 @@ fn processRootQueuedNavigation(self: *Session, page: *Page) !void {// Synthetic navigations (about:blank, blob:) commit instantly — no HTTP,// so there is no in-flight window to worry about. Use the optimized// immediate-swap path for them.- const is_synthetic = qn.is_about_blank or std.mem.startsWith(u8, qn.url, "blob:");+ const is_synthetic = qn.is_about_something or std.mem.startsWith(u8, qn.url, "blob:");// The qn arena is consumed here regardless of success — frame.navigate// dupes the URL into the page's own arena, so we can release the qndiff --git a/src/browser/tests/frames/srcdoc.html b/src/browser/tests/frames/srcdoc.htmlnew file mode 100644index 000000000..debd1333d--- /dev/null+++ b/src/browser/tests/frames/srcdoc.html@@ -0,0 +1,183 @@+<!DOCTYPE html>+<script src="../testing.js"></script>++<!-- parser-inserted srcdoc iframe; its load event is queued while the parent+ is still parsing, so a later script's onload check observes it -->+<iframe id=static_srcdoc srcdoc="<p>static</p>"></iframe>++<script id=static_parse>+ testing.onload(() => {+ const doc = document.getElementById('static_srcdoc').contentDocument;+ testing.expectEqual('static', doc.querySelector('p').textContent);+ });+</script>++<script id=basic type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '<p>hello</p>';+ f.onload = () => state.resolve(f);+ document.body.appendChild(f);++ await state.done((f) => {+ // same-origin as the parent: contentDocument is readable+ testing.expectEqual('hello', f.contentDocument.querySelector('p').textContent);+ testing.expectEqual('about:srcdoc', f.contentWindow.location.href);+ testing.expectEqual('about:srcdoc', f.contentDocument.URL);+ // base URL is inherited from the parent+ testing.expectEqual(document.baseURI, f.contentDocument.baseURI);+ });+ }+</script>++<script id=srcdoc_precedence_over_src type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.setAttribute('src', '/xhr');+ f.setAttribute('srcdoc', '<p>doc wins</p>');+ f.onload = () => state.resolve(f);+ document.body.appendChild(f);++ await state.done((f) => {+ testing.expectEqual('doc wins', f.contentDocument.querySelector('p').textContent);+ testing.expectEqual('about:srcdoc', f.contentWindow.location.href);+ });+ }+</script>++<script id=script_runs_and_posts type=module>+ {+ const state = await testing.async();+ const listener = (e) => {+ if (e.data && e.data.test === 'script_runs') {+ window.removeEventListener('message', listener);+ state.resolve(e.data);+ }+ };+ window.addEventListener('message', listener);++ const f = document.createElement('iframe');+ f.srcdoc = "<script>parent.postMessage({test: 'script_runs', base: document.baseURI}, '*')<\/script>";+ document.body.appendChild(f);++ await state.done((data) => {+ testing.expectEqual(document.baseURI, data.base);+ });+ }+</script>++<script id=reassign_reloads type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '<p>first</p>';+ let loads = 0;+ f.onload = () => {+ loads += 1;+ if (loads === 1) {+ testing.expectEqual('first', f.contentDocument.querySelector('p').textContent);+ // setAttribute on a connected iframe re-navigates too+ f.setAttribute('srcdoc', '<p>second</p>');+ return;+ }+ state.resolve(f);+ };+ document.body.appendChild(f);++ await state.done((f) => {+ testing.expectEqual('second', f.contentDocument.querySelector('p').textContent);+ });+ }+</script>++<script id=remove_falls_back_to_src type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '<p>content</p>';+ let loads = 0;+ f.onload = () => {+ loads += 1;+ if (loads === 1) {+ f.removeAttribute('srcdoc');+ return;+ }+ state.resolve(f);+ };+ document.body.appendChild(f);++ await state.done((f) => {+ // no src attribute, so removal navigates back to about:blank+ testing.expectEqual('about:blank', f.contentWindow.location.href);+ testing.expectEqual(null, f.contentDocument.querySelector('p'));+ });+ }+</script>++<script id=empty_srcdoc type=module>+ {+ const state = await testing.async();++ const f = document.createElement('iframe');+ f.srcdoc = '';+ f.onload = () => state.resolve(f);+ document.body.appendChild(f);++ await state.done((f) => {+ testing.expectEqual('about:srcdoc', f.contentWindow.location.href);+ testing.expectEqual('', f.contentDocument.body.textContent);+ });+ }+</script>++<script id=referrer_is_parent type=module>+ {+ // a request from inside a srcdoc frame carries the parent's URL as the+ // Referer (about:srcdoc isn't a valid referrer source); the relative+ // fetch URL also proves base inheritance+ const state = await testing.async();+ const listener = (e) => {+ if (e.data && e.data.test === 'referrer') {+ window.removeEventListener('message', listener);+ state.resolve(e.data);+ }+ };+ window.addEventListener('message', listener);++ const f = document.createElement('iframe');+ f.srcdoc = "<script>fetch('/echo_referer').then(r => r.text()).then(t => parent.postMessage({test: 'referrer', body: t}, '*'))<\/script>";+ document.body.appendChild(f);++ await state.done((data) => {+ testing.expectEqual(true, data.body.includes('referer=' + location.href));+ });+ }+</script>++<script id=meta_referrer_in_srcdoc type=module>+ {+ // <meta name=referrer> inside the srcdoc content sets that frame's policy+ const state = await testing.async();+ const listener = (e) => {+ if (e.data && e.data.test === 'meta_referrer') {+ window.removeEventListener('message', listener);+ state.resolve(e.data);+ }+ };+ window.addEventListener('message', listener);++ const f = document.createElement('iframe');+ f.srcdoc = "<meta name='referrer' content='no-referrer'><script>fetch('/echo_referer').then(r => r.text()).then(t => parent.postMessage({test: 'meta_referrer', body: t}, '*'))<\/script>";+ document.body.appendChild(f);++ await state.done((data) => {+ testing.expectEqual(true, data.body.includes('referer=NONE'));+ });+ }+</script>diff --git a/src/browser/webapi/element/html/IFrame.zig b/src/browser/webapi/element/html/IFrame.zigindex 8849c8f6b..4dc882b4d 100644--- a/src/browser/webapi/element/html/IFrame.zig+++ b/src/browser/webapi/element/html/IFrame.zig@@ -31,6 +31,7 @@ const DOMTokenList = @import("../../collections.zig").DOMTokenList;const HtmlElement = @import("../Html.zig");+const String = lp.String;const IFrame = @This();pub const Proto = HtmlElement;@@ -79,6 +80,19 @@ pub fn setSrc(self: *IFrame, src: []const u8, frame: *Frame) !void {}}+pub fn hasSrcdoc(self: *IFrame) bool {+ return self.asElement().getAttributeSafe(comptime .wrap("srcdoc")) != null;+}++pub fn getSrcdoc(self: *IFrame) []const u8 {+ return self.asElement().getAttributeSafe(comptime .wrap("srcdoc")) orelse "";+}++pub fn setSrcdoc(self: *IFrame, value: []const u8, frame: *Frame) !void {+ // Build.attributeChange triggers the (re)navigation.+ try self.asElement().setAttributeSafe(comptime .wrap("srcdoc"), .wrap(value), frame);+}+pub fn getName(self: *IFrame) []const u8 {return self.asElement().getAttributeSafe(comptime .wrap("name")) orelse "";}@@ -105,6 +119,7 @@ pub const JsApi = struct {};pub const src = bridge.accessor(IFrame.getSrc, IFrame.setSrc, .{ .ce_reactions = true });+ pub const srcdoc = bridge.accessor(IFrame.getSrcdoc, IFrame.setSrcdoc, .{ .ce_reactions = true });pub const name = bridge.accessor(IFrame.getName, IFrame.setName, .{ .ce_reactions = true });pub const contentWindow = bridge.accessor(IFrame.getContentWindow, null, .{});pub const contentDocument = bridge.accessor(IFrame.getContentDocument, null, .{});@@ -117,4 +132,28 @@ pub const Build = struct {const element = self.asElement();self._src = element.getAttributeSafe(comptime .wrap("src")) orelse "";}++ pub fn attributeChange(element: *Element, name: String, _: String, frame: *Frame) !void {+ if (!name.eql(comptime .wrap("srcdoc"))) {+ return;+ }+ if (element.asNode().isConnected()) {+ // like src, setting srcdoc reloads the frame even if the value didn't change+ const self = element.as(IFrame);+ self._executed = false;+ try frame.iframeAddedCallback(self);+ }+ }++ pub fn attributeRemove(element: *Element, name: String, frame: *Frame) !void {+ if (!name.eql(comptime .wrap("srcdoc"))) {+ return;+ }+ if (element.asNode().isConnected()) {+ const self = element.as(IFrame);+ // removing srcdoc falls back to src (or about:blank)+ self._executed = false;+ try frame.iframeAddedCallback(self);+ }+ }};