Skip to content

Commit

Permalink
Adapt to free js slices
Browse files Browse the repository at this point in the history
Relates to PR lightpanda-io/zig-js-runtime#111

Relates to issue lightpanda-io/zig-js-runtime#142

Signed-off-by: Francis Bouvier <[email protected]>
  • Loading branch information
francisbouvier committed Nov 26, 2023
1 parent f8e4876 commit 7186600
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
26 changes: 22 additions & 4 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,33 @@ pub const Document = struct {
// the spec changed to return an HTMLCollection instead.
// That's why we reimplemented getElementsByTagName by using an
// HTMLCollection in zig here.
pub fn _getElementsByTagName(self: *parser.Document, tag_name: []const u8) collection.HTMLCollection {
pub fn _getElementsByTagName(
self: *parser.Document,
alloc: std.mem.Allocator,
tag_name: []const u8,
) !collection.HTMLCollection {
const root = parser.documentGetDocumentElement(self);
return collection.HTMLCollectionByTagName(parser.elementToNode(root), tag_name);
return try collection.HTMLCollectionByTagName(
alloc,
parser.elementToNode(root),
tag_name,
);
}

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

pub fn deinit(_: *parser.Document, _: std.mem.Allocator) void {}
};

// Tests
Expand Down
46 changes: 38 additions & 8 deletions src/dom/html_collection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ const Matcher = union(enum) {
inline else => |case| return case.match(node),
}
}

pub fn deinit(self: Matcher, alloc: std.mem.Allocator) void {
switch (self) {
inline else => |case| return case.deinit(alloc),
}
}
};

pub const MatchByTagName = struct {
Expand All @@ -28,33 +34,45 @@ pub const MatchByTagName = struct {
tag: []const u8,
is_wildcard: bool,

fn init(tag_name: []const u8) MatchByTagName {
fn init(alloc: std.mem.Allocator, tag_name: []const u8) !MatchByTagName {
const tag_name_alloc = try alloc.alloc(u8, tag_name.len);
@memcpy(tag_name_alloc, tag_name);
return MatchByTagName{
.tag = tag_name,
.tag = tag_name_alloc,
.is_wildcard = std.mem.eql(u8, tag_name, "*"),
};
}

pub fn match(self: MatchByTagName, node: *parser.Node) bool {
return self.is_wildcard or std.ascii.eqlIgnoreCase(self.tag, parser.nodeName(node));
}

fn deinit(self: MatchByTagName, alloc: std.mem.Allocator) void {
alloc.free(self.tag);
}
};

pub fn HTMLCollectionByTagName(root: *parser.Node, tag_name: []const u8) HTMLCollection {
pub fn HTMLCollectionByTagName(
alloc: std.mem.Allocator,
root: *parser.Node,
tag_name: []const u8,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.matcher = Matcher{
.matchByTagName = MatchByTagName.init(tag_name),
.matchByTagName = try MatchByTagName.init(alloc, tag_name),
},
};
}

pub const MatchByClassName = struct {
classNames: []const u8,

fn init(classNames: []const u8) MatchByClassName {
fn init(alloc: std.mem.Allocator, classNames: []const u8) !MatchByClassName {
const class_names_alloc = try alloc.alloc(u8, classNames.len);
@memcpy(class_names_alloc, classNames);
return MatchByClassName{
.classNames = classNames,
.classNames = class_names_alloc,
};
}

Expand All @@ -69,13 +87,21 @@ pub const MatchByClassName = struct {

return true;
}

fn deinit(self: MatchByClassName, alloc: std.mem.Allocator) void {
alloc.free(self.classNames);
}
};

pub fn HTMLCollectionByClassName(root: *parser.Node, classNames: []const u8) HTMLCollection {
pub fn HTMLCollectionByClassName(
alloc: std.mem.Allocator,
root: *parser.Node,
classNames: []const u8,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.matcher = Matcher{
.matchByClassName = MatchByClassName.init(classNames),
.matchByClassName = try MatchByClassName.init(alloc, classNames),
},
};
}
Expand Down Expand Up @@ -232,6 +258,10 @@ pub const HTMLCollection = struct {

return null;
}

pub fn deinit(self: *HTMLCollection, alloc: std.mem.Allocator) void {
self.matcher.deinit(alloc);
}
};

// Tests
Expand Down

0 comments on commit 7186600

Please sign in to comment.