mirror of
https://github.com/servo/servo.git
synced 2025-07-23 23:33:43 +01:00
Move parse_integer and parse_unsigned_integer from util::str to style::attr
This commit is contained in:
parent
f0d4c03bd9
commit
43d527fcc8
3 changed files with 49 additions and 49 deletions
|
@ -18,7 +18,8 @@ use dom::node::{Node, document_from_node};
|
||||||
use dom::virtualmethods::VirtualMethods;
|
use dom::virtualmethods::VirtualMethods;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use string_cache::Atom;
|
use string_cache::Atom;
|
||||||
use util::str::{self, DOMString, LengthOrPercentageOrAuto};
|
use style::attr::parse_unsigned_integer;
|
||||||
|
use util::str::{DOMString, LengthOrPercentageOrAuto};
|
||||||
|
|
||||||
#[dom_struct]
|
#[dom_struct]
|
||||||
pub struct HTMLTableElement {
|
pub struct HTMLTableElement {
|
||||||
|
@ -176,12 +177,12 @@ impl VirtualMethods for HTMLTableElement {
|
||||||
atom!("border") => {
|
atom!("border") => {
|
||||||
// According to HTML5 § 14.3.9, invalid values map to 1px.
|
// According to HTML5 § 14.3.9, invalid values map to 1px.
|
||||||
self.border.set(mutation.new_value(attr).map(|value| {
|
self.border.set(mutation.new_value(attr).map(|value| {
|
||||||
str::parse_unsigned_integer(value.chars()).unwrap_or(1)
|
parse_unsigned_integer(value.chars()).unwrap_or(1)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
atom!("cellspacing") => {
|
atom!("cellspacing") => {
|
||||||
self.cellspacing.set(mutation.new_value(attr).and_then(|value| {
|
self.cellspacing.set(mutation.new_value(attr).and_then(|value| {
|
||||||
str::parse_unsigned_integer(value.chars())
|
parse_unsigned_integer(value.chars())
|
||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
|
|
@ -5,12 +5,13 @@
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use cssparser::{self, Color, RGBA};
|
use cssparser::{self, Color, RGBA};
|
||||||
use euclid::num::Zero;
|
use euclid::num::Zero;
|
||||||
|
use num::ToPrimitive;
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use string_cache::{Atom, Namespace};
|
use string_cache::{Atom, Namespace};
|
||||||
use url::Url;
|
use url::Url;
|
||||||
use util::str::{DOMString, LengthOrPercentageOrAuto, WHITESPACE, parse_unsigned_integer, parse_length};
|
use util::str::{DOMString, LengthOrPercentageOrAuto, HTML_SPACE_CHARACTERS, WHITESPACE};
|
||||||
use util::str::{split_html_space_chars, str_join, parse_integer};
|
use util::str::{parse_length, 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.
|
||||||
|
@ -29,6 +30,48 @@ pub enum AttrValue {
|
||||||
Url(DOMString, Option<Url>),
|
Url(DOMString, Option<Url>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shared implementation to parse an integer according to
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers> or
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
|
||||||
|
fn do_parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i64> {
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
|
||||||
|
let value = read_numbers(input);
|
||||||
|
|
||||||
|
value.and_then(|value| value.checked_mul(sign))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse an integer according to
|
||||||
|
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers>.
|
||||||
|
pub fn parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i32> {
|
||||||
|
do_parse_integer(input).and_then(|result| {
|
||||||
|
result.to_i32()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Parse an integer according to
|
||||||
|
/// <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> {
|
||||||
|
do_parse_integer(input).and_then(|result| {
|
||||||
|
result.to_u32()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
impl AttrValue {
|
impl AttrValue {
|
||||||
pub fn from_serialized_tokenlist(tokens: DOMString) -> AttrValue {
|
pub fn from_serialized_tokenlist(tokens: DOMString) -> AttrValue {
|
||||||
let atoms =
|
let atoms =
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
use app_units::Au;
|
use app_units::Au;
|
||||||
use libc::c_char;
|
use libc::c_char;
|
||||||
use num_lib::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;
|
||||||
|
@ -174,49 +173,6 @@ pub fn read_numbers<I: Iterator<Item=char>>(mut iter: Peekable<I>) -> Option<i64
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Shared implementation to parse an integer according to
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers> or
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-non-negative-integers>
|
|
||||||
fn do_parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i64> {
|
|
||||||
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,
|
|
||||||
};
|
|
||||||
|
|
||||||
let value = read_numbers(input);
|
|
||||||
|
|
||||||
value.and_then(|value| value.checked_mul(sign))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse an integer according to
|
|
||||||
/// <https://html.spec.whatwg.org/multipage/#rules-for-parsing-integers>.
|
|
||||||
pub fn parse_integer<T: Iterator<Item=char>>(input: T) -> Option<i32> {
|
|
||||||
do_parse_integer(input).and_then(|result| {
|
|
||||||
result.to_i32()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Parse an integer according to
|
|
||||||
/// <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> {
|
|
||||||
do_parse_integer(input).and_then(|result| {
|
|
||||||
result.to_u32()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, HeapSizeOf, PartialEq)]
|
#[derive(Clone, Copy, Debug, HeapSizeOf, PartialEq)]
|
||||||
pub enum LengthOrPercentageOrAuto {
|
pub enum LengthOrPercentageOrAuto {
|
||||||
Auto,
|
Auto,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue