From 61685ca9b31caa0c6b4aeee0f5f6ec96804b49f7 Mon Sep 17 00:00:00 2001 From: Boris Chiou Date: Mon, 12 Oct 2020 15:05:46 +0000 Subject: [PATCH] style: Serialize a 0/0 ratio as 0/0 in all value stages. Based on the update of github.com/w3c/csswg-drafts/issues/5084, a 0/0 ratio will serialize as 0/0 in all value stages. Differential Revision: https://phabricator.services.mozilla.com/D93182 --- components/style/gecko/media_features.rs | 6 +++++- components/style/values/computed/position.rs | 12 +++++++++++- components/style/values/specified/position.rs | 9 +-------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index 4fea1030dbc..4fd6e622ac6 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -87,8 +87,12 @@ fn eval_aspect_ratio_for( where F: FnOnce(&Device) -> Size2D, { + // A ratio of 0/0 behaves as the ratio 1/0, so we need to call used_value() + // to convert it if necessary. + // FIXME: we may need to update here once + // https://github.com/w3c/csswg-drafts/issues/4954 got resolved. let query_value = match query_value { - Some(v) => v, + Some(v) => v.used_value(), None => return true, }; diff --git a/components/style/values/computed/position.rs b/components/style/values/computed/position.rs index 9e5fe3be1d2..f5e586fdfa4 100644 --- a/components/style/values/computed/position.rs +++ b/components/style/values/computed/position.rs @@ -15,7 +15,7 @@ use crate::values::generics::position::PositionOrAuto as GenericPositionOrAuto; use crate::values::generics::position::Ratio as GenericRatio; use crate::values::generics::position::ZIndex as GenericZIndex; pub use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow}; -use crate::Zero; +use crate::{One, Zero}; use std::cmp::{Ordering, PartialOrd}; use std::fmt::{self, Write}; use style_traits::{CssWriter, ToCss}; @@ -89,6 +89,16 @@ impl Ratio { pub fn new(a: f32, b: f32) -> Self { GenericRatio(a.into(), b.into()) } + + /// Returns the used value. A ratio of 0/0 behaves as the ratio 1/0. + /// https://drafts.csswg.org/css-values-4/#ratios + pub fn used_value(self) -> Self { + if self.0.is_zero() && self.1.is_zero() { + Ratio::new(One::one(), Zero::zero()) + } else { + self + } + } } /// A computed value for the `aspect-ratio` property. diff --git a/components/style/values/specified/position.rs b/components/style/values/specified/position.rs index 1b55e010878..9810959f3a6 100644 --- a/components/style/values/specified/position.rs +++ b/components/style/values/specified/position.rs @@ -951,13 +951,6 @@ impl Parse for Ratio { _ => One::one(), }; - // The computed value of a is the pair of numbers provided, unless - // both numbers are zero, in which case the computed value is the pair (1, 0) - // (same as 1 / 0). - // https://drafts.csswg.org/css-values-4/#ratios - if a.is_zero() && b.is_zero() { - return Ok(GenericRatio(One::one(), Zero::zero())); - } - return Ok(GenericRatio(a, b)); + Ok(GenericRatio(a, b)) } }