mirror of
https://github.com/servo/servo.git
synced 2025-08-05 13:40:08 +01:00
Convert remaining CSS values types to use AllowedNumericType when parsing numbers
This commit is contained in:
parent
138596e861
commit
226b757789
1 changed files with 36 additions and 45 deletions
|
@ -310,21 +310,21 @@ pub mod specified {
|
||||||
const AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
|
const AU_PER_PC: CSSFloat = AU_PER_PT * 12.;
|
||||||
impl Length {
|
impl Length {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn parse_internal(input: &mut Parser, negative_ok: bool) -> Result<Length, ()> {
|
fn parse_internal(input: &mut Parser, context: &AllowedNumericType) -> Result<Length, ()> {
|
||||||
match try!(input.next()) {
|
match try!(input.next()) {
|
||||||
Token::Dimension(ref value, ref unit) if negative_ok || value.value >= 0. => {
|
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||||
Length::parse_dimension(value.value, unit)
|
Length::parse_dimension(value.value, unit),
|
||||||
}
|
Token::Number(ref value) if value.value == 0. =>
|
||||||
Token::Number(ref value) if value.value == 0. => Ok(Length::Absolute(Au(0))),
|
Ok(Length::Absolute(Au(0))),
|
||||||
_ => Err(())
|
_ => Err(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub fn parse(input: &mut Parser) -> Result<Length, ()> {
|
pub fn parse(input: &mut Parser) -> Result<Length, ()> {
|
||||||
Length::parse_internal(input, /* negative_ok = */ true)
|
Length::parse_internal(input, &AllowedNumericType::All)
|
||||||
}
|
}
|
||||||
pub fn parse_non_negative(input: &mut Parser) -> Result<Length, ()> {
|
pub fn parse_non_negative(input: &mut Parser) -> Result<Length, ()> {
|
||||||
Length::parse_internal(input, /* negative_ok = */ false)
|
Length::parse_internal(input, &AllowedNumericType::NonNegative)
|
||||||
}
|
}
|
||||||
pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result<Length, ()> {
|
pub fn parse_dimension(value: CSSFloat, unit: &str) -> Result<Length, ()> {
|
||||||
match_ignore_ascii_case! { unit,
|
match_ignore_ascii_case! { unit,
|
||||||
|
@ -369,30 +369,27 @@ pub mod specified {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl LengthOrPercentage {
|
impl LengthOrPercentage {
|
||||||
fn parse_internal(input: &mut Parser, negative_ok: bool)
|
fn parse_internal(input: &mut Parser, context: &AllowedNumericType)
|
||||||
-> Result<LengthOrPercentage, ()> {
|
-> Result<LengthOrPercentage, ()>
|
||||||
|
{
|
||||||
match try!(input.next()) {
|
match try!(input.next()) {
|
||||||
Token::Dimension(ref value, ref unit) if negative_ok || value.value >= 0. => {
|
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||||
Length::parse_dimension(value.value, unit)
|
Length::parse_dimension(value.value, unit).map(LengthOrPercentage::Length),
|
||||||
.map(LengthOrPercentage::Length)
|
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||||
}
|
Ok(LengthOrPercentage::Percentage(value.unit_value)),
|
||||||
Token::Percentage(ref value) if negative_ok || value.unit_value >= 0. => {
|
Token::Number(ref value) if value.value == 0. =>
|
||||||
Ok(LengthOrPercentage::Percentage(value.unit_value))
|
Ok(LengthOrPercentage::Length(Length::Absolute(Au(0)))),
|
||||||
}
|
|
||||||
Token::Number(ref value) if value.value == 0. => {
|
|
||||||
Ok(LengthOrPercentage::Length(Length::Absolute(Au(0))))
|
|
||||||
}
|
|
||||||
_ => Err(())
|
_ => Err(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
|
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
|
||||||
LengthOrPercentage::parse_internal(input, /* negative_ok = */ true)
|
LengthOrPercentage::parse_internal(input, &AllowedNumericType::All)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
|
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentage, ()> {
|
||||||
LengthOrPercentage::parse_internal(input, /* negative_ok = */ false)
|
LengthOrPercentage::parse_internal(input, &AllowedNumericType::NonNegative)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -419,13 +416,11 @@ pub mod specified {
|
||||||
-> Result<LengthOrPercentageOrAuto, ()>
|
-> Result<LengthOrPercentageOrAuto, ()>
|
||||||
{
|
{
|
||||||
match try!(input.next()) {
|
match try!(input.next()) {
|
||||||
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) => {
|
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||||
Length::parse_dimension(value.value, unit)
|
Length::parse_dimension(value.value, unit).map(LengthOrPercentageOrAuto::Length),
|
||||||
.map(LengthOrPercentageOrAuto::Length)
|
|
||||||
}
|
|
||||||
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||||
Ok(LengthOrPercentageOrAuto::Percentage(value.unit_value)),
|
Ok(LengthOrPercentageOrAuto::Percentage(value.unit_value)),
|
||||||
Token::Number(ref value) if context.is_ok(value.value) =>
|
Token::Number(ref value) if value.value == 0. =>
|
||||||
Ok(LengthOrPercentageOrAuto::Length(Length::Absolute(Au(0)))),
|
Ok(LengthOrPercentageOrAuto::Length(Length::Absolute(Au(0)))),
|
||||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
|
Token::Ident(ref value) if value.eq_ignore_ascii_case("auto") =>
|
||||||
Ok(LengthOrPercentageOrAuto::Auto),
|
Ok(LengthOrPercentageOrAuto::Auto),
|
||||||
|
@ -453,40 +448,36 @@ pub mod specified {
|
||||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||||
match self {
|
match self {
|
||||||
&LengthOrPercentageOrNone::Length(length) => length.to_css(dest),
|
&LengthOrPercentageOrNone::Length(length) => length.to_css(dest),
|
||||||
&LengthOrPercentageOrNone::Percentage(percentage)
|
&LengthOrPercentageOrNone::Percentage(percentage) =>
|
||||||
=> write!(dest, "{}%", percentage * 100.),
|
write!(dest, "{}%", percentage * 100.),
|
||||||
&LengthOrPercentageOrNone::None => dest.write_str("none"),
|
&LengthOrPercentageOrNone::None => dest.write_str("none"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl LengthOrPercentageOrNone {
|
impl LengthOrPercentageOrNone {
|
||||||
fn parse_internal(input: &mut Parser, negative_ok: bool)
|
fn parse_internal(input: &mut Parser, context: &AllowedNumericType)
|
||||||
-> Result<LengthOrPercentageOrNone, ()> {
|
-> Result<LengthOrPercentageOrNone, ()>
|
||||||
|
{
|
||||||
match try!(input.next()) {
|
match try!(input.next()) {
|
||||||
Token::Dimension(ref value, ref unit) if negative_ok || value.value >= 0. => {
|
Token::Dimension(ref value, ref unit) if context.is_ok(value.value) =>
|
||||||
Length::parse_dimension(value.value, unit)
|
Length::parse_dimension(value.value, unit).map(LengthOrPercentageOrNone::Length),
|
||||||
.map(LengthOrPercentageOrNone::Length)
|
Token::Percentage(ref value) if context.is_ok(value.unit_value) =>
|
||||||
}
|
Ok(LengthOrPercentageOrNone::Percentage(value.unit_value)),
|
||||||
Token::Percentage(ref value) if negative_ok || value.unit_value >= 0. => {
|
Token::Number(ref value) if value.value == 0. =>
|
||||||
Ok(LengthOrPercentageOrNone::Percentage(value.unit_value))
|
Ok(LengthOrPercentageOrNone::Length(Length::Absolute(Au(0)))),
|
||||||
}
|
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") =>
|
||||||
Token::Number(ref value) if value.value == 0. => {
|
Ok(LengthOrPercentageOrNone::None),
|
||||||
Ok(LengthOrPercentageOrNone::Length(Length::Absolute(Au(0))))
|
|
||||||
}
|
|
||||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("none") => {
|
|
||||||
Ok(LengthOrPercentageOrNone::None)
|
|
||||||
}
|
|
||||||
_ => Err(())
|
_ => Err(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
|
pub fn parse(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
|
||||||
LengthOrPercentageOrNone::parse_internal(input, /* negative_ok = */ true)
|
LengthOrPercentageOrNone::parse_internal(input, &AllowedNumericType::All)
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
|
pub fn parse_non_negative(input: &mut Parser) -> Result<LengthOrPercentageOrNone, ()> {
|
||||||
LengthOrPercentageOrNone::parse_internal(input, /* negative_ok = */ false)
|
LengthOrPercentageOrNone::parse_internal(input, &AllowedNumericType::NonNegative)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue