dom: expose document.location

This commit is contained in:
Pierre Tachoire
2025-01-23 17:08:02 +01:00
parent 09ba4bcf43
commit 318e2bd1c6
8 changed files with 28 additions and 12 deletions

View File

@@ -391,7 +391,7 @@ pub const Page = struct {
// TODO set the referrer to the document.
self.session.window.replaceDocument(html_doc);
try self.session.window.replaceDocument(html_doc);
self.session.window.setStorageShelf(
try self.session.storageShed.getOrPut(self.origin orelse "null"),
);

View File

@@ -28,6 +28,7 @@ const Node = @import("../dom/node.zig").Node;
const Document = @import("../dom/document.zig").Document;
const NodeList = @import("../dom/nodelist.zig").NodeList;
const HTMLElem = @import("elements.zig");
const Location = @import("location.zig").Location;
const collection = @import("../dom/html_collection.zig");
const Walker = @import("../dom/walker.zig").WalkerDepthFirst;
@@ -157,6 +158,10 @@ pub const HTMLDocument = struct {
return try parser.documentHTMLGetCurrentScript(self);
}
pub fn get_location(self: *parser.DocumentHTML) !?*Location {
return try parser.documentHTMLGetLocation(Location, self);
}
pub fn get_designMode(_: *parser.DocumentHTML) []const u8 {
return "off";
}

View File

@@ -115,6 +115,7 @@ pub fn testExecFn(
) anyerror!void {
var location = [_]Case{
.{ .src = "location.href", .ex = "" },
.{ .src = "document.location.href", .ex = "" },
};
try checkCases(js_env, &location);
}

View File

@@ -62,12 +62,17 @@ pub const Window = struct {
};
}
pub fn replaceLocation(self: *Window, loc: Location) void {
pub fn replaceLocation(self: *Window, loc: Location) !void {
self.location = loc;
if (self.doc != null) {
try parser.documentHTMLSetLocation(Location, self.doc.?, &self.location);
}
}
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) void {
pub fn replaceDocument(self: *Window, doc: *parser.DocumentHTML) !void {
self.document = doc;
try parser.documentHTMLSetLocation(Location, doc, &self.location);
}
pub fn setStorageShelf(self: *Window, shelf: *storage.Shelf) void {

View File

@@ -55,7 +55,7 @@ fn execJS(
// alias global as self and window
var window = Window.create(null, null);
window.replaceDocument(doc);
try window.replaceDocument(doc);
window.setStorageShelf(&storageShelf);
try js_env.bindGlobal(window);

View File

@@ -2265,14 +2265,19 @@ pub fn documentHTMLGetCurrentScript(doc: *DocumentHTML) !?*Script {
return @ptrCast(elem.?);
}
pub fn documentHTMLSetLocation(doc: *DocumentHTML, location: ?*anyopaque) !void {
const err = documentHTMLVtable(doc).set_location.?(doc, location);
pub fn documentHTMLSetLocation(T: type, doc: *DocumentHTML, location: *T) !void {
const l = @as(*anyopaque, @ptrCast(location));
const err = documentHTMLVtable(doc).set_location.?(doc, l);
try DOMErr(err);
}
pub fn documentHTMLGetLocation(doc: *DocumentHTML) !?*anyopaque {
var loc: ?*anyopaque = undefined;
const err = documentHTMLVtable(doc).get_location.?(doc, &loc);
pub fn documentHTMLGetLocation(T: type, doc: *DocumentHTML) !?*T {
var l: ?*anyopaque = undefined;
const err = documentHTMLVtable(doc).get_location.?(doc, &l);
try DOMErr(err);
return loc;
if (l == null) return null;
const ptr: *align(@alignOf(*T)) anyopaque = @alignCast(l.?);
return @as(*T, @ptrCast(ptr));
}

View File

@@ -98,7 +98,7 @@ fn testExecFn(
// alias global as self and window
var window = Window.create(null, null);
window.replaceDocument(doc);
try window.replaceDocument(doc);
window.setStorageShelf(&storageShelf);
try js_env.bindGlobal(window);

View File

@@ -91,7 +91,7 @@ pub fn run(arena: *std.heap.ArenaAllocator, comptime dir: []const u8, f: []const
// setup global env vars.
var window = Window.create(null, null);
window.replaceDocument(html_doc);
try window.replaceDocument(html_doc);
window.setStorageShelf(&storageShelf);
try js_env.bindGlobal(&window);