use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix, local_name};
use js::rust::HandleObject;
use style::attr::AttrValue;
use crate::dom::activation::Activatable;
use crate::dom::attr::Attr;
use crate::dom::bindings::codegen::Bindings::AttrBinding::AttrMethods;
use crate::dom::bindings::codegen::Bindings::ElementBinding::ElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLLabelElementBinding::HTMLLabelElementMethods;
use crate::dom::bindings::codegen::Bindings::NodeBinding::{GetRootNodeOptions, NodeMethods};
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::{AttributeMutation, Element};
use crate::dom::event::Event;
use crate::dom::eventtarget::EventTarget;
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::html::htmlformelement::{FormControl, FormControlElementHelpers, HTMLFormElement};
use crate::dom::node::{Node, ShadowIncluding};
use crate::dom::virtualmethods::VirtualMethods;
use crate::script_runtime::CanGc;
#[dom_struct]
pub(crate) struct HTMLLabelElement {
htmlelement: HTMLElement,
}
impl HTMLLabelElement {
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
) -> HTMLLabelElement {
HTMLLabelElement {
htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
}
}
pub(crate) fn new(
cx: &mut js::context::JSContext,
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
) -> DomRoot<HTMLLabelElement> {
Node::reflect_node_with_proto(
cx,
Box::new(HTMLLabelElement::new_inherited(
local_name, prefix, document,
)),
document,
proto,
)
}
}
impl Activatable for HTMLLabelElement {
fn as_element(&self) -> &Element {
self.upcast::<Element>()
}
fn is_instance_activatable(&self) -> bool {
true
}
fn activation_behavior(
&self,
cx: &mut js::context::JSContext,
_event: &Event,
_target: &EventTarget,
) {
if let Some(e) = self.GetControl() {
e.Click(CanGc::from_cx(cx));
}
}
}
impl HTMLLabelElementMethods<crate::DomTypeHolder> for HTMLLabelElement {
fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
self.form_owner()
}
make_getter!(HtmlFor, "for");
make_atomic_setter!(SetHtmlFor, "for");
fn GetControl(&self) -> Option<DomRoot<HTMLElement>> {
let for_attr = match self.upcast::<Element>().get_attribute(&local_name!("for")) {
Some(for_attr) => for_attr,
None => return self.first_labelable_descendant(),
};
let for_value = for_attr.Value();
let maybe_found = self
.upcast::<Node>()
.GetRootNode(&GetRootNodeOptions::empty())
.traverse_preorder(ShadowIncluding::No)
.find_map(|e| {
if let Some(htmle) = e.downcast::<HTMLElement>() {
if htmle.upcast::<Element>().Id() == for_value {
Some(DomRoot::from_ref(htmle))
} else {
None
}
} else {
None
}
});
if let Some(ref maybe_labelable) = maybe_found {
if maybe_labelable.is_labelable_element() {
return maybe_found;
}
}
None
}
}
impl VirtualMethods for HTMLLabelElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn parse_plain_attribute(&self, name: &LocalName, value: DOMString) -> AttrValue {
match name {
&local_name!("for") => AttrValue::from_atomic(value.into()),
_ => self
.super_type()
.unwrap()
.parse_plain_attribute(name, value),
}
}
fn attribute_mutated(
&self,
cx: &mut js::context::JSContext,
attr: &Attr,
mutation: AttributeMutation,
) {
self.super_type()
.unwrap()
.attribute_mutated(cx, attr, mutation);
if *attr.local_name() == local_name!("form") {
self.form_attribute_mutated(mutation, CanGc::from_cx(cx));
}
}
}
impl HTMLLabelElement {
pub(crate) fn first_labelable_descendant(&self) -> Option<DomRoot<HTMLElement>> {
self.upcast::<Node>()
.traverse_preorder(ShadowIncluding::No)
.filter_map(DomRoot::downcast::<HTMLElement>)
.find(|elem| elem.is_labelable_element())
}
}
impl FormControl for HTMLLabelElement {
fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
self.GetControl()
.map(DomRoot::upcast::<Element>)
.and_then(|elem| {
elem.as_maybe_form_control()
.and_then(|control| control.form_owner())
})
}
fn set_form_owner(&self, _: Option<&HTMLFormElement>) {
}
fn to_element(&self) -> &Element {
self.upcast::<Element>()
}
}