servo: Merge #14159 - Allow empty media query list (from upsuper:media-query); r=Wafflespeanut

<!-- Please describe your changes on the following line: -->

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [x] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

Source-Repo: https://github.com/servo/servo
Source-Revision: c3dc50b7ac553acc7116a2fca4c4498e8b3a8a72
This commit is contained in:
Xidorn Quan 2016-11-10 01:37:57 -06:00
Родитель eda4eab5e7
Коммит 306c3d3de4
7 изменённых файлов: 23 добавлений и 23 удалений

Просмотреть файл

@ -359,8 +359,7 @@ impl FetchResponseListener for StylesheetContext {
Some(environment_encoding), Origin::Author,
win.css_error_reporter(),
ParserContextExtraData::default());
let media = self.media.take().unwrap();
sheet.set_media(Some(media));
sheet.set_media(self.media.take().unwrap());
let sheet = Arc::new(sheet);
let win = window_from_node(&*elem);

Просмотреть файл

@ -83,7 +83,7 @@ impl HTMLMetaElement {
*self.stylesheet.borrow_mut() = Some(Arc::new(Stylesheet {
rules: vec![CSSRule::Viewport(Arc::new(RwLock::new(translated_rule)))],
origin: Origin::Author,
media: None,
media: Default::default(),
// Viewport constraints are always recomputed on resize; they don't need to
// force all styles to be recomputed.
dirty_on_viewport_size_change: false,

Просмотреть файл

@ -65,8 +65,7 @@ impl HTMLStyleElement {
let mut sheet = Stylesheet::from_str(&data, url, Origin::Author, win.css_error_reporter(),
ParserContextExtraData::default());
let mut css_parser = CssParser::new(&mq_str);
let media = parse_media_query_list(&mut css_parser);
sheet.set_media(Some(media));
sheet.set_media(parse_media_query_list(&mut css_parser));
let sheet = Arc::new(sheet);
win.layout_chan().send(Msg::AddStylesheet(sheet.clone())).unwrap();

Просмотреть файл

@ -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,

Просмотреть файл

@ -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

Просмотреть файл

@ -59,11 +59,7 @@ fn media_query_test(device: &Device, css: &str, expected_rule_count: usize) {
#[test]
fn test_mq_empty() {
test_media_rule("@media { }", |list, css| {
assert!(list.media_queries.len() == 1, css.to_owned());
let q = &list.media_queries[0];
assert!(q.qualifier == None, css.to_owned());
assert!(q.media_type == MediaQueryType::All, css.to_owned());
assert!(q.expressions.len() == 0, css.to_owned());
assert!(list.media_queries.len() == 0, css.to_owned());
});
}

Просмотреть файл

@ -54,7 +54,7 @@ fn test_parse_stylesheet() {
ParserContextExtraData::default());
let expected = Stylesheet {
origin: Origin::UserAgent,
media: None,
media: Default::default(),
dirty_on_viewport_size_change: false,
rules: vec![
CSSRule::Namespace(Arc::new(RwLock::new(NamespaceRule {