use std::cell::Cell;
use std::default::Default;
use dom_struct::dom_struct;
use html5ever::{local_name, namespace_url, LocalName, Prefix};
use js::rust::HandleObject;
use style_dom::ElementState;
use crate::dom::activation::Activatable;
use crate::dom::attr::Attr;
use crate::dom::bindings::codegen::Bindings::HTMLButtonElementBinding::HTMLButtonElementMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
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::htmlfieldsetelement::HTMLFieldSetElement;
use crate::dom::htmlformelement::{
FormControl, FormDatum, FormDatumValue, FormSubmitterElement, HTMLFormElement, ResetFrom,
SubmittedFrom,
};
use crate::dom::node::{window_from_node, BindContext, Node, UnbindContext};
use crate::dom::nodelist::NodeList;
use crate::dom::validation::{is_barred_by_datalist_ancestor, Validatable};
use crate::dom::validitystate::{ValidationFlags, ValidityState};
use crate::dom::virtualmethods::VirtualMethods;
use crate::script_runtime::CanGc;
#[derive(Clone, Copy, JSTraceable, MallocSizeOf, PartialEq)]
enum ButtonType {
Submit,
Reset,
Button,
}
#[dom_struct]
pub struct HTMLButtonElement {
htmlelement: HTMLElement,
button_type: Cell<ButtonType>,
form_owner: MutNullableDom<HTMLFormElement>,
labels_node_list: MutNullableDom<NodeList>,
validity_state: MutNullableDom<ValidityState>,
}
impl HTMLButtonElement {
fn new_inherited(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
) -> HTMLButtonElement {
HTMLButtonElement {
htmlelement: HTMLElement::new_inherited_with_state(
ElementState::ENABLED,
local_name,
prefix,
document,
),
button_type: Cell::new(ButtonType::Submit),
form_owner: Default::default(),
labels_node_list: Default::default(),
validity_state: Default::default(),
}
}
#[allow(crown::unrooted_must_root)]
pub fn new(
local_name: LocalName,
prefix: Option<Prefix>,
document: &Document,
proto: Option<HandleObject>,
can_gc: CanGc,
) -> DomRoot<HTMLButtonElement> {
Node::reflect_node_with_proto(
Box::new(HTMLButtonElement::new_inherited(
local_name, prefix, document,
)),
document,
proto,
can_gc,
)
}
#[inline]
pub fn is_submit_button(&self) -> bool {
self.button_type.get() == ButtonType::Submit
}
}
impl HTMLButtonElementMethods for HTMLButtonElement {
make_bool_getter!(Disabled, "disabled");
make_bool_setter!(SetDisabled, "disabled");
fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
self.form_owner()
}
make_enumerated_getter!(Type, "type", "submit", "reset" | "button");
make_setter!(SetType, "type");
make_form_action_getter!(FormAction, "formaction");
make_setter!(SetFormAction, "formaction");
make_enumerated_getter!(
FormEnctype,
"formenctype",
"application/x-www-form-urlencoded",
"text/plain" | "multipart/form-data"
);
make_setter!(SetFormEnctype, "formenctype");
make_enumerated_getter!(FormMethod, "formmethod", "get", "post" | "dialog");
make_setter!(SetFormMethod, "formmethod");
make_getter!(FormTarget, "formtarget");
make_setter!(SetFormTarget, "formtarget");
make_bool_getter!(FormNoValidate, "formnovalidate");
make_bool_setter!(SetFormNoValidate, "formnovalidate");
make_getter!(Name, "name");
make_atomic_setter!(SetName, "name");
make_getter!(Value, "value");
make_setter!(SetValue, "value");
make_labels_getter!(Labels, labels_node_list);
fn WillValidate(&self) -> bool {
self.is_instance_validatable()
}
fn Validity(&self) -> DomRoot<ValidityState> {
self.validity_state()
}
fn CheckValidity(&self, can_gc: CanGc) -> bool {
self.check_validity(can_gc)
}
fn ReportValidity(&self, can_gc: CanGc) -> bool {
self.report_validity(can_gc)
}
fn ValidationMessage(&self) -> DOMString {
self.validation_message()
}
fn SetCustomValidity(&self, error: DOMString) {
self.validity_state().set_custom_error_message(error);
}
}
impl HTMLButtonElement {
pub fn form_datum(&self, submitter: Option<FormSubmitterElement>) -> Option<FormDatum> {
if let Some(FormSubmitterElement::Button(submitter)) = submitter {
if submitter != self {
return None;
}
} else {
return None;
}
let ty = self.Type();
let name = self.Name();
if name.is_empty() {
return None;
}
Some(FormDatum {
ty,
name,
value: FormDatumValue::String(self.Value()),
})
}
}
impl VirtualMethods for HTMLButtonElement {
fn super_type(&self) -> Option<&dyn VirtualMethods> {
Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
}
fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) {
self.super_type().unwrap().attribute_mutated(attr, mutation);
match *attr.local_name() {
local_name!("disabled") => {
let el = self.upcast::<Element>();
match mutation {
AttributeMutation::Set(Some(_)) => {},
AttributeMutation::Set(None) => {
el.set_disabled_state(true);
el.set_enabled_state(false);
},
AttributeMutation::Removed => {
el.set_disabled_state(false);
el.set_enabled_state(true);
el.check_ancestors_disabled_state_for_form_control();
},
}
el.update_sequentially_focusable_status(CanGc::note());
self.validity_state()
.perform_validation_and_update(ValidationFlags::all());
},
local_name!("type") => match mutation {
AttributeMutation::Set(_) => {
let value = match &**attr.value() {
"reset" => ButtonType::Reset,
"button" => ButtonType::Button,
_ => ButtonType::Submit,
};
self.button_type.set(value);
self.validity_state()
.perform_validation_and_update(ValidationFlags::all());
},
AttributeMutation::Removed => {
self.button_type.set(ButtonType::Submit);
},
},
local_name!("form") => {
self.form_attribute_mutated(mutation);
self.validity_state()
.perform_validation_and_update(ValidationFlags::empty());
},
_ => {},
}
}
fn bind_to_tree(&self, context: &BindContext) {
if let Some(s) = self.super_type() {
s.bind_to_tree(context);
}
self.upcast::<Element>()
.check_ancestors_disabled_state_for_form_control();
}
fn unbind_from_tree(&self, context: &UnbindContext) {
self.super_type().unwrap().unbind_from_tree(context);
let node = self.upcast::<Node>();
let el = self.upcast::<Element>();
if node
.ancestors()
.any(|ancestor| ancestor.is::<HTMLFieldSetElement>())
{
el.check_ancestors_disabled_state_for_form_control();
} else {
el.check_disabled_attribute();
}
}
}
impl FormControl for HTMLButtonElement {
fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
self.form_owner.get()
}
fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
self.form_owner.set(form);
}
fn to_element(&self) -> &Element {
self.upcast::<Element>()
}
}
impl Validatable for HTMLButtonElement {
fn as_element(&self) -> &Element {
self.upcast()
}
fn validity_state(&self) -> DomRoot<ValidityState> {
self.validity_state
.or_init(|| ValidityState::new(&window_from_node(self), self.upcast()))
}
fn is_instance_validatable(&self) -> bool {
self.button_type.get() == ButtonType::Submit &&
!self.upcast::<Element>().disabled_state() &&
!is_barred_by_datalist_ancestor(self.upcast())
}
}
impl Activatable for HTMLButtonElement {
fn as_element(&self) -> &Element {
self.upcast()
}
fn is_instance_activatable(&self) -> bool {
!self.upcast::<Element>().disabled_state()
}
fn activation_behavior(&self, _event: &Event, _target: &EventTarget, _can_gc: CanGc) {
let ty = self.button_type.get();
match ty {
ButtonType::Submit => {
if let Some(owner) = self.form_owner() {
owner.submit(
SubmittedFrom::NotFromForm,
FormSubmitterElement::Button(self),
CanGc::note(),
);
}
},
ButtonType::Reset => {
if let Some(owner) = self.form_owner() {
owner.reset(ResetFrom::NotFromForm, CanGc::note());
}
},
_ => (),
}
}
}