use dom_struct::dom_struct;
use html5ever::{local_name, namespace_url, ns, LocalName, Prefix};
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::htmlelement::HTMLElement;
use crate::dom::htmlformelement::{FormControl, FormControlElementHelpers, HTMLFormElement};
use crate::dom::node::{Node, ShadowIncluding};
use crate::dom::virtualmethods::VirtualMethods;
use crate::script_runtime::CanGc;
#[dom_struct]
pub 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),
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<HTMLLabelElement> {
Node::reflect_node_with_proto(
Box::new(HTMLLabelElement::new_inherited(
local_name, prefix, document,
)),
document,
proto,
can_gc,
)
}
}
impl Activatable for HTMLLabelElement {
fn as_element(&self) -> &Element {
self.upcast::<Element>()
}
fn is_instance_activatable(&self) -> bool {
true
}
fn activation_behavior(&self, _event: &Event, _target: &EventTarget, can_gc: CanGc) {
if let Some(e) = self.GetControl() {
e.Click(can_gc);
}
}
}
impl HTMLLabelElementMethods 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(&ns!(), &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, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
if *attr.local_name() == local_name!("form") {
self.form_attribute_mutated(mutation);
}
}
}
impl HTMLLabelElement {
pub 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>()
}
}