mirror of
https://github.com/servo/servo.git
synced 2025-07-25 16:20:36 +01:00
Add parsing of double values in style attributes
This commit is contained in:
parent
84ab7e9fe8
commit
48b2e9c055
11 changed files with 132 additions and 14 deletions
|
@ -150,8 +150,8 @@ pub fn parse_legacy_font_size(mut input: &str) -> Option<&'static str> {
|
||||||
|
|
||||||
// Steps 6, 7, 8
|
// Steps 6, 7, 8
|
||||||
let mut value = match read_numbers(input_chars) {
|
let mut value = match read_numbers(input_chars) {
|
||||||
Some(v) => v,
|
(Some(v), _) => v,
|
||||||
None => return None,
|
(None, _) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Step 9
|
// Step 9
|
||||||
|
|
|
@ -410,7 +410,7 @@ impl VirtualMethods for HTMLTableElement {
|
||||||
}
|
}
|
||||||
atom!("cellspacing") => {
|
atom!("cellspacing") => {
|
||||||
self.cellspacing.set(mutation.new_value(attr).and_then(|value| {
|
self.cellspacing.set(mutation.new_value(attr).and_then(|value| {
|
||||||
parse_unsigned_integer(value.chars())
|
parse_unsigned_integer(value.chars()).ok()
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
|
1
components/servo/Cargo.lock
generated
1
components/servo/Cargo.lock
generated
|
@ -2264,6 +2264,7 @@ dependencies = [
|
||||||
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"plugins 0.0.1",
|
"plugins 0.0.1",
|
||||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -12,7 +12,7 @@ use std::str::FromStr;
|
||||||
use string_cache::{Atom, Namespace};
|
use string_cache::{Atom, Namespace};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use util::str::{DOMString, LengthOrPercentageOrAuto, HTML_SPACE_CHARACTERS};
|
use util::str::{DOMString, LengthOrPercentageOrAuto, HTML_SPACE_CHARACTERS};
|
||||||
use util::str::{read_numbers, split_html_space_chars, str_join};
|
use util::str::{read_exponent, read_fraction, read_numbers, split_html_space_chars, str_join};
|
||||||
use values::specified::{Length};
|
use values::specified::{Length};
|
||||||
|
|
||||||
// Duplicated from script::dom::values.
|
// Duplicated from script::dom::values.
|
||||||
|
@ -24,6 +24,7 @@ pub enum AttrValue {
|
||||||
TokenList(DOMString, Vec<Atom>),
|
TokenList(DOMString, Vec<Atom>),
|
||||||
UInt(DOMString, u32),
|
UInt(DOMString, u32),
|
||||||
Int(DOMString, i32),
|
Int(DOMString, i32),
|
||||||
|
Double(DOMString, f64),
|
||||||
Atom(Atom),
|
Atom(Atom),
|
||||||
Length(DOMString, Option<Length>),
|
Length(DOMString, Option<Length>),
|
||||||
Color(DOMString, Option<RGBA>),
|
Color(DOMString, Option<RGBA>),
|
||||||
|
@ -52,25 +53,64 @@ fn do_parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i64> {
|
||||||
Some(_) => 1,
|
Some(_) => 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
let value = read_numbers(input);
|
let (value, _) = read_numbers(input);
|
||||||
|
|
||||||
value.and_then(|value| value.checked_mul(sign))
|
value.and_then(|value| value.checked_mul(sign))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an integer according to
|
/// Parse an integer according to
|
||||||
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers>.
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers>.
|
||||||
pub fn parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i32> {
|
pub fn parse_integer<T: Iterator<Item=char>>(input: T) -> Result<i32, ()> {
|
||||||
do_parse_integer(input).and_then(|result| {
|
do_parse_integer(input).and_then(|result| {
|
||||||
result.to_i32()
|
result.to_i32()
|
||||||
})
|
}).ok_or(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse an integer according to
|
/// Parse an integer according to
|
||||||
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
|
||||||
pub fn parse_unsigned_integer<T: Iterator<Item=char>>(input: T) -> Option<u32> {
|
pub fn parse_unsigned_integer<T: Iterator<Item=char>>(input: T) -> Result<u32, ()> {
|
||||||
do_parse_integer(input).and_then(|result| {
|
do_parse_integer(input).and_then(|result| {
|
||||||
result.to_u32()
|
result.to_u32()
|
||||||
})
|
}).ok_or(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse a floating-point number according to
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-floating-point-number-values>
|
||||||
|
pub fn parse_double(string: &DOMString) -> Result<f64, ()> {
|
||||||
|
let trimmed = string.trim_matches(HTML_SPACE_CHARACTERS);
|
||||||
|
let mut input = trimmed.chars().peekable();
|
||||||
|
|
||||||
|
let (value, divisor) = match input.peek() {
|
||||||
|
None => return Err(()),
|
||||||
|
Some(&'-') => {
|
||||||
|
input.next();
|
||||||
|
(-1f64, -1f64)
|
||||||
|
}
|
||||||
|
Some(&'+') => {
|
||||||
|
input.next();
|
||||||
|
(1f64, 1f64)
|
||||||
|
}
|
||||||
|
_ => (1f64, 1f64)
|
||||||
|
};
|
||||||
|
|
||||||
|
let (value, value_digits) = if let Some(&'.') = input.peek() {
|
||||||
|
(0f64, 0)
|
||||||
|
} else {
|
||||||
|
let (read_val, read_digits) = read_numbers(input);
|
||||||
|
(value * read_val.and_then(|result| result.to_f64()).unwrap_or(1f64), read_digits)
|
||||||
|
};
|
||||||
|
|
||||||
|
let input = trimmed.chars().skip(value_digits).peekable();
|
||||||
|
|
||||||
|
let (mut value, fraction_digits) = read_fraction(input, divisor, value);
|
||||||
|
|
||||||
|
let input = trimmed.chars().skip(value_digits + fraction_digits).peekable();
|
||||||
|
|
||||||
|
if let Some(exp) = read_exponent(input) {
|
||||||
|
value *= 10f64.powi(exp)
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AttrValue {
|
impl AttrValue {
|
||||||
|
@ -107,6 +147,17 @@ impl AttrValue {
|
||||||
AttrValue::Int(string, result)
|
AttrValue::Int(string, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-double
|
||||||
|
pub fn from_double(string: DOMString, default: f64) -> AttrValue {
|
||||||
|
let result = parse_double(&string).unwrap_or(default);
|
||||||
|
let result = if result.is_infinite() {
|
||||||
|
default
|
||||||
|
} else {
|
||||||
|
result
|
||||||
|
};
|
||||||
|
AttrValue::Double(string, result)
|
||||||
|
}
|
||||||
|
|
||||||
// https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers
|
// https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers
|
||||||
pub fn from_limited_i32(string: DOMString, default: i32) -> AttrValue {
|
pub fn from_limited_i32(string: DOMString, default: i32) -> AttrValue {
|
||||||
let result = parse_integer(string.chars()).unwrap_or(default);
|
let result = parse_integer(string.chars()).unwrap_or(default);
|
||||||
|
@ -250,6 +301,7 @@ impl Deref for AttrValue {
|
||||||
AttrValue::String(ref value) |
|
AttrValue::String(ref value) |
|
||||||
AttrValue::TokenList(ref value, _) |
|
AttrValue::TokenList(ref value, _) |
|
||||||
AttrValue::UInt(ref value, _) |
|
AttrValue::UInt(ref value, _) |
|
||||||
|
AttrValue::Double(ref value, _) |
|
||||||
AttrValue::Length(ref value, _) |
|
AttrValue::Length(ref value, _) |
|
||||||
AttrValue::Color(ref value, _) |
|
AttrValue::Color(ref value, _) |
|
||||||
AttrValue::Int(ref value, _) |
|
AttrValue::Int(ref value, _) |
|
||||||
|
|
|
@ -38,6 +38,7 @@ heapsize_plugin = "0.1.2"
|
||||||
lazy_static = "0.1"
|
lazy_static = "0.1"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
log = "0.3.5"
|
log = "0.3.5"
|
||||||
|
num = "0.1.32"
|
||||||
num_cpus = "0.2.2"
|
num_cpus = "0.2.2"
|
||||||
rand = "0.3"
|
rand = "0.3"
|
||||||
rustc-serialize = "0.3"
|
rustc-serialize = "0.3"
|
||||||
|
|
|
@ -34,6 +34,7 @@ extern crate lazy_static;
|
||||||
extern crate libc;
|
extern crate libc;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate log;
|
extern crate log;
|
||||||
|
extern crate num;
|
||||||
extern crate num_cpus;
|
extern crate num_cpus;
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use libc::c_char;
|
use libc::c_char;
|
||||||
|
use num::ToPrimitive;
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::convert::AsRef;
|
use std::convert::AsRef;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
|
@ -158,24 +159,74 @@ fn is_ascii_digit(c: &char) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_decimal_point(c: char) -> bool {
|
||||||
|
c == '.'
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read_numbers<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> Option<i64> {
|
fn is_exponent_char(c: char) -> bool {
|
||||||
|
match c {
|
||||||
|
'e' | 'E' => true,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_numbers<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> (Option<i64>, usize) {
|
||||||
match iter.peek() {
|
match iter.peek() {
|
||||||
Some(c) if is_ascii_digit(c) => (),
|
Some(c) if is_ascii_digit(c) => (),
|
||||||
_ => return None,
|
_ => return (None, 0),
|
||||||
}
|
}
|
||||||
|
|
||||||
iter.take_while(is_ascii_digit).map(|d| {
|
iter.take_while(is_ascii_digit).map(|d| {
|
||||||
d as i64 - '0' as i64
|
d as i64 - '0' as i64
|
||||||
}).fold(Some(0i64), |accumulator, d| {
|
}).fold((Some(0i64), 0), |accumulator, d| {
|
||||||
accumulator.and_then(|accumulator| {
|
let digits = accumulator.0.and_then(|accumulator| {
|
||||||
accumulator.checked_mul(10)
|
accumulator.checked_mul(10)
|
||||||
}).and_then(|accumulator| {
|
}).and_then(|accumulator| {
|
||||||
accumulator.checked_add(d)
|
accumulator.checked_add(d)
|
||||||
|
});
|
||||||
|
(digits, accumulator.1 + 1)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_fraction<I: Iterator<Item=char>>(mut iter: Peekable<I>,
|
||||||
|
mut divisor: f64,
|
||||||
|
value: f64) -> (f64, usize) {
|
||||||
|
match iter.peek() {
|
||||||
|
Some(c) if is_decimal_point(*c) => (),
|
||||||
|
_ => return (value, 0),
|
||||||
|
}
|
||||||
|
iter.next();
|
||||||
|
|
||||||
|
iter.take_while(is_ascii_digit).map(|d|
|
||||||
|
d as i64 - '0' as i64
|
||||||
|
).fold((value, 1), |accumulator, d| {
|
||||||
|
divisor *= 10f64;
|
||||||
|
(accumulator.0 + d as f64 / divisor,
|
||||||
|
accumulator.1 + 1)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn read_exponent<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> Option<i32> {
|
||||||
|
match iter.peek() {
|
||||||
|
Some(c) if is_exponent_char(*c) => (),
|
||||||
|
_ => return None,
|
||||||
|
}
|
||||||
|
iter.next();
|
||||||
|
|
||||||
|
match iter.peek() {
|
||||||
|
None => None,
|
||||||
|
Some(&'-') => {
|
||||||
|
iter.next();
|
||||||
|
read_numbers(iter).0.map(|exp| -exp.to_i32().unwrap_or(0))
|
||||||
|
}
|
||||||
|
Some(&'+') => {
|
||||||
|
iter.next();
|
||||||
|
read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0))
|
||||||
|
}
|
||||||
|
Some(_) => read_numbers(iter).0.map(|exp| exp.to_i32().unwrap_or(0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, HeapSizeOf, PartialEq)]
|
#[derive(Clone, Copy, Debug, HeapSizeOf, PartialEq)]
|
||||||
pub enum LengthOrPercentageOrAuto {
|
pub enum LengthOrPercentageOrAuto {
|
||||||
Auto,
|
Auto,
|
||||||
|
|
1
ports/cef/Cargo.lock
generated
1
ports/cef/Cargo.lock
generated
|
@ -2141,6 +2141,7 @@ dependencies = [
|
||||||
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"plugins 0.0.1",
|
"plugins 0.0.1",
|
||||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
1
ports/geckolib/Cargo.lock
generated
1
ports/geckolib/Cargo.lock
generated
|
@ -556,6 +556,7 @@ dependencies = [
|
||||||
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"plugins 0.0.1",
|
"plugins 0.0.1",
|
||||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
1
ports/gonk/Cargo.lock
generated
1
ports/gonk/Cargo.lock
generated
|
@ -2122,6 +2122,7 @@ dependencies = [
|
||||||
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
"lazy_static 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
"num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
"num_cpus 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"plugins 0.0.1",
|
"plugins 0.0.1",
|
||||||
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -6,6 +6,15 @@ use app_units::Au;
|
||||||
use style::attr::{AttrValue, parse_length};
|
use style::attr::{AttrValue, parse_length};
|
||||||
use util::str::{DOMString, LengthOrPercentageOrAuto};
|
use util::str::{DOMString, LengthOrPercentageOrAuto};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_parse_double() {
|
||||||
|
let value = DOMString::from("432.5e2");
|
||||||
|
match AttrValue::from_double(value, 0.0) {
|
||||||
|
AttrValue::Double(_, num) => assert_eq!(num, 43250f64),
|
||||||
|
_ => panic!("expected a double value")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_from_limited_i32_should_be_default_when_less_than_0() {
|
fn test_from_limited_i32_should_be_default_when_less_than_0() {
|
||||||
let value = DOMString::from("-1");
|
let value = DOMString::from("-1");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue