script/dom/html/
htmlaudioelement.rs1use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, QualName, local_name, ns};
7use js::context::JSContext;
8use js::rust::HandleObject;
9use style::attr::AttrValue;
10
11use crate::dom::bindings::codegen::Bindings::HTMLAudioElementBinding::HTMLAudioElementMethods;
12use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator};
18use crate::dom::html::htmlmediaelement::HTMLMediaElement;
19use crate::dom::node::Node;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct HTMLAudioElement {
25 htmlmediaelement: HTMLMediaElement,
26}
27
28impl HTMLAudioElement {
29 fn new_inherited(
30 local_name: LocalName,
31 prefix: Option<Prefix>,
32 document: &Document,
33 ) -> HTMLAudioElement {
34 HTMLAudioElement {
35 htmlmediaelement: HTMLMediaElement::new_inherited(local_name, prefix, document),
36 }
37 }
38
39 pub(crate) fn new(
40 local_name: LocalName,
41 prefix: Option<Prefix>,
42 document: &Document,
43 proto: Option<HandleObject>,
44 can_gc: CanGc,
45 ) -> DomRoot<HTMLAudioElement> {
46 Node::reflect_node_with_proto(
47 Box::new(HTMLAudioElement::new_inherited(
48 local_name, prefix, document,
49 )),
50 document,
51 proto,
52 can_gc,
53 )
54 }
55}
56
57impl HTMLAudioElementMethods<crate::DomTypeHolder> for HTMLAudioElement {
58 fn Audio(
60 cx: &mut JSContext,
61 window: &Window,
62 proto: Option<HandleObject>,
63 src: Option<DOMString>,
64 ) -> Fallible<DomRoot<HTMLAudioElement>> {
65 let document = window.Document();
67
68 let audio = Element::create(
71 QualName::new(None, ns!(html), local_name!("audio")),
72 None,
73 &document,
74 ElementCreator::ScriptCreated,
75 CustomElementCreationMode::Synchronous,
76 proto,
77 CanGc::from_cx(cx),
78 );
79
80 audio.set_attribute(
82 &local_name!("preload"),
83 AttrValue::String("auto".to_owned()),
84 CanGc::from_cx(cx),
85 );
86
87 if let Some(s) = src {
91 audio.set_attribute(
92 &local_name!("src"),
93 AttrValue::String(s.into()),
94 CanGc::from_cx(cx),
95 );
96 }
97
98 Ok(DomRoot::downcast::<HTMLAudioElement>(audio).unwrap())
100 }
101}