Skip to main content

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::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;
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        cx: &mut js::context::JSContext,
40        local_name: LocalName,
41        prefix: Option<Prefix>,
42        document: &Document,
43        proto: Option<HandleObject>,
44    ) -> DomRoot<HTMLAudioElement> {
45        Node::reflect_node_with_proto(
46            cx,
47            Box::new(HTMLAudioElement::new_inherited(
48                local_name, prefix, document,
49            )),
50            document,
51            proto,
52        )
53    }
54}
55
56impl HTMLAudioElementMethods<crate::DomTypeHolder> for HTMLAudioElement {
57    /// <https://html.spec.whatwg.org/multipage/#dom-audio>
58    fn Audio(
59        cx: &mut JSContext,
60        window: &Window,
61        proto: Option<HandleObject>,
62        src: Option<DOMString>,
63    ) -> Fallible<DomRoot<HTMLAudioElement>> {
64        // Step 1. Let document be the current global object's associated Document.
65        let document = window.Document();
66
67        // Step 2. Let audio be the result of creating an element given document, "audio", and the
68        // HTML namespace.
69        let audio = Element::create(
70            cx,
71            QualName::new(None, ns!(html), local_name!("audio")),
72            None,
73            &document,
74            ElementCreator::ScriptCreated,
75            CustomElementCreationMode::Synchronous,
76            proto,
77        );
78
79        // Step 3. Set an attribute value for audio using "preload" and "auto".
80        audio.set_attribute(
81            cx,
82            &local_name!("preload"),
83            AttrValue::String("auto".to_owned()),
84        );
85
86        // Step 4. If src is given, then set an attribute value for audio using "src" and src. (This
87        // will cause the user agent to invoke the object's resource selection algorithm before
88        // returning).
89        if let Some(s) = src {
90            audio.set_attribute(cx, &local_name!("src"), AttrValue::String(s.into()));
91        }
92
93        // Step 5. Return audio.
94        Ok(DomRoot::downcast::<HTMLAudioElement>(audio).unwrap())
95    }
96}