mirror of
https://github.com/servo/servo.git
synced 2025-08-07 06:25:32 +01:00
Rework MediaType to be an atom-based struct instead of an enum.
MozReview-Commit-ID: 1Tfrs9PBkhA
This commit is contained in:
parent
77cb5371b3
commit
557ffa979d
9 changed files with 92 additions and 72 deletions
|
@ -70,3 +70,6 @@ gattserverdisconnected
|
|||
onchange
|
||||
|
||||
reftest-wait
|
||||
|
||||
screen
|
||||
print
|
||||
|
|
|
@ -481,7 +481,7 @@ impl LayoutThread {
|
|||
// The device pixel ratio is incorrect (it does not have the hidpi value),
|
||||
// but it will be set correctly when the initial reflow takes place.
|
||||
let device = Device::new(
|
||||
MediaType::Screen,
|
||||
MediaType::screen(),
|
||||
opts::get().initial_window_size.to_f32() * ScaleFactor::new(1.0),
|
||||
ScaleFactor::new(opts::get().device_pixels_per_px.unwrap_or(1.0)));
|
||||
|
||||
|
@ -1156,7 +1156,7 @@ impl LayoutThread {
|
|||
let document_shared_lock = document.style_shared_lock();
|
||||
self.document_shared_lock = Some(document_shared_lock.clone());
|
||||
let author_guard = document_shared_lock.read();
|
||||
let device = Device::new(MediaType::Screen, initial_viewport, device_pixel_ratio);
|
||||
let device = Device::new(MediaType::screen(), initial_viewport, device_pixel_ratio);
|
||||
self.stylist.set_device(device, &author_guard, &data.document_stylesheets);
|
||||
|
||||
self.viewport_size =
|
||||
|
|
|
@ -77,7 +77,7 @@ impl MediaQueryList {
|
|||
if let Some(window_size) = self.document.window().window_size() {
|
||||
let viewport_size = window_size.initial_viewport;
|
||||
let device_pixel_ratio = window_size.device_pixel_ratio;
|
||||
let device = Device::new(MediaType::Screen, viewport_size, device_pixel_ratio);
|
||||
let device = Device::new(MediaType::screen(), viewport_size, device_pixel_ratio);
|
||||
self.media_query_list.evaluate(&device, self.document.quirks_mode())
|
||||
} else {
|
||||
false
|
||||
|
|
|
@ -18,6 +18,7 @@ use gecko_bindings::structs::{nsCSSKeyword, nsCSSProps_KTableEntry, nsCSSValue,
|
|||
use gecko_bindings::structs::{nsMediaExpression_Range, nsMediaFeature};
|
||||
use gecko_bindings::structs::{nsMediaFeature_ValueType, nsMediaFeature_RangeType, nsMediaFeature_RequirementFlags};
|
||||
use gecko_bindings::structs::{nsPresContext, RawGeckoPresContextOwned};
|
||||
use gecko_bindings::structs::nsIAtom;
|
||||
use media_queries::MediaType;
|
||||
use parser::ParserContext;
|
||||
use properties::{ComputedValues, StyleBuilder};
|
||||
|
@ -31,7 +32,7 @@ use string_cache::Atom;
|
|||
use style_traits::{CSSPixel, DevicePixel};
|
||||
use style_traits::{ToCss, ParseError, StyleParseError};
|
||||
use style_traits::viewport::ViewportConstraints;
|
||||
use values::{CSSFloat, specified};
|
||||
use values::{CSSFloat, specified, CustomIdent};
|
||||
use values::computed::{self, ToComputedValue};
|
||||
|
||||
/// The `Device` in Gecko wraps a pres context, has a default values computed,
|
||||
|
@ -140,15 +141,16 @@ impl Device {
|
|||
/// Returns the current media type of the device.
|
||||
pub fn media_type(&self) -> MediaType {
|
||||
unsafe {
|
||||
// FIXME(emilio): Gecko allows emulating random media with
|
||||
// mIsEmulatingMedia / mMediaEmulated . Refactor both sides so that
|
||||
// is supported (probably just making MediaType an Atom).
|
||||
if self.pres_context().mMedium == atom!("screen").as_ptr() {
|
||||
MediaType::Screen
|
||||
// Gecko allows emulating random media with mIsEmulatingMedia and
|
||||
// mMediaEmulated.
|
||||
let context = self.pres_context();
|
||||
let medium_to_use = if context.mIsEmulatingMedia() != 0 {
|
||||
context.mMediaEmulated.raw::<nsIAtom>()
|
||||
} else {
|
||||
debug_assert!(self.pres_context().mMedium == atom!("print").as_ptr());
|
||||
MediaType::Print
|
||||
}
|
||||
context.mMedium
|
||||
};
|
||||
|
||||
MediaType(CustomIdent(Atom::from(medium_to_use)))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,9 @@ use parser::ParserContext;
|
|||
use selectors::parser::SelectorParseError;
|
||||
use serialize_comma_separated_list;
|
||||
use std::fmt;
|
||||
use str::string_as_ascii_lowercase;
|
||||
use style_traits::{ToCss, ParseError, StyleParseError};
|
||||
use values::CustomIdent;
|
||||
|
||||
#[cfg(feature = "servo")]
|
||||
pub use servo::media_queries::{Device, Expression};
|
||||
|
@ -108,9 +110,7 @@ impl ToCss for MediaQuery {
|
|||
write!(dest, "all")?;
|
||||
}
|
||||
},
|
||||
MediaQueryType::Known(MediaType::Screen) => write!(dest, "screen")?,
|
||||
MediaQueryType::Known(MediaType::Print) => write!(dest, "print")?,
|
||||
MediaQueryType::Unknown(ref desc) => write!(dest, "{}", desc)?,
|
||||
MediaQueryType::Concrete(MediaType(ref desc)) => desc.to_css(dest)?,
|
||||
}
|
||||
|
||||
if self.expressions.is_empty() {
|
||||
|
@ -137,35 +137,25 @@ impl ToCss for MediaQuery {
|
|||
pub enum MediaQueryType {
|
||||
/// A media type that matches every device.
|
||||
All,
|
||||
/// A known media type, that we parse and understand.
|
||||
Known(MediaType),
|
||||
/// An unknown media type.
|
||||
Unknown(Atom),
|
||||
/// A specific media type.
|
||||
Concrete(MediaType),
|
||||
}
|
||||
|
||||
impl MediaQueryType {
|
||||
fn parse(ident: &str) -> Result<Self, ()> {
|
||||
match_ignore_ascii_case! { ident,
|
||||
"all" => return Ok(MediaQueryType::All),
|
||||
// From https://drafts.csswg.org/mediaqueries/#mq-syntax:
|
||||
//
|
||||
// The <media-type> production does not include the keywords only,
|
||||
// not, and, and or.
|
||||
"not" | "or" | "and" | "only" => return Err(()),
|
||||
_ => (),
|
||||
};
|
||||
|
||||
Ok(match MediaType::parse(ident) {
|
||||
Some(media_type) => MediaQueryType::Known(media_type),
|
||||
None => MediaQueryType::Unknown(Atom::from(ident)),
|
||||
})
|
||||
// If parseable, accept this type as a concrete type.
|
||||
MediaType::parse(ident).map(MediaQueryType::Concrete)
|
||||
}
|
||||
|
||||
fn matches(&self, other: MediaType) -> bool {
|
||||
match *self {
|
||||
MediaQueryType::All => true,
|
||||
MediaQueryType::Known(ref known_type) => *known_type == other,
|
||||
MediaQueryType::Unknown(..) => false,
|
||||
MediaQueryType::Concrete(ref known_type) => *known_type == other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -173,20 +163,30 @@ impl MediaQueryType {
|
|||
/// https://drafts.csswg.org/mediaqueries/#media-types
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
pub enum MediaType {
|
||||
/// The "screen" media type.
|
||||
Screen,
|
||||
/// The "print" media type.
|
||||
Print,
|
||||
}
|
||||
pub struct MediaType(pub CustomIdent);
|
||||
|
||||
impl MediaType {
|
||||
fn parse(name: &str) -> Option<Self> {
|
||||
Some(match_ignore_ascii_case! { name,
|
||||
"screen" => MediaType::Screen,
|
||||
"print" => MediaType::Print,
|
||||
_ => return None
|
||||
})
|
||||
/// The `screen` media type.
|
||||
pub fn screen() -> Self {
|
||||
MediaType(CustomIdent(atom!("screen")))
|
||||
}
|
||||
|
||||
/// The `print` media type.
|
||||
pub fn print() -> Self {
|
||||
MediaType(CustomIdent(atom!("print")))
|
||||
}
|
||||
|
||||
fn parse(name: &str) -> Result<Self, ()> {
|
||||
// From https://drafts.csswg.org/mediaqueries/#mq-syntax:
|
||||
//
|
||||
// The <media-type> production does not include the keywords not, or, and, and only.
|
||||
//
|
||||
// Here we also perform the to-ascii-lowercase part of the serialization
|
||||
// algorithm: https://drafts.csswg.org/cssom/#serializing-media-queries
|
||||
match_ignore_ascii_case! { name,
|
||||
"not" | "or" | "and" | "only" => Err(()),
|
||||
_ => Ok(MediaType(CustomIdent(Atom::from(string_as_ascii_lowercase(name))))),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl MediaQuery {
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
use num_traits::ToPrimitive;
|
||||
use std::ascii::AsciiExt;
|
||||
use std::borrow::Cow;
|
||||
use std::convert::AsRef;
|
||||
use std::iter::{Filter, Peekable};
|
||||
use std::str::Split;
|
||||
|
@ -151,3 +152,13 @@ pub fn starts_with_ignore_ascii_case(string: &str, prefix: &str) -> bool {
|
|||
string.len() >= prefix.len() &&
|
||||
string.as_bytes()[0..prefix.len()].eq_ignore_ascii_case(prefix.as_bytes())
|
||||
}
|
||||
|
||||
/// Returns an ascii lowercase version of a string, only allocating if needed.
|
||||
pub fn string_as_ascii_lowercase<'a>(input: &'a str) -> Cow<'a, str> {
|
||||
if input.bytes().any(|c| matches!(c, b'A'...b'Z')) {
|
||||
input.to_ascii_lowercase().into()
|
||||
} else {
|
||||
// Already ascii lowercase.
|
||||
Cow::Borrowed(input)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue