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 std::rc::Rc;
6
7use dom_struct::dom_struct;
8use html5ever::{LocalName, Prefix, QualName, local_name, ns};
9use js::context::JSContext;
10use js::rust::HandleObject;
11use style::attr::AttrValue;
12
13use crate::dom::bindings::codegen::Bindings::HTMLAudioElementBinding::HTMLAudioElementMethods;
14use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
15use crate::dom::bindings::error::Fallible;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::document::Document;
19use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator};
20use crate::dom::html::htmlmediaelement::HTMLMediaElement;
21use crate::dom::node::Node;
22use crate::dom::window::Window;
23
24#[dom_struct]
25pub(crate) struct HTMLAudioElement {
26    htmlmediaelement: HTMLMediaElement,
27}
28
29impl HTMLAudioElement {
30    fn new_inherited(
31        local_name: LocalName,
32        prefix: Option<Prefix>,
33        document: &Document,
34    ) -> HTMLAudioElement {
35        HTMLAudioElement {
36            htmlmediaelement: HTMLMediaElement::new_inherited(local_name, prefix, document),
37        }
38    }
39
40    pub(crate) fn new(
41        cx: &mut js::context::JSContext,
42        local_name: LocalName,
43        prefix: Option<Prefix>,
44        document: &Document,
45        proto: Option<HandleObject>,
46    ) -> DomRoot<HTMLAudioElement> {
47        Node::reflect_weak_referenceable_node_with_proto(
48            cx,
49            Rc::new(HTMLAudioElement::new_inherited(
50                local_name, prefix, document,
51            )),
52            document,
53            proto,
54        )
55    }
56}
57
58impl HTMLAudioElementMethods<crate::DomTypeHolder> for HTMLAudioElement {
59    /// <https://html.spec.whatwg.org/multipage/#dom-audio>
60    fn Audio(
61        cx: &mut JSContext,
62        window: &Window,
63        proto: Option<HandleObject>,
64        src: Option<DOMString>,
65    ) -> Fallible<DomRoot<HTMLAudioElement>> {
66        // Step 1. Let document be the current global object's associated Document.
67        let document = window.Document();
68
69        // Step 2. Let audio be the result of creating an element given document, "audio", and the
70        // HTML namespace.
71        let audio = Element::create(
72            cx,
73            QualName::new(None, ns!(html), local_name!("audio")),
74            None,
75            &document,
76            ElementCreator::ScriptCreated,
77            CustomElementCreationMode::Synchronous,
78            proto,
79        );
80
81        // Step 3. Set an attribute value for audio using "preload" and "auto".
82        audio.set_attribute(
83            cx,
84            &local_name!("preload"),
85            AttrValue::String("auto".to_owned()),
86        );
87
88        // Step 4. If src is given, then set an attribute value for audio using "src" and src. (This
89        // will cause the user agent to invoke the object's resource selection algorithm before
90        // returning).
91        if let Some(s) = src {
92            audio.set_attribute(cx, &local_name!("src"), AttrValue::String(s.into()));
93        }
94
95        // Step 5. Return audio.
96        Ok(DomRoot::downcast::<HTMLAudioElement>(audio).unwrap())
97    }
98}