diff options
author | Ms2ger <ms2ger@gmail.com> | 2014-08-19 21:13:48 +0200 |
---|---|---|
committer | Ms2ger <ms2ger@gmail.com> | 2014-09-03 19:48:07 +0200 |
commit | e1b5b5183fc106db6358d3b6e6526591c19e968c (patch) | |
tree | 1059f39397a70fd21dce7650db14b341512e66a4 /src | |
parent | 940c0131769e4f0f25376907f8c3f74f7bfef913 (diff) | |
download | servo-e1b5b5183fc106db6358d3b6e6526591c19e968c.tar.gz servo-e1b5b5183fc106db6358d3b6e6526591c19e968c.zip |
Implement the rules for parsing integers as defined by HTML, and use them for canvas.
Diffstat (limited to 'src')
-rw-r--r-- | src/components/script/dom/htmlcanvaselement.rs | 7 | ||||
-rw-r--r-- | src/components/util/str.rs | 61 |
2 files changed, 64 insertions, 4 deletions
diff --git a/src/components/script/dom/htmlcanvaselement.rs b/src/components/script/dom/htmlcanvaselement.rs index 50198b197ee..5d6f09badce 100644 --- a/src/components/script/dom/htmlcanvaselement.rs +++ b/src/components/script/dom/htmlcanvaselement.rs @@ -19,12 +19,11 @@ use dom::node::{Node, ElementNodeTypeId, window_from_node}; use dom::virtualmethods::VirtualMethods; use servo_util::atom::Atom; -use servo_util::str::DOMString; +use servo_util::str::{DOMString, parse_unsigned_integer}; use geom::size::Size2D; use std::cell::Cell; -use std::num; static DefaultWidth: u32 = 300; static DefaultHeight: u32 = 150; @@ -134,11 +133,11 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLCanvasElement> { let recreate = match name.as_slice() { "width" => { - self.width.set(num::from_str_radix(value.as_slice(), 10).unwrap()); + self.width.set(parse_unsigned_integer(value.as_slice().chars()).unwrap()); true } "height" => { - self.height.set(num::from_str_radix(value.as_slice(), 10).unwrap()); + self.height.set(parse_unsigned_integer(value.as_slice().chars()).unwrap()); true } _ => false, diff --git a/src/components/util/str.rs b/src/components/util/str.rs index 69e50a78e59..8f698ac3271 100644 --- a/src/components/util/str.rs +++ b/src/components/util/str.rs @@ -46,3 +46,64 @@ pub static HTML_SPACE_CHARACTERS: StaticCharVec = &[ pub fn split_html_space_chars<'a>(s: &'a str) -> Filter<'a, &'a str, CharSplits<'a, StaticCharVec>> { s.split(HTML_SPACE_CHARACTERS).filter(|&split| !split.is_empty()) } + +/// Shared implementation to parse an integer according to +/// <http://www.whatwg.org/html/#rules-for-parsing-integers> or +/// <http://www.whatwg.org/html/#rules-for-parsing-non-negative-integers>. +fn do_parse_integer<T: Iterator<char>>(input: T) -> Option<i64> { + fn as_ascii_digit(c: char) -> Option<i64> { + match c { + '0'..'9' => Some(c as i64 - '0' as i64), + _ => None, + } + } + + + let mut input = input.skip_while(|c| { + HTML_SPACE_CHARACTERS.iter().any(|s| s == c) + }).peekable(); + + let sign = match input.peek() { + None => return None, + Some(&'-') => { + input.next(); + -1 + }, + Some(&'+') => { + input.next(); + 1 + }, + Some(_) => 1, + }; + + match input.peek() { + Some(&c) if as_ascii_digit(c).is_some() => (), + _ => return None, + } + + let value = input.filter_map(as_ascii_digit).fuse().fold(Some(0i64), |accumulator, d| { + accumulator.and_then(|accumulator| { + accumulator.checked_mul(&10) + }).and_then(|accumulator| { + accumulator.checked_add(&d) + }) + }); + + return value.and_then(|value| value.checked_mul(&sign)); +} + +/// Parse an integer according to +/// <http://www.whatwg.org/html/#rules-for-parsing-integers>. +pub fn parse_integer<T: Iterator<char>>(input: T) -> Option<i32> { + do_parse_integer(input).and_then(|result| { + result.to_i32() + }) +} + +/// Parse an integer according to +/// <http://www.whatwg.org/html/#rules-for-parsing-non-negative-integers>. +pub fn parse_unsigned_integer<T: Iterator<char>>(input: T) -> Option<u32> { + do_parse_integer(input).and_then(|result| { + result.to_u32() + }) +} |