diff options
Diffstat (limited to 'components/script/dom/htmloutputelement.rs')
-rw-r--r-- | components/script/dom/htmloutputelement.rs | 181 |
1 files changed, 140 insertions, 41 deletions
diff --git a/components/script/dom/htmloutputelement.rs b/components/script/dom/htmloutputelement.rs index 719efad95d3..e20dbcc29b2 100644 --- a/components/script/dom/htmloutputelement.rs +++ b/components/script/dom/htmloutputelement.rs @@ -1,72 +1,155 @@ /* 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 http://mozilla.org/MPL/2.0/. */ - -use dom::attr::Attr; -use dom::bindings::codegen::Bindings::HTMLOutputElementBinding; -use dom::bindings::codegen::Bindings::HTMLOutputElementBinding::HTMLOutputElementMethods; -use dom::bindings::inheritance::Castable; -use dom::bindings::js::{MutNullableJS, Root}; -use dom::bindings::str::DOMString; -use dom::document::Document; -use dom::element::{AttributeMutation, Element}; -use dom::htmlelement::HTMLElement; -use dom::htmlformelement::{FormControl, HTMLFormElement}; -use dom::node::{Node, window_from_node}; -use dom::nodelist::NodeList; -use dom::validitystate::ValidityState; -use dom::virtualmethods::VirtualMethods; + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +use crate::dom::attr::Attr; +use crate::dom::bindings::cell::DomRefCell; +use crate::dom::bindings::codegen::Bindings::HTMLOutputElementBinding::HTMLOutputElementMethods; +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::htmlelement::HTMLElement; +use crate::dom::htmlformelement::{FormControl, HTMLFormElement}; +use crate::dom::node::{window_from_node, Node}; +use crate::dom::nodelist::NodeList; +use crate::dom::validation::Validatable; +use crate::dom::validitystate::ValidityState; +use crate::dom::virtualmethods::VirtualMethods; use dom_struct::dom_struct; -use html5ever_atoms::LocalName; +use html5ever::{LocalName, Prefix}; #[dom_struct] pub struct HTMLOutputElement { htmlelement: HTMLElement, - form_owner: MutNullableJS<HTMLFormElement>, + form_owner: MutNullableDom<HTMLFormElement>, + labels_node_list: MutNullableDom<NodeList>, + default_value_override: DomRefCell<Option<DOMString>>, + validity_state: MutNullableDom<ValidityState>, } impl HTMLOutputElement { - fn new_inherited(local_name: LocalName, - prefix: Option<DOMString>, - document: &Document) -> HTMLOutputElement { + fn new_inherited( + local_name: LocalName, + prefix: Option<Prefix>, + document: &Document, + ) -> HTMLOutputElement { HTMLOutputElement { - htmlelement: - HTMLElement::new_inherited(local_name, prefix, document), + htmlelement: HTMLElement::new_inherited(local_name, prefix, document), form_owner: Default::default(), + labels_node_list: Default::default(), + default_value_override: DomRefCell::new(None), + validity_state: Default::default(), } } #[allow(unrooted_must_root)] - pub fn new(local_name: LocalName, - prefix: Option<DOMString>, - document: &Document) -> Root<HTMLOutputElement> { - Node::reflect_node(box HTMLOutputElement::new_inherited(local_name, prefix, document), - document, - HTMLOutputElementBinding::Wrap) + pub fn new( + local_name: LocalName, + prefix: Option<Prefix>, + document: &Document, + ) -> DomRoot<HTMLOutputElement> { + Node::reflect_node( + Box::new(HTMLOutputElement::new_inherited( + local_name, prefix, document, + )), + document, + ) } -} -impl HTMLOutputElementMethods for HTMLOutputElement { - // https://html.spec.whatwg.org/multipage/#dom-cva-validity - fn Validity(&self) -> Root<ValidityState> { - let window = window_from_node(self); - ValidityState::new(&window, self.upcast()) + pub fn reset(&self) { + Node::string_replace_all(self.DefaultValue(), self.upcast::<Node>()); + *self.default_value_override.borrow_mut() = None; } +} +impl HTMLOutputElementMethods for HTMLOutputElement { // https://html.spec.whatwg.org/multipage/#dom-fae-form - fn GetForm(&self) -> Option<Root<HTMLFormElement>> { + fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> { self.form_owner() } // https://html.spec.whatwg.org/multipage/#dom-lfe-labels - fn Labels(&self) -> Root<NodeList> { - self.upcast::<HTMLElement>().labels() + make_labels_getter!(Labels, labels_node_list); + + // https://html.spec.whatwg.org/multipage/#dom-output-defaultvaleu + fn DefaultValue(&self) -> DOMString { + let dvo = self.default_value_override.borrow(); + if let Some(ref dv) = *dvo { + dv.clone() + } else { + self.upcast::<Node>().descendant_text_content() + } + } + + // https://html.spec.whatwg.org/multipage/#dom-output-defaultvalue + fn SetDefaultValue(&self, value: DOMString) { + if self.default_value_override.borrow().is_none() { + // Step 1 ("and return") + Node::string_replace_all(value.clone(), self.upcast::<Node>()); + } else { + // Step 2, if not returned from step 1 + *self.default_value_override.borrow_mut() = Some(value); + } + } + + // https://html.spec.whatwg.org/multipage/#dom-output-value + fn Value(&self) -> DOMString { + self.upcast::<Node>().descendant_text_content() + } + + // https://html.spec.whatwg.org/multipage/#dom-output-value + fn SetValue(&self, value: DOMString) { + *self.default_value_override.borrow_mut() = Some(self.DefaultValue()); + Node::string_replace_all(value, self.upcast::<Node>()); + } + + // https://html.spec.whatwg.org/multipage/#dom-output-type + fn Type(&self) -> DOMString { + return DOMString::from("output"); + } + + // https://html.spec.whatwg.org/multipage/#dom-fe-name + make_atomic_setter!(SetName, "name"); + + // https://html.spec.whatwg.org/multipage/#dom-fe-name + make_getter!(Name, "name"); + + // https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate + fn WillValidate(&self) -> bool { + self.is_instance_validatable() + } + + // https://html.spec.whatwg.org/multipage/#dom-cva-validity + fn Validity(&self) -> DomRoot<ValidityState> { + self.validity_state() + } + + // https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity + fn CheckValidity(&self) -> bool { + self.check_validity() + } + + // https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity + fn ReportValidity(&self) -> bool { + self.report_validity() + } + + // https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage + fn ValidationMessage(&self) -> DOMString { + self.validation_message() + } + + // https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity + fn SetCustomValidity(&self, error: DOMString) { + self.validity_state().set_custom_error_message(error); } } impl VirtualMethods for HTMLOutputElement { - fn super_type<'b>(&'b self) -> Option<&'b VirtualMethods> { - Some(self.upcast::<HTMLElement>() as &VirtualMethods) + fn super_type<'b>(&'b self) -> Option<&'b dyn VirtualMethods> { + Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods) } fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation) { @@ -81,7 +164,7 @@ impl VirtualMethods for HTMLOutputElement { } impl FormControl for HTMLOutputElement { - fn form_owner(&self) -> Option<Root<HTMLFormElement>> { + fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> { self.form_owner.get() } @@ -93,3 +176,19 @@ impl FormControl for HTMLOutputElement { self.upcast::<Element>() } } + +impl Validatable for HTMLOutputElement { + 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 { + // output is not a submittable element (https://html.spec.whatwg.org/multipage/#category-submit) + false + } +} |