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