Skip to content

Partial window.frames implementation #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/browser/dom/nodelist.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub const NodeList = struct {
try self.nodes.append(alloc, node);
}

pub fn get_length(self: *NodeList) u32 {
pub fn get_length(self: *const NodeList) u32 {
return @intCast(self.nodes.items.len);
}

Expand Down
8 changes: 1 addition & 7 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ pub const Interfaces = .{
HTMLHeadElement,
HTMLHeadingElement,
HTMLHtmlElement,
HTMLIFrameElement,
HTMLImageElement,
HTMLImageElement.Factory,
HTMLInputElement,
Expand Down Expand Up @@ -102,6 +101,7 @@ pub const Interfaces = .{
HTMLVideoElement,

@import("form.zig").HTMLFormElement,
@import("iframe.zig").HTMLIFrameElement,
@import("select.zig").Interfaces,
};

Expand Down Expand Up @@ -584,12 +584,6 @@ pub const HTMLHtmlElement = struct {
pub const subtype = .node;
};

pub const HTMLIFrameElement = struct {
pub const Self = parser.IFrame;
pub const prototype = *HTMLElement;
pub const subtype = .node;
};

pub const HTMLImageElement = struct {
pub const Self = parser.Image;
pub const prototype = *HTMLElement;
Expand Down
30 changes: 30 additions & 0 deletions src/browser/html/iframe.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
//
// Francis Bouvier <[email protected]>
// Pierre Tachoire <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const Allocator = std.mem.Allocator;

const parser = @import("../netsurf.zig");
const Page = @import("../page.zig").Page;
const HTMLElement = @import("elements.zig").HTMLElement;

// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#htmliframeelement
pub const HTMLIFrameElement = struct {
pub const Self = parser.IFrame;
pub const prototype = *HTMLElement;
pub const subtype = .node;
};
70 changes: 69 additions & 1 deletion src/browser/html/window.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const MediaQueryList = @import("media_query_list.zig").MediaQueryList;
const Performance = @import("../dom/performance.zig").Performance;
const CSSStyleDeclaration = @import("../cssom/CSSStyleDeclaration.zig");
const Screen = @import("screen.zig").Screen;
const domcss = @import("../dom/css.zig");
const Css = @import("../css/css.zig").Css;

const Function = Env.Function;
Expand Down Expand Up @@ -127,7 +128,43 @@ pub const Window = struct {
return self;
}

// TODO: frames
// frames return the window itself, but accessing it via a pseudo
// array returns the Window object corresponding to the given frame or
// iframe.
// https://developer.mozilla.org/en-US/docs/Web/API/Window/frames
pub fn get_frames(self: *Window) *Window {
return self;
}

pub fn indexed_get(self: *Window, index: u32, has_value: *bool, page: *Page) !*Window {
const frames = try domcss.querySelectorAll(
page.call_arena,
parser.documentHTMLToNode(self.document),
"iframe",
);

if (index >= frames.nodes.items.len) {
has_value.* = false;
return undefined;
}

has_value.* = true;
// TODO return the correct frame's window
// frames.nodes.items[indexed]
return error.TODO;
}

// Retrieve the numbre of frames/iframes from the DOM dynamically.
pub fn get_length(self: *const Window, page: *Page) !u32 {
const frames = try domcss.querySelectorAll(
page.call_arena,
parser.documentHTMLToNode(self.document),
"iframe",
);

return frames.get_length();
}

pub fn get_top(self: *Window) *Window {
return self;
}
Expand Down Expand Up @@ -504,6 +541,14 @@ test "Browser.HTML.Window" {
.{ "scrollend", "true" },
}, .{});

try runner.testCases(&.{
.{ "window == window.self", "true" },
.{ "window == window.parent", "true" },
.{ "window == window.top", "true" },
.{ "window == window.frames", "true" },
.{ "window.frames.length", "0" },
}, .{});

try runner.testCases(&.{
.{ "var qm = false; window.queueMicrotask(() => {qm = true });", null },
.{ "qm", "true" },
Expand All @@ -526,3 +571,26 @@ test "Browser.HTML.Window" {
}, .{});
}
}

test "Browser.HTML.Window.frames" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html =
\\<body>
\\ <iframe
\\ src="https://httpbin.io/html"
\\ title="iframea">
\\ </iframe>
\\ <iframe
\\ src="https://httpbin.io/html"
\\ title="iframeb">
\\ </iframe>
\\</body>
});

defer runner.deinit();

try runner.testCases(&.{
.{ "frames.length", "2" },
.{ "try { frames[1] } catch (e) { e }", "Error: TODO" }, // TODO fixme
.{ "frames[3]", "undefined" },
}, .{});
}