script/dom/
validitystate.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 std::cell::Cell;
6use std::fmt;
7
8use bitflags::bitflags;
9use dom_struct::dom_struct;
10use itertools::Itertools;
11use stylo_dom::ElementState;
12
13use super::bindings::codegen::Bindings::ElementInternalsBinding::ValidityStateFlags;
14use crate::dom::bindings::cell::{DomRefCell, Ref};
15use crate::dom::bindings::codegen::Bindings::ValidityStateBinding::ValidityStateMethods;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
18use crate::dom::bindings::root::{Dom, DomRoot};
19use crate::dom::bindings::str::DOMString;
20use crate::dom::element::Element;
21use crate::dom::html::htmlfieldsetelement::HTMLFieldSetElement;
22use crate::dom::html::htmlformelement::FormControlElementHelpers;
23use crate::dom::node::Node;
24use crate::dom::window::Window;
25use crate::script_runtime::CanGc;
26
27/// <https://html.spec.whatwg.org/multipage/#validity-states>
28#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
29pub(crate) struct ValidationFlags(u32);
30
31bitflags! {
32    impl ValidationFlags: u32 {
33        const VALUE_MISSING    = 0b0000000001;
34        const TYPE_MISMATCH    = 0b0000000010;
35        const PATTERN_MISMATCH = 0b0000000100;
36        const TOO_LONG         = 0b0000001000;
37        const TOO_SHORT        = 0b0000010000;
38        const RANGE_UNDERFLOW  = 0b0000100000;
39        const RANGE_OVERFLOW   = 0b0001000000;
40        const STEP_MISMATCH    = 0b0010000000;
41        const BAD_INPUT        = 0b0100000000;
42        const CUSTOM_ERROR     = 0b1000000000;
43    }
44}
45
46impl fmt::Display for ValidationFlags {
47    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
48        let flag_to_message = [
49            (ValidationFlags::VALUE_MISSING, "Value missing"),
50            (ValidationFlags::TYPE_MISMATCH, "Type mismatch"),
51            (ValidationFlags::PATTERN_MISMATCH, "Pattern mismatch"),
52            (ValidationFlags::TOO_LONG, "Too long"),
53            (ValidationFlags::TOO_SHORT, "Too short"),
54            (ValidationFlags::RANGE_UNDERFLOW, "Range underflow"),
55            (ValidationFlags::RANGE_OVERFLOW, "Range overflow"),
56            (ValidationFlags::STEP_MISMATCH, "Step mismatch"),
57            (ValidationFlags::BAD_INPUT, "Bad input"),
58            (ValidationFlags::CUSTOM_ERROR, "Custom error"),
59        ];
60
61        flag_to_message
62            .iter()
63            .filter_map(|&(flag, flag_str)| {
64                if self.contains(flag) {
65                    Some(flag_str)
66                } else {
67                    None
68                }
69            })
70            .format(", ")
71            .fmt(formatter)
72    }
73}
74
75// https://html.spec.whatwg.org/multipage/#validitystate
76#[dom_struct]
77pub(crate) struct ValidityState {
78    reflector_: Reflector,
79    element: Dom<Element>,
80    custom_error_message: DomRefCell<DOMString>,
81    invalid_flags: Cell<ValidationFlags>,
82}
83
84impl ValidityState {
85    fn new_inherited(element: &Element) -> ValidityState {
86        ValidityState {
87            reflector_: Reflector::new(),
88            element: Dom::from_ref(element),
89            custom_error_message: DomRefCell::new(DOMString::new()),
90            invalid_flags: Cell::new(ValidationFlags::empty()),
91        }
92    }
93
94    pub(crate) fn new(window: &Window, element: &Element, can_gc: CanGc) -> DomRoot<ValidityState> {
95        reflect_dom_object(
96            Box::new(ValidityState::new_inherited(element)),
97            window,
98            can_gc,
99        )
100    }
101
102    // https://html.spec.whatwg.org/multipage/#custom-validity-error-message
103    pub(crate) fn custom_error_message(&self) -> Ref<'_, DOMString> {
104        self.custom_error_message.borrow()
105    }
106
107    // https://html.spec.whatwg.org/multipage/#custom-validity-error-message
108    pub(crate) fn set_custom_error_message(&self, error: DOMString) {
109        *self.custom_error_message.borrow_mut() = error;
110        self.perform_validation_and_update(ValidationFlags::CUSTOM_ERROR, CanGc::note());
111    }
112
113    /// Given a set of [ValidationFlags], recalculate their value by performing
114    /// validation on this [ValidityState]'s associated element. Additionally,
115    /// if [ValidationFlags::CUSTOM_ERROR] is in `update_flags` and a custom
116    /// error has been set on this [ValidityState], the state will be updated
117    /// to reflect the existance of a custom error.
118    pub(crate) fn perform_validation_and_update(
119        &self,
120        update_flags: ValidationFlags,
121        can_gc: CanGc,
122    ) {
123        let mut invalid_flags = self.invalid_flags.get();
124        invalid_flags.remove(update_flags);
125
126        if let Some(validatable) = self.element.as_maybe_validatable() {
127            let new_flags = validatable.perform_validation(update_flags, can_gc);
128            invalid_flags.insert(new_flags);
129        }
130
131        // https://html.spec.whatwg.org/multipage/#suffering-from-a-custom-error
132        if update_flags.contains(ValidationFlags::CUSTOM_ERROR) &&
133            !self.custom_error_message().is_empty()
134        {
135            invalid_flags.insert(ValidationFlags::CUSTOM_ERROR);
136        }
137
138        self.invalid_flags.set(invalid_flags);
139        self.update_pseudo_classes(can_gc);
140    }
141
142    pub(crate) fn update_invalid_flags(&self, update_flags: ValidationFlags) {
143        self.invalid_flags.set(update_flags);
144    }
145
146    pub(crate) fn invalid_flags(&self) -> ValidationFlags {
147        self.invalid_flags.get()
148    }
149
150    pub(crate) fn update_pseudo_classes(&self, can_gc: CanGc) {
151        if self.element.is_instance_validatable() {
152            let is_valid = self.invalid_flags.get().is_empty();
153            self.element.set_state(ElementState::VALID, is_valid);
154            self.element.set_state(ElementState::INVALID, !is_valid);
155        } else {
156            self.element.set_state(ElementState::VALID, false);
157            self.element.set_state(ElementState::INVALID, false);
158        }
159
160        if let Some(form_control) = self.element.as_maybe_form_control() {
161            if let Some(form_owner) = form_control.form_owner() {
162                form_owner.update_validity(can_gc);
163            }
164        }
165
166        if let Some(fieldset) = self
167            .element
168            .upcast::<Node>()
169            .ancestors()
170            .filter_map(DomRoot::downcast::<HTMLFieldSetElement>)
171            .next()
172        {
173            fieldset.update_validity(can_gc);
174        }
175    }
176}
177
178impl ValidityStateMethods<crate::DomTypeHolder> for ValidityState {
179    // https://html.spec.whatwg.org/multipage/#dom-validitystate-valuemissing
180    fn ValueMissing(&self) -> bool {
181        self.invalid_flags()
182            .contains(ValidationFlags::VALUE_MISSING)
183    }
184
185    // https://html.spec.whatwg.org/multipage/#dom-validitystate-typemismatch
186    fn TypeMismatch(&self) -> bool {
187        self.invalid_flags()
188            .contains(ValidationFlags::TYPE_MISMATCH)
189    }
190
191    // https://html.spec.whatwg.org/multipage/#dom-validitystate-patternmismatch
192    fn PatternMismatch(&self) -> bool {
193        self.invalid_flags()
194            .contains(ValidationFlags::PATTERN_MISMATCH)
195    }
196
197    // https://html.spec.whatwg.org/multipage/#dom-validitystate-toolong
198    fn TooLong(&self) -> bool {
199        self.invalid_flags().contains(ValidationFlags::TOO_LONG)
200    }
201
202    // https://html.spec.whatwg.org/multipage/#dom-validitystate-tooshort
203    fn TooShort(&self) -> bool {
204        self.invalid_flags().contains(ValidationFlags::TOO_SHORT)
205    }
206
207    // https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeunderflow
208    fn RangeUnderflow(&self) -> bool {
209        self.invalid_flags()
210            .contains(ValidationFlags::RANGE_UNDERFLOW)
211    }
212
213    // https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeoverflow
214    fn RangeOverflow(&self) -> bool {
215        self.invalid_flags()
216            .contains(ValidationFlags::RANGE_OVERFLOW)
217    }
218
219    // https://html.spec.whatwg.org/multipage/#dom-validitystate-stepmismatch
220    fn StepMismatch(&self) -> bool {
221        self.invalid_flags()
222            .contains(ValidationFlags::STEP_MISMATCH)
223    }
224
225    // https://html.spec.whatwg.org/multipage/#dom-validitystate-badinput
226    fn BadInput(&self) -> bool {
227        self.invalid_flags().contains(ValidationFlags::BAD_INPUT)
228    }
229
230    // https://html.spec.whatwg.org/multipage/#dom-validitystate-customerror
231    fn CustomError(&self) -> bool {
232        self.invalid_flags().contains(ValidationFlags::CUSTOM_ERROR)
233    }
234
235    // https://html.spec.whatwg.org/multipage/#dom-validitystate-valid
236    fn Valid(&self) -> bool {
237        self.invalid_flags().is_empty()
238    }
239}
240
241impl From<&ValidityStateFlags> for ValidationFlags {
242    fn from(flags: &ValidityStateFlags) -> Self {
243        let mut bits = ValidationFlags::empty();
244        if flags.valueMissing {
245            bits |= ValidationFlags::VALUE_MISSING;
246        }
247        if flags.typeMismatch {
248            bits |= ValidationFlags::TYPE_MISMATCH;
249        }
250        if flags.patternMismatch {
251            bits |= ValidationFlags::PATTERN_MISMATCH;
252        }
253        if flags.tooLong {
254            bits |= ValidationFlags::TOO_LONG;
255        }
256        if flags.tooShort {
257            bits |= ValidationFlags::TOO_SHORT;
258        }
259        if flags.rangeUnderflow {
260            bits |= ValidationFlags::RANGE_UNDERFLOW;
261        }
262        if flags.rangeOverflow {
263            bits |= ValidationFlags::RANGE_OVERFLOW;
264        }
265        if flags.stepMismatch {
266            bits |= ValidationFlags::STEP_MISMATCH;
267        }
268        if flags.badInput {
269            bits |= ValidationFlags::BAD_INPUT;
270        }
271        if flags.customError {
272            bits |= ValidationFlags::CUSTOM_ERROR;
273        }
274        bits
275    }
276}