mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-02-04 14:33:47 +00:00
change after rebase
This commit is contained in:
@@ -36,14 +36,6 @@ pub fn asNode(self: *Image) *Node {
|
|||||||
return self.asElement().asNode();
|
return self.asElement().asNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getOnLoad(self: *Image, page: *Page) ?js.Function.Global {
|
|
||||||
return page._event_manager.getInlineListener(self.asElement().asEventTarget(), .load);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn setOnLoad(self: *Image, callback: js.Function.Global, page: *Page) !void {
|
|
||||||
return page._event_manager.setInlineListener(self.asElement().asEventTarget(), .load, callback);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn getSrc(self: *const Image, page: *Page) ![]const u8 {
|
pub fn getSrc(self: *const Image, page: *Page) ![]const u8 {
|
||||||
const element = self.asConstElement();
|
const element = self.asConstElement();
|
||||||
const src = element.getAttributeSafe(comptime .wrap("src")) orelse return "";
|
const src = element.getAttributeSafe(comptime .wrap("src")) orelse return "";
|
||||||
@@ -152,7 +144,6 @@ pub const JsApi = struct {
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub const constructor = bridge.constructor(Image.constructor, .{});
|
pub const constructor = bridge.constructor(Image.constructor, .{});
|
||||||
pub const onload = bridge.accessor(Image.getOnLoad, Image.setOnLoad, .{});
|
|
||||||
pub const src = bridge.accessor(Image.getSrc, Image.setSrc, .{});
|
pub const src = bridge.accessor(Image.getSrc, Image.setSrc, .{});
|
||||||
pub const alt = bridge.accessor(Image.getAlt, Image.setAlt, .{});
|
pub const alt = bridge.accessor(Image.getAlt, Image.setAlt, .{});
|
||||||
pub const width = bridge.accessor(Image.getWidth, Image.setWidth, .{});
|
pub const width = bridge.accessor(Image.getWidth, Image.setWidth, .{});
|
||||||
@@ -195,84 +186,6 @@ fn dispatchLoadEvent(raw: *anyopaque) !?u32 {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const Build = struct {
|
|
||||||
pub fn created(node: *Node, page: *Page) !void {
|
|
||||||
const self = node.as(Image);
|
|
||||||
const image = self.asElement();
|
|
||||||
// Exit if src not set. We might want to check if src point to valid image.
|
|
||||||
_ = image.getAttributeSafe("src") orelse return;
|
|
||||||
|
|
||||||
// Set `onload` if provided.
|
|
||||||
blk: {
|
|
||||||
// Exit if there's no `onload`.
|
|
||||||
const on_load_str = image.getAttributeSafe("onload") orelse break :blk;
|
|
||||||
// Derive function from string.
|
|
||||||
const on_load_func = page.js.stringToPersistedFunction(on_load_str) catch |err| {
|
|
||||||
log.err(.js, "Image.onload", .{ .err = err, .str = on_load_str });
|
|
||||||
break :blk;
|
|
||||||
};
|
|
||||||
// Set onload.
|
|
||||||
try self.setOnLoad(on_load_func, page);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Since src set, we should send dispatch operation to Scheduler.
|
|
||||||
const args = try page._factory.create(CallbackParams{
|
|
||||||
.page = page,
|
|
||||||
.element = image,
|
|
||||||
});
|
|
||||||
errdefer page._factory.destroy(args);
|
|
||||||
|
|
||||||
// Execute it asynchronously; Chrome and FF seem to do like this.
|
|
||||||
// We may change the behavior if its inconsistent.
|
|
||||||
//
|
|
||||||
// I'm well aware that such scenario is possible:
|
|
||||||
// imageElement.onload = () => console.warn("HA HA HA overwritten!");
|
|
||||||
//
|
|
||||||
// this is allowed both in Chrome and FF. Honestly it makes the logic
|
|
||||||
// quite easier, which might be the reason.
|
|
||||||
return page.scheduler.add(
|
|
||||||
args,
|
|
||||||
dispatchLoadEvent,
|
|
||||||
0,
|
|
||||||
.{
|
|
||||||
.low_priority = false,
|
|
||||||
.name = "Image.Build.created",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn attributeChange(
|
|
||||||
element: *Element,
|
|
||||||
attr_name: []const u8,
|
|
||||||
attr_value: []const u8,
|
|
||||||
page: *Page,
|
|
||||||
) !void {
|
|
||||||
const self = element.as(Image);
|
|
||||||
const image = self.asElement();
|
|
||||||
|
|
||||||
const src_changed = std.mem.eql(u8, attr_name, "src") and attr_value.len > 0;
|
|
||||||
if (src_changed) {
|
|
||||||
// Have to do this since `Scheduler` only allow passing a single arg.
|
|
||||||
const args = try page._factory.create(CallbackParams{
|
|
||||||
.page = page,
|
|
||||||
.element = image,
|
|
||||||
});
|
|
||||||
errdefer page._factory.destroy(args);
|
|
||||||
|
|
||||||
// We don't actually fetch the media, here we fake the load call.
|
|
||||||
try page.scheduler.add(
|
|
||||||
args,
|
|
||||||
dispatchLoadEvent,
|
|
||||||
0,
|
|
||||||
.{
|
|
||||||
.low_priority = false,
|
|
||||||
.name = "Image.Build.attributeChange",
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const testing = @import("../../../../testing.zig");
|
const testing = @import("../../../../testing.zig");
|
||||||
test "WebApi: HTML.Image" {
|
test "WebApi: HTML.Image" {
|
||||||
try testing.htmlRunner("element/html/image.html", .{});
|
try testing.htmlRunner("element/html/image.html", .{});
|
||||||
|
|||||||
Reference in New Issue
Block a user