mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
style: Simplify media-query parsing so it's easier to split out.
This commit is contained in:
parent
8120906a15
commit
bb672f1a2c
1 changed files with 35 additions and 16 deletions
|
@ -11,6 +11,7 @@ use app_units::Au;
|
||||||
use cssparser::{Delimiter, Parser, Token};
|
use cssparser::{Delimiter, Parser, Token};
|
||||||
use euclid::size::{Size2D, TypedSize2D};
|
use euclid::size::{Size2D, TypedSize2D};
|
||||||
use serialize_comma_separated_list;
|
use serialize_comma_separated_list;
|
||||||
|
use std::ascii::AsciiExt;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use style_traits::{ToCss, ViewportPx};
|
use style_traits::{ToCss, ViewportPx};
|
||||||
use values::computed::{self, ToComputedValue};
|
use values::computed::{self, ToComputedValue};
|
||||||
|
@ -162,6 +163,17 @@ pub enum MediaQueryType {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MediaQueryType {
|
impl MediaQueryType {
|
||||||
|
fn parse(ident: &str) -> Self {
|
||||||
|
if ident.eq_ignore_ascii_case("all") {
|
||||||
|
return MediaQueryType::All;
|
||||||
|
}
|
||||||
|
|
||||||
|
match MediaType::parse(ident) {
|
||||||
|
Some(media_type) => MediaQueryType::Known(media_type),
|
||||||
|
None => MediaQueryType::Unknown(Atom::from(ident)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn matches(&self, other: &MediaType) -> bool {
|
fn matches(&self, other: &MediaType) -> bool {
|
||||||
match *self {
|
match *self {
|
||||||
MediaQueryType::All => true,
|
MediaQueryType::All => true,
|
||||||
|
@ -178,6 +190,16 @@ pub enum MediaType {
|
||||||
Print,
|
Print,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MediaType {
|
||||||
|
fn parse(name: &str) -> Option<Self> {
|
||||||
|
Some(match_ignore_ascii_case! { name,
|
||||||
|
"screen" => MediaType::Screen,
|
||||||
|
"print" => MediaType::Print,
|
||||||
|
_ => return None
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
pub struct Device {
|
pub struct Device {
|
||||||
|
@ -236,23 +258,20 @@ impl MediaQuery {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
let media_type;
|
let media_type = match input.try(|input| input.expect_ident()) {
|
||||||
if let Ok(ident) = input.try(|input| input.expect_ident()) {
|
Ok(ident) => MediaQueryType::parse(&*ident),
|
||||||
media_type = match_ignore_ascii_case! { ident,
|
Err(()) => {
|
||||||
"screen" => MediaQueryType::Known(MediaType::Screen),
|
// Media type is only optional if qualifier is not specified.
|
||||||
"print" => MediaQueryType::Known(MediaType::Print),
|
if qualifier.is_some() {
|
||||||
"all" => MediaQueryType::All,
|
return Err(())
|
||||||
_ => MediaQueryType::Unknown(Atom::from(&*ident))
|
}
|
||||||
|
|
||||||
|
// Without a media type, require at least one expression.
|
||||||
|
expressions.push(try!(Expression::parse(input)));
|
||||||
|
|
||||||
|
MediaQueryType::All
|
||||||
}
|
}
|
||||||
} else {
|
};
|
||||||
// Media type is only optional if qualifier is not specified.
|
|
||||||
if qualifier.is_some() {
|
|
||||||
return Err(())
|
|
||||||
}
|
|
||||||
media_type = MediaQueryType::All;
|
|
||||||
// Without a media type, require at least one expression
|
|
||||||
expressions.push(try!(Expression::parse(input)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse any subsequent expressions
|
// Parse any subsequent expressions
|
||||||
loop {
|
loop {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue