diff options
author | Nathan <nlincoln@intellifarms.com> | 2018-01-08 22:39:39 -0600 |
---|---|---|
committer | Nathan <nlincoln@intellifarms.com> | 2018-01-09 20:08:09 -0600 |
commit | 5b6e8215591c835dfb7092af25676d8ced344ebe (patch) | |
tree | 8e841116b134463bc4773d9b11f1ffa97bd53ce8 /components/script/dom | |
parent | d0300ffd67c2278f6c9ce8cac23923f13641abf3 (diff) | |
download | servo-5b6e8215591c835dfb7092af25676d8ced344ebe.tar.gz servo-5b6e8215591c835dfb7092af25676d8ced344ebe.zip |
input type=number validations
Diffstat (limited to 'components/script/dom')
-rw-r--r-- | components/script/dom/bindings/str.rs | 10 | ||||
-rwxr-xr-x | components/script/dom/htmlinputelement.rs | 6 |
2 files changed, 16 insertions, 0 deletions
diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index 9e7c01ed5ca..eaabc29806c 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -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 diff --git a/components/script/dom/htmlinputelement.rs b/components/script/dom/htmlinputelement.rs index defbc1f4b6f..38f0287d63a 100755 --- a/components/script/dom/htmlinputelement.rs +++ b/components/script/dom/htmlinputelement.rs @@ -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 _ => () } |