Merge pull request #1375 from lightpanda-io/nikneym/audio-constructor
Some checks failed
nightly build / build-linux-x86_64 (push) Has been cancelled
nightly build / build-linux-aarch64 (push) Has been cancelled
nightly build / build-macos-aarch64 (push) Has been cancelled
nightly build / build-macos-x86_64 (push) Has been cancelled
wpt / web platform tests json output (push) Has been cancelled
wpt / perf-fmt (push) Has been cancelled
e2e-test / zig build release (push) Has been cancelled
e2e-integration-test / zig build release (push) Has been cancelled
e2e-integration-test / demo-integration-scripts (push) Has been cancelled
zig-test / zig test (push) Has been cancelled
e2e-test / demo-scripts (push) Has been cancelled
e2e-test / cdp-and-hyperfine-bench (push) Has been cancelled
e2e-test / perf-fmt (push) Has been cancelled
e2e-test / browser fetch (push) Has been cancelled
zig-test / perf-fmt (push) Has been cancelled

Add `Audio` constructor
This commit is contained in:
Karl Seguin
2026-01-16 23:25:56 +00:00
committed by GitHub
2 changed files with 28 additions and 0 deletions

View File

@@ -238,6 +238,15 @@
testing.expectEqual('[object HTMLAudioElement]', audio.toString()); testing.expectEqual('[object HTMLAudioElement]', audio.toString());
testing.expectEqual(true, audio.paused); testing.expectEqual(true, audio.paused);
} }
// Create with `Audio` constructor.
{
const audio = new Audio();
testing.expectEqual(true, audio instanceof HTMLAudioElement);
testing.expectEqual("[object HTMLAudioElement]", audio.toString());
testing.expectEqual(true, audio.paused);
testing.expectEqual("auto", audio.getAttribute("preload"));
}
</script> </script>
<script id="create_video_element"> <script id="create_video_element">

View File

@@ -17,6 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
const js = @import("../../../js/js.zig"); const js = @import("../../../js/js.zig");
const Page = @import("../../../Page.zig");
const Node = @import("../../Node.zig"); const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig"); const Element = @import("../../Element.zig");
@@ -26,6 +27,21 @@ const Audio = @This();
_proto: *Media, _proto: *Media,
pub fn constructor(maybe_url: ?[]const u8, page: *Page) !*Media {
const node = try page.createElementNS(.html, "audio", null);
const el = node.as(Element);
const list = try el.getOrCreateAttributeList(page);
// Always set to "auto" initially.
_ = try list.putSafe("preload", "auto", el, page);
// Set URL if provided.
if (maybe_url) |url| {
_ = try list.putSafe("src", url, el, page);
}
return node.as(Media);
}
pub fn asMedia(self: *Audio) *Media { pub fn asMedia(self: *Audio) *Media {
return self._proto; return self._proto;
} }
@@ -43,7 +59,10 @@ pub const JsApi = struct {
pub const Meta = struct { pub const Meta = struct {
pub const name = "HTMLAudioElement"; pub const name = "HTMLAudioElement";
pub const constructor_alias = "Audio";
pub const prototype_chain = bridge.prototypeChain(); pub const prototype_chain = bridge.prototypeChain();
pub var class_id: bridge.ClassId = undefined; pub var class_id: bridge.ClassId = undefined;
}; };
pub const constructor = bridge.constructor(Audio.constructor, .{});
}; };