Skip to content
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
4 changes: 2 additions & 2 deletions src/browser/dom/dom.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const DOMException = @import("exceptions.zig").DOMException;
const EventTarget = @import("event_target.zig").EventTarget;
const DOMImplementation = @import("implementation.zig").DOMImplementation;
const NamedNodeMap = @import("namednodemap.zig").NamedNodeMap;
const DOMTokenList = @import("token_list.zig").DOMTokenList;
const DOMTokenList = @import("token_list.zig");
const NodeList = @import("nodelist.zig");
const Node = @import("node.zig");
const MutationObserver = @import("mutation_observer.zig");
Expand All @@ -30,7 +30,7 @@ pub const Interfaces = .{
EventTarget,
DOMImplementation,
NamedNodeMap,
DOMTokenList,
DOMTokenList.Interfaces,
NodeList.Interfaces,
Node.Node,
Node.Interfaces,
Expand Down
8 changes: 4 additions & 4 deletions src/browser/dom/html_collection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const utils = @import("utils.z");
const Element = @import("element.zig").Element;
const Union = @import("element.zig").Union;

const JsObject = @import("../env.zig").JsObject;
const JsThis = @import("../env.zig").JsThis;

const Walker = @import("walker.zig").Walker;
const WalkerDepthFirst = @import("walker.zig").WalkerDepthFirst;
Expand Down Expand Up @@ -443,15 +443,15 @@ pub const HTMLCollection = struct {
return null;
}

pub fn postAttach(self: *HTMLCollection, js_obj: JsObject) !void {
pub fn postAttach(self: *HTMLCollection, js_this: JsThis) !void {
const len = try self.get_length();
for (0..len) |i| {
const node = try self.item(@intCast(i)) orelse unreachable;
const e = @as(*parser.Element, @ptrCast(node));
try js_obj.setIndex(@intCast(i), e);
try js_this.setIndex(@intCast(i), e);

if (try item_name(e)) |name| {
try js_obj.set(name, e);
try js_this.set(name, e);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/browser/dom/mutation_observer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const parser = @import("../netsurf.zig");
const SessionState = @import("../env.zig").SessionState;

const Env = @import("../env.zig").Env;
const JsObject = @import("../env.zig").JsObject;
const JsThis = @import("../env.zig").JsThis;
const NodeList = @import("nodelist.zig").NodeList;

pub const Interfaces = .{
Expand Down Expand Up @@ -184,9 +184,9 @@ pub const MutationRecords = struct {
return null;
};
}
pub fn postAttach(self: *const MutationRecords, js_obj: JsObject) !void {
pub fn postAttach(self: *const MutationRecords, js_this: JsThis) !void {
if (self.first) |mr| {
try js_obj.set("0", mr);
try js_this.set("0", mr);
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/browser/dom/nodelist.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const std = @import("std");

const parser = @import("../netsurf.zig");

const JsObject = @import("../env.zig").JsObject;
const JsThis = @import("../env.zig").JsThis;
const Callback = @import("../env.zig").Callback;
const SessionState = @import("../env.zig").SessionState;

Expand Down Expand Up @@ -177,11 +177,11 @@ pub const NodeList = struct {
}

// TODO entries() https://developer.mozilla.org/en-US/docs/Web/API/NodeList/entries
pub fn postAttach(self: *NodeList, js_obj: JsObject) !void {
pub fn postAttach(self: *NodeList, js_this: JsThis) !void {
const len = self.get_length();
for (0..len) |i| {
const node = try self._item(@intCast(i)) orelse unreachable;
try js_obj.setIndex(i, node);
try js_this.setIndex(i, node);
}
}
};
Expand Down
93 changes: 92 additions & 1 deletion src/browser/dom/token_list.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,22 @@
const std = @import("std");

const parser = @import("../netsurf.zig");
const iterator = @import("../iterator/iterator.zig");

const Callback = @import("../env.zig").Callback;
const JsObject = @import("../env.zig").JsObject;
const SessionState = @import("../env.zig").SessionState;
const DOMException = @import("exceptions.zig").DOMException;

const log = std.log.scoped(.token_list);

pub const Interfaces = .{
DOMTokenList,
DOMTokenListIterable,
TokenListEntriesIterator,
TokenListEntriesIterator.Iterable,
};

// https://dom.spec.whatwg.org/#domtokenlist
pub const DOMTokenList = struct {
pub const Self = parser.TokenList;
Expand Down Expand Up @@ -98,7 +111,60 @@ pub const DOMTokenList = struct {
}

pub fn get_value(self: *parser.TokenList) !?[]const u8 {
return try parser.tokenListGetValue(self);
return (try parser.tokenListGetValue(self)) orelse "";
}

pub fn set_value(self: *parser.TokenList, value: []const u8) !void {
return parser.tokenListSetValue(self, value);
}

pub fn _toString(self: *parser.TokenList) ![]const u8 {
return (try get_value(self)) orelse "";
}

pub fn _keys(self: *parser.TokenList) !iterator.U32Iterator {
return .{ .length = try get_length(self) };
}

pub fn _values(self: *parser.TokenList) DOMTokenListIterable {
return DOMTokenListIterable.init(.{ .token_list = self });
}

pub fn _entries(self: *parser.TokenList) TokenListEntriesIterator {
return TokenListEntriesIterator.init(.{ .token_list = self });
}

pub fn _symbol_iterator(self: *parser.TokenList) DOMTokenListIterable {
return _values(self);
}

// TODO handle thisArg
pub fn _forEach(self: *parser.TokenList, cbk: Callback, this_arg: JsObject) !void {
var entries = _entries(self);
while (try entries._next()) |entry| {
var result: Callback.Result = undefined;
cbk.tryCallWithThis(this_arg, .{ entry.@"1", entry.@"0", self }, &result) catch {
log.err("callback error: {s}", .{result.exception});
log.debug("stack:\n{s}", .{result.stack orelse "???"});
};
}
}
};

const DOMTokenListIterable = iterator.Iterable(Iterator, "DOMTokenListIterable");
const TokenListEntriesIterator = iterator.NumericEntries(Iterator, "TokenListEntriesIterator");

pub const Iterator = struct {
index: u32 = 0,
token_list: *parser.TokenList,

// used when wrapped in an iterator.NumericEntries
pub const Error = parser.DOMError;

pub fn _next(self: *Iterator) !?[]const u8 {
const index = self.index;
self.index = index + 1;
return DOMTokenList._item(self.token_list, index);
}
};

Expand Down Expand Up @@ -150,4 +216,29 @@ test "Browser.DOM.TokenList" {
.{ "cl4.replace('nok', 'ok')", "true" },
.{ "cl4.value", "empty ok" },
}, .{});

try runner.testCases(&.{
.{ "let cl5 = gs.classList", "undefined" },
.{ "let keys = [...cl5.keys()]", "undefined" },
.{ "keys.length", "2" },
.{ "keys[0]", "0" },
.{ "keys[1]", "1" },

.{ "let values = [...cl5.values()]", "undefined" },
.{ "values.length", "2" },
.{ "values[0]", "empty" },
.{ "values[1]", "ok" },

.{ "let entries = [...cl5.entries()]", "undefined" },
.{ "entries.length", "2" },
.{ "entries[0]", "0,empty" },
.{ "entries[1]", "1,ok" },
}, .{});

try runner.testCases(&.{
.{ "let cl6 = gs.classList", "undefined" },
.{ "cl6.value = 'a b ccc'", "a b ccc" },
.{ "cl6.value", "a b ccc" },
.{ "cl6.toString()", "a b ccc" },
}, .{});
}
1 change: 1 addition & 0 deletions src/browser/env.zig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Interfaces = generate.Tuple(.{
@import("xmlserializer/xmlserializer.zig").Interfaces,
});

pub const JsThis = Env.JsThis;
pub const JsObject = Env.JsObject;
pub const Callback = Env.Callback;
pub const Env = js.Env(*SessionState, Interfaces{});
Expand Down
Loading
Loading