servo: Merge #14192 - implement support for the width query (from gterzian:support_equality_constraints); r=emilio

<!-- Please describe your changes on the following line: -->
implement support for the `width` media query

---
<!-- 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
- [x] These changes fix #https://github.com/servo/servo/issues/13670 (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: bcc76f8a58f2ebe3886d16ff3eebbe68e3debf6c
This commit is contained in:
Gregory 2016-11-26 04:09:24 -08:00
Родитель 366f5975d5
Коммит dd3f6b0834
2 изменённых файлов: 33 добавлений и 6 удалений

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

@ -42,7 +42,7 @@ impl Default for MediaList {
pub enum Range<T> { pub enum Range<T> {
Min(T), Min(T),
Max(T), Max(T),
//Eq(T), // FIXME: Implement parsing support for equality then re-enable this. Eq(T),
} }
impl Range<specified::Length> { impl Range<specified::Length> {
@ -54,7 +54,7 @@ impl Range<specified::Length> {
match *self { match *self {
Range::Min(ref width) => Range::Min(width.to_computed_value(&context)), Range::Min(ref width) => Range::Min(width.to_computed_value(&context)),
Range::Max(ref width) => Range::Max(width.to_computed_value(&context)), Range::Max(ref width) => Range::Max(width.to_computed_value(&context)),
//Range::Eq(ref width) => Range::Eq(compute_width(width)) Range::Eq(ref width) => Range::Eq(width.to_computed_value(&context))
} }
} }
} }
@ -64,7 +64,7 @@ impl<T: Ord> Range<T> {
match *self { match *self {
Range::Min(ref width) => { value >= *width }, Range::Min(ref width) => { value >= *width },
Range::Max(ref width) => { value <= *width }, Range::Max(ref width) => { value <= *width },
//Range::Eq(ref width) => { value == *width }, Range::Eq(ref width) => { value == *width },
} }
} }
} }
@ -127,10 +127,11 @@ impl ToCss for MediaQuery {
for (i, &e) in self.expressions.iter().enumerate() { for (i, &e) in self.expressions.iter().enumerate() {
try!(write!(dest, "(")); try!(write!(dest, "("));
let (mm, l) = match e { let (mm, l) = match e {
Expression::Width(Range::Min(ref l)) => ("min", l), Expression::Width(Range::Min(ref l)) => ("min-", l),
Expression::Width(Range::Max(ref l)) => ("max", l), Expression::Width(Range::Max(ref l)) => ("max-", l),
Expression::Width(Range::Eq(ref l)) => ("", l),
}; };
try!(write!(dest, "{}-width: ", mm)); try!(write!(dest, "{}width: ", mm));
try!(l.to_css(dest)); try!(l.to_css(dest));
if i == self.expressions.len() - 1 { if i == self.expressions.len() - 1 {
try!(write!(dest, ")")); try!(write!(dest, ")"));
@ -195,6 +196,9 @@ impl Expression {
"max-width" => { "max-width" => {
Ok(Expression::Width(Range::Max(try!(specified::Length::parse_non_negative(input))))) Ok(Expression::Width(Range::Max(try!(specified::Length::parse_non_negative(input)))))
}, },
"width" => {
Ok(Expression::Width(Range::Eq(try!(specified::Length::parse_non_negative(input)))))
},
_ => Err(()) _ => Err(())
} }
}) })

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

@ -13,6 +13,7 @@ use style::media_queries::*;
use style::parser::ParserContextExtraData; use style::parser::ParserContextExtraData;
use style::stylesheets::{Stylesheet, Origin, CssRule}; use style::stylesheets::{Stylesheet, Origin, CssRule};
use style::values::specified; use style::values::specified;
use style_traits::ToCss;
pub struct CSSErrorReporterTest; pub struct CSSErrorReporterTest;
@ -242,6 +243,18 @@ fn test_mq_expressions() {
} }
}); });
test_media_rule("@media print and (width: 43px) { }", |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::MediaType(MediaType::Print), css.to_owned());
assert!(q.expressions.len() == 1, css.to_owned());
match q.expressions[0] {
Expression::Width(Range::Eq(w)) => assert!(w == specified::Length::Absolute(Au::from_px(43))),
_ => panic!("wrong expression type"),
}
});
test_media_rule("@media fridge and (max-width: 52px) { }", |list, css| { test_media_rule("@media fridge and (max-width: 52px) { }", |list, css| {
assert!(list.media_queries.len() == 1, css.to_owned()); assert!(list.media_queries.len() == 1, css.to_owned());
let q = &list.media_queries[0]; let q = &list.media_queries[0];
@ -255,6 +268,16 @@ fn test_mq_expressions() {
}); });
} }
#[test]
fn test_to_css() {
test_media_rule("@media print and (width: 43px) { }", |list, _| {
let q = &list.media_queries[0];
let mut dest = String::new();
assert_eq!(Ok(()), q.to_css(&mut dest));
assert_eq!(dest, "print and (width: 43px)");
});
}
#[test] #[test]
fn test_mq_multiple_expressions() { fn test_mq_multiple_expressions() {
test_media_rule("@media (min-width: 100px) and (max-width: 200px) { }", |list, css| { test_media_rule("@media (min-width: 100px) and (max-width: 200px) { }", |list, css| {