style: Properly use integers on grid properties, and derive ToComputedValue.

This commit is contained in:
Emilio Cobos Álvarez 2017-09-09 14:33:53 +02:00
parent 02d24061e7
commit 64ab73eabd
No known key found for this signature in database
GPG key ID: 056B727BB9C1027C
7 changed files with 112 additions and 91 deletions

View file

@ -14,7 +14,7 @@ use values::{CSSFloat, CustomIdent};
use values::computed::{self, Context, ToComputedValue};
use values::generics::grid::{GridTemplateComponent, RepeatCount, TrackBreadth, TrackKeyword, TrackRepeat};
use values::generics::grid::{LineNameList, TrackSize, TrackList, TrackListType, TrackListValue};
use values::specified::LengthOrPercentage;
use values::specified::{LengthOrPercentage, Integer};
/// Parse a single flexible length.
pub fn parse_flex<'i, 't>(input: &mut Parser<'i, 't>) -> Result<CSSFloat, ParseError<'i>> {
@ -97,9 +97,11 @@ enum RepeatType {
Fixed,
}
impl TrackRepeat<LengthOrPercentage> {
fn parse_with_repeat_type<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<(TrackRepeat<LengthOrPercentage>, RepeatType), ParseError<'i>> {
impl TrackRepeat<LengthOrPercentage, Integer> {
fn parse_with_repeat_type<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<(Self, RepeatType), ParseError<'i>> {
input.try(|i| i.expect_function_matching("repeat").map_err(|e| e.into())).and_then(|_| {
input.parse_nested_block(|input| {
let count = RepeatCount::parse(context, input)?;
@ -165,7 +167,7 @@ impl TrackRepeat<LengthOrPercentage> {
}
}
impl Parse for TrackList<LengthOrPercentage> {
impl Parse for TrackList<LengthOrPercentage, Integer> {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
let mut current_names = vec![];
let mut names = vec![];
@ -248,8 +250,8 @@ impl Parse for TrackList<LengthOrPercentage> {
}
}
impl ToComputedValue for TrackList<LengthOrPercentage> {
type ComputedValue = TrackList<computed::LengthOrPercentage>;
impl ToComputedValue for TrackList<LengthOrPercentage, Integer> {
type ComputedValue = TrackList<computed::LengthOrPercentage, computed::Integer>;
#[inline]
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
@ -321,7 +323,7 @@ impl ToComputedValue for TrackList<LengthOrPercentage> {
}
}
impl Parse for GridTemplateComponent<LengthOrPercentage> {
impl Parse for GridTemplateComponent<LengthOrPercentage, Integer> {
// FIXME: Derive Parse (probably with None_)
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("none")).is_ok() {
@ -332,10 +334,12 @@ impl Parse for GridTemplateComponent<LengthOrPercentage> {
}
}
impl GridTemplateComponent<LengthOrPercentage> {
impl GridTemplateComponent<LengthOrPercentage, Integer> {
/// Parses a `GridTemplateComponent<LengthOrPercentage>` except `none` keyword.
pub fn parse_without_none<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
pub fn parse_without_none<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
if let Ok(t) = input.try(|i| TrackList::parse(context, i)) {
return Ok(GridTemplateComponent::TrackList(t))
}

View file

@ -19,8 +19,8 @@ use style_traits::values::specified::AllowedNumericType;
use super::{Auto, CSSFloat, CSSInteger, Either, None_};
use super::computed::{Context, ToComputedValue};
use super::generics::{GreaterThanOrEqualToOne, NonNegative};
use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize};
use super::generics::grid::TrackList as GenericTrackList;
use super::generics::grid::{GridLine as GenericGridLine, TrackBreadth as GenericTrackBreadth};
use super::generics::grid::{TrackSize as GenericTrackSize, TrackList as GenericTrackList};
use values::computed::ComputedValueAsSpecified;
use values::specified::calc::CalcNode;
@ -52,7 +52,6 @@ pub use self::svg::{SVGLength, SVGOpacity, SVGPaint, SVGPaintKind, SVGStrokeDash
pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing};
pub use self::time::Time;
pub use self::transform::{TimingFunction, TransformOrigin};
pub use super::generics::grid::GridLine;
pub use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent;
#[cfg(feature = "gecko")]
@ -356,9 +355,11 @@ impl ToComputedValue for Opacity {
}
}
/// An specified `<integer>`, optionally coming from a `calc()` expression.
///
/// https://drafts.csswg.org/css-values/#integers
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[allow(missing_docs)]
pub struct Integer {
value: CSSInteger,
was_calc: bool,
@ -387,7 +388,6 @@ impl Integer {
}
}
impl Parse for Integer {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
parse_integer(context, input)
@ -395,25 +395,37 @@ impl Parse for Integer {
}
impl Integer {
#[allow(missing_docs)]
pub fn parse_with_minimum<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>, min: i32)
-> Result<Integer, ParseError<'i>> {
/// Parse an integer value which is at least `min`.
pub fn parse_with_minimum<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
min: i32
) -> Result<Integer, ParseError<'i>> {
match parse_integer(context, input) {
// FIXME(emilio): The spec asks us to avoid rejecting it at parse
// time except until computed value time.
//
// It's not totally clear it's worth it though, and no other browser
// does this.
Ok(value) if value.value() >= min => Ok(value),
Ok(_value) => Err(StyleParseError::UnspecifiedError.into()),
Err(e) => Err(e),
}
}
#[allow(missing_docs)]
pub fn parse_non_negative<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Integer, ParseError<'i>> {
/// Parse a non-negative integer.
pub fn parse_non_negative<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Integer, ParseError<'i>> {
Integer::parse_with_minimum(context, input, 0)
}
#[allow(missing_docs)]
pub fn parse_positive<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>)
-> Result<Integer, ParseError<'i>> {
/// Parse a positive integer (>= 1).
pub fn parse_positive<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>
) -> Result<Integer, ParseError<'i>> {
Integer::parse_with_minimum(context, input, 1)
}
}
@ -484,10 +496,13 @@ pub type TrackSize = GenericTrackSize<LengthOrPercentage>;
/// The specified value of a grid `<track-list>`
/// (could also be `<auto-track-list>` or `<explicit-track-list>`)
pub type TrackList = GenericTrackList<LengthOrPercentage>;
pub type TrackList = GenericTrackList<LengthOrPercentage, Integer>;
/// The specified value of a `<grid-line>`.
pub type GridLine = GenericGridLine<Integer>;
/// `<grid-template-rows> | <grid-template-columns>`
pub type GridTemplateComponent = GenericGridTemplateComponent<LengthOrPercentage>;
pub type GridTemplateComponent = GenericGridTemplateComponent<LengthOrPercentage, Integer>;
/// <length> | <percentage> | <number>
pub type LengthOrPercentageOrNumber = Either<Number, LengthOrPercentage>;