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:
J. Ryan Stinnett 2017-04-11 16:00:37 +08:00
parent 1ae1d370f2
commit 1a31b87c22
31 changed files with 304 additions and 251 deletions

View file

@ -482,7 +482,7 @@ ${helpers.single_keyword("background-origin",
})
}
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
if input.try(|input| input.expect_ident_matching("cover")).is_ok() {
return Ok(SpecifiedValue::Cover);
}
@ -492,12 +492,12 @@ ${helpers.single_keyword("background-origin",
}
let width =
try!(specified::LengthOrPercentageOrAuto::parse_non_negative(input));
try!(specified::LengthOrPercentageOrAuto::parse_non_negative(context, input));
let height = if input.is_exhausted() {
specified::LengthOrPercentageOrAuto::Auto
} else {
try!(specified::LengthOrPercentageOrAuto::parse_non_negative(input))
try!(specified::LengthOrPercentageOrAuto::parse_non_negative(context, input))
};
Ok(SpecifiedValue::Explicit(ExplicitSize {

View file

@ -525,16 +525,16 @@ ${helpers.single_keyword("-moz-float-edge", "content-box margin-box",
}
impl Parse for SingleSpecifiedValue {
fn parse(_context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
if input.try(|input| input.expect_ident_matching("auto")).is_ok() {
return Ok(SingleSpecifiedValue::Auto);
}
if let Ok(len) = input.try(|input| LengthOrPercentage::parse_non_negative(input)) {
if let Ok(len) = input.try(|input| LengthOrPercentage::parse_non_negative(context, input)) {
return Ok(SingleSpecifiedValue::LengthOrPercentage(len));
}
let num = try!(Number::parse_non_negative(input));
let num = try!(Number::parse_non_negative(context, input));
Ok(SingleSpecifiedValue::Number(num))
}
}

View file

@ -558,20 +558,20 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
}
impl Parse for SpecifiedValue {
fn parse(_context: &ParserContext, input: &mut ::cssparser::Parser) -> Result<Self, ()> {
fn parse(context: &ParserContext, input: &mut ::cssparser::Parser) -> Result<Self, ()> {
if let Ok(function_name) = input.try(|input| input.expect_function()) {
return match_ignore_ascii_case! { &function_name,
"cubic-bezier" => {
let (mut p1x, mut p1y, mut p2x, mut p2y) =
(Number::new(0.0), Number::new(0.0), Number::new(0.0), Number::new(0.0));
try!(input.parse_nested_block(|input| {
p1x = try!(specified::parse_number(input));
p1x = try!(specified::parse_number(context, input));
try!(input.expect_comma());
p1y = try!(specified::parse_number(input));
p1y = try!(specified::parse_number(context, input));
try!(input.expect_comma());
p2x = try!(specified::parse_number(input));
p2x = try!(specified::parse_number(context, input));
try!(input.expect_comma());
p2y = try!(specified::parse_number(input));
p2y = try!(specified::parse_number(context, input));
Ok(())
}));
if p1x.value < 0.0 || p1x.value > 1.0 ||
@ -585,7 +585,7 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto",
"steps" => {
let (mut step_count, mut start_end) = (specified::Integer::new(0), StartEnd::End);
try!(input.parse_nested_block(|input| {
step_count = try!(specified::parse_integer(input));
step_count = try!(specified::parse_integer(context, input));
if step_count.value() < 1 {
return Err(())
}
@ -1057,12 +1057,12 @@ ${helpers.single_keyword("animation-fill-mode",
}
}
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
Ok(SpecifiedValue::None)
} else if input.try(|input| input.expect_function_matching("repeat")).is_ok() {
input.parse_nested_block(|input| {
LengthOrPercentage::parse_non_negative(input).map(SpecifiedValue::Repeat)
LengthOrPercentage::parse_non_negative(context, input).map(SpecifiedValue::Repeat)
})
} else {
Err(())
@ -1349,7 +1349,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
"matrix" => {
try!(input.parse_nested_block(|input| {
let values = try!(input.parse_comma_separated(|input| {
specified::parse_number(input)
specified::parse_number(context, input)
}));
if values.len() != 6 {
return Err(())
@ -1367,7 +1367,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
},
"matrix3d" => {
try!(input.parse_nested_block(|input| {
let values = try!(input.parse_comma_separated(specified::parse_number));
let values = try!(input.parse_comma_separated(|i| specified::parse_number(context, i)));
if values.len() != 16 {
return Err(())
}
@ -1426,9 +1426,9 @@ ${helpers.predefined_type("scroll-snap-coordinate",
},
"scale" => {
try!(input.parse_nested_block(|input| {
let sx = try!(specified::parse_number(input));
let sx = try!(specified::parse_number(context, input));
if input.try(|input| input.expect_comma()).is_ok() {
let sy = try!(specified::parse_number(input));
let sy = try!(specified::parse_number(context, input));
result.push(SpecifiedOperation::Scale(sx, Some(sy)));
} else {
result.push(SpecifiedOperation::Scale(sx, None));
@ -1438,32 +1438,32 @@ ${helpers.predefined_type("scroll-snap-coordinate",
},
"scalex" => {
try!(input.parse_nested_block(|input| {
let sx = try!(specified::parse_number(input));
let sx = try!(specified::parse_number(context, input));
result.push(SpecifiedOperation::ScaleX(sx));
Ok(())
}))
},
"scaley" => {
try!(input.parse_nested_block(|input| {
let sy = try!(specified::parse_number(input));
let sy = try!(specified::parse_number(context, input));
result.push(SpecifiedOperation::ScaleY(sy));
Ok(())
}))
},
"scalez" => {
try!(input.parse_nested_block(|input| {
let sz = try!(specified::parse_number(input));
let sz = try!(specified::parse_number(context, input));
result.push(SpecifiedOperation::ScaleZ(sz));
Ok(())
}))
},
"scale3d" => {
try!(input.parse_nested_block(|input| {
let sx = try!(specified::parse_number(input));
let sx = try!(specified::parse_number(context, input));
try!(input.expect_comma());
let sy = try!(specified::parse_number(input));
let sy = try!(specified::parse_number(context, input));
try!(input.expect_comma());
let sz = try!(specified::parse_number(input));
let sz = try!(specified::parse_number(context, input));
result.push(SpecifiedOperation::Scale3D(sx, sy, sz));
Ok(())
}))
@ -1498,11 +1498,11 @@ ${helpers.predefined_type("scroll-snap-coordinate",
},
"rotate3d" => {
try!(input.parse_nested_block(|input| {
let ax = try!(specified::parse_number(input));
let ax = try!(specified::parse_number(context, input));
try!(input.expect_comma());
let ay = try!(specified::parse_number(input));
let ay = try!(specified::parse_number(context, input));
try!(input.expect_comma());
let az = try!(specified::parse_number(input));
let az = try!(specified::parse_number(context, input));
try!(input.expect_comma());
let theta = try!(specified::Angle::parse_with_unitless(context,input));
// TODO(gw): Check the axis can be normalized!!
@ -1538,7 +1538,7 @@ ${helpers.predefined_type("scroll-snap-coordinate",
},
"perspective" => {
try!(input.parse_nested_block(|input| {
let d = try!(specified::Length::parse_non_negative(input));
let d = try!(specified::Length::parse_non_negative(context, input));
result.push(SpecifiedOperation::Perspective(d));
Ok(())
}))

View file

@ -330,11 +330,11 @@
}
}
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
parse_common(1, input)
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
parse_common(context, 1, input)
}
pub fn parse_common(default_value: i32, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse_common(context: &ParserContext, default_value: i32, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue(Vec::new()))
}
@ -349,8 +349,8 @@
if content::counter_name_is_illegal(&counter_name) {
return Err(())
}
let counter_delta =
input.try(|input| specified::parse_integer(input)).unwrap_or(specified::Integer::new(default_value));
let counter_delta = input.try(|input| specified::parse_integer(context, input))
.unwrap_or(specified::Integer::new(default_value));
counters.push((counter_name, counter_delta))
}
@ -367,7 +367,7 @@
pub use super::counter_increment::{SpecifiedValue, computed_value, get_initial_value};
use super::counter_increment::parse_common;
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
parse_common(0, input)
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
parse_common(context, 0, input)
}
</%helpers:longhand>

View file

@ -339,7 +339,7 @@ ${helpers.predefined_type("clip",
if let Ok(function_name) = input.try(|input| input.expect_function()) {
filters.push(try!(input.parse_nested_block(|input| {
match_ignore_ascii_case! { &function_name,
"blur" => specified::Length::parse_non_negative(input).map(SpecifiedFilter::Blur),
"blur" => specified::Length::parse_non_negative(context, input).map(SpecifiedFilter::Blur),
"brightness" => parse_factor(input).map(SpecifiedFilter::Brightness),
"contrast" => parse_factor(input).map(SpecifiedFilter::Contrast),
"grayscale" => parse_factor(input).map(SpecifiedFilter::Grayscale),

View file

@ -659,8 +659,8 @@ ${helpers.single_keyword("font-variant-caps",
}
}
/// <length> | <percentage> | <absolute-size> | <relative-size>
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if let Ok(lop) = input.try(specified::LengthOrPercentage::parse_non_negative) {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
if let Ok(lop) = input.try(|i| specified::LengthOrPercentage::parse_non_negative(context, i)) {
return Ok(SpecifiedValue::Length(lop))
}
@ -766,14 +766,14 @@ ${helpers.single_keyword("font-variant-caps",
}
/// none | <number>
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
use values::specified::Number;
if input.try(|input| input.expect_ident_matching("none")).is_ok() {
return Ok(SpecifiedValue::None);
}
Ok(SpecifiedValue::Number(try!(Number::parse_non_negative(input))))
Ok(SpecifiedValue::Number(try!(Number::parse_non_negative(context, input))))
}
</%helpers:longhand>

View file

@ -71,7 +71,6 @@ ${helpers.predefined_type(
"parse_numbers_are_pixels_non_negative",
products="gecko",
animation_type="normal",
needs_context=False,
spec="https://www.w3.org/TR/SVG2/painting.html#StrokeWidth")}
${helpers.single_keyword("stroke-linecap", "butt round square",
@ -84,7 +83,6 @@ ${helpers.single_keyword("stroke-linejoin", "miter round bevel",
${helpers.predefined_type("stroke-miterlimit", "Number", "4.0",
"parse_at_least_one", products="gecko",
needs_context=False,
animation_type="none",
spec="https://www.w3.org/TR/SVG11/painting.html#StrokeMiterlimitProperty")}
@ -108,7 +106,6 @@ ${helpers.predefined_type(
"parse_numbers_are_pixels",
products="gecko",
animation_type="normal",
needs_context=False,
spec="https://www.w3.org/TR/SVG2/painting.html#StrokeDashing")}
// Section 14 - Clipping, Masking and Compositing

View file

@ -114,14 +114,14 @@ ${helpers.single_keyword("caption-side", "top bottom",
}
}
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue,()> {
let mut first = None;
let mut second = None;
match specified::Length::parse_non_negative(input) {
match specified::Length::parse_non_negative(context, input) {
Err(()) => (),
Ok(length) => {
first = Some(length);
if let Ok(len) = input.try(|input| specified::Length::parse_non_negative(input)) {
if let Ok(len) = input.try(|input| specified::Length::parse_non_negative(context, input)) {
second = Some(len);
}
}

View file

@ -45,18 +45,18 @@
}
}
/// normal | <number> | <length> | <percentage>
pub fn parse(_: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
use cssparser::Token;
use std::ascii::AsciiExt;
// We try to parse as a Number first because, for 'line-height', 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(number) = input.try(specified::Number::parse_non_negative) {
if let Ok(number) = input.try(|i| specified::Number::parse_non_negative(context, i)) {
return Ok(SpecifiedValue::Number(number))
}
if let Ok(lop) = input.try(specified::LengthOrPercentage::parse_non_negative) {
if let Ok(lop) = input.try(|i| specified::LengthOrPercentage::parse_non_negative(context, i)) {
return Ok(SpecifiedValue::LengthOrPercentage(lop))
}

View file

@ -77,8 +77,8 @@ ${helpers.predefined_type("outline-color", "CSSColor", "computed::CSSColor::Curr
}
}
pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
specified::parse_border_width(input).map(SpecifiedValue)
pub fn parse(context: &ParserContext, input: &mut Parser) -> Result<SpecifiedValue, ()> {
specified::parse_border_width(context, input).map(SpecifiedValue)
}
impl HasViewportPercentage for SpecifiedValue {

View file

@ -16,7 +16,6 @@
"computed::LengthOrPercentage::Length(Au(0))",
"parse_non_negative",
alias=maybe_moz_logical_alias(product, side, "-moz-padding-%s"),
needs_context=False,
animation_type="normal",
logical = side[1],
spec = spec)}

View file

@ -95,14 +95,12 @@ ${helpers.predefined_type("flex-grow", "Number",
"0.0", "parse_non_negative",
spec="https://drafts.csswg.org/css-flexbox/#flex-grow-property",
extra_prefixes="webkit",
needs_context=False,
animation_type="normal")}
${helpers.predefined_type("flex-shrink", "Number",
"1.0", "parse_non_negative",
spec="https://drafts.csswg.org/css-flexbox/#flex-shrink-property",
extra_prefixes="webkit",
needs_context=False,
animation_type="normal")}
// https://drafts.csswg.org/css-align/#align-self-property
@ -142,7 +140,6 @@ ${helpers.predefined_type("flex-basis",
"computed::LengthOrPercentageOrAuto::Auto" if product == "gecko" else
"computed::LengthOrPercentageOrAutoOrContent::Auto",
"parse_non_negative",
needs_context=False,
spec="https://drafts.csswg.org/css-flexbox/#flex-basis-property",
extra_prefixes="webkit",
animation_type="normal" if product == "gecko" else "none")}
@ -158,7 +155,6 @@ ${helpers.predefined_type("flex-basis",
"LengthOrPercentageOrAuto",
"computed::LengthOrPercentageOrAuto::Auto",
"parse_non_negative",
needs_context=False,
spec=spec % size,
animation_type="normal", logical = logical)}
% if product == "gecko":
@ -255,14 +251,12 @@ ${helpers.predefined_type("flex-basis",
"LengthOrPercentage",
"computed::LengthOrPercentage::Length(Au(0))",
"parse_non_negative",
needs_context=False,
spec=spec % ("min-%s" % size),
animation_type="normal", logical = logical)}
${helpers.predefined_type("max-%s" % size,
"LengthOrPercentageOrNone",
"computed::LengthOrPercentageOrNone::None",
"parse_non_negative",
needs_context=False,
spec=spec % ("min-%s" % size),
animation_type="normal", logical = logical)}
% endif

View file

@ -288,7 +288,7 @@ ${helpers.predefined_type(
return Ok(SpecifiedValue::Normal);
}
let size = try!(Number::parse_at_least_one(input));
let size = try!(Number::parse_at_least_one(context, input));
match input.try(|input| Integer::parse(context, input)) {
Ok(number) => {

View file

@ -25,7 +25,6 @@ ${helpers.single_keyword("-moz-box-direction", "normal reverse",
${helpers.predefined_type("-moz-box-flex", "Number", "0.0", "parse_non_negative",
products="gecko", gecko_ffi_name="mBoxFlex",
needs_context=False,
animation_type="none",
alias="-webkit-box-flex",
spec="Nonstandard (https://developer.mozilla.org/en-US/docs/Web/CSS/box-flex)")}
@ -52,7 +51,6 @@ ${helpers.single_keyword("-moz-stack-sizing", "stretch-to-fit ignore",
${helpers.predefined_type("-moz-box-ordinal-group", "Integer", "0",
parse_method="parse_non_negative",
needs_context=False,
products="gecko",
alias="-webkit-box-ordinal-group",
gecko_ffi_name="mBoxOrdinal",

View file

@ -50,10 +50,10 @@
spec="https://drafts.csswg.org/css-flexbox/#flex-property">
use values::specified::Number;
fn parse_flexibility(input: &mut Parser)
fn parse_flexibility(context: &ParserContext, input: &mut Parser)
-> Result<(Number, Option<Number>),()> {
let grow = try!(Number::parse_non_negative(input));
let shrink = input.try(Number::parse_non_negative).ok();
let grow = try!(Number::parse_non_negative(context, input));
let shrink = input.try(|i| Number::parse_non_negative(context, i)).ok();
Ok((grow, shrink))
}
@ -71,7 +71,7 @@
}
loop {
if grow.is_none() {
if let Ok((flex_grow, flex_shrink)) = input.try(parse_flexibility) {
if let Ok((flex_grow, flex_shrink)) = input.try(|i| parse_flexibility(context, i)) {
grow = Some(flex_grow);
shrink = flex_shrink;
continue