script/dom/html/form_controls/
htmloutputelement.rs1use 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 fn GetForm(&self) -> Option<DomRoot<HTMLFormElement>> {
77 self.form_owner()
78 }
79
80 make_labels_getter!(Labels, labels_node_list);
82
83 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 fn SetDefaultValue(&self, cx: &mut JSContext, value: DOMString) {
95 if self.default_value_override.borrow().is_none() {
96 Node::string_replace_all(cx, value, self.upcast::<Node>());
98 } else {
99 *self.default_value_override.safe_borrow_mut(cx.no_gc()) = Some(value.traced());
101 }
102 }
103
104 fn Value(&self) -> DOMString {
106 self.upcast::<Node>().descendant_text_content()
107 }
108
109 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 fn Type(&self) -> DOMString {
118 DOMString::from_static("output")
119 }
120
121 make_atomic_setter!(SetName, "name");
123
124 make_getter!(Name, "name");
126
127 fn WillValidate(&self, no_gc: &NoGC) -> bool {
129 self.is_instance_validatable(no_gc)
130 }
131
132 fn Validity(&self, cx: &mut JSContext) -> DomRoot<ValidityState> {
134 self.validity_state(cx)
135 }
136
137 fn CheckValidity(&self, cx: &mut JSContext) -> bool {
139 self.check_validity(cx)
140 }
141
142 fn ReportValidity(&self, cx: &mut JSContext) -> bool {
144 self.report_validity(cx)
145 }
146
147 fn ValidationMessage(&self, cx: &mut JSContext) -> DOMString {
149 self.validation_message(cx)
150 }
151
152 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 false
205 }
206}