script/dom/html/
htmloutputelement.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use html5ever::{LocalName, Prefix, local_name};
7use js::rust::HandleObject;
8
9use crate::dom::attr::Attr;
10use crate::dom::bindings::cell::DomRefCell;
11use crate::dom::bindings::codegen::Bindings::HTMLOutputElementBinding::HTMLOutputElementMethods;
12use crate::dom::bindings::inheritance::Castable;
13use crate::dom::bindings::root::{DomRoot, MutNullableDom};
14use crate::dom::bindings::str::DOMString;
15use crate::dom::document::Document;
16use crate::dom::element::{AttributeMutation, Element};
17use crate::dom::html::htmlelement::HTMLElement;
18use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
19use crate::dom::node::{Node, NodeTraits};
20use crate::dom::nodelist::NodeList;
21use crate::dom::validation::Validatable;
22use crate::dom::validitystate::ValidityState;
23use crate::dom::virtualmethods::VirtualMethods;
24use crate::script_runtime::CanGc;
25
26#[dom_struct]
27pub(crate) struct HTMLOutputElement {
28    htmlelement: HTMLElement,
29    form_owner: MutNullableDom<HTMLFormElement>,
30    labels_node_list: MutNullableDom<NodeList>,
31    default_value_override: DomRefCell<Option<DOMString>>,
32    validity_state: MutNullableDom<ValidityState>,
33}
34
35impl HTMLOutputElement {
36    fn new_inherited(
37        local_name: LocalName,
38        prefix: Option<Prefix>,
39        document: &Document,
40    ) -> HTMLOutputElement {
41        HTMLOutputElement {
42            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
43            form_owner: Default::default(),
44            labels_node_list: Default::default(),
45            default_value_override: DomRefCell::new(None),
46            validity_state: Default::default(),
47        }
48    }
49
50    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
51    pub(crate) fn new(
52        local_name: LocalName,
53        prefix: Option<Prefix>,
54        document: &Document,
55        proto: Option<HandleObject>,
56        can_gc: CanGc,
57    ) -> DomRoot<HTMLOutputElement> {
58        Node::reflect_node_with_proto(
59            Box::new(HTMLOutputElement::new_inherited(
60                local_name, prefix, document,
61            )),
62            document,
63            proto,
64            can_gc,
65        )
66    }
67
68    pub(crate) fn reset(&self, can_gc: CanGc) {
69        Node::string_replace_all(self.DefaultValue(), self.upcast::<Node>(), can_gc);
70        *self.default_value_override.borrow_mut() = None;
71    }
72}
73
74impl HTMLOutputElementMethods<crate::DomTypeHolder> for HTMLOutputElement {
75    // https://html.spec.whatwg.org/multipage/#dom-fae-form
76    fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
77        self.form_owner()
78    }
79
80    // https://html.spec.whatwg.org/multipage/#dom-lfe-labels
81    make_labels_getter!(Labels, labels_node_list);
82
83    // https://html.spec.whatwg.org/multipage/#dom-output-defaultvaleu
84    fn DefaultValue(&self) -> DOMString {
85        let dvo = self.default_value_override.borrow();
86        if let Some(ref dv) = *dvo {
87            dv.clone()
88        } else {
89            self.upcast::<Node>().descendant_text_content()
90        }
91    }
92
93    // https://html.spec.whatwg.org/multipage/#dom-output-defaultvalue
94    fn SetDefaultValue(&self, value: DOMString, can_gc: CanGc) {
95        if self.default_value_override.borrow().is_none() {
96            // Step 1 ("and return")
97            Node::string_replace_all(value.clone(), self.upcast::<Node>(), can_gc);
98        } else {
99            // Step 2, if not returned from step 1
100            *self.default_value_override.borrow_mut() = Some(value);
101        }
102    }
103
104    // https://html.spec.whatwg.org/multipage/#dom-output-value
105    fn Value(&self) -> DOMString {
106        self.upcast::<Node>().descendant_text_content()
107    }
108
109    // https://html.spec.whatwg.org/multipage/#dom-output-value
110    fn SetValue(&self, value: DOMString, can_gc: CanGc) {
111        *self.default_value_override.borrow_mut() = Some(self.DefaultValue());
112        Node::string_replace_all(value, self.upcast::<Node>(), can_gc);
113    }
114
115    // https://html.spec.whatwg.org/multipage/#dom-output-type
116    fn Type(&self) -> DOMString {
117        DOMString::from("output")
118    }
119
120    // https://html.spec.whatwg.org/multipage/#dom-fe-name
121    make_atomic_setter!(SetName, "name");
122
123    // https://html.spec.whatwg.org/multipage/#dom-fe-name
124    make_getter!(Name, "name");
125
126    // https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate
127    fn WillValidate(&self) -> bool {
128        self.is_instance_validatable()
129    }
130
131    // https://html.spec.whatwg.org/multipage/#dom-cva-validity
132    fn Validity(&self) -> DomRoot<ValidityState> {
133        self.validity_state()
134    }
135
136    // https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity
137    fn CheckValidity(&self, can_gc: CanGc) -> bool {
138        self.check_validity(can_gc)
139    }
140
141    // https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity
142    fn ReportValidity(&self, can_gc: CanGc) -> bool {
143        self.report_validity(can_gc)
144    }
145
146    // https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage
147    fn ValidationMessage(&self) -> DOMString {
148        self.validation_message()
149    }
150
151    // https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity
152    fn SetCustomValidity(&self, error: DOMString) {
153        self.validity_state().set_custom_error_message(error);
154    }
155}
156
157impl VirtualMethods for HTMLOutputElement {
158    fn super_type(&self) -> Option<&dyn VirtualMethods> {
159        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
160    }
161
162    fn attribute_mutated(&self, attr: &Attr, mutation: AttributeMutation, can_gc: CanGc) {
163        self.super_type()
164            .unwrap()
165            .attribute_mutated(attr, mutation, can_gc);
166        if attr.local_name() == &local_name!("form") {
167            self.form_attribute_mutated(mutation, can_gc);
168        }
169    }
170}
171
172impl FormControl for HTMLOutputElement {
173    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
174        self.form_owner.get()
175    }
176
177    fn set_form_owner(&self, form: Option<&HTMLFormElement>) {
178        self.form_owner.set(form);
179    }
180
181    fn to_element(&self) -> &Element {
182        self.upcast::<Element>()
183    }
184}
185
186impl Validatable for HTMLOutputElement {
187    fn as_element(&self) -> &Element {
188        self.upcast()
189    }
190
191    fn validity_state(&self) -> DomRoot<ValidityState> {
192        self.validity_state
193            .or_init(|| ValidityState::new(&self.owner_window(), self.upcast(), CanGc::note()))
194    }
195
196    fn is_instance_validatable(&self) -> bool {
197        // output is not a submittable element (https://html.spec.whatwg.org/multipage/#category-submit)
198        false
199    }
200}