From b7d8fd0ff5334ae92fd5de0dead7862fd6418b4b Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Thu, 8 Jun 2017 10:42:21 +1000 Subject: [PATCH] Move ToComputedValue impl of color types into specified::color. --- components/style/values/computed/mod.rs | 80 --------------------- components/style/values/specified/color.rs | 83 +++++++++++++++++++++- 2 files changed, 82 insertions(+), 81 deletions(-) diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 1547cf29424..9c9e23887a8 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -360,86 +360,6 @@ impl ToCss for Time { } } -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; diff --git a/components/style/values/specified/color.rs b/components/style/values/specified/color.rs index f4d16f6f5ca..4d2443e5081 100644 --- a/components/style/values/specified/color.rs +++ b/components/style/values/specified/color.rs @@ -4,13 +4,14 @@ //! 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; @@ -250,3 +251,83 @@ impl CSSColor { 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() + } +}