style: Use NonNegative more in the border code.

This ended up not being so small of a patch as I'd have thought, since it
propagated a bit. But most of it is mechanical. Interesting part is
NonNegativeNumberOrPercentage and the actual uses of the NonNegative stuff and
during parsing.

This looks like it'd fix a few correctness issues during interpolation for all
the types except for BorderRadius and co (which handled it manually).

I should write tests for those in a different patch.

Differential Revision: https://phabricator.services.mozilla.com/D14673
This commit is contained in:
Emilio Cobos Álvarez 2018-12-17 21:35:14 +00:00
parent 19035590ce
commit ca1ad003bd
14 changed files with 161 additions and 106 deletions

View file

@ -16,7 +16,7 @@ use crate::values::specified::border::BorderRadius;
use crate::values::specified::image::Image;
use crate::values::specified::position::{HorizontalPosition, Position, VerticalPosition};
use crate::values::specified::url::SpecifiedUrl;
use crate::values::specified::LengthOrPercentage;
use crate::values::specified::{LengthOrPercentage, NonNegativeLengthOrPercentage};
use crate::values::specified::SVGPathData;
use cssparser::Parser;
use std::fmt::{self, Write};
@ -32,10 +32,10 @@ pub type ClippingShape = generic::ClippingShape<BasicShape, SpecifiedUrl>;
pub type FloatAreaShape = generic::FloatAreaShape<BasicShape, Image>;
/// A specified basic shape.
pub type BasicShape = generic::BasicShape<HorizontalPosition, VerticalPosition, LengthOrPercentage>;
pub type BasicShape = generic::BasicShape<HorizontalPosition, VerticalPosition, LengthOrPercentage, NonNegativeLengthOrPercentage>;
/// The specified value of `inset()`
pub type InsetRect = generic::InsetRect<LengthOrPercentage>;
pub type InsetRect = generic::InsetRect<LengthOrPercentage, NonNegativeLengthOrPercentage>;
/// A specified circle.
pub type Circle = generic::Circle<HorizontalPosition, VerticalPosition, LengthOrPercentage>;
@ -199,10 +199,7 @@ impl InsetRect {
} else {
None
};
Ok(generic::InsetRect {
rect: rect,
round: round,
})
Ok(generic::InsetRect { rect, round })
}
}

View file

@ -13,8 +13,8 @@ use crate::values::generics::border::BorderRadius as GenericBorderRadius;
use crate::values::generics::border::BorderSpacing as GenericBorderSpacing;
use crate::values::generics::rect::Rect;
use crate::values::generics::size::Size;
use crate::values::specified::length::{Length, LengthOrPercentage, NonNegativeLength};
use crate::values::specified::{AllowQuirks, Number, NumberOrPercentage};
use crate::values::specified::length::{NonNegativeLengthOrPercentage, NonNegativeLength};
use crate::values::specified::{AllowQuirks, NonNegativeNumber, NonNegativeNumberOrPercentage};
use cssparser::Parser;
use std::fmt::{self, Write};
use style_traits::{CssWriter, ParseError, ToCss};
@ -71,28 +71,45 @@ pub enum BorderSideWidth {
/// `thick`
Thick,
/// `<length>`
Length(Length),
Length(NonNegativeLength),
}
/// A specified value for the `border-image-width` property.
pub type BorderImageWidth = Rect<BorderImageSideWidth>;
/// A specified value for a single side of a `border-image-width` property.
pub type BorderImageSideWidth = GenericBorderImageSideWidth<LengthOrPercentage, Number>;
pub type BorderImageSideWidth = GenericBorderImageSideWidth<NonNegativeLengthOrPercentage, NonNegativeNumber>;
/// A specified value for the `border-image-slice` property.
pub type BorderImageSlice = GenericBorderImageSlice<NumberOrPercentage>;
pub type BorderImageSlice = GenericBorderImageSlice<NonNegativeNumberOrPercentage>;
/// A specified value for the `border-radius` property.
pub type BorderRadius = GenericBorderRadius<LengthOrPercentage>;
pub type BorderRadius = GenericBorderRadius<NonNegativeLengthOrPercentage>;
/// A specified value for the `border-*-radius` longhand properties.
pub type BorderCornerRadius = GenericBorderCornerRadius<LengthOrPercentage>;
pub type BorderCornerRadius = GenericBorderCornerRadius<NonNegativeLengthOrPercentage>;
/// A specified value for the `border-spacing` longhand properties.
pub type BorderSpacing = GenericBorderSpacing<NonNegativeLength>;
impl BorderImageSlice {
/// Returns the `100%` value.
#[inline]
pub fn hundred_percent() -> Self {
GenericBorderImageSlice {
offsets: Rect::all(NonNegativeNumberOrPercentage::hundred_percent()),
fill: false,
}
}
}
impl BorderSideWidth {
/// Returns the `0px` value.
#[inline]
pub fn zero() -> Self {
BorderSideWidth::Length(NonNegativeLength::zero())
}
/// Parses, with quirks.
pub fn parse_quirky<'i, 't>(
context: &ParserContext,
@ -100,7 +117,7 @@ impl BorderSideWidth {
allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
if let Ok(length) =
input.try(|i| Length::parse_non_negative_quirky(context, i, allow_quirks))
input.try(|i| NonNegativeLength::parse_quirky(context, i, allow_quirks))
{
return Ok(BorderSideWidth::Length(length));
}
@ -130,9 +147,9 @@ impl ToComputedValue for BorderSideWidth {
// Spec: https://drafts.csswg.org/css-backgrounds-3/#line-width
// Gecko: https://bugzilla.mozilla.org/show_bug.cgi?id=1312155#c0
match *self {
BorderSideWidth::Thin => Length::from_px(1.).to_computed_value(context),
BorderSideWidth::Medium => Length::from_px(3.).to_computed_value(context),
BorderSideWidth::Thick => Length::from_px(5.).to_computed_value(context),
BorderSideWidth::Thin => NonNegativeLength::from_px(1.).to_computed_value(context),
BorderSideWidth::Medium => NonNegativeLength::from_px(3.).to_computed_value(context),
BorderSideWidth::Thick => NonNegativeLength::from_px(5.).to_computed_value(context),
BorderSideWidth::Length(ref length) => length.to_computed_value(context),
}
.into()
@ -140,7 +157,7 @@ impl ToComputedValue for BorderSideWidth {
#[inline]
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
BorderSideWidth::Length(ToComputedValue::from_computed_value(&computed.0))
BorderSideWidth::Length(ToComputedValue::from_computed_value(computed))
}
}
@ -148,7 +165,7 @@ impl BorderImageSideWidth {
/// Returns `1`.
#[inline]
pub fn one() -> Self {
GenericBorderImageSideWidth::Number(Number::new(1.))
GenericBorderImageSideWidth::Number(NonNegativeNumber::new(1.))
}
}
@ -161,11 +178,11 @@ impl Parse for BorderImageSideWidth {
return Ok(GenericBorderImageSideWidth::Auto);
}
if let Ok(len) = input.try(|i| LengthOrPercentage::parse_non_negative(context, i)) {
if let Ok(len) = input.try(|i| NonNegativeLengthOrPercentage::parse(context, i)) {
return Ok(GenericBorderImageSideWidth::Length(len));
}
let num = Number::parse_non_negative(context, input)?;
let num = NonNegativeNumber::parse(context, input)?;
Ok(GenericBorderImageSideWidth::Number(num))
}
}
@ -176,14 +193,11 @@ impl Parse for BorderImageSlice {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let mut fill = input.try(|i| i.expect_ident_matching("fill")).is_ok();
let offsets = Rect::parse_with(context, input, NumberOrPercentage::parse_non_negative)?;
let offsets = Rect::parse_with(context, input, NonNegativeNumberOrPercentage::parse)?;
if !fill {
fill = input.try(|i| i.expect_ident_matching("fill")).is_ok();
}
Ok(GenericBorderImageSlice {
offsets: offsets,
fill: fill,
})
Ok(GenericBorderImageSlice { offsets, fill })
}
}
@ -192,9 +206,9 @@ impl Parse for BorderRadius {
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
let widths = Rect::parse_with(context, input, LengthOrPercentage::parse_non_negative)?;
let widths = Rect::parse_with(context, input, NonNegativeLengthOrPercentage::parse)?;
let heights = if input.try(|i| i.expect_delim('/')).is_ok() {
Rect::parse_with(context, input, LengthOrPercentage::parse_non_negative)?
Rect::parse_with(context, input, NonNegativeLengthOrPercentage::parse)?
} else {
widths.clone()
};
@ -213,7 +227,7 @@ impl Parse for BorderCornerRadius {
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Size::parse_with(context, input, LengthOrPercentage::parse_non_negative)
Size::parse_with(context, input, NonNegativeLengthOrPercentage::parse)
.map(GenericBorderCornerRadius)
}
}
@ -224,7 +238,7 @@ impl Parse for BorderSpacing {
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Size::parse_with(context, input, |context, input| {
Length::parse_non_negative_quirky(context, input, AllowQuirks::Yes).map(From::from)
NonNegativeLength::parse_quirky(context, input, AllowQuirks::Yes).map(From::from)
})
.map(GenericBorderSpacing)
}

View file

@ -717,6 +717,16 @@ impl NonNegativeLength {
pub fn from_px(px_value: CSSFloat) -> Self {
Length::from_px(px_value.max(0.)).into()
}
/// Parses a non-negative length, optionally with quirks.
#[inline]
pub fn parse_quirky<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks,
) -> Result<Self, ParseError<'i>> {
Ok(NonNegative(Length::parse_non_negative_quirky(context, input, allow_quirks)?))
}
}
/// Either a NonNegativeLength or the `auto` keyword.

View file

@ -359,6 +359,26 @@ impl Parse for NumberOrPercentage {
}
}
/// A non-negative <number> | <percentage>.
pub type NonNegativeNumberOrPercentage = NonNegative<NumberOrPercentage>;
impl NonNegativeNumberOrPercentage {
/// Returns the `100%` value.
#[inline]
pub fn hundred_percent() -> Self {
NonNegative(NumberOrPercentage::Percentage(Percentage::hundred()))
}
}
impl Parse for NonNegativeNumberOrPercentage {
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Ok(NonNegative(NumberOrPercentage::parse_non_negative(context, input)?))
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, SpecifiedValueInfo, ToCss)]
pub struct Opacity(Number);