From c9c5e560791591c02bd1dda0648898229830ad45 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Fri, 17 Aug 2018 12:46:02 +0000 Subject: [PATCH] style: Use AspectRatio directly for RangeOrOperator::evaluate. Differential Revision: https://phabricator.services.mozilla.com/D3587 --- components/style/gecko/media_features.rs | 7 ++-- .../media_queries/media_feature_expression.rs | 34 +++++++++++++++---- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index 0ff420a1612..58afc901f48 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -118,11 +118,8 @@ where }; let size = get_size(device); - RangeOrOperator::evaluate( - range_or_operator, - Some(size.height.0 as u64 * query_value.0 as u64), - size.width.0 as u64 * query_value.1 as u64, - ) + let value = AspectRatio(size.width.0 as u32, size.height.0 as u32); + RangeOrOperator::evaluate_with_query_value(range_or_operator, query_value, value) } /// https://drafts.csswg.org/mediaqueries-4/#aspect-ratio diff --git a/components/style/media_queries/media_feature_expression.rs b/components/style/media_queries/media_feature_expression.rs index 60fb2b2a087..81bbb69c1a1 100644 --- a/components/style/media_queries/media_feature_expression.rs +++ b/components/style/media_queries/media_feature_expression.rs @@ -14,6 +14,7 @@ use cssparser::{Parser, Token}; use context::QuirksMode; use num_traits::Zero; use parser::{Parse, ParserContext}; +use std::cmp::{PartialOrd, Ordering}; use std::fmt::{self, Write}; use str::{starts_with_ignore_ascii_case, string_as_ascii_lowercase}; use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; @@ -45,6 +46,15 @@ impl ToCss for AspectRatio { } } +impl PartialOrd for AspectRatio { + fn partial_cmp(&self, other: &AspectRatio) -> Option { + u64::partial_cmp( + &(self.0 as u64 * other.1 as u64), + &(self.1 as u64 * other.0 as u64), + ) + } +} + /// The kind of matching that should be performed on a media feature value. #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)] pub enum Range { @@ -97,7 +107,8 @@ pub enum RangeOrOperator { } impl RangeOrOperator { - /// Evaluate a given range given a query value and a value from the browser. + /// Evaluate a given range given an optional query value and a value from + /// the browser. pub fn evaluate( range_or_op: Option, query_value: Option, @@ -106,13 +117,22 @@ impl RangeOrOperator { where T: PartialOrd + Zero { - use std::cmp::Ordering; - - let query_value = match query_value { - Some(v) => v, - None => return value != Zero::zero(), - }; + match query_value { + Some(v) => Self::evaluate_with_query_value(range_or_op, v, value), + None => !value.is_zero(), + } + } + /// Evaluate a given range given a non-optional query value and a value from + /// the browser. + pub fn evaluate_with_query_value( + range_or_op: Option, + query_value: T, + value: T, + ) -> bool + where + T: PartialOrd, + { let cmp = match value.partial_cmp(&query_value) { Some(c) => c, None => return false,