script/dom/html/
htmlaudioelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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::inheritance::Castable;
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    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
40    pub(crate) fn new(
41        local_name: LocalName,
42        prefix: Option<Prefix>,
43        document: &Document,
44        proto: Option<HandleObject>,
45        can_gc: CanGc,
46    ) -> DomRoot<HTMLAudioElement> {
47        Node::reflect_node_with_proto(
48            Box::new(HTMLAudioElement::new_inherited(
49                local_name, prefix, document,
50            )),
51            document,
52            proto,
53            can_gc,
54        )
55    }
56}
57
58impl HTMLAudioElementMethods<crate::DomTypeHolder> for HTMLAudioElement {
59    // https://html.spec.whatwg.org/multipage/#dom-audio
60    fn Audio(
61        window: &Window,
62        proto: Option<HandleObject>,
63        can_gc: CanGc,
64        src: Option<DOMString>,
65    ) -> Fallible<DomRoot<HTMLAudioElement>> {
66        let element = Element::create(
67            QualName::new(None, ns!(html), local_name!("audio")),
68            None,
69            &window.Document(),
70            ElementCreator::ScriptCreated,
71            CustomElementCreationMode::Synchronous,
72            proto,
73            can_gc,
74        );
75
76        let audio = DomRoot::downcast::<HTMLAudioElement>(element).unwrap();
77
78        audio.upcast::<Element>().set_attribute(
79            &local_name!("preload"),
80            AttrValue::String("auto".to_owned()),
81            can_gc,
82        );
83        if let Some(s) = src {
84            audio.upcast::<Element>().set_attribute(
85                &local_name!("src"),
86                AttrValue::String(s.into()),
87                can_gc,
88            );
89        }
90
91        Ok(audio)
92    }
93}