dom: add innerHTML getter

This commit is contained in:
Pierre Tachoire
2024-02-19 10:30:09 +01:00
parent 64ce07340b
commit 37f4a9c72c

View File

@@ -8,6 +8,7 @@ const checkCases = jsruntime.test_utils.checkCases;
const Variadic = jsruntime.Variadic; const Variadic = jsruntime.Variadic;
const collection = @import("html_collection.zig"); const collection = @import("html_collection.zig");
const dumpNode = @import("../browser/dump.zig").nodeFile;
const Node = @import("node.zig").Node; const Node = @import("node.zig").Node;
const Walker = @import("walker.zig").WalkerDepthFirst; const Walker = @import("walker.zig").WalkerDepthFirst;
@@ -78,6 +79,16 @@ pub const Element = struct {
return try parser.nodeGetAttributes(parser.elementToNode(self)); 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();
try dumpNode(parser.elementToNode(self), buf.writer());
// TODO express the caller owned the slice.
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
return buf.toOwnedSlice();
}
pub fn _hasAttributes(self: *parser.Element) !bool { pub fn _hasAttributes(self: *parser.Element) !bool {
return try parser.nodeHasAttributes(parser.elementToNode(self)); return try parser.nodeHasAttributes(parser.elementToNode(self));
} }
@@ -420,4 +431,10 @@ pub fn testExecFn(
.{ .src = "f.getAttributeNode('bar')", .ex = "null" }, .{ .src = "f.getAttributeNode('bar')", .ex = "null" },
}; };
try checkCases(js_env, &attrNode); try checkCases(js_env, &attrNode);
var innerHTML = [_]Case{
.{ .src = "document.getElementById('para').innerHTML", .ex = " And" },
.{ .src = "document.getElementById('para-empty').innerHTML.trim()", .ex = "<span id=\"para-empty-child\"></span>" },
};
try checkCases(js_env, &innerHTML);
} }