From 71866001db191a441f7683102049dbbcca7b87c9 Mon Sep 17 00:00:00 2001 From: Francis Bouvier Date: Sun, 26 Nov 2023 23:49:21 +0100 Subject: [PATCH] Adapt to free js slices Relates to PR https://github.com/Browsercore/jsruntime-lib/issues/111 Relates to issue https://github.com/Browsercore/jsruntime-lib/pull/142 Signed-off-by: Francis Bouvier --- src/dom/document.zig | 26 +++++++++++++++++---- src/dom/html_collection.zig | 46 ++++++++++++++++++++++++++++++------- 2 files changed, 60 insertions(+), 12 deletions(-) diff --git a/src/dom/document.zig b/src/dom/document.zig index 22eac8d2..b39e7cd9 100644 --- a/src/dom/document.zig +++ b/src/dom/document.zig @@ -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 diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig index de04cad4..642ef75f 100644 --- a/src/dom/html_collection.zig +++ b/src/dom/html_collection.zig @@ -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