mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
Merge pull request #86 from Browsercore/jsruntime_changes
Adapt to free js slices
This commit is contained in:
@@ -95,15 +95,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
|
||||
|
||||
@@ -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 {
|
||||
@@ -28,9 +34,11 @@ 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, "*"),
|
||||
};
|
||||
}
|
||||
@@ -38,13 +46,21 @@ pub const MatchByTagName = struct {
|
||||
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),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -52,9 +68,11 @@ pub fn HTMLCollectionByTagName(root: *parser.Node, tag_name: []const u8) HTMLCol
|
||||
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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user