style: Make some grid/grid-template values serialize to a shorter form.

Differential Revision: https://phabricator.services.mozilla.com/D53913
This commit is contained in:
Mats Palmgren 2019-11-23 19:58:35 +00:00 committed by Emilio Cobos Álvarez
parent 014c41f54a
commit 70ec6ffe36
No known key found for this signature in database
GPG key ID: E1152D0994E4BF8A
3 changed files with 62 additions and 20 deletions

View file

@ -261,6 +261,19 @@ pub enum GenericTrackSize<L> {
pub use self::GenericTrackSize as TrackSize;
impl<L> TrackSize<L> {
/// The initial value.
const INITIAL_VALUE: Self = TrackSize::Breadth(TrackBreadth::Auto);
/// Returns the initial value.
pub const fn initial_value() -> Self {
Self::INITIAL_VALUE
}
/// Returns true if `self` is the initial value.
pub fn is_initial(&self) -> bool {
matches!(*self, TrackSize::Breadth(TrackBreadth::Auto)) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
}
/// Check whether this is a `<fixed-size>`
///
/// <https://drafts.csswg.org/css-grid/#typedef-fixed-size>
@ -286,17 +299,9 @@ impl<L> TrackSize<L> {
}
}
impl<L: PartialEq> TrackSize<L> {
/// Return true if it is `auto`.
#[inline]
pub fn is_auto(&self) -> bool {
*self == TrackSize::Breadth(TrackBreadth::Auto)
}
}
impl<L> Default for TrackSize<L> {
fn default() -> Self {
TrackSize::Breadth(TrackBreadth::Auto)
Self::initial_value()
}
}
@ -513,9 +518,24 @@ pub enum GenericTrackListValue<LengthPercentage, Integer> {
pub use self::GenericTrackListValue as TrackListValue;
impl<L, I> TrackListValue<L, I> {
// FIXME: can't use TrackSize::initial_value() here b/c rustc error "is not yet stable as a const fn"
const INITIAL_VALUE: Self = TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto));
fn is_repeat(&self) -> bool {
matches!(*self, TrackListValue::TrackRepeat(..))
}
/// Returns true if `self` is the initial value.
pub fn is_initial(&self) -> bool {
matches!(*self, TrackListValue::TrackSize(TrackSize::Breadth(TrackBreadth::Auto))) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
}
}
impl<L, I> Default for TrackListValue<L, I> {
#[inline]
fn default() -> Self {
Self::INITIAL_VALUE
}
}
/// A grid `<track-list>` type.
@ -755,6 +775,9 @@ pub enum GenericGridTemplateComponent<L, I> {
pub use self::GenericGridTemplateComponent as GridTemplateComponent;
impl<L, I> GridTemplateComponent<L, I> {
/// The initial value.
const INITIAL_VALUE: Self = Self::None;
/// Returns length of the <track-list>s <track-size>
pub fn track_list_len(&self) -> usize {
match *self {
@ -762,4 +785,16 @@ impl<L, I> GridTemplateComponent<L, I> {
_ => 0,
}
}
/// Returns true if `self` is the initial value.
pub fn is_initial(&self) -> bool {
matches!(*self, Self::None) // FIXME: can't use Self::INITIAL_VALUE here yet: https://github.com/rust-lang/rust/issues/66585
}
}
impl<L, I> Default for GridTemplateComponent<L, I> {
#[inline]
fn default() -> Self {
Self::INITIAL_VALUE
}
}

View file

@ -102,8 +102,8 @@ impl Parse for ImplicitGridTracks<TrackSize<LengthPercentage>> {
) -> Result<Self, ParseError<'i>> {
use style_traits::{Separator, Space};
let track_sizes = Space::parse(input, |i| TrackSize::parse(context, i))?;
if track_sizes.len() == 1 && track_sizes[0].is_auto() {
//`auto`, which is the initial value, is always represented by an empty slice.
if track_sizes.len() == 1 && track_sizes[0].is_initial() {
// A single track with the initial value is always represented by an empty slice.
return Ok(Default::default());
}
return Ok(ImplicitGridTracks(track_sizes.into()));