Skip to content

Commit

Permalink
style: Use AspectRatio directly for RangeOrOperator::evaluate.
Browse files Browse the repository at this point in the history
  • Loading branch information
upsuper authored and emilio committed Aug 18, 2018
1 parent 07ffc09 commit c9c5e56
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
7 changes: 2 additions & 5 deletions components/style/gecko/media_features.rs
Expand Up @@ -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
Expand Down
34 changes: 27 additions & 7 deletions components/style/media_queries/media_feature_expression.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -45,6 +46,15 @@ impl ToCss for AspectRatio {
}
}

impl PartialOrd for AspectRatio {
fn partial_cmp(&self, other: &AspectRatio) -> Option<Ordering> {
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 {
Expand Down Expand Up @@ -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<T>(
range_or_op: Option<Self>,
query_value: Option<T>,
Expand All @@ -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<T>(
range_or_op: Option<Self>,
query_value: T,
value: T,
) -> bool
where
T: PartialOrd,
{
let cmp = match value.partial_cmp(&query_value) {
Some(c) => c,
None => return false,
Expand Down

0 comments on commit c9c5e56

Please sign in to comment.