Bug 1422225: Add serialization code for MediaCondition. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 15 Jun 2018 23:58:20 -0700
changeset 809430 d598d1ac9eb6c2d98118838e4ad40e33c0baff69
parent 809429 7033642cea72d178672b15b543c55447889f5875
child 809431 d1b3ed553ce958ce2aca224e0dc670e1844db9bf
push id113676
push userbmo:emilio@crisal.io
push dateFri, 22 Jun 2018 03:46:37 +0000
reviewersxidorn
bugs1422225
milestone62.0a1
Bug 1422225: Add serialization code for MediaCondition. r?xidorn MozReview-Commit-ID: AxQQottV1hG
servo/components/style/media_queries/media_condition.rs
--- a/servo/components/style/media_queries/media_condition.rs
+++ b/servo/components/style/media_queries/media_condition.rs
@@ -3,17 +3,18 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! A media query condition:
 //!
 //! https://drafts.csswg.org/mediaqueries-4/#typedef-media-condition
 
 use cssparser::{Parser, Token};
 use parser::ParserContext;
-use style_traits::ParseError;
+use std::fmt::{self, Write};
+use style_traits::{CssWriter, ParseError, ToCss};
 
 use super::MediaFeatureExpression;
 
 
 /// A binary `and` or `or` operator.
 #[derive(Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, Parse, ToCss)]
 #[allow(missing_docs)]
 pub enum Operator {
@@ -28,16 +29,49 @@ pub enum MediaCondition {
     /// A negation of a condition.
     Not(Box<MediaCondition>),
     /// A set of joint operations.
     Operation(Box<[MediaCondition]>, Operator),
     /// A condition wrapped in parenthesis.
     InParens(Box<MediaCondition>),
 }
 
+impl ToCss for MediaCondition {
+    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
+    where
+        W: fmt::Write,
+    {
+        match *self {
+            // NOTE(emilio): MediaFeatureExpression already includes the
+            // parenthesis.
+            MediaCondition::Feature(ref f) => f.to_css(dest),
+            MediaCondition::Not(ref c) => {
+                dest.write_str("not ")?;
+                c.to_css(dest)
+            }
+            MediaCondition::InParens(ref c) => {
+                dest.write_char('(')?;
+                c.to_css(dest)?;
+                dest.write_char(')')
+            }
+            MediaCondition::Operation(ref list, op) => {
+                let mut iter = list.iter();
+                iter.next().unwrap().to_css(dest)?;
+                for item in iter {
+                    dest.write_char(' ')?;
+                    op.to_css(dest)?;
+                    dest.write_char(' ')?;
+                    item.to_css(dest)?;
+                }
+                Ok(())
+            }
+        }
+    }
+}
+
 impl MediaCondition {
     /// Parse a single media condition.
     pub fn parse<'i, 't>(
         context: &ParserContext,
         input: &mut Parser<'i, 't>,
     ) -> Result<Self, ParseError<'i>> {
         let location = input.current_source_location();