mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
input type=number validations
This commit is contained in:
parent
d0300ffd67
commit
5b6e821559
5 changed files with 16 additions and 137 deletions
|
@ -303,6 +303,16 @@ impl DOMString {
|
|||
parse_week_string(&*self.0).is_ok()
|
||||
}
|
||||
|
||||
/// A valid number is the same as what rust considers to be valid,
|
||||
/// except for +1., NaN, and Infinity.
|
||||
/// https://html.spec.whatwg.org/multipage/#valid-floating-point-number
|
||||
pub fn is_valid_number_string(&self) -> bool {
|
||||
let input = &self.0;
|
||||
input.parse::<f64>().ok().map_or(false, |val| {
|
||||
!(val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+"))
|
||||
})
|
||||
}
|
||||
|
||||
/// A valid normalized local date and time string should be "{date}T{time}"
|
||||
/// where date and time are both valid, and the time string must be as short as possible
|
||||
/// https://html.spec.whatwg.org/multipage/#valid-normalised-local-date-and-time-string
|
||||
|
|
|
@ -1045,6 +1045,12 @@ impl HTMLInputElement {
|
|||
textinput.single_line_content_mut().clear();
|
||||
}
|
||||
}
|
||||
InputType::Number => {
|
||||
let mut textinput = self.textinput.borrow_mut();
|
||||
if !textinput.single_line_content().is_valid_number_string() {
|
||||
textinput.single_line_content_mut().clear();
|
||||
}
|
||||
}
|
||||
// TODO: Implement more value sanitization algorithms for different types of inputs
|
||||
_ => ()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue