Skip to content
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

Adapt to free js slices #86

Merged
merged 1 commit into from
Nov 27, 2023
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
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
Loading