style: Don't allow negative radii in radial gradients.

Mostly renaming for clarity, as the gradient parsing code is a bit hairy.

This also changes -webkit- gradients, which is, I think, the right thing to do
(otherwise I need to give up on the type system and sprinkle parse_non_negatives
around, which would be unfortunate).

I filed https://bugs.chromium.org/p/chromium/issues/detail?id=1008112 on
Chromium still accepting negative radii for those, so will wait to submit the
patch for review until they reply there with their intentions.

Differential Revision: https://phabricator.services.mozilla.com/D47141
This commit is contained in:
Emilio Cobos Álvarez 2019-10-01 21:44:41 +00:00
parent 19ddfd57d5
commit d43632c9f8
5 changed files with 61 additions and 46 deletions

View file

@ -73,9 +73,16 @@ pub use self::GenericImage as Image;
/// <https://drafts.csswg.org/css-images/#gradients>
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C)]
pub struct GenericGradient<LineDirection, Length, LengthPercentage, Position, Color> {
pub struct GenericGradient<
LineDirection,
LengthPercentage,
NonNegativeLength,
NonNegativeLengthPercentage,
Position,
Color,
> {
/// Gradients can be linear or radial.
pub kind: GenericGradientKind<LineDirection, Length, LengthPercentage, Position>,
pub kind: GenericGradientKind<LineDirection, NonNegativeLength, NonNegativeLengthPercentage, Position>,
/// The color stops and interpolation hints.
pub items: crate::OwnedSlice<GenericGradientItem<Color, LengthPercentage>>,
/// True if this is a repeating gradient.
@ -101,11 +108,11 @@ pub enum GradientCompatMode {
/// A gradient kind.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C, u8)]
pub enum GenericGradientKind<LineDirection, Length, LengthPercentage, Position> {
pub enum GenericGradientKind<LineDirection, NonNegativeLength, NonNegativeLengthPercentage, Position> {
/// A linear gradient.
Linear(LineDirection),
/// A radial gradient.
Radial(GenericEndingShape<Length, LengthPercentage>, Position),
Radial(GenericEndingShape<NonNegativeLength, NonNegativeLengthPercentage>, Position),
}
pub use self::GenericGradientKind as GradientKind;
@ -115,11 +122,11 @@ pub use self::GenericGradientKind as GradientKind;
Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss, ToResolvedValue, ToShmem,
)]
#[repr(C, u8)]
pub enum GenericEndingShape<Length, LengthPercentage> {
pub enum GenericEndingShape<NonNegativeLength, NonNegativeLengthPercentage> {
/// A circular gradient.
Circle(GenericCircle<Length>),
Circle(GenericCircle<NonNegativeLength>),
/// An elliptic gradient.
Ellipse(GenericEllipse<LengthPercentage>),
Ellipse(GenericEllipse<NonNegativeLengthPercentage>),
}
pub use self::GenericEndingShape as EndingShape;
@ -127,9 +134,9 @@ pub use self::GenericEndingShape as EndingShape;
/// A circle shape.
#[derive(Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
#[repr(C, u8)]
pub enum GenericCircle<Length> {
pub enum GenericCircle<NonNegativeLength> {
/// A circle radius.
Radius(Length),
Radius(NonNegativeLength),
/// A circle extent.
Extent(ShapeExtent),
}
@ -141,9 +148,9 @@ pub use self::GenericCircle as Circle;
Clone, Copy, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToCss, ToResolvedValue, ToShmem,
)]
#[repr(C, u8)]
pub enum GenericEllipse<LengthPercentage> {
pub enum GenericEllipse<NonNegativeLengthPercentage> {
/// An ellipse pair of radii.
Radii(LengthPercentage, LengthPercentage),
Radii(NonNegativeLengthPercentage, NonNegativeLengthPercentage),
/// An ellipse extent.
Extent(ShapeExtent),
}
@ -314,11 +321,12 @@ where
}
}
impl<D, L, LoP, P, C> ToCss for Gradient<D, L, LoP, P, C>
impl<D, LP, NL, NLP, P, C> ToCss for Gradient<D, LP, NL, NLP, P, C>
where
D: LineDirection,
L: ToCss,
LoP: ToCss,
LP: ToCss,
NL: ToCss,
NLP: ToCss,
P: ToCss,
C: ToCss,
{