Skip to content

Commit 7adaa53

Browse files
committed
image constructor
1 parent 0df531a commit 7adaa53

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/browser/html/elements.zig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,26 @@ pub const HTMLImageElement = struct {
563563
pub const Self = parser.Image;
564564
pub const prototype = *HTMLElement;
565565
pub const subtype = .node;
566+
pub const js_name = "Image";
567+
568+
pub fn constructor(width: ?[]const u8, height: ?[]const u8, state: *const SessionState) !*parser.Image {
569+
const element = try parser.documentCreateElement(parser.documentHTMLToDocument(state.window.document), "img");
570+
if (width) |width_| try parser.elementSetAttribute(element, "width", width_);
571+
if (height) |height_| try parser.elementSetAttribute(element, "height", height_);
572+
return @ptrCast(element);
573+
}
574+
575+
pub fn get_width(self: *parser.Image) !u32 {
576+
const width = try parser.elementGetAttribute(@ptrCast(self), "width") orelse return 0;
577+
return try std.fmt.parseInt(u32, width, 10);
578+
}
579+
pub fn get_height(self: *parser.Image) !u32 {
580+
const width = try parser.elementGetAttribute(@ptrCast(self), "height") orelse return 0;
581+
return try std.fmt.parseInt(u32, width, 10);
582+
}
583+
pub fn get_src(self: *parser.Image) ![]const u8 {
584+
return try parser.elementGetAttribute(@ptrCast(self), "src") orelse return "";
585+
}
566586
};
567587

568588
pub const HTMLInputElement = struct {
@@ -1059,4 +1079,14 @@ test "Browser.HTML.Element" {
10591079
.{ "document.getElementById('content').click()", "undefined" },
10601080
.{ "click_count", "1" },
10611081
}, .{});
1082+
1083+
try runner.testCases(&.{
1084+
.{ "(new Image).width", "0" },
1085+
.{ "(new Image).height", "0" },
1086+
.{ "(new Image).src", "" },
1087+
.{ "(new Image(4)).width", "4" },
1088+
.{ "(new Image(4, 6)).height", "6" },
1089+
.{ "(new Image).width = 15", "15" },
1090+
.{ "(new Image).src = 'abc'", "abc" },
1091+
}, .{});
10621092
}

0 commit comments

Comments
 (0)