From 1418ddc6850b000c0e3fa293103c237e5baafb66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 3 Mar 2019 11:31:40 +0000 Subject: [PATCH] style: Use contextual_skip_if for background-size. Also drive-by cleanup. Differential Revision: https://phabricator.services.mozilla.com/D21861 --- .../style/values/generics/background.rs | 40 ++++++------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/components/style/values/generics/background.rs b/components/style/values/generics/background.rs index 14ac9744b14..3780f9a4fc3 100644 --- a/components/style/values/generics/background.rs +++ b/components/style/values/generics/background.rs @@ -5,8 +5,13 @@ //! Generic types for CSS values related to backgrounds. use crate::values::generics::length::{GenericLengthPercentageOrAuto, LengthPercentageOrAuto}; -use std::fmt::{self, Write}; -use style_traits::{CssWriter, ToCss}; + +fn width_and_height_are_auto( + width: &LengthPercentageOrAuto, + height: &LengthPercentageOrAuto, +) -> bool { + width.is_auto() && height.is_auto() +} /// A generic value for the `background-size` property. #[derive( @@ -21,6 +26,7 @@ use style_traits::{CssWriter, ToCss}; ToAnimatedValue, ToAnimatedZero, ToComputedValue, + ToCss, )] #[repr(C, u8)] pub enum GenericBackgroundSize { @@ -29,6 +35,10 @@ pub enum GenericBackgroundSize { /// Explicit width. width: GenericLengthPercentageOrAuto, /// Explicit height. + /// NOTE(emilio): We should probably simplify all these in case `width` + /// and `height` are the same, but all other browsers agree on only + /// special-casing `auto`. + #[css(contextual_skip_if = "width_and_height_are_auto")] height: GenericLengthPercentageOrAuto, }, /// `cover` @@ -41,32 +51,6 @@ pub enum GenericBackgroundSize { pub use self::GenericBackgroundSize as BackgroundSize; -impl ToCss for BackgroundSize -where - LengthPercentage: ToCss, -{ - fn to_css(&self, dest: &mut CssWriter) -> fmt::Result - where - W: Write, - { - match self { - BackgroundSize::ExplicitSize { width, height } => { - width.to_css(dest)?; - // NOTE(emilio): We should probably simplify all these in case - // `width == `height`, but all other browsers agree on only - // special-casing `auto`. - if !width.is_auto() || !height.is_auto() { - dest.write_str(" ")?; - height.to_css(dest)?; - } - Ok(()) - }, - BackgroundSize::Cover => dest.write_str("cover"), - BackgroundSize::Contain => dest.write_str("contain"), - } - } -} - impl BackgroundSize { /// Returns `auto auto`. pub fn auto() -> Self {