Skip to content
Permalink
Browse files

style: Use AspectRatio directly for RangeOrOperator::evaluate.

  • Loading branch information
upsuper authored and emilio committed Aug 18, 2018
1 parent 07ffc09 commit c9c5e560791591c02bd1dda0648898229830ad45
@@ -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
@@ -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<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 {
@@ -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>,
@@ -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,

0 comments on commit c9c5e56

Please sign in to comment.
You can’t perform that action at this time.