style: Implement parsing and serialization for most of image-set().

This implements the basic image-set notation without the format()
function (for simplicity).

There's a remaining serialization issue (we should probably skip 1x
resolutions), but that's fine for now, I'll address this in a follow-up
when the feature is testable.

The intention is to do the image selection at computed value time
(keeping a selected index or such), but same, follow-up.

This also fixes an issue where the cors-mode for -moz-image-rect and
cross-fade() was getting ignored when parsing.

Differential Revision: https://phabricator.services.mozilla.com/D100640
This commit is contained in:
Emilio Cobos Álvarez 2021-01-07 04:15:29 +00:00
parent f27003c810
commit 4f28a8cd31
6 changed files with 139 additions and 30 deletions

View file

@ -22,7 +22,7 @@ use style_traits::{CssWriter, ToCss};
Clone, MallocSizeOf, PartialEq, SpecifiedValueInfo, ToComputedValue, ToResolvedValue, ToShmem,
)]
#[repr(C, u8)]
pub enum GenericImage<G, MozImageRect, ImageUrl, Color, Percentage> {
pub enum GenericImage<G, MozImageRect, ImageUrl, Color, Percentage, Resolution> {
/// `none` variant.
None,
/// A `<url()>` image.
@ -51,6 +51,9 @@ pub enum GenericImage<G, MozImageRect, ImageUrl, Color, Percentage> {
/// and store images directly inside of cross-fade instead of
/// boxing them there.
CrossFade(Box<GenericCrossFade<Self, Color, Percentage>>),
/// An `image-set()` function.
ImageSet(Box<GenericImageSet<Self, Resolution>>),
}
pub use self::GenericImage as Image;
@ -135,6 +138,36 @@ pub use self::GenericCrossFade as CrossFade;
pub use self::GenericCrossFadeElement as CrossFadeElement;
pub use self::GenericCrossFadeImage as CrossFadeImage;
/// https://drafts.csswg.org/css-images-4/#image-set-notation
#[css(comma, function = "image-set")]
#[derive(
Clone, Debug, MallocSizeOf, PartialEq, ToResolvedValue, ToShmem, ToCss, ToComputedValue,
)]
#[repr(C)]
pub struct GenericImageSet<Image, Resolution> {
/// All of the image and resolution pairs.
#[css(iterable)]
pub items: crate::OwnedSlice<GenericImageSetItem<Image, Resolution>>,
}
/// An optional percent and a cross fade image.
#[derive(
Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem, ToCss,
)]
#[repr(C)]
pub struct GenericImageSetItem<Image, Resolution> {
/// `<image>`. `<string>` is converted to `Image::Url` at parse time.
pub image: Image,
/// The `<resolution>`.
///
/// TODO: Skip serialization if it is 1x.
pub resolution: Resolution,
// TODO: type() function.
}
pub use self::GenericImageSet as ImageSet;
pub use self::GenericImageSetItem as ImageSetItem;
/// A CSS gradient.
/// <https://drafts.csswg.org/css-images/#gradients>
#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToComputedValue, ToResolvedValue, ToShmem)]
@ -377,26 +410,23 @@ pub struct GenericMozImageRect<NumberOrPercentage, MozImageRectUrl> {
pub use self::GenericMozImageRect as MozImageRect;
impl<G, R, U, C, P> fmt::Debug for Image<G, R, U, C, P>
impl<G, R, U, C, P, Resolution> fmt::Debug for Image<G, R, U, C, P, Resolution>
where
G: ToCss,
R: ToCss,
U: ToCss,
C: ToCss,
P: ToCss,
Image<G, R, U, C, P, Resolution>: ToCss,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_css(&mut CssWriter::new(f))
}
}
impl<G, R, U, C, P> ToCss for Image<G, R, U, C, P>
impl<G, R, U, C, P, Resolution> ToCss for Image<G, R, U, C, P, Resolution>
where
G: ToCss,
R: ToCss,
U: ToCss,
C: ToCss,
P: ToCss,
Resolution: ToCss,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
@ -415,6 +445,7 @@ where
serialize_atom_identifier(selector, dest)?;
dest.write_str(")")
},
Image::ImageSet(ref is) => is.to_css(dest),
Image::CrossFade(ref cf) => cf.to_css(dest),
}
}