Move ToComputedValue impl of color types into specified::color. r?manishearth draft
authorXidorn Quan <me@upsuper.org>
Tue, 06 Jun 2017 17:06:20 +1000
changeset 590135 0522bc54293f6499d3f4c0d67b5eb8363d22f36d
parent 590134 e9c1854ec27b0e0f882d0b18b4037303cfb2b310
child 590136 1000b2c31a1d4e8cb1dd5cbcc15abd45b9a3f484
push id62604
push userxquan@mozilla.com
push dateWed, 07 Jun 2017 07:07:54 +0000
reviewersmanishearth
milestone55.0a1
Move ToComputedValue impl of color types into specified::color. r?manishearth MozReview-Commit-ID: BJl2zHd2kZv
servo/components/style/values/computed/mod.rs
servo/components/style/values/specified/color.rs
--- a/servo/components/style/values/computed/mod.rs
+++ b/servo/components/style/values/computed/mod.rs
@@ -350,96 +350,16 @@ impl Time {
 impl ToCss for Time {
     fn to_css<W>(&self, dest: &mut W) -> fmt::Result
         where W: fmt::Write,
     {
         write!(dest, "{}s", self.seconds())
     }
 }
 
-impl ToComputedValue for specified::Color {
-    type ComputedValue = RGBA;
-
-    #[cfg(not(feature = "gecko"))]
-    fn to_computed_value(&self, context: &Context) -> RGBA {
-        match *self {
-            specified::Color::RGBA(rgba) => rgba,
-            specified::Color::CurrentColor => context.inherited_style.get_color().clone_color(),
-        }
-    }
-
-    #[cfg(feature = "gecko")]
-    fn to_computed_value(&self, context: &Context) -> RGBA {
-        use gecko::values::convert_nscolor_to_rgba as to_rgba;
-        // It's safe to access the nsPresContext immutably during style computation.
-        let pres_context = unsafe { &*context.device.pres_context };
-        match *self {
-            specified::Color::RGBA(rgba) => rgba,
-            specified::Color::System(system) => to_rgba(system.to_computed_value(context)),
-            specified::Color::CurrentColor => context.inherited_style.get_color().clone_color(),
-            specified::Color::MozDefaultColor => to_rgba(pres_context.mDefaultColor),
-            specified::Color::MozDefaultBackgroundColor => to_rgba(pres_context.mBackgroundColor),
-            specified::Color::MozHyperlinktext => to_rgba(pres_context.mLinkColor),
-            specified::Color::MozActiveHyperlinktext => to_rgba(pres_context.mActiveLinkColor),
-            specified::Color::MozVisitedHyperlinktext => to_rgba(pres_context.mVisitedLinkColor),
-            specified::Color::InheritFromBodyQuirk => {
-                use dom::TElement;
-                use gecko::wrapper::GeckoElement;
-                use gecko_bindings::bindings::Gecko_GetBody;
-                let body = unsafe {
-                    Gecko_GetBody(pres_context)
-                };
-                if let Some(body) = body {
-                    let wrap = GeckoElement(body);
-                    let borrow = wrap.borrow_data();
-                    borrow.as_ref().unwrap()
-                          .styles().primary.values()
-                          .get_color()
-                          .clone_color()
-                } else {
-                    to_rgba(pres_context.mDefaultColor)
-                }
-            },
-        }
-    }
-
-    fn from_computed_value(computed: &RGBA) -> Self {
-        specified::Color::RGBA(*computed)
-    }
-}
-
-impl ToComputedValue for specified::CSSColor {
-    type ComputedValue = CSSColor;
-
-    #[cfg(not(feature = "gecko"))]
-    #[inline]
-    fn to_computed_value(&self, _context: &Context) -> CSSColor {
-        self.parsed
-    }
-
-    #[cfg(feature = "gecko")]
-    #[inline]
-    fn to_computed_value(&self, context: &Context) -> CSSColor {
-        match self.parsed {
-            specified::Color::RGBA(rgba) => CSSColor::RGBA(rgba),
-            specified::Color::CurrentColor => CSSColor::CurrentColor,
-            // Resolve non-standard -moz keywords to RGBA:
-            non_standard => CSSColor::RGBA(non_standard.to_computed_value(context)),
-        }
-    }
-
-    #[inline]
-    fn from_computed_value(computed: &CSSColor) -> Self {
-        (match *computed {
-            CSSColor::RGBA(rgba) => specified::Color::RGBA(rgba),
-            CSSColor::CurrentColor => specified::Color::CurrentColor,
-        }).into()
-    }
-}
-
 #[cfg(feature = "gecko")]
 impl ToComputedValue for specified::JustifyItems {
     type ComputedValue = JustifyItems;
 
     // https://drafts.csswg.org/css-align/#valdef-justify-items-auto
     fn to_computed_value(&self, context: &Context) -> JustifyItems {
         use values::specified::align;
         // If the inherited value of `justify-items` includes the `legacy` keyword, `auto` computes
--- a/servo/components/style/values/specified/color.rs
+++ b/servo/components/style/values/specified/color.rs
@@ -1,21 +1,22 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 //! Specified color values.
 
-use cssparser::{self, Parser, Token};
+use cssparser::{self, Color as CSSParserColor, Parser, RGBA, Token};
 use itoa;
 use parser::{ParserContext, Parse};
 use std::fmt;
 use std::io::Write;
 use style_traits::ToCss;
 use super::AllowQuirks;
+use values::computed::{Context, ToComputedValue};
 
 #[cfg(not(feature = "gecko"))] pub use self::servo::Color;
 #[cfg(feature = "gecko")] pub use self::gecko::Color;
 
 #[cfg(not(feature = "gecko"))]
 mod servo {
     pub use cssparser::Color;
     use cssparser::Parser;
@@ -245,8 +246,88 @@ impl CSSColor {
 
     #[inline]
     /// Returns transparent value.
     pub fn transparent() -> CSSColor {
         // We should probably set authored to "transparent", but maybe it doesn't matter.
         Color::RGBA(cssparser::RGBA::transparent()).into()
     }
 }
+
+impl ToComputedValue for Color {
+    type ComputedValue = RGBA;
+
+    #[cfg(not(feature = "gecko"))]
+    fn to_computed_value(&self, context: &Context) -> RGBA {
+        match *self {
+            Color::RGBA(rgba) => rgba,
+            Color::CurrentColor => context.inherited_style.get_color().clone_color(),
+        }
+    }
+
+    #[cfg(feature = "gecko")]
+    fn to_computed_value(&self, context: &Context) -> RGBA {
+        use gecko::values::convert_nscolor_to_rgba as to_rgba;
+        // It's safe to access the nsPresContext immutably during style computation.
+        let pres_context = unsafe { &*context.device.pres_context };
+        match *self {
+            Color::RGBA(rgba) => rgba,
+            Color::System(system) => to_rgba(system.to_computed_value(context)),
+            Color::CurrentColor => context.inherited_style.get_color().clone_color(),
+            Color::MozDefaultColor => to_rgba(pres_context.mDefaultColor),
+            Color::MozDefaultBackgroundColor => to_rgba(pres_context.mBackgroundColor),
+            Color::MozHyperlinktext => to_rgba(pres_context.mLinkColor),
+            Color::MozActiveHyperlinktext => to_rgba(pres_context.mActiveLinkColor),
+            Color::MozVisitedHyperlinktext => to_rgba(pres_context.mVisitedLinkColor),
+            Color::InheritFromBodyQuirk => {
+                use dom::TElement;
+                use gecko::wrapper::GeckoElement;
+                use gecko_bindings::bindings::Gecko_GetBody;
+                let body = unsafe {
+                    Gecko_GetBody(pres_context)
+                };
+                if let Some(body) = body {
+                    let wrap = GeckoElement(body);
+                    let borrow = wrap.borrow_data();
+                    borrow.as_ref().unwrap()
+                          .styles().primary.values()
+                          .get_color()
+                          .clone_color()
+                } else {
+                    to_rgba(pres_context.mDefaultColor)
+                }
+            },
+        }
+    }
+
+    fn from_computed_value(computed: &RGBA) -> Self {
+        Color::RGBA(*computed)
+    }
+}
+
+impl ToComputedValue for CSSColor {
+    type ComputedValue = CSSParserColor;
+
+    #[cfg(not(feature = "gecko"))]
+    #[inline]
+    fn to_computed_value(&self, _context: &Context) -> CSSParserColor {
+        self.parsed
+    }
+
+    #[cfg(feature = "gecko")]
+    #[inline]
+    fn to_computed_value(&self, context: &Context) -> CSSParserColor {
+        match self.parsed {
+            Color::RGBA(rgba) => CSSParserColor::RGBA(rgba),
+            Color::CurrentColor => CSSParserColor::CurrentColor,
+            // Resolve non-standard -moz keywords to RGBA:
+            non_standard => CSSParserColor::RGBA(non_standard.to_computed_value(context)),
+        }
+    }
+
+    #[inline]
+    fn from_computed_value(computed: &CSSParserColor) -> Self {
+        (match *computed {
+            CSSParserColor::RGBA(rgba) => Color::RGBA(rgba),
+            CSSParserColor::CurrentColor => Color::CurrentColor,
+        }).into()
+    }
+}