mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Implement the rules for parsing integers as defined by HTML, and use them for canvas.
This commit is contained in:
parent
940c013176
commit
e1b5b5183f
2 changed files with 64 additions and 4 deletions
|
@ -19,12 +19,11 @@ use dom::node::{Node, ElementNodeTypeId, window_from_node};
|
||||||
use dom::virtualmethods::VirtualMethods;
|
use dom::virtualmethods::VirtualMethods;
|
||||||
|
|
||||||
use servo_util::atom::Atom;
|
use servo_util::atom::Atom;
|
||||||
use servo_util::str::DOMString;
|
use servo_util::str::{DOMString, parse_unsigned_integer};
|
||||||
|
|
||||||
use geom::size::Size2D;
|
use geom::size::Size2D;
|
||||||
|
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::num;
|
|
||||||
|
|
||||||
static DefaultWidth: u32 = 300;
|
static DefaultWidth: u32 = 300;
|
||||||
static DefaultHeight: u32 = 150;
|
static DefaultHeight: u32 = 150;
|
||||||
|
@ -134,11 +133,11 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLCanvasElement> {
|
||||||
|
|
||||||
let recreate = match name.as_slice() {
|
let recreate = match name.as_slice() {
|
||||||
"width" => {
|
"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
|
true
|
||||||
}
|
}
|
||||||
"height" => {
|
"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
|
true
|
||||||
}
|
}
|
||||||
_ => false,
|
_ => false,
|
||||||
|
|
|
@ -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>> {
|
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())
|
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()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue