mirror of
https://github.com/servo/servo.git
synced 2025-08-04 21:20:23 +01:00
Merge pull request #3205 from Ms2ger/parse-integers
Parse canvas's width and height attributes according to the specification; r=jdm
This commit is contained in:
commit
f7877008d2
24 changed files with 106 additions and 42 deletions
|
@ -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_or(DefaultWidth));
|
||||
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_or(DefaultHeight));
|
||||
true
|
||||
}
|
||||
_ => 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>> {
|
||||
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()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.em.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.parse.empty.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.exp.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.hex.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.parse.junk.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.parse.minus.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.parse.onlyspace.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.percent.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.space.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.trailingjunk.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.parse.whitespace.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.em.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.setAttribute.empty.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.exp.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.hex.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.setAttribute.junk.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.setAttribute.minus.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +0,0 @@
|
|||
[size.attributes.setAttribute.onlyspace.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.percent.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.space.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.trailingjunk.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
[size.attributes.setAttribute.whitespace.html]
|
||||
type: testharness
|
||||
expected: TIMEOUT
|
||||
[Parsing of non-negative integers in setAttribute]
|
||||
expected: FAIL
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue