mirror of
https://github.com/servo/servo.git
synced 2025-08-08 06:55:31 +01:00
Pass ParserContext down to lengths
To make it possible to check the rule type when parsing lengths, we need to pass the `ParserContext` down through many layers to the place where length units are parsed. This change leaves it unused, so it's only to prepare for the next change. MozReview-Commit-ID: 70YwtcCxnWw
This commit is contained in:
parent
1ae1d370f2
commit
1a31b87c22
31 changed files with 304 additions and 251 deletions
|
@ -208,12 +208,12 @@ impl<'a> Mul<CSSFloat> for &'a SimplifiedValueNode {
|
|||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_integer(input: &mut Parser) -> Result<Integer, ()> {
|
||||
pub fn parse_integer(context: &ParserContext, input: &mut Parser) -> Result<Integer, ()> {
|
||||
match try!(input.next()) {
|
||||
Token::Number(ref value) => value.int_value.ok_or(()).map(Integer::new),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
let ast = try!(input.parse_nested_block(|i| {
|
||||
CalcLengthOrPercentage::parse_sum(i, CalcUnit::Integer)
|
||||
CalcLengthOrPercentage::parse_sum(context, i, CalcUnit::Integer)
|
||||
}));
|
||||
|
||||
let mut result = None;
|
||||
|
@ -236,7 +236,7 @@ pub fn parse_integer(input: &mut Parser) -> Result<Integer, ()> {
|
|||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_number(input: &mut Parser) -> Result<Number, ()> {
|
||||
pub fn parse_number(context: &ParserContext, input: &mut Parser) -> Result<Number, ()> {
|
||||
match try!(input.next()) {
|
||||
Token::Number(ref value) => {
|
||||
Ok(Number {
|
||||
|
@ -245,7 +245,9 @@ pub fn parse_number(input: &mut Parser) -> Result<Number, ()> {
|
|||
})
|
||||
},
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
let ast = try!(input.parse_nested_block(|i| CalcLengthOrPercentage::parse_sum(i, CalcUnit::Number)));
|
||||
let ast = try!(input.parse_nested_block(|i| {
|
||||
CalcLengthOrPercentage::parse_sum(context, i, CalcUnit::Number)
|
||||
}));
|
||||
|
||||
let mut result = None;
|
||||
|
||||
|
@ -298,9 +300,9 @@ impl BorderRadiusSize {
|
|||
|
||||
impl Parse for BorderRadiusSize {
|
||||
#[inline]
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
let first = try!(LengthOrPercentage::parse_non_negative(input));
|
||||
let second = input.try(LengthOrPercentage::parse_non_negative)
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
let first = try!(LengthOrPercentage::parse_non_negative(context, input));
|
||||
let second = input.try(|i| LengthOrPercentage::parse_non_negative(context, i))
|
||||
.unwrap_or_else(|()| first.clone());
|
||||
Ok(BorderRadiusSize(Size2D::new(first, second)))
|
||||
}
|
||||
|
@ -428,11 +430,11 @@ impl Angle {
|
|||
|
||||
impl Parse for Angle {
|
||||
/// Parses an angle according to CSS-VALUES § 6.1.
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
match try!(input.next()) {
|
||||
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
input.parse_nested_block(CalcLengthOrPercentage::parse_angle)
|
||||
input.parse_nested_block(|i| CalcLengthOrPercentage::parse_angle(context, i))
|
||||
},
|
||||
_ => Err(())
|
||||
}
|
||||
|
@ -457,12 +459,12 @@ impl Angle {
|
|||
/// unitless 0 angle and stores it as '0deg'. We can remove this and
|
||||
/// get back to the unified version Angle::parse once
|
||||
/// https://github.com/w3c/csswg-drafts/issues/1162 is resolved.
|
||||
pub fn parse_with_unitless(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
pub fn parse_with_unitless(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
match try!(input.next()) {
|
||||
Token::Dimension(ref value, ref unit) => Angle::parse_dimension(value.value, unit),
|
||||
Token::Number(ref value) if value.value == 0. => Ok(Angle::zero()),
|
||||
Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => {
|
||||
input.parse_nested_block(CalcLengthOrPercentage::parse_angle)
|
||||
input.parse_nested_block(|i| CalcLengthOrPercentage::parse_angle(context, i))
|
||||
},
|
||||
_ => Err(())
|
||||
}
|
||||
|
@ -485,8 +487,8 @@ pub fn parse_border_radius(context: &ParserContext, input: &mut Parser) -> Resul
|
|||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_border_width(input: &mut Parser) -> Result<Length, ()> {
|
||||
input.try(Length::parse_non_negative).or_else(|()| {
|
||||
pub fn parse_border_width(context: &ParserContext, input: &mut Parser) -> Result<Length, ()> {
|
||||
input.try(|i| Length::parse_non_negative(context, i)).or_else(|()| {
|
||||
match_ignore_ascii_case! { &try!(input.expect_ident()),
|
||||
"thin" => Ok(Length::from_px(1.)),
|
||||
"medium" => Ok(Length::from_px(3.)),
|
||||
|
@ -507,8 +509,8 @@ pub enum BorderWidth {
|
|||
}
|
||||
|
||||
impl Parse for BorderWidth {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<BorderWidth, ()> {
|
||||
match input.try(Length::parse_non_negative) {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<BorderWidth, ()> {
|
||||
match input.try(|i| Length::parse_non_negative(context, i)) {
|
||||
Ok(length) => Ok(BorderWidth::Width(length)),
|
||||
Err(_) => match_ignore_ascii_case! { &try!(input.expect_ident()),
|
||||
"thin" => Ok(BorderWidth::Thin),
|
||||
|
@ -659,13 +661,13 @@ impl ToComputedValue for Time {
|
|||
}
|
||||
|
||||
impl Parse for Time {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
match input.next() {
|
||||
Ok(Token::Dimension(ref value, ref unit)) => {
|
||||
Time::parse_dimension(value.value, &unit)
|
||||
}
|
||||
Ok(Token::Function(ref name)) if name.eq_ignore_ascii_case("calc") => {
|
||||
input.parse_nested_block(CalcLengthOrPercentage::parse_time)
|
||||
input.parse_nested_block(|i| CalcLengthOrPercentage::parse_time(context, i))
|
||||
}
|
||||
_ => Err(())
|
||||
}
|
||||
|
@ -700,14 +702,14 @@ pub struct Number {
|
|||
no_viewport_percentage!(Number);
|
||||
|
||||
impl Parse for Number {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(input)
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(context, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl Number {
|
||||
fn parse_with_minimum(input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
|
||||
match parse_number(input) {
|
||||
fn parse_with_minimum(context: &ParserContext, input: &mut Parser, min: CSSFloat) -> Result<Number, ()> {
|
||||
match parse_number(context, input) {
|
||||
Ok(value) if value.value >= min => Ok(value),
|
||||
_ => Err(()),
|
||||
}
|
||||
|
@ -722,13 +724,13 @@ impl Number {
|
|||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_non_negative(input: &mut Parser) -> Result<Number, ()> {
|
||||
Number::parse_with_minimum(input, 0.0)
|
||||
pub fn parse_non_negative(context: &ParserContext, input: &mut Parser) -> Result<Number, ()> {
|
||||
Number::parse_with_minimum(context, input, 0.0)
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_at_least_one(input: &mut Parser) -> Result<Number, ()> {
|
||||
Number::parse_with_minimum(input, 1.0)
|
||||
pub fn parse_at_least_one(context: &ParserContext, input: &mut Parser) -> Result<Number, ()> {
|
||||
Number::parse_with_minimum(context, input, 1.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -775,12 +777,12 @@ pub enum NumberOrPercentage {
|
|||
no_viewport_percentage!(NumberOrPercentage);
|
||||
|
||||
impl Parse for NumberOrPercentage {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
if let Ok(per) = input.try(Percentage::parse_non_negative) {
|
||||
return Ok(NumberOrPercentage::Percentage(per));
|
||||
}
|
||||
|
||||
Number::parse_non_negative(input).map(NumberOrPercentage::Number)
|
||||
Number::parse_non_negative(context, input).map(NumberOrPercentage::Number)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -801,8 +803,8 @@ pub struct Opacity(Number);
|
|||
no_viewport_percentage!(Opacity);
|
||||
|
||||
impl Parse for Opacity {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(input).map(Opacity)
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_number(context, input).map(Opacity)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -860,27 +862,27 @@ impl Integer {
|
|||
no_viewport_percentage!(Integer);
|
||||
|
||||
impl Parse for Integer {
|
||||
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_integer(input)
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
parse_integer(context, input)
|
||||
}
|
||||
}
|
||||
|
||||
impl Integer {
|
||||
fn parse_with_minimum(input: &mut Parser, min: i32) -> Result<Integer, ()> {
|
||||
match parse_integer(input) {
|
||||
fn parse_with_minimum(context: &ParserContext, input: &mut Parser, min: i32) -> Result<Integer, ()> {
|
||||
match parse_integer(context, input) {
|
||||
Ok(value) if value.value() >= min => Ok(value),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_non_negative(input: &mut Parser) -> Result<Integer, ()> {
|
||||
Integer::parse_with_minimum(input, 0)
|
||||
pub fn parse_non_negative(context: &ParserContext, input: &mut Parser) -> Result<Integer, ()> {
|
||||
Integer::parse_with_minimum(context, input, 0)
|
||||
}
|
||||
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse_positive(input: &mut Parser) -> Result<Integer, ()> {
|
||||
Integer::parse_with_minimum(input, 1)
|
||||
pub fn parse_positive(context: &ParserContext, input: &mut Parser) -> Result<Integer, ()> {
|
||||
Integer::parse_with_minimum(context, input, 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -990,7 +992,7 @@ impl ToComputedValue for Shadow {
|
|||
impl Shadow {
|
||||
// disable_spread_and_inset is for filter: drop-shadow(...)
|
||||
#[allow(missing_docs)]
|
||||
pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result<Shadow, ()> {
|
||||
pub fn parse(context: &ParserContext, input: &mut Parser, disable_spread_and_inset: bool) -> Result<Shadow, ()> {
|
||||
let mut lengths = [Length::zero(), Length::zero(), Length::zero(), Length::zero()];
|
||||
let mut lengths_parsed = false;
|
||||
let mut color = None;
|
||||
|
@ -1007,7 +1009,7 @@ impl Shadow {
|
|||
if let Ok(value) = input.try(|i| Length::parse(context, i)) {
|
||||
lengths[0] = value;
|
||||
lengths[1] = try!(Length::parse(context, input));
|
||||
if let Ok(value) = input.try(|i| Length::parse_non_negative(i)) {
|
||||
if let Ok(value) = input.try(|i| Length::parse_non_negative(context, i)) {
|
||||
lengths[2] = value;
|
||||
if !disable_spread_and_inset {
|
||||
if let Ok(value) = input.try(|i| Length::parse(context, i)) {
|
||||
|
@ -1204,14 +1206,14 @@ pub type LengthOrPercentageOrNumber = Either<LengthOrPercentage, Number>;
|
|||
|
||||
impl LengthOrPercentageOrNumber {
|
||||
/// parse a <length-percentage> | <number> enforcing that the contents aren't negative
|
||||
pub fn parse_non_negative(_: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
pub fn parse_non_negative(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
// NB: Parse numbers before Lengths so we are consistent about how to
|
||||
// recognize and serialize "0".
|
||||
if let Ok(num) = input.try(Number::parse_non_negative) {
|
||||
if let Ok(num) = input.try(|i| Number::parse_non_negative(context, i)) {
|
||||
return Ok(Either::Second(num))
|
||||
}
|
||||
|
||||
LengthOrPercentage::parse_non_negative(input).map(Either::First)
|
||||
LengthOrPercentage::parse_non_negative(context, input).map(Either::First)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue