input type=number validations

This commit is contained in:
Nathan 2018-01-08 22:39:39 -06:00
parent d0300ffd67
commit 5b6e821559
5 changed files with 16 additions and 137 deletions

View file

@ -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

View file

@ -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
_ => ()
}