1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! Constraint validation (HTML §4.10.20) for the controls rdom ships.
//!
//! ## Contract
//!
//! - **Candidates** — `Dom::will_validate` (rdom-core): a submittable
//! control that is not barred (disabled, `readonly` text control,
//! hidden / reset / button input, non-submit button, inside a
//! `<datalist>`). Only candidates are checked, reported or block a
//! submission.
//! - **Validity states** — [`validity`] computes a [`ValidityState`]
//! from the control's current value and attributes (`states`), as the
//! `ValidityState` getters do — for barred controls too:
//! - `value_missing` — `required` and an empty value; an unchecked
//! checkbox; a radio group with a required member and no checked
//! member; a select with nothing selected or only its placeholder
//! label option;
//! - `type_mismatch` — `type=email` (the HTML "valid e-mail address"
//! grammar, each address of a `multiple` list) and `type=url` (a
//! simplified absolute-URL check, see DIVERGENCES);
//! - `pattern_mismatch` — `pattern` against the whole value
//! (`^(?:pattern)$`, module `pattern`), text / search / url / tel / email /
//! password only;
//! - `too_long` / `too_short` — `maxlength` / `minlength` in UTF-16
//! code units, only while the value was last changed by a user edit
//! (HTML's dirty value flag: an authored or programmatic value is
//! exempt);
//! - `range_underflow` / `range_overflow` / `step_mismatch` — `min` /
//! `max` / `step` on `type=number` (a range's value is always in
//! range and on a step, so it never suffers them);
//! - `bad_input` — `type=number` text that is not a valid
//! floating-point number;
//! - `custom_error` — [`set_custom_validity`] with a non-empty message.
//! - **API** — [`check_validity`] fires a cancelable, non-bubbling
//! `invalid` at an invalid candidate (at every invalid control a form
//! owns, for a `<form>`) and returns false; [`report_validity`] does
//! the same and then *reports*: rdom has no validation bubble, so it
//! focuses the (first) invalid control whose `invalid` was not
//! canceled. [`validation_message`] is rdom's English message for the
//! first failing state (`messages`), `""` for a valid or barred control.
//! - **Selectors** — [`install`] gives rdom-core's `:valid` /
//! `:invalid` their verdict; `ValidityMarks` (module `marks`) keeps
//! them current in the App's incremental cascade.
//! - **Submission** — `form::submit` runs `interactively_validate`
//! unless `SubmitDetail::no_validate` (the form's `novalidate`, the
//! submitter's `formnovalidate`): any invalid control blocks the
//! `submit` event, whether or not its `invalid` was canceled.
use NodeId;
use crate::;
pub use ValidityMarks;
pub use PatternCache;
/// The validity states of a control (HTML `ValidityState`). Every flag
/// is a state the control *suffers from*; [`ValidityState::valid`] is
/// true when it suffers from none.
/// Hook the validity states into the selector engine: `:valid` /
/// `:invalid` then match by [`validity`] (`Dom::set_validity_hook`).
/// `App` construction calls it; a bare `TuiDom` that matches those
/// pseudo-classes calls it once itself. The App also re-cascades the
/// elements whose validity changed before each frame
/// (`ValidityMarks`).
/// The [`rdom_core::ValidityHook`] rdom-tui installs.
/// The validity states of `id`, computed from its current value and
/// attributes (all false for an element without constraints).
/// Whether `id` is a candidate for constraint validation that does not
/// satisfy its constraints.
/// `validationMessage`: rdom's English message for the first failing
/// state (a custom error's own message first), or `""` when `id` is
/// valid or barred.
///
/// HTML leaves the wording to the user agent; rdom's messages are fixed
/// English modeled on Chromium's, checked in this order:
///
/// | state | message |
/// |---|---|
/// | custom error | the `setCustomValidity` message |
/// | value missing | `Please fill out this field.` (checkbox: `Please check this box if you want to proceed.`; radio: `Please select one of these options.`; select: `Please select an item in the list.`) |
/// | type mismatch | `Please enter an email address.` / `Please enter a URL.` |
/// | bad input | `Please enter a number.` |
/// | pattern mismatch | `Please match the requested format.` |
/// | too long | `Please shorten this text to N characters or less (you are currently using M characters).` |
/// | too short | `Please lengthen this text to N characters or more (you are currently using M characters).` |
/// | range underflow | `Value must be greater than or equal to N.` |
/// | range overflow | `Value must be less than or equal to N.` |
/// | step mismatch | `Please enter a valid value. The two nearest valid values are A and B.` |
///
/// Character counts are UTF-16 code units, as the constraint is.
/// `setCustomValidity(message)`: a non-empty `message` makes `id`
/// suffer from a custom error with that message; `""` clears it.
/// `checkValidity()`. On a `<form>`: statically validate it — fire
/// `invalid` at each invalid control it owns, in tree order — and
/// return whether there were none. On a control: fire `invalid` if it is
/// an invalid candidate and return false; true otherwise.
/// `reportValidity()`: [`check_validity`], then report the problem —
/// focus the control (on a `<form>`, the first invalid control) whose
/// `invalid` event was not canceled. rdom shows no validation bubble.
/// HTML §4.10.20.2 "interactively validate the constraints" of `form`:
/// statically validate it, then report the first unhandled invalid
/// control (focus it). True when every owned candidate is valid.
pub
/// HTML §4.10.20.2 "statically validate the constraints": snapshot the
/// invalid candidates `form` owns, then fire `invalid` at each in tree
/// order. Returns each with whether its event went uncanceled (`None`:
/// dropped by an earlier listener before its turn — still counted
/// invalid, as it was in the snapshot, but never reported).
/// Fire a cancelable, non-bubbling `invalid` at `id`. Returns whether
/// no listener canceled it, or `None` when `id` is no longer a node — a
/// listener of an earlier control's `invalid` dropped it: nothing was
/// fired and there is nothing to report.
/// Report a problem with `id` to the user: focus it (browsers also show
/// a bubble with the validation message, which rdom does not).