mirror of
https://github.com/servo/servo.git
synced 2025-08-10 16:05:43 +01:00
style: Rewrite media queries so that they work on an evaluator function.
This moves most of the code to be Rust, except potentially some evaluator functions, and allows to unblock the use case from any-hover / any-pointer and remove nsMediaFeatures. Differential Revision: https://phabricator.services.mozilla.com/D2976
This commit is contained in:
parent
a0cb37d29d
commit
dc0f937224
10 changed files with 1374 additions and 782 deletions
|
@ -6,34 +6,23 @@
|
|||
|
||||
use app_units::AU_PER_PX;
|
||||
use app_units::Au;
|
||||
use context::QuirksMode;
|
||||
use cssparser::{Parser, RGBA, Token};
|
||||
use cssparser::RGBA;
|
||||
use euclid::Size2D;
|
||||
use euclid::TypedScale;
|
||||
use gecko::values::{convert_nscolor_to_rgba, convert_rgba_to_nscolor};
|
||||
use gecko_bindings::bindings;
|
||||
use gecko_bindings::structs;
|
||||
use gecko_bindings::structs::{nsCSSKTableEntry, nsCSSKeyword, nsCSSUnit, nsCSSValue};
|
||||
use gecko_bindings::structs::{nsMediaFeature, nsMediaFeature_RangeType};
|
||||
use gecko_bindings::structs::{nsMediaFeature_ValueType, nsPresContext};
|
||||
use gecko_bindings::structs::RawGeckoPresContextOwned;
|
||||
use gecko_bindings::structs::nsCSSKeywordAndBoolTableEntry;
|
||||
use gecko_bindings::structs::{nsPresContext, RawGeckoPresContextOwned};
|
||||
use media_queries::MediaType;
|
||||
use parser::{Parse, ParserContext};
|
||||
use properties::ComputedValues;
|
||||
use servo_arc::Arc;
|
||||
use std::fmt::{self, Write};
|
||||
use std::fmt;
|
||||
use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering};
|
||||
use str::{starts_with_ignore_ascii_case, string_as_ascii_lowercase};
|
||||
use string_cache::Atom;
|
||||
use style_traits::{CSSPixel, CssWriter, DevicePixel};
|
||||
use style_traits::{ParseError, StyleParseErrorKind, ToCss};
|
||||
use style_traits::{CSSPixel, DevicePixel};
|
||||
use style_traits::viewport::ViewportConstraints;
|
||||
use stylesheets::Origin;
|
||||
use values::{serialize_atom_identifier, CSSFloat, CustomIdent, KeyframesName};
|
||||
use values::computed::{self, ToComputedValue};
|
||||
use values::{CustomIdent, KeyframesName};
|
||||
use values::computed::font::FontSize;
|
||||
use values::specified::{Integer, Length, Number, Resolution};
|
||||
|
||||
/// The `Device` in Gecko wraps a pres context, has a default values computed,
|
||||
/// and contains all the viewport rule state.
|
||||
|
@ -71,11 +60,8 @@ impl fmt::Debug for Device {
|
|||
|
||||
let mut doc_uri = nsCString::new();
|
||||
unsafe {
|
||||
let doc =
|
||||
&*self.pres_context().mDocument.raw::<structs::nsIDocument>();
|
||||
|
||||
bindings::Gecko_nsIURI_Debug(
|
||||
doc.mDocumentURI.raw::<structs::nsIURI>(),
|
||||
(*self.document()).mDocumentURI.raw::<structs::nsIURI>(),
|
||||
&mut doc_uri,
|
||||
)
|
||||
};
|
||||
|
@ -157,10 +143,17 @@ impl Device {
|
|||
}
|
||||
|
||||
/// Gets the pres context associated with this document.
|
||||
#[inline]
|
||||
pub fn pres_context(&self) -> &nsPresContext {
|
||||
unsafe { &*self.pres_context }
|
||||
}
|
||||
|
||||
/// Gets the document pointer.
|
||||
#[inline]
|
||||
pub fn document(&self) -> *mut structs::nsIDocument {
|
||||
self.pres_context().mDocument.raw::<structs::nsIDocument>()
|
||||
}
|
||||
|
||||
/// Recreates the default computed values.
|
||||
pub fn reset_computed_values(&mut self) {
|
||||
self.default_values = ComputedValues::default_values(self.pres_context());
|
||||
|
@ -248,758 +241,3 @@ impl Device {
|
|||
size.scale_by(1. / self.pres_context().mEffectiveTextZoom)
|
||||
}
|
||||
}
|
||||
|
||||
/// The kind of matching that should be performed on a media feature value.
|
||||
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
|
||||
pub enum Range {
|
||||
/// At least the specified value.
|
||||
Min,
|
||||
/// At most the specified value.
|
||||
Max,
|
||||
}
|
||||
|
||||
/// The operator that was specified in this media feature.
|
||||
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
|
||||
enum Operator {
|
||||
Equal,
|
||||
GreaterThan,
|
||||
GreaterThanEqual,
|
||||
LessThan,
|
||||
LessThanEqual,
|
||||
}
|
||||
|
||||
impl ToCss for Operator {
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: fmt::Write,
|
||||
{
|
||||
dest.write_str(match *self {
|
||||
Operator::Equal => "=",
|
||||
Operator::LessThan => "<",
|
||||
Operator::LessThanEqual => "<=",
|
||||
Operator::GreaterThan => ">",
|
||||
Operator::GreaterThanEqual => ">=",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Either a `Range` or an `Operator`.
|
||||
///
|
||||
/// Ranged media features are not allowed with operations (that'd make no
|
||||
/// sense).
|
||||
#[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq)]
|
||||
enum RangeOrOperator {
|
||||
Range(Range),
|
||||
Operator(Operator),
|
||||
}
|
||||
|
||||
/// A feature expression for gecko contains a reference to the media feature,
|
||||
/// the value the media query contained, and the range to evaluate.
|
||||
#[derive(Clone, Debug, MallocSizeOf)]
|
||||
pub struct MediaFeatureExpression {
|
||||
feature: &'static nsMediaFeature,
|
||||
value: Option<MediaExpressionValue>,
|
||||
range_or_operator: Option<RangeOrOperator>,
|
||||
}
|
||||
|
||||
impl ToCss for MediaFeatureExpression {
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: fmt::Write,
|
||||
{
|
||||
dest.write_str("(")?;
|
||||
|
||||
if (self.feature.mReqFlags & structs::nsMediaFeature_RequirementFlags_eHasWebkitPrefix) != 0
|
||||
{
|
||||
dest.write_str("-webkit-")?;
|
||||
}
|
||||
|
||||
if let Some(RangeOrOperator::Range(range)) = self.range_or_operator {
|
||||
match range {
|
||||
Range::Min => dest.write_str("min-")?,
|
||||
Range::Max => dest.write_str("max-")?,
|
||||
}
|
||||
}
|
||||
|
||||
// NB: CssStringWriter not needed, feature names are under control.
|
||||
write!(dest, "{}", unsafe {
|
||||
Atom::from_static(*self.feature.mName)
|
||||
})?;
|
||||
|
||||
if let Some(RangeOrOperator::Operator(op)) = self.range_or_operator {
|
||||
dest.write_char(' ')?;
|
||||
op.to_css(dest)?;
|
||||
dest.write_char(' ')?;
|
||||
} else if self.value.is_some() {
|
||||
dest.write_str(": ")?;
|
||||
}
|
||||
|
||||
if let Some(ref val) = self.value {
|
||||
val.to_css(dest, self)?;
|
||||
}
|
||||
|
||||
dest.write_str(")")
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq for MediaFeatureExpression {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.feature.mName == other.feature.mName && self.value == other.value &&
|
||||
self.range_or_operator == other.range_or_operator
|
||||
}
|
||||
}
|
||||
|
||||
/// A value found or expected in a media expression.
|
||||
///
|
||||
/// FIXME(emilio): How should calc() serialize in the Number / Integer /
|
||||
/// BoolInteger / IntRatio case, as computed or as specified value?
|
||||
///
|
||||
/// If the first, this would need to store the relevant values.
|
||||
///
|
||||
/// See: https://github.com/w3c/csswg-drafts/issues/1968
|
||||
#[derive(Clone, Debug, MallocSizeOf, PartialEq)]
|
||||
pub enum MediaExpressionValue {
|
||||
/// A length.
|
||||
Length(Length),
|
||||
/// A (non-negative) integer.
|
||||
Integer(u32),
|
||||
/// A floating point value.
|
||||
Float(CSSFloat),
|
||||
/// A boolean value, specified as an integer (i.e., either 0 or 1).
|
||||
BoolInteger(bool),
|
||||
/// Two integers separated by '/', with optional whitespace on either side
|
||||
/// of the '/'.
|
||||
IntRatio(u32, u32),
|
||||
/// A resolution.
|
||||
Resolution(Resolution),
|
||||
/// An enumerated value, defined by the variant keyword table in the
|
||||
/// feature's `mData` member.
|
||||
Enumerated(i16),
|
||||
/// Similar to the above Enumerated but the variant keyword table has an
|
||||
/// additional field what the keyword value means in the Boolean Context.
|
||||
BoolEnumerated(i16),
|
||||
/// An identifier.
|
||||
Ident(Atom),
|
||||
}
|
||||
|
||||
impl MediaExpressionValue {
|
||||
fn from_css_value(
|
||||
for_expr: &MediaFeatureExpression,
|
||||
css_value: &nsCSSValue,
|
||||
) -> Option<Self> {
|
||||
// NB: If there's a null value, that means that we don't support the
|
||||
// feature.
|
||||
if css_value.mUnit == nsCSSUnit::eCSSUnit_Null {
|
||||
return None;
|
||||
}
|
||||
|
||||
match for_expr.feature.mValueType {
|
||||
nsMediaFeature_ValueType::eLength => {
|
||||
debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Pixel);
|
||||
let pixels = css_value.float_unchecked();
|
||||
Some(MediaExpressionValue::Length(Length::from_px(pixels)))
|
||||
},
|
||||
nsMediaFeature_ValueType::eInteger => {
|
||||
let i = css_value.integer_unchecked();
|
||||
debug_assert!(i >= 0);
|
||||
Some(MediaExpressionValue::Integer(i as u32))
|
||||
},
|
||||
nsMediaFeature_ValueType::eFloat => {
|
||||
debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Number);
|
||||
Some(MediaExpressionValue::Float(css_value.float_unchecked()))
|
||||
},
|
||||
nsMediaFeature_ValueType::eBoolInteger => {
|
||||
debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Integer);
|
||||
let i = css_value.integer_unchecked();
|
||||
debug_assert!(i == 0 || i == 1);
|
||||
Some(MediaExpressionValue::BoolInteger(i == 1))
|
||||
},
|
||||
nsMediaFeature_ValueType::eResolution => {
|
||||
debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_Pixel);
|
||||
Some(MediaExpressionValue::Resolution(Resolution::Dppx(
|
||||
css_value.float_unchecked(),
|
||||
)))
|
||||
},
|
||||
nsMediaFeature_ValueType::eEnumerated => {
|
||||
let value = css_value.integer_unchecked() as i16;
|
||||
Some(MediaExpressionValue::Enumerated(value))
|
||||
},
|
||||
nsMediaFeature_ValueType::eBoolEnumerated => {
|
||||
let value = css_value.integer_unchecked() as i16;
|
||||
Some(MediaExpressionValue::BoolEnumerated(value))
|
||||
},
|
||||
nsMediaFeature_ValueType::eIdent => {
|
||||
debug_assert_eq!(css_value.mUnit, nsCSSUnit::eCSSUnit_AtomIdent);
|
||||
Some(MediaExpressionValue::Ident(unsafe {
|
||||
Atom::from_raw(*css_value.mValue.mAtom.as_ref())
|
||||
}))
|
||||
},
|
||||
nsMediaFeature_ValueType::eIntRatio => {
|
||||
let array = unsafe { css_value.array_unchecked() };
|
||||
debug_assert_eq!(array.len(), 2);
|
||||
let first = array[0].integer_unchecked();
|
||||
let second = array[1].integer_unchecked();
|
||||
|
||||
debug_assert!(first >= 0 && second >= 0);
|
||||
Some(MediaExpressionValue::IntRatio(first as u32, second as u32))
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl MediaExpressionValue {
|
||||
fn to_css<W>(&self, dest: &mut CssWriter<W>, for_expr: &MediaFeatureExpression) -> fmt::Result
|
||||
where
|
||||
W: fmt::Write,
|
||||
{
|
||||
match *self {
|
||||
MediaExpressionValue::Length(ref l) => l.to_css(dest),
|
||||
MediaExpressionValue::Integer(v) => v.to_css(dest),
|
||||
MediaExpressionValue::Float(v) => v.to_css(dest),
|
||||
MediaExpressionValue::BoolInteger(v) => dest.write_str(if v { "1" } else { "0" }),
|
||||
MediaExpressionValue::IntRatio(a, b) => {
|
||||
a.to_css(dest)?;
|
||||
dest.write_char('/')?;
|
||||
b.to_css(dest)
|
||||
},
|
||||
MediaExpressionValue::Resolution(ref r) => r.to_css(dest),
|
||||
MediaExpressionValue::Ident(ref ident) => serialize_atom_identifier(ident, dest),
|
||||
MediaExpressionValue::Enumerated(value) => unsafe {
|
||||
let keyword = find_in_table(
|
||||
*for_expr.feature.mData.mKeywordTable.as_ref(),
|
||||
|_kw, val| val == value,
|
||||
|e| e.keyword(),
|
||||
).expect("Value not found in the keyword table?");
|
||||
|
||||
MediaExpressionValue::keyword_to_css(keyword, dest)
|
||||
},
|
||||
MediaExpressionValue::BoolEnumerated(value) => unsafe {
|
||||
let keyword = find_in_table(
|
||||
*for_expr.feature.mData.mKeywordAndBoolTable.as_ref(),
|
||||
|_kw, val| val == value,
|
||||
|e| e.keyword(),
|
||||
).expect("Value not found in the keyword table?");
|
||||
|
||||
MediaExpressionValue::keyword_to_css(keyword, dest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn keyword_to_css<W>(keyword: nsCSSKeyword, dest: &mut CssWriter<W>) -> fmt::Result
|
||||
where
|
||||
W: fmt::Write,
|
||||
{
|
||||
use std::{slice, str};
|
||||
use std::os::raw::c_char;
|
||||
|
||||
// NB: All the keywords on nsMediaFeatures are static,
|
||||
// well-formed utf-8.
|
||||
let mut length = 0;
|
||||
unsafe {
|
||||
let buffer: *const c_char = bindings::Gecko_CSSKeywordString(keyword, &mut length);
|
||||
let buffer = slice::from_raw_parts(buffer as *const u8, length as usize);
|
||||
|
||||
let string = str::from_utf8_unchecked(buffer);
|
||||
|
||||
dest.write_str(string)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn find_feature<F>(mut f: F) -> Option<&'static nsMediaFeature>
|
||||
where
|
||||
F: FnMut(&'static nsMediaFeature) -> bool,
|
||||
{
|
||||
unsafe {
|
||||
let mut features = structs::nsMediaFeatures_features.as_ptr();
|
||||
while !(*features).mName.is_null() {
|
||||
if f(&*features) {
|
||||
return Some(&*features);
|
||||
}
|
||||
features = features.offset(1);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
trait TableEntry {
|
||||
fn value(&self) -> i16;
|
||||
fn keyword(&self) -> nsCSSKeyword;
|
||||
}
|
||||
|
||||
impl TableEntry for nsCSSKTableEntry {
|
||||
fn value(&self) -> i16 {
|
||||
self.mValue
|
||||
}
|
||||
fn keyword(&self) -> nsCSSKeyword {
|
||||
self.mKeyword
|
||||
}
|
||||
}
|
||||
|
||||
impl TableEntry for nsCSSKeywordAndBoolTableEntry {
|
||||
fn value(&self) -> i16 {
|
||||
self._base.mValue
|
||||
}
|
||||
fn keyword(&self) -> nsCSSKeyword {
|
||||
self._base.mKeyword
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn find_in_table<T, R, FindFunc, ResultFunc>(
|
||||
current_entry: *const T,
|
||||
find: FindFunc,
|
||||
result_func: ResultFunc,
|
||||
) -> Option<R>
|
||||
where
|
||||
T: TableEntry,
|
||||
FindFunc: Fn(nsCSSKeyword, i16) -> bool,
|
||||
ResultFunc: Fn(&T) -> R,
|
||||
{
|
||||
let mut current_entry = current_entry;
|
||||
|
||||
loop {
|
||||
let value = (*current_entry).value();
|
||||
let keyword = (*current_entry).keyword();
|
||||
|
||||
if value == -1 {
|
||||
return None; // End of the table.
|
||||
}
|
||||
|
||||
if find(keyword, value) {
|
||||
return Some(result_func(&*current_entry));
|
||||
}
|
||||
|
||||
current_entry = current_entry.offset(1);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_feature_value<'i, 't>(
|
||||
feature: &nsMediaFeature,
|
||||
feature_value_type: nsMediaFeature_ValueType,
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<MediaExpressionValue, ParseError<'i>> {
|
||||
let value = match feature_value_type {
|
||||
nsMediaFeature_ValueType::eLength => {
|
||||
let length = Length::parse_non_negative(context, input)?;
|
||||
MediaExpressionValue::Length(length)
|
||||
},
|
||||
nsMediaFeature_ValueType::eInteger => {
|
||||
let integer = Integer::parse_non_negative(context, input)?;
|
||||
MediaExpressionValue::Integer(integer.value() as u32)
|
||||
},
|
||||
nsMediaFeature_ValueType::eBoolInteger => {
|
||||
let integer = Integer::parse_non_negative(context, input)?;
|
||||
let value = integer.value();
|
||||
if value > 1 {
|
||||
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
|
||||
}
|
||||
MediaExpressionValue::BoolInteger(value == 1)
|
||||
},
|
||||
nsMediaFeature_ValueType::eFloat => {
|
||||
let number = Number::parse(context, input)?;
|
||||
MediaExpressionValue::Float(number.get())
|
||||
},
|
||||
nsMediaFeature_ValueType::eIntRatio => {
|
||||
let a = Integer::parse_positive(context, input)?;
|
||||
input.expect_delim('/')?;
|
||||
let b = Integer::parse_positive(context, input)?;
|
||||
MediaExpressionValue::IntRatio(a.value() as u32, b.value() as u32)
|
||||
},
|
||||
nsMediaFeature_ValueType::eResolution => {
|
||||
MediaExpressionValue::Resolution(Resolution::parse(context, input)?)
|
||||
},
|
||||
nsMediaFeature_ValueType::eEnumerated => {
|
||||
let first_table_entry: *const nsCSSKTableEntry =
|
||||
unsafe { *feature.mData.mKeywordTable.as_ref() };
|
||||
|
||||
let value = parse_keyword(input, first_table_entry)?;
|
||||
|
||||
MediaExpressionValue::Enumerated(value)
|
||||
},
|
||||
nsMediaFeature_ValueType::eBoolEnumerated => {
|
||||
let first_table_entry: *const nsCSSKeywordAndBoolTableEntry =
|
||||
unsafe { *feature.mData.mKeywordAndBoolTable.as_ref() };
|
||||
|
||||
let value = parse_keyword(input, first_table_entry)?;
|
||||
|
||||
MediaExpressionValue::BoolEnumerated(value)
|
||||
},
|
||||
nsMediaFeature_ValueType::eIdent => {
|
||||
MediaExpressionValue::Ident(Atom::from(input.expect_ident()?.as_ref()))
|
||||
},
|
||||
};
|
||||
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Parse a keyword and returns the corresponding i16 value.
|
||||
fn parse_keyword<'i, 't, T>(
|
||||
input: &mut Parser<'i, 't>,
|
||||
first_table_entry: *const T,
|
||||
) -> Result<i16, ParseError<'i>>
|
||||
where
|
||||
T: TableEntry,
|
||||
{
|
||||
let location = input.current_source_location();
|
||||
let keyword = input.expect_ident()?;
|
||||
let keyword = unsafe {
|
||||
bindings::Gecko_LookupCSSKeyword(keyword.as_bytes().as_ptr(), keyword.len() as u32)
|
||||
};
|
||||
|
||||
let value = unsafe {
|
||||
find_in_table(first_table_entry, |kw, _| kw == keyword, |e| e.value())
|
||||
};
|
||||
|
||||
match value {
|
||||
Some(value) => Ok(value),
|
||||
None => Err(location.new_custom_error(StyleParseErrorKind::UnspecifiedError)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Consumes an operation or a colon, or returns an error.
|
||||
fn consume_operation_or_colon(
|
||||
input: &mut Parser,
|
||||
) -> Result<Option<Operator>, ()> {
|
||||
let first_delim = {
|
||||
let next_token = match input.next() {
|
||||
Ok(t) => t,
|
||||
Err(..) => return Err(()),
|
||||
};
|
||||
|
||||
match *next_token {
|
||||
Token::Colon => return Ok(None),
|
||||
Token::Delim(oper) => oper,
|
||||
_ => return Err(()),
|
||||
}
|
||||
};
|
||||
Ok(Some(match first_delim {
|
||||
'=' => Operator::Equal,
|
||||
'>' => {
|
||||
if input.try(|i| i.expect_delim('=')).is_ok() {
|
||||
Operator::GreaterThanEqual
|
||||
} else {
|
||||
Operator::GreaterThan
|
||||
}
|
||||
}
|
||||
'<' => {
|
||||
if input.try(|i| i.expect_delim('=')).is_ok() {
|
||||
Operator::LessThanEqual
|
||||
} else {
|
||||
Operator::LessThan
|
||||
}
|
||||
}
|
||||
_ => return Err(()),
|
||||
}))
|
||||
}
|
||||
|
||||
impl MediaFeatureExpression {
|
||||
/// Trivially construct a new expression.
|
||||
fn new(
|
||||
feature: &'static nsMediaFeature,
|
||||
value: Option<MediaExpressionValue>,
|
||||
range_or_operator: Option<RangeOrOperator>,
|
||||
) -> Self {
|
||||
Self {
|
||||
feature,
|
||||
value,
|
||||
range_or_operator,
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a media expression of the form:
|
||||
///
|
||||
/// ```
|
||||
/// (media-feature: media-value)
|
||||
/// ```
|
||||
pub fn parse<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
input.expect_parenthesis_block()?;
|
||||
input.parse_nested_block(|input| {
|
||||
Self::parse_in_parenthesis_block(context, input)
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse a media feature expression where we've already consumed the
|
||||
/// parenthesis.
|
||||
pub fn parse_in_parenthesis_block<'i, 't>(
|
||||
context: &ParserContext,
|
||||
input: &mut Parser<'i, 't>,
|
||||
) -> Result<Self, ParseError<'i>> {
|
||||
// FIXME: remove extra indented block when lifetimes are non-lexical
|
||||
let feature;
|
||||
let range;
|
||||
{
|
||||
let location = input.current_source_location();
|
||||
let ident = input.expect_ident()?;
|
||||
|
||||
let mut flags = 0;
|
||||
|
||||
if context.chrome_rules_enabled() || context.stylesheet_origin == Origin::UserAgent
|
||||
{
|
||||
flags |= structs::nsMediaFeature_RequirementFlags_eUserAgentAndChromeOnly;
|
||||
}
|
||||
|
||||
let result = {
|
||||
let mut feature_name = &**ident;
|
||||
|
||||
if unsafe { structs::StaticPrefs_sVarCache_layout_css_prefixes_webkit } &&
|
||||
starts_with_ignore_ascii_case(feature_name, "-webkit-")
|
||||
{
|
||||
feature_name = &feature_name[8..];
|
||||
flags |= structs::nsMediaFeature_RequirementFlags_eHasWebkitPrefix;
|
||||
if unsafe {
|
||||
structs::StaticPrefs_sVarCache_layout_css_prefixes_device_pixel_ratio_webkit
|
||||
} {
|
||||
flags |= structs::nsMediaFeature_RequirementFlags_eWebkitDevicePixelRatioPrefEnabled;
|
||||
}
|
||||
}
|
||||
|
||||
let range = if starts_with_ignore_ascii_case(feature_name, "min-") {
|
||||
feature_name = &feature_name[4..];
|
||||
Some(Range::Min)
|
||||
} else if starts_with_ignore_ascii_case(feature_name, "max-") {
|
||||
feature_name = &feature_name[4..];
|
||||
Some(Range::Max)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let atom = Atom::from(string_as_ascii_lowercase(feature_name));
|
||||
match find_feature(|f| atom.as_ptr() == unsafe { *f.mName as *mut _ }) {
|
||||
Some(f) => Ok((f, range)),
|
||||
None => Err(()),
|
||||
}
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok((f, r)) => {
|
||||
feature = f;
|
||||
range = r;
|
||||
},
|
||||
Err(()) => {
|
||||
return Err(location.new_custom_error(
|
||||
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
|
||||
))
|
||||
},
|
||||
}
|
||||
|
||||
if (feature.mReqFlags & !flags) != 0 {
|
||||
return Err(location.new_custom_error(
|
||||
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
|
||||
));
|
||||
}
|
||||
|
||||
if range.is_some() &&
|
||||
feature.mRangeType != nsMediaFeature_RangeType::eMinMaxAllowed
|
||||
{
|
||||
return Err(location.new_custom_error(
|
||||
StyleParseErrorKind::MediaQueryExpectedFeatureName(ident.clone()),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
let feature_allows_ranges =
|
||||
feature.mRangeType == nsMediaFeature_RangeType::eMinMaxAllowed;
|
||||
|
||||
let operator = input.try(consume_operation_or_colon);
|
||||
let operator = match operator {
|
||||
Err(..) => {
|
||||
// If there's no colon, this is a media query of the
|
||||
// form '(<feature>)', that is, there's no value
|
||||
// specified.
|
||||
//
|
||||
// Gecko doesn't allow ranged expressions without a
|
||||
// value, so just reject them here too.
|
||||
if range.is_some() {
|
||||
return Err(input.new_custom_error(
|
||||
StyleParseErrorKind::RangedExpressionWithNoValue
|
||||
));
|
||||
}
|
||||
|
||||
return Ok(Self::new(feature, None, None));
|
||||
}
|
||||
Ok(operator) => operator,
|
||||
};
|
||||
|
||||
let range_or_operator = match range {
|
||||
Some(range) => {
|
||||
if operator.is_some() {
|
||||
return Err(input.new_custom_error(
|
||||
StyleParseErrorKind::MediaQueryUnexpectedOperator
|
||||
));
|
||||
}
|
||||
Some(RangeOrOperator::Range(range))
|
||||
}
|
||||
None => {
|
||||
match operator {
|
||||
Some(operator) => {
|
||||
if !feature_allows_ranges {
|
||||
return Err(input.new_custom_error(
|
||||
StyleParseErrorKind::MediaQueryUnexpectedOperator
|
||||
));
|
||||
}
|
||||
Some(RangeOrOperator::Operator(operator))
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let value =
|
||||
parse_feature_value(feature, feature.mValueType, context, input).map_err(|err| {
|
||||
err.location
|
||||
.new_custom_error(StyleParseErrorKind::MediaQueryExpectedFeatureValue)
|
||||
})?;
|
||||
|
||||
Ok(Self::new(feature, Some(value), range_or_operator))
|
||||
}
|
||||
|
||||
/// Returns whether this media query evaluates to true for the given device.
|
||||
pub fn matches(&self, device: &Device, quirks_mode: QuirksMode) -> bool {
|
||||
let mut css_value = nsCSSValue::null();
|
||||
unsafe {
|
||||
(self.feature.mGetter.unwrap())(
|
||||
device
|
||||
.pres_context()
|
||||
.mDocument
|
||||
.raw::<structs::nsIDocument>(),
|
||||
self.feature,
|
||||
&mut css_value,
|
||||
)
|
||||
};
|
||||
|
||||
let value = match MediaExpressionValue::from_css_value(self, &css_value) {
|
||||
Some(v) => v,
|
||||
None => return false,
|
||||
};
|
||||
|
||||
self.evaluate_against(device, &value, quirks_mode)
|
||||
}
|
||||
|
||||
fn evaluate_against(
|
||||
&self,
|
||||
device: &Device,
|
||||
actual_value: &MediaExpressionValue,
|
||||
quirks_mode: QuirksMode,
|
||||
) -> bool {
|
||||
use self::MediaExpressionValue::*;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
debug_assert!(
|
||||
self.feature.mRangeType == nsMediaFeature_RangeType::eMinMaxAllowed ||
|
||||
self.range_or_operator.is_none(),
|
||||
"Whoops, wrong range"
|
||||
);
|
||||
|
||||
// http://dev.w3.org/csswg/mediaqueries3/#units
|
||||
// em units are relative to the initial font-size.
|
||||
let required_value = match self.value {
|
||||
Some(ref v) => v,
|
||||
None => {
|
||||
// If there's no value, always match unless it's a zero length
|
||||
// or a zero integer or boolean.
|
||||
return match *actual_value {
|
||||
BoolInteger(v) => v,
|
||||
Integer(v) => v != 0,
|
||||
Length(ref l) => computed::Context::for_media_query_evaluation(
|
||||
device,
|
||||
quirks_mode,
|
||||
|context| l.to_computed_value(&context).px() != 0.,
|
||||
),
|
||||
BoolEnumerated(value) => {
|
||||
let value = unsafe {
|
||||
find_in_table(
|
||||
*self.feature.mData.mKeywordAndBoolTable.as_ref(),
|
||||
|_kw, val| val == value,
|
||||
|e| e.mValueInBooleanContext,
|
||||
)
|
||||
};
|
||||
value.expect("Value not found in the keyword table?")
|
||||
},
|
||||
_ => true,
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
// FIXME(emilio): Handle the possible floating point errors?
|
||||
let cmp = match (actual_value, required_value) {
|
||||
(&Length(ref one), &Length(ref other)) => {
|
||||
computed::Context::for_media_query_evaluation(device, quirks_mode, |context| {
|
||||
one.to_computed_value(&context)
|
||||
.to_i32_au()
|
||||
.cmp(&other.to_computed_value(&context).to_i32_au())
|
||||
})
|
||||
},
|
||||
(&Integer(one), &Integer(ref other)) => one.cmp(other),
|
||||
(&BoolInteger(one), &BoolInteger(ref other)) => one.cmp(other),
|
||||
(&Float(one), &Float(ref other)) => one.partial_cmp(other).unwrap(),
|
||||
(&IntRatio(one_num, one_den), &IntRatio(other_num, other_den)) => {
|
||||
// Extend to avoid overflow.
|
||||
(one_num as u64 * other_den as u64).cmp(&(other_num as u64 * one_den as u64))
|
||||
},
|
||||
(&Resolution(ref one), &Resolution(ref other)) => {
|
||||
let actual_dpi = unsafe {
|
||||
if (*device.pres_context).mOverrideDPPX > 0.0 {
|
||||
self::Resolution::Dppx((*device.pres_context).mOverrideDPPX).to_dpi()
|
||||
} else {
|
||||
one.to_dpi()
|
||||
}
|
||||
};
|
||||
|
||||
actual_dpi.partial_cmp(&other.to_dpi()).unwrap()
|
||||
},
|
||||
(&Ident(ref one), &Ident(ref other)) => {
|
||||
debug_assert_ne!(
|
||||
self.feature.mRangeType,
|
||||
nsMediaFeature_RangeType::eMinMaxAllowed
|
||||
);
|
||||
return one == other;
|
||||
},
|
||||
(&Enumerated(one), &Enumerated(other)) => {
|
||||
debug_assert_ne!(
|
||||
self.feature.mRangeType,
|
||||
nsMediaFeature_RangeType::eMinMaxAllowed
|
||||
);
|
||||
return one == other;
|
||||
},
|
||||
(&BoolEnumerated(one), &BoolEnumerated(other)) => {
|
||||
debug_assert_ne!(
|
||||
self.feature.mRangeType,
|
||||
nsMediaFeature_RangeType::eMinMaxAllowed
|
||||
);
|
||||
return one == other;
|
||||
},
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
let range_or_op = match self.range_or_operator {
|
||||
Some(r) => r,
|
||||
None => return cmp == Ordering::Equal,
|
||||
};
|
||||
|
||||
match range_or_op {
|
||||
RangeOrOperator::Range(range) => {
|
||||
cmp == Ordering::Equal || match range {
|
||||
Range::Min => cmp == Ordering::Greater,
|
||||
Range::Max => cmp == Ordering::Less,
|
||||
}
|
||||
}
|
||||
RangeOrOperator::Operator(op) => {
|
||||
match op {
|
||||
Operator::Equal => cmp == Ordering::Equal,
|
||||
Operator::GreaterThan => cmp == Ordering::Greater,
|
||||
Operator::GreaterThanEqual => {
|
||||
cmp == Ordering::Equal || cmp == Ordering::Greater
|
||||
}
|
||||
Operator::LessThan => cmp == Ordering::Less,
|
||||
Operator::LessThanEqual => {
|
||||
cmp == Ordering::Equal || cmp == Ordering::Less
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue