Allow empty media query list

And make empty list the default value of MediaList.

This commit also removes Option wrapper of Stylesheet.media because
a stylesheet should always have an associated media query list.
This commit is contained in:
Xidorn Quan 2016-11-10 15:32:16 +11:00
parent a91f48ee05
commit 5dfcb07f6a
7 changed files with 23 additions and 23 deletions

View file

@ -31,6 +31,12 @@ impl ToCss for MediaList {
}
}
impl Default for MediaList {
fn default() -> MediaList {
MediaList { media_queries: vec![] }
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum Range<T> {
@ -253,8 +259,8 @@ impl MediaQuery {
}
pub fn parse_media_query_list(input: &mut Parser) -> MediaList {
let queries = if input.is_exhausted() {
vec![MediaQuery::new(None, MediaQueryType::All, vec!())]
if input.is_exhausted() {
Default::default()
} else {
let mut media_queries = vec![];
loop {
@ -269,17 +275,17 @@ pub fn parse_media_query_list(input: &mut Parser) -> MediaList {
Err(()) => break,
}
}
media_queries
};
MediaList { media_queries: queries }
MediaList { media_queries: media_queries }
}
}
impl MediaList {
pub fn evaluate(&self, device: &Device) -> bool {
let viewport_size = device.au_viewport_size();
// Check if any queries match (OR condition)
self.media_queries.iter().any(|mq| {
// Check if it is an empty media query list or any queries match (OR condition)
// https://drafts.csswg.org/mediaqueries-4/#mq-list
self.media_queries.is_empty() || self.media_queries.iter().any(|mq| {
// Check if media matches. Unknown media never matches.
let media_match = match mq.media_type {
MediaQueryType::MediaType(MediaType::Unknown(_)) => false,

View file

@ -45,8 +45,8 @@ pub struct Stylesheet {
/// List of rules in the order they were found (important for
/// cascading order)
pub rules: Vec<CSSRule>,
/// List of media associated with the Stylesheet, if any.
pub media: Option<MediaList>,
/// List of media associated with the Stylesheet.
pub media: MediaList,
pub origin: Origin,
pub dirty_on_viewport_size_change: bool,
}
@ -181,14 +181,14 @@ impl Stylesheet {
Stylesheet {
origin: origin,
rules: rules,
media: None,
media: Default::default(),
dirty_on_viewport_size_change:
input.seen_viewport_percentages(),
}
}
/// Set the MediaList associated with the style-sheet.
pub fn set_media(&mut self, media: Option<MediaList>) {
pub fn set_media(&mut self, media: MediaList) {
self.media = media;
}
@ -197,7 +197,7 @@ impl Stylesheet {
///
/// Always true if no associated MediaList exists.
pub fn is_effective_for_device(&self, device: &Device) -> bool {
self.media.as_ref().map_or(true, |ref media| media.evaluate(device))
self.media.evaluate(device)
}
/// Return an iterator over the effective rules within the style-sheet, as