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            .find_map(DomRoot::downcast::<HTMLFieldSetElement>)
171        {
172            fieldset.update_validity(can_gc);
173        }
174    }
175}
176
177impl ValidityStateMethods<crate::DomTypeHolder> for ValidityState {
178    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-valuemissing>
179    fn ValueMissing(&self) -> bool {
180        self.invalid_flags()
181            .contains(ValidationFlags::VALUE_MISSING)
182    }
183
184    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-typemismatch>
185    fn TypeMismatch(&self) -> bool {
186        self.invalid_flags()
187            .contains(ValidationFlags::TYPE_MISMATCH)
188    }
189
190    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-patternmismatch>
191    fn PatternMismatch(&self) -> bool {
192        self.invalid_flags()
193            .contains(ValidationFlags::PATTERN_MISMATCH)
194    }
195
196    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-toolong>
197    fn TooLong(&self) -> bool {
198        self.invalid_flags().contains(ValidationFlags::TOO_LONG)
199    }
200
201    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-tooshort>
202    fn TooShort(&self) -> bool {
203        self.invalid_flags().contains(ValidationFlags::TOO_SHORT)
204    }
205
206    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeunderflow>
207    fn RangeUnderflow(&self) -> bool {
208        self.invalid_flags()
209            .contains(ValidationFlags::RANGE_UNDERFLOW)
210    }
211
212    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeoverflow>
213    fn RangeOverflow(&self) -> bool {
214        self.invalid_flags()
215            .contains(ValidationFlags::RANGE_OVERFLOW)
216    }
217
218    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-stepmismatch>
219    fn StepMismatch(&self) -> bool {
220        self.invalid_flags()
221            .contains(ValidationFlags::STEP_MISMATCH)
222    }
223
224    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-badinput>
225    fn BadInput(&self) -> bool {
226        self.invalid_flags().contains(ValidationFlags::BAD_INPUT)
227    }
228
229    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-customerror>
230    fn CustomError(&self) -> bool {
231        self.invalid_flags().contains(ValidationFlags::CUSTOM_ERROR)
232    }
233
234    /// <https://html.spec.whatwg.org/multipage/#dom-validitystate-valid>
235    fn Valid(&self) -> bool {
236        self.invalid_flags().is_empty()
237    }
238}
239
240impl From<&ValidityStateFlags> for ValidationFlags {
241    fn from(flags: &ValidityStateFlags) -> Self {
242        let mut bits = ValidationFlags::empty();
243        if flags.valueMissing {
244            bits |= ValidationFlags::VALUE_MISSING;
245        }
246        if flags.typeMismatch {
247            bits |= ValidationFlags::TYPE_MISMATCH;
248        }
249        if flags.patternMismatch {
250            bits |= ValidationFlags::PATTERN_MISMATCH;
251        }
252        if flags.tooLong {
253            bits |= ValidationFlags::TOO_LONG;
254        }
255        if flags.tooShort {
256            bits |= ValidationFlags::TOO_SHORT;
257        }
258        if flags.rangeUnderflow {
259            bits |= ValidationFlags::RANGE_UNDERFLOW;
260        }
261        if flags.rangeOverflow {
262            bits |= ValidationFlags::RANGE_OVERFLOW;
263        }
264        if flags.stepMismatch {
265            bits |= ValidationFlags::STEP_MISMATCH;
266        }
267        if flags.badInput {
268            bits |= ValidationFlags::BAD_INPUT;
269        }
270        if flags.customError {
271            bits |= ValidationFlags::CUSTOM_ERROR;
272        }
273        bits
274    }
275}