mirror of
https://github.com/servo/servo.git
synced 2025-06-08 08:33:26 +00:00
use create_html_element for HTMLAudioElement and HTMLImageElement <!-- Please describe your changes on the following line: --> Updated the Image and Audio constructors to use `create_html_element` via the Element::create method. This was done to meet these specifications of "Let (audio/image) be the result of **creating an element** given document, audio, and the HTML namespace." for [dom-image](https://html.spec.whatwg.org/multipage/embedded-content.html#dom-image) and [dom-audio](https://html.spec.whatwg.org/multipage/media.html#dom-audio) Not sure what _is_ is according to the [create-element guidelines](https://dom.spec.whatwg.org/#concept-create-element) so I left it as None copying from #25393. Also copied the ElementCreator and CustomElementCreationMode from #25393 as I do not know what they do. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] These changes fix #25421 (GitHub issue number if applicable) <!-- Either: --> - [ ] There are tests for these changes OR - [x] These changes do not require tests because it is a small swap out of the way used to generate these HTML elements. The pre-existing tests should be sufficient. <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
77 lines
2.6 KiB
Rust
77 lines
2.6 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementBinding::ElementMethods;
|
|
use crate::dom::bindings::codegen::Bindings::HTMLAudioElementBinding;
|
|
use crate::dom::bindings::codegen::Bindings::WindowBinding::WindowMethods;
|
|
use crate::dom::bindings::error::Fallible;
|
|
use crate::dom::bindings::inheritance::Castable;
|
|
use crate::dom::bindings::root::DomRoot;
|
|
use crate::dom::bindings::str::DOMString;
|
|
use crate::dom::document::Document;
|
|
use crate::dom::element::{CustomElementCreationMode, Element, ElementCreator};
|
|
use crate::dom::htmlmediaelement::HTMLMediaElement;
|
|
use crate::dom::node::Node;
|
|
use crate::dom::window::Window;
|
|
use dom_struct::dom_struct;
|
|
use html5ever::{LocalName, Prefix, QualName};
|
|
|
|
#[dom_struct]
|
|
pub struct HTMLAudioElement {
|
|
htmlmediaelement: HTMLMediaElement,
|
|
}
|
|
|
|
impl HTMLAudioElement {
|
|
fn new_inherited(
|
|
local_name: LocalName,
|
|
prefix: Option<Prefix>,
|
|
document: &Document,
|
|
) -> HTMLAudioElement {
|
|
HTMLAudioElement {
|
|
htmlmediaelement: HTMLMediaElement::new_inherited(local_name, prefix, document),
|
|
}
|
|
}
|
|
|
|
#[allow(unrooted_must_root)]
|
|
pub fn new(
|
|
local_name: LocalName,
|
|
prefix: Option<Prefix>,
|
|
document: &Document,
|
|
) -> DomRoot<HTMLAudioElement> {
|
|
Node::reflect_node(
|
|
Box::new(HTMLAudioElement::new_inherited(
|
|
local_name, prefix, document,
|
|
)),
|
|
document,
|
|
HTMLAudioElementBinding::Wrap,
|
|
)
|
|
}
|
|
|
|
// https://html.spec.whatwg.org/multipage/#dom-audio
|
|
#[allow(non_snake_case)]
|
|
pub fn Audio(window: &Window, src: Option<DOMString>) -> Fallible<DomRoot<HTMLAudioElement>> {
|
|
let element = Element::create(
|
|
QualName::new(None, ns!(html), local_name!("audio")),
|
|
None,
|
|
&window.Document(),
|
|
ElementCreator::ScriptCreated,
|
|
CustomElementCreationMode::Synchronous,
|
|
);
|
|
|
|
let audio = DomRoot::downcast::<HTMLAudioElement>(element).unwrap();
|
|
|
|
audio
|
|
.upcast::<Element>()
|
|
.SetAttribute(DOMString::from("preload"), DOMString::from("auto"))
|
|
.expect("should be infallible");
|
|
if let Some(s) = src {
|
|
audio
|
|
.upcast::<Element>()
|
|
.SetAttribute(DOMString::from("src"), s)
|
|
.expect("should be infallible");
|
|
}
|
|
|
|
Ok(audio)
|
|
}
|
|
}
|