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

View file

@ -1,41 +0,0 @@
[number.html]
type: testharness
[value is not a valid floating-point number: 1d+2]
expected: FAIL
[value not a valid floating-point number: random string]
expected: FAIL
[value ending with '.']
expected: FAIL
[ value = Infinity]
expected: FAIL
[value = -Infinity]
expected: FAIL
[value = NaN]
expected: FAIL
[value >= Number.MAX_VALUE]
expected: FAIL
[value = 1e]
expected: FAIL
[value = +1]
expected: FAIL
[value = '+']
expected: FAIL
[value = '-']
expected: FAIL
[value with a leading whitespace]
expected: FAIL
[value = 1trailing junk]
expected: FAIL

View file

@ -6,9 +6,6 @@
[change state from hidden to datetime]
expected: FAIL
[change state from hidden to number]
expected: FAIL
[change state from hidden to range]
expected: FAIL
@ -18,9 +15,6 @@
[change state from text to datetime]
expected: FAIL
[change state from text to number]
expected: FAIL
[change state from text to range]
expected: FAIL
@ -30,9 +24,6 @@
[change state from search to datetime]
expected: FAIL
[change state from search to number]
expected: FAIL
[change state from search to range]
expected: FAIL
@ -42,9 +33,6 @@
[change state from tel to datetime]
expected: FAIL
[change state from tel to number]
expected: FAIL
[change state from tel to range]
expected: FAIL
@ -63,9 +51,6 @@
[change state from url to datetime]
expected: FAIL
[change state from url to number]
expected: FAIL
[change state from url to range]
expected: FAIL
@ -93,9 +78,6 @@
[change state from email to datetime]
expected: FAIL
[change state from email to number]
expected: FAIL
[change state from email to range]
expected: FAIL
@ -105,9 +87,6 @@
[change state from password to datetime]
expected: FAIL
[change state from password to number]
expected: FAIL
[change state from password to range]
expected: FAIL
@ -132,9 +111,6 @@
[change state from datetime to week]
expected: FAIL
[change state from datetime to number]
expected: FAIL
[change state from datetime to range]
expected: FAIL
@ -162,33 +138,6 @@
[change state from time to range]
expected: FAIL
[change state from number to hidden]
expected: FAIL
[change state from number to checkbox]
expected: FAIL
[change state from number to radio]
expected: FAIL
[change state from number to submit]
expected: FAIL
[change state from number to image]
expected: FAIL
[change state from number to reset]
expected: FAIL
[change state from number to button]
expected: FAIL
[change state from number to email]
expected: FAIL
[change state from number to datetime]
expected: FAIL
[change state from number to range]
expected: FAIL
@ -219,15 +168,9 @@
[change state from range to datetime]
expected: FAIL
[change state from range to number]
expected: FAIL
[change state from checkbox to email]
expected: FAIL
[change state from checkbox to number]
expected: FAIL
[change state from checkbox to range]
expected: FAIL
@ -237,9 +180,6 @@
[change state from radio to datetime]
expected: FAIL
[change state from radio to number]
expected: FAIL
[change state from radio to range]
expected: FAIL
@ -249,9 +189,6 @@
[change state from submit to datetime]
expected: FAIL
[change state from submit to number]
expected: FAIL
[change state from submit to range]
expected: FAIL
@ -261,9 +198,6 @@
[change state from image to datetime]
expected: FAIL
[change state from image to number]
expected: FAIL
[change state from image to range]
expected: FAIL
@ -273,9 +207,6 @@
[change state from reset to datetime]
expected: FAIL
[change state from reset to number]
expected: FAIL
[change state from reset to range]
expected: FAIL
@ -285,30 +216,12 @@
[change state from button to datetime]
expected: FAIL
[change state from button to number]
expected: FAIL
[change state from button to range]
expected: FAIL
[change state from datetime-local to range]
expected: FAIL
[change state from number to text]
expected: FAIL
[change state from number to search]
expected: FAIL
[change state from number to tel]
expected: FAIL
[change state from number to url]
expected: FAIL
[change state from number to password]
expected: FAIL
[change state from range to text]
expected: FAIL
@ -324,9 +237,6 @@
[change state from range to password]
expected: FAIL
[change state from color to number]
expected: FAIL
[change state from color to range]
expected: FAIL

View file

@ -6,12 +6,6 @@
[value IDL attribute of input type datetime with value attribute]
expected: FAIL
[value IDL attribute of input type number without value attribute]
expected: FAIL
[value IDL attribute of input type number with value attribute]
expected: FAIL
[value IDL attribute of input type range without value attribute]
expected: FAIL