mirror of
https://github.com/servo/servo.git
synced 2025-08-06 22:15:33 +01:00
implement parsing of line_height and border_image_outset to parse plain zero as Number
This commit is contained in:
parent
b38da9b920
commit
beeca1213c
4 changed files with 69 additions and 18 deletions
|
@ -48,23 +48,27 @@
|
||||||
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
|
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
|
||||||
use cssparser::Token;
|
use cssparser::Token;
|
||||||
use std::ascii::AsciiExt;
|
use std::ascii::AsciiExt;
|
||||||
input.try(specified::LengthOrPercentage::parse_non_negative)
|
// We try to parse as a Number first because, for 'line-height', we want "0" to be
|
||||||
.map(SpecifiedValue::LengthOrPercentage)
|
// parsed as a plain Number rather than a Length (0px); this matches the behaviour
|
||||||
|
// of all major browsers
|
||||||
|
input.try(specified::Number::parse_non_negative)
|
||||||
|
.map(|n| SpecifiedValue::Number(n.0))
|
||||||
.or_else(|()| {
|
.or_else(|()| {
|
||||||
match try!(input.next()) {
|
input.try(specified::LengthOrPercentage::parse_non_negative)
|
||||||
Token::Number(ref value) if value.value >= 0. => {
|
.map(SpecifiedValue::LengthOrPercentage)
|
||||||
Ok(SpecifiedValue::Number(value.value))
|
.or_else(|()| {
|
||||||
|
match try!(input.next()) {
|
||||||
|
Token::Ident(ref value) if value.eq_ignore_ascii_case("normal") => {
|
||||||
|
Ok(SpecifiedValue::Normal)
|
||||||
|
}
|
||||||
|
% if product == "gecko":
|
||||||
|
Token::Ident(ref value) if value.eq_ignore_ascii_case("-moz-block-height") => {
|
||||||
|
Ok(SpecifiedValue::MozBlockHeight)
|
||||||
|
}
|
||||||
|
% endif
|
||||||
|
_ => Err(()),
|
||||||
}
|
}
|
||||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("normal") => {
|
})
|
||||||
Ok(SpecifiedValue::Normal)
|
|
||||||
}
|
|
||||||
% if product == "gecko":
|
|
||||||
Token::Ident(ref value) if value.eq_ignore_ascii_case("-moz-block-height") => {
|
|
||||||
Ok(SpecifiedValue::MozBlockHeight)
|
|
||||||
}
|
|
||||||
% endif
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub mod computed_value {
|
pub mod computed_value {
|
||||||
|
|
|
@ -1117,10 +1117,13 @@ pub type LengthOrNumber = Either<Length, Number>;
|
||||||
impl LengthOrNumber {
|
impl LengthOrNumber {
|
||||||
/// Parse a non-negative LengthOrNumber.
|
/// Parse a non-negative LengthOrNumber.
|
||||||
pub fn parse_non_negative(input: &mut Parser) -> Result<Self, ()> {
|
pub fn parse_non_negative(input: &mut Parser) -> Result<Self, ()> {
|
||||||
if let Ok(v) = input.try(Length::parse_non_negative) {
|
// We try to parse as a Number first because, for cases like LengthOrNumber,
|
||||||
Ok(Either::First(v))
|
// we want "0" to be parsed as a plain Number rather than a Length (0px); this
|
||||||
|
// matches the behaviour of all major browsers
|
||||||
|
if let Ok(v) = input.try(Number::parse_non_negative) {
|
||||||
|
Ok(Either::Second(v))
|
||||||
} else {
|
} else {
|
||||||
Number::parse_non_negative(input).map(Either::Second)
|
Length::parse_non_negative(input).map(Either::First)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -104,3 +104,21 @@ fn border_image_outset_should_error_on_negative_number() {
|
||||||
let result = border_image_outset::parse(&context, &mut parser);
|
let result = border_image_outset::parse(&context, &mut parser);
|
||||||
assert_eq!(result, Err(()));
|
assert_eq!(result, Err(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn border_image_outset_should_return_number_on_plain_zero() {
|
||||||
|
let url = ServoUrl::parse("http://localhost").unwrap();
|
||||||
|
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
|
||||||
|
let mut parser = Parser::new("0");
|
||||||
|
let result = border_image_outset::parse(&context, &mut parser);
|
||||||
|
assert_eq!(result.unwrap(), parse_longhand!(border_image_outset, "0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn border_image_outset_should_return_length_on_length_zero() {
|
||||||
|
let url = ServoUrl::parse("http://localhost").unwrap();
|
||||||
|
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
|
||||||
|
let mut parser = Parser::new("0em");
|
||||||
|
let result = border_image_outset::parse(&context, &mut parser);
|
||||||
|
assert_eq!(result.unwrap(), parse_longhand!(border_image_outset, "0em"));
|
||||||
|
}
|
||||||
|
|
|
@ -102,3 +102,29 @@ fn webkit_text_stroke_shorthand_should_parse_properly() {
|
||||||
assert_eq!(result._webkit_text_stroke_color.unwrap(), parse_longhand!(_webkit_text_stroke_color, "red"));
|
assert_eq!(result._webkit_text_stroke_color.unwrap(), parse_longhand!(_webkit_text_stroke_color, "red"));
|
||||||
assert_eq!(result._webkit_text_stroke_width.unwrap(), parse_longhand!(_webkit_text_stroke_width, "thin"));
|
assert_eq!(result._webkit_text_stroke_width.unwrap(), parse_longhand!(_webkit_text_stroke_width, "thin"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn line_height_should_return_number_on_plain_zero() {
|
||||||
|
use media_queries::CSSErrorReporterTest;
|
||||||
|
use servo_url::ServoUrl;
|
||||||
|
use style::properties::longhands::line_height;
|
||||||
|
|
||||||
|
let url = ServoUrl::parse("http://localhost").unwrap();
|
||||||
|
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
|
||||||
|
let mut parser = Parser::new("0");
|
||||||
|
let result = line_height::parse(&context, &mut parser);
|
||||||
|
assert_eq!(result.unwrap(), parse_longhand!(line_height, "0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn line_height_should_return_length_on_length_zero() {
|
||||||
|
use media_queries::CSSErrorReporterTest;
|
||||||
|
use servo_url::ServoUrl;
|
||||||
|
use style::properties::longhands::line_height;
|
||||||
|
|
||||||
|
let url = ServoUrl::parse("http://localhost").unwrap();
|
||||||
|
let context = ParserContext::new(Origin::Author, &url, Box::new(CSSErrorReporterTest));
|
||||||
|
let mut parser = Parser::new("0px");
|
||||||
|
let result = line_height::parse(&context, &mut parser);
|
||||||
|
assert_eq!(result.unwrap(), parse_longhand!(line_height, "0px"));
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue