Skip to content
Closed
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
16 changes: 8 additions & 8 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,18 @@ pub const Document = struct {
// HTMLCollection in zig here.
pub fn _getElementsByTagName(
self: *parser.Document,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(alloc, parser.documentToNode(self), tag_name, true);
return try collection.HTMLCollectionByTagName(arena, parser.documentToNode(self), tag_name, true);
}

pub fn _getElementsByClassName(
self: *parser.Document,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
classNames: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByClassName(alloc, parser.documentToNode(self), classNames, true);
return try collection.HTMLCollectionByClassName(arena, parser.documentToNode(self), classNames, true);
}

pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment {
Expand Down Expand Up @@ -218,18 +218,18 @@ pub const Document = struct {
return 1;
}

pub fn _querySelector(self: *parser.Document, alloc: std.mem.Allocator, selector: []const u8) !?ElementUnion {
pub fn _querySelector(self: *parser.Document, arena: std.mem.Allocator, selector: []const u8) !?ElementUnion {
if (selector.len == 0) return null;

const n = try css.querySelector(alloc, parser.documentToNode(self), selector);
const n = try css.querySelector(arena, parser.documentToNode(self), selector);

if (n == null) return null;

return try Element.toInterface(parser.nodeToElement(n.?));
}

pub fn _querySelectorAll(self: *parser.Document, alloc: std.mem.Allocator, selector: []const u8) !NodeList {
return css.querySelectorAll(alloc, parser.documentToNode(self), selector);
pub fn _querySelectorAll(self: *parser.Document, arena: std.mem.Allocator, selector: []const u8) !NodeList {
return css.querySelectorAll(arena, parser.documentToNode(self), selector);
}

// TODO according with https://dom.spec.whatwg.org/#parentnode, the
Expand Down
36 changes: 14 additions & 22 deletions src/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,16 @@ pub const Element = struct {
return try parser.nodeGetAttributes(parser.elementToNode(self));
}

pub fn get_innerHTML(self: *parser.Element, alloc: std.mem.Allocator) ![]const u8 {
var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit();

pub fn get_innerHTML(self: *parser.Element, arena: std.mem.Allocator) ![]const u8 {
var buf = std.ArrayList(u8).init(arena);
try dump.writeChildren(parser.elementToNode(self), buf.writer());
// TODO express the caller owned the slice.
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
return buf.toOwnedSlice();
return buf.items;
}

pub fn get_outerHTML(self: *parser.Element, alloc: std.mem.Allocator) ![]const u8 {
var buf = std.ArrayList(u8).init(alloc);
defer buf.deinit();

pub fn get_outerHTML(self: *parser.Element, arena: std.mem.Allocator) ![]const u8 {
var buf = std.ArrayList(u8).init(arena);
try dump.writeNode(parser.elementToNode(self), buf.writer());
// TODO express the caller owned the slice.
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
return buf.toOwnedSlice();
return buf.items;
}

pub fn set_innerHTML(self: *parser.Element, str: []const u8) !void {
Expand Down Expand Up @@ -232,11 +224,11 @@ pub const Element = struct {

pub fn _getElementsByTagName(
self: *parser.Element,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(
alloc,
arena,
parser.elementToNode(self),
tag_name,
false,
Expand All @@ -245,11 +237,11 @@ pub const Element = struct {

pub fn _getElementsByClassName(
self: *parser.Element,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
classNames: []const u8,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByClassName(
alloc,
arena,
parser.elementToNode(self),
classNames,
false,
Expand Down Expand Up @@ -312,18 +304,18 @@ pub const Element = struct {
}
}

pub fn _querySelector(self: *parser.Element, alloc: std.mem.Allocator, selector: []const u8) !?Union {
pub fn _querySelector(self: *parser.Element, arena: std.mem.Allocator, selector: []const u8) !?Union {
if (selector.len == 0) return null;

const n = try css.querySelector(alloc, parser.elementToNode(self), selector);
const n = try css.querySelector(arena, parser.elementToNode(self), selector);

if (n == null) return null;

return try toInterface(parser.nodeToElement(n.?));
}

pub fn _querySelectorAll(self: *parser.Element, alloc: std.mem.Allocator, selector: []const u8) !NodeList {
return css.querySelectorAll(alloc, parser.elementToNode(self), selector);
pub fn _querySelectorAll(self: *parser.Element, arena: std.mem.Allocator, selector: []const u8) !NodeList {
return css.querySelectorAll(arena, parser.elementToNode(self), selector);
}

// TODO according with https://dom.spec.whatwg.org/#parentnode, the
Expand Down
7 changes: 3 additions & 4 deletions src/dom/exceptions.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,18 @@ pub const DOMException = struct {
pub const _INVALID_NODE_TYPE_ERR = 24;
pub const _DATA_CLONE_ERR = 25;

// TODO: deinit
pub fn init(alloc: std.mem.Allocator, err: anyerror, callerName: []const u8) anyerror!DOMException {
pub fn init(arena: std.mem.Allocator, err: anyerror, callerName: []const u8) anyerror!DOMException {
const errCast = @as(parser.DOMError, @errorCast(err));
const errName = DOMException.name(errCast);
const str = switch (errCast) {
error.HierarchyRequest => try allocPrint(
alloc,
arena,
"{s}: Failed to execute '{s}' on 'Node': The new child element contains the parent.",
.{ errName, callerName },
),
error.NoError => unreachable,
else => try allocPrint(
alloc,
arena,
"{s}: TODO message", // TODO: implement other messages
.{DOMException.name(errCast)},
),
Expand Down
21 changes: 7 additions & 14 deletions src/dom/implementation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,34 @@ pub const DOMImplementation = struct {

pub fn _createDocumentType(
_: *DOMImplementation,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
qname: []const u8,
publicId: []const u8,
systemId: []const u8,
) !*parser.DocumentType {
const cqname = try alloc.dupeZ(u8, qname);
defer alloc.free(cqname);

const cpublicId = try alloc.dupeZ(u8, publicId);
defer alloc.free(cpublicId);

const csystemId = try alloc.dupeZ(u8, systemId);
defer alloc.free(csystemId);
const cqname = try arena.dupeZ(u8, qname);
const cpublicId = try arena.dupeZ(u8, publicId);
const csystemId = try arena.dupeZ(u8, systemId);

return try parser.domImplementationCreateDocumentType(cqname, cpublicId, csystemId);
}

pub fn _createDocument(
_: *DOMImplementation,
alloc: std.mem.Allocator,
arena: std.mem.Allocator,
namespace: ?[]const u8,
qname: ?[]const u8,
doctype: ?*parser.DocumentType,
) !*parser.Document {
var cnamespace: ?[:0]const u8 = null;
if (namespace) |ns| {
cnamespace = try alloc.dupeZ(u8, ns);
cnamespace = try arena.dupeZ(u8, ns);
}
defer if (cnamespace) |v| alloc.free(v);

var cqname: ?[:0]const u8 = null;
if (qname) |qn| {
cqname = try alloc.dupeZ(u8, qn);
cqname = try arena.dupeZ(u8, qn);
}
defer if (cqname) |v| alloc.free(v);

return try parser.domImplementationCreateDocument(cnamespace, cqname, doctype);
}
Expand Down
5 changes: 2 additions & 3 deletions src/dom/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,12 @@ pub const Node = struct {
return try parser.nodeHasChildNodes(self);
}

pub fn get_childNodes(self: *parser.Node, alloc: std.mem.Allocator) !NodeList {
pub fn get_childNodes(self: *parser.Node, arena: std.mem.Allocator) !NodeList {
var list = NodeList.init();
errdefer list.deinit(alloc);

var n = try parser.nodeFirstChild(self) orelse return list;
while (true) {
try list.append(alloc, n);
try list.append(arena, n);
n = try parser.nodeNextSibling(n) orelse return list;
}
}
Expand Down
13 changes: 6 additions & 7 deletions src/dom/nodelist.zig
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ pub const NodeList = struct {
self.nodes.deinit(alloc);
}

pub fn append(self: *NodeList, alloc: std.mem.Allocator, node: *parser.Node) !void {
try self.nodes.append(alloc, node);
pub fn append(self: *NodeList, arena: std.mem.Allocator, node: *parser.Node) !void {
try self.nodes.append(arena, node);
}

pub fn get_length(self: *NodeList) u32 {
Expand All @@ -139,9 +139,8 @@ pub const NodeList = struct {
return try Node.toInterface(n);
}

pub fn _forEach(self: *NodeList, alloc: std.mem.Allocator, cbk: Callback) !void { // TODO handle thisArg
var res = CallbackResult.init(alloc);
defer res.deinit();
pub fn _forEach(self: *NodeList, arena: std.mem.Allocator, cbk: Callback) !void { // TODO handle thisArg
var res = CallbackResult.init(arena);

for (self.nodes.items, 0..) |n, i| {
const ii: u32 = @intCast(i);
Expand Down Expand Up @@ -172,12 +171,12 @@ pub const NodeList = struct {

// TODO entries() https://developer.mozilla.org/en-US/docs/Web/API/NodeList/entries

pub fn postAttach(self: *NodeList, alloc: std.mem.Allocator, js_obj: jsruntime.JSObject) !void {
pub fn postAttach(self: *NodeList, arena: std.mem.Allocator, js_obj: jsruntime.JSObject) !void {
const ln = self.get_length();
var i: u32 = 0;
while (i < ln) {
defer i += 1;
const k = try std.fmt.allocPrint(alloc, "{d}", .{i});
const k = try std.fmt.allocPrint(arena, "{d}", .{i});

const node = try self._item(i) orelse unreachable;
try js_obj.set(k, node);
Expand Down
27 changes: 13 additions & 14 deletions src/html/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -100,44 +100,43 @@ pub const HTMLDocument = struct {
return v;
}

pub fn _getElementsByName(self: *parser.DocumentHTML, alloc: std.mem.Allocator, name: []const u8) !NodeList {
pub fn _getElementsByName(self: *parser.DocumentHTML, arena: std.mem.Allocator, name: []const u8) !NodeList {
var list = NodeList.init();
errdefer list.deinit(alloc);

if (name.len == 0) return list;

const root = parser.documentHTMLToNode(self);
var c = try collection.HTMLCollectionByName(alloc, root, name, false);
var c = try collection.HTMLCollectionByName(arena, root, name, false);

const ln = try c.get_length();
var i: u32 = 0;
while (i < ln) {
const n = try c.item(i) orelse break;
try list.append(alloc, n);
try list.append(arena, n);
i += 1;
}

return list;
}

pub fn get_images(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "img", false);
pub fn get_images(self: *parser.DocumentHTML, arena: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(arena, parser.documentHTMLToNode(self), "img", false);
}

pub fn get_embeds(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "embed", false);
pub fn get_embeds(self: *parser.DocumentHTML, arena: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(arena, parser.documentHTMLToNode(self), "embed", false);
}

pub fn get_plugins(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection {
return get_embeds(self, alloc);
pub fn get_plugins(self: *parser.DocumentHTML, arena: std.mem.Allocator) !collection.HTMLCollection {
return get_embeds(self, arena);
}

pub fn get_forms(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "form", false);
pub fn get_forms(self: *parser.DocumentHTML, arena: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(arena, parser.documentHTMLToNode(self), "form", false);
}

pub fn get_scripts(self: *parser.DocumentHTML, alloc: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(alloc, parser.documentHTMLToNode(self), "script", false);
pub fn get_scripts(self: *parser.DocumentHTML, arena: std.mem.Allocator) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(arena, parser.documentHTMLToNode(self), "script", false);
}

pub fn get_applets(_: *parser.DocumentHTML) !collection.HTMLCollection {
Expand Down
Loading