Skip to main content

script/dom/html/form_controls/
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::context::{JSContext, NoGC};
8use js::rust::HandleObject;
9use script_bindings::cell::DomRefCell;
10use script_bindings::domstring::TracedDOMString;
11
12use crate::dom::bindings::codegen::Bindings::HTMLOutputElementBinding::HTMLOutputElementMethods;
13use crate::dom::bindings::inheritance::Castable;
14use crate::dom::bindings::root::{DomRoot, MutNullableDom};
15use crate::dom::bindings::str::DOMString;
16use crate::dom::document::Document;
17use crate::dom::element::attributes::storage::AttrRef;
18use crate::dom::element::{AttributeMutation, Element};
19use crate::dom::html::htmlelement::HTMLElement;
20use crate::dom::html::htmlformelement::{FormControl, HTMLFormElement};
21use crate::dom::node::virtualmethods::VirtualMethods;
22use crate::dom::node::{Node, NodeTraits};
23use crate::dom::nodelist::NodeList;
24use crate::dom::validation::Validatable;
25use crate::dom::validitystate::ValidityState;
26
27#[dom_struct]
28pub(crate) struct HTMLOutputElement {
29    htmlelement: HTMLElement,
30    form_owner: MutNullableDom<HTMLFormElement>,
31    labels_node_list: MutNullableDom<NodeList>,
32    default_value_override: DomRefCell<Option<TracedDOMString>>,
33    validity_state: MutNullableDom<ValidityState>,
34}
35
36impl HTMLOutputElement {
37    fn new_inherited(
38        local_name: LocalName,
39        prefix: Option<Prefix>,
40        document: &Document,
41    ) -> HTMLOutputElement {
42        HTMLOutputElement {
43            htmlelement: HTMLElement::new_inherited(local_name, prefix, document),
44            form_owner: Default::default(),
45            labels_node_list: Default::default(),
46            default_value_override: DomRefCell::new(None),
47            validity_state: Default::default(),
48        }
49    }
50
51    pub(crate) fn new(
52        cx: &mut js::context::JSContext,
53        local_name: LocalName,
54        prefix: Option<Prefix>,
55        document: &Document,
56        proto: Option<HandleObject>,
57    ) -> DomRoot<HTMLOutputElement> {
58        Node::reflect_node_with_proto(
59            cx,
60            Box::new(HTMLOutputElement::new_inherited(
61                local_name, prefix, document,
62            )),
63            document,
64            proto,
65        )
66    }
67
68    pub(crate) fn reset(&self, cx: &mut JSContext) {
69        Node::string_replace_all(cx, self.DefaultValue(), self.upcast::<Node>());
70        *self.default_value_override.safe_borrow_mut(cx.no_gc()) = 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().root()
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, cx: &mut JSContext, value: DOMString) {
95        if self.default_value_override.borrow().is_none() {
96            // Step 1 ("and return")
97            Node::string_replace_all(cx, value, self.upcast::<Node>());
98        } else {
99            // Step 2, if not returned from step 1
100            *self.default_value_override.safe_borrow_mut(cx.no_gc()) = Some(value.traced());
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, cx: &mut JSContext, value: DOMString) {
111        *self.default_value_override.safe_borrow_mut(cx.no_gc()) =
112            Some(self.DefaultValue().traced());
113        Node::string_replace_all(cx, value, self.upcast::<Node>());
114    }
115
116    /// <https://html.spec.whatwg.org/multipage/#dom-output-type>
117    fn Type(&self) -> DOMString {
118        DOMString::from_static("output")
119    }
120
121    // https://html.spec.whatwg.org/multipage/#dom-fe-name
122    make_atomic_setter!(SetName, "name");
123
124    // https://html.spec.whatwg.org/multipage/#dom-fe-name
125    make_getter!(Name, "name");
126
127    /// <https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate>
128    fn WillValidate(&self, no_gc: &NoGC) -> bool {
129        self.is_instance_validatable(no_gc)
130    }
131
132    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
133    fn Validity(&self, cx: &mut JSContext) -> DomRoot<ValidityState> {
134        self.validity_state(cx)
135    }
136
137    /// <https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity>
138    fn CheckValidity(&self, cx: &mut JSContext) -> bool {
139        self.check_validity(cx)
140    }
141
142    /// <https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity>
143    fn ReportValidity(&self, cx: &mut JSContext) -> bool {
144        self.report_validity(cx)
145    }
146
147    /// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
148    fn ValidationMessage(&self, cx: &mut JSContext) -> DOMString {
149        self.validation_message(cx)
150    }
151
152    /// <https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity>
153    fn SetCustomValidity(&self, cx: &mut JSContext, error: DOMString) {
154        self.validity_state(cx).set_custom_error_message(cx, error);
155    }
156}
157
158impl VirtualMethods for HTMLOutputElement {
159    fn super_type(&self) -> Option<&dyn VirtualMethods> {
160        Some(self.upcast::<HTMLElement>() as &dyn VirtualMethods)
161    }
162
163    fn attribute_mutated(
164        &self,
165        cx: &mut js::context::JSContext,
166        attr: AttrRef<'_>,
167        mutation: AttributeMutation,
168    ) {
169        self.super_type()
170            .unwrap()
171            .attribute_mutated(cx, attr, mutation);
172        if attr.local_name() == &local_name!("form") {
173            self.form_attribute_mutated(cx, mutation);
174        }
175    }
176}
177
178impl FormControl for HTMLOutputElement {
179    fn form_owner(&self) -> Option<DomRoot<HTMLFormElement>> {
180        self.form_owner.get()
181    }
182
183    fn set_form_owner(&self, _cx: &mut JSContext, form: Option<&HTMLFormElement>) {
184        self.form_owner.set(form);
185    }
186
187    fn to_html_element(&self) -> &HTMLElement {
188        self.upcast::<HTMLElement>()
189    }
190}
191
192impl Validatable for HTMLOutputElement {
193    fn as_element(&self) -> &Element {
194        self.upcast()
195    }
196
197    fn validity_state(&self, cx: &mut JSContext) -> DomRoot<ValidityState> {
198        self.validity_state
199            .or_init(|| ValidityState::new(cx, &self.owner_window(), self.upcast()))
200    }
201
202    fn is_instance_validatable(&self, _no_gc: &NoGC) -> bool {
203        // output is not a submittable element (https://html.spec.whatwg.org/multipage/#category-submit)
204        false
205    }
206}