From 206f4ea5b83dcd7cfdab20b9fb67387ce66f1880 Mon Sep 17 00:00:00 2001 From: Thiago Pontes Date: Fri, 25 Nov 2016 15:51:53 -0500 Subject: [PATCH] use Either type for UrlOrNone fix test-tidy errors fix style unit test use the Parse trait instead of ParseWithContext fix stylo test and geckolib build move all specified url parse code to parse trait and remove refs to unused type --- components/layout/construct.rs | 7 ++-- components/style/font_face.rs | 2 +- components/style/properties/gecko.mako.rs | 12 +++---- .../style/properties/longhand/box.mako.rs | 2 +- .../style/properties/longhand/list.mako.rs | 2 +- .../style/properties/shorthand/list.mako.rs | 7 ++-- components/style/values/specified/mod.rs | 32 ++----------------- components/style/values/specified/url.rs | 11 +++++-- tests/unit/style/properties/serialization.rs | 4 +-- 9 files changed, 30 insertions(+), 49 deletions(-) diff --git a/components/layout/construct.rs b/components/layout/construct.rs index 453c68dcae1..f3309b97b75 100644 --- a/components/layout/construct.rs +++ b/components/layout/construct.rs @@ -45,7 +45,7 @@ use std::marker::PhantomData; use std::mem; use std::sync::Arc; use std::sync::atomic::Ordering; -use style::computed_values::{caption_side, display, empty_cells, float, list_style_image, list_style_position}; +use style::computed_values::{caption_side, display, empty_cells, float, list_style_position}; use style::computed_values::content::ContentItem; use style::computed_values::position; use style::context::SharedStyleContext; @@ -54,6 +54,7 @@ use style::properties::{self, ServoComputedValues}; use style::selector_parser::{PseudoElement, RestyleDamage}; use style::servo::restyle_damage::{BUBBLE_ISIZES, RECONSTRUCT_FLOW}; use style::stylist::Stylist; +use style::values::Either; use table::TableFlow; use table_caption::TableCaptionFlow; use table_cell::TableCellFlow; @@ -1205,13 +1206,13 @@ impl<'a, ConcreteThreadSafeLayoutNode: ThreadSafeLayoutNode> -> ConstructionResult { let flotation = FloatKind::from_property(flotation); let marker_fragments = match node.style(self.style_context()).get_list().list_style_image { - list_style_image::T::Url(ref url_value) => { + Either::First(ref url_value) => { let image_info = box ImageFragmentInfo::new(node, url_value.url().map(|u| u.clone()), &self.layout_context.shared); vec![Fragment::new(node, SpecificFragmentInfo::Image(image_info), self.layout_context)] } - list_style_image::T::None => { + Either::Second(_none) => { match ListStyleTypeContent::from_list_style_type(node.style(self.style_context()) .get_list() .list_style_type) { diff --git a/components/style/font_face.rs b/components/style/font_face.rs index 4a5f34237ad..70f904c17a4 100644 --- a/components/style/font_face.rs +++ b/components/style/font_face.rs @@ -8,7 +8,7 @@ use computed_values::font_family::FontFamily; use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser}; -use parser::{ParserContext, log_css_error}; +use parser::{ParserContext, log_css_error, Parse}; use properties::longhands::font_family::parse_one_family; use std::fmt; use std::iter; diff --git a/components/style/properties/gecko.mako.rs b/components/style/properties/gecko.mako.rs index 86a1bbc9e5f..036f545a82c 100644 --- a/components/style/properties/gecko.mako.rs +++ b/components/style/properties/gecko.mako.rs @@ -1089,10 +1089,10 @@ fn static_assert() { #[allow(non_snake_case)] pub fn set__moz_binding(&mut self, v: longhands::_moz_binding::computed_value::T) { - use properties::longhands::_moz_binding::computed_value::T as BindingValue; + use values::Either; match v { - BindingValue::None => debug_assert!(self.gecko.mBinding.mRawPtr.is_null()), - BindingValue::Url(ref url) => { + Either::Second(_none) => debug_assert!(self.gecko.mBinding.mRawPtr.is_null()), + Either::First(ref url) => { let extra_data = url.extra_data(); let (ptr, len) = url.as_slice_components(); unsafe { @@ -1593,14 +1593,14 @@ fn static_assert() { skip_additionals="*"> pub fn set_list_style_image(&mut self, image: longhands::list_style_image::computed_value::T) { - use values::computed::UrlOrNone; + use values::Either; match image { - UrlOrNone::None => { + Either::Second(_none) => { unsafe { Gecko_SetListStyleImageNone(&mut self.gecko); } } - UrlOrNone::Url(ref url) => { + Either::First(ref url) => { let (ptr, len) = url.as_slice_components(); let extra_data = url.extra_data(); unsafe { diff --git a/components/style/properties/longhand/box.mako.rs b/components/style/properties/longhand/box.mako.rs index 39f859f2e65..c48d4948376 100644 --- a/components/style/properties/longhand/box.mako.rs +++ b/components/style/properties/longhand/box.mako.rs @@ -1551,7 +1551,7 @@ ${helpers.single_keyword("-moz-appearance", animatable=False)} // Non-standard: https://developer.mozilla.org/en-US/docs/Web/CSS/-moz-binding -${helpers.predefined_type("-moz-binding", "UrlOrNone", "computed_value::T::None", +${helpers.predefined_type("-moz-binding", "UrlOrNone", "Either::Second(None_)", products="gecko", animatable="False", disable_when_testing="True")} diff --git a/components/style/properties/longhand/list.mako.rs b/components/style/properties/longhand/list.mako.rs index 5419ca12935..d979685101d 100644 --- a/components/style/properties/longhand/list.mako.rs +++ b/components/style/properties/longhand/list.mako.rs @@ -26,7 +26,7 @@ ${helpers.single_keyword("list-style-type", """ gecko_constant_prefix="NS_STYLE_LIST_STYLE", animatable=False)} -${helpers.predefined_type("list-style-image", "UrlOrNone", "computed_value::T::None", +${helpers.predefined_type("list-style-image", "UrlOrNone", "Either::Second(None_)", animatable="False")} <%helpers:longhand name="quotes" animatable="False"> diff --git a/components/style/properties/shorthand/list.mako.rs b/components/style/properties/shorthand/list.mako.rs index 955d93f7c16..f425c71c9f7 100644 --- a/components/style/properties/shorthand/list.mako.rs +++ b/components/style/properties/shorthand/list.mako.rs @@ -7,6 +7,7 @@ <%helpers:shorthand name="list-style" sub_properties="list-style-image list-style-position list-style-type"> use properties::longhands::{list_style_image, list_style_position, list_style_type}; + use values::{Either, None_}; pub fn parse_value(context: &ParserContext, input: &mut Parser) -> Result { // `none` is ambiguous until we've finished parsing the shorthands, so we count the number @@ -56,7 +57,7 @@ (true, 2, None, None) => { Ok(Longhands { list_style_position: position, - list_style_image: Some(list_style_image::SpecifiedValue::None), + list_style_image: Some(Either::Second(None_)), list_style_type: Some(list_style_type::SpecifiedValue::none), }) } @@ -70,14 +71,14 @@ (true, 1, Some(list_style_type), None) => { Ok(Longhands { list_style_position: position, - list_style_image: Some(list_style_image::SpecifiedValue::None), + list_style_image: Some(Either::Second(None_)), list_style_type: Some(list_style_type), }) } (true, 1, None, None) => { Ok(Longhands { list_style_position: position, - list_style_image: Some(list_style_image::SpecifiedValue::None), + list_style_image: Some(Either::Second(None_)), list_style_type: Some(list_style_type::SpecifiedValue::none), }) } diff --git a/components/style/values/specified/mod.rs b/components/style/values/specified/mod.rs index ddfb44e03be..b71eb8140bf 100644 --- a/components/style/values/specified/mod.rs +++ b/components/style/values/specified/mod.rs @@ -5,14 +5,14 @@ use app_units::Au; use cssparser::{self, Parser, Token}; use euclid::size::Size2D; -use parser::{Parse, ParserContext}; +use parser::{ParserContext, Parse}; use self::url::SpecifiedUrl; use std::ascii::AsciiExt; use std::f32::consts::PI; use std::fmt; use std::ops::Mul; use style_traits::ToCss; -use super::{CSSFloat, HasViewportPercentage, NoViewportPercentage}; +use super::{CSSFloat, HasViewportPercentage, NoViewportPercentage, Either, None_}; use super::computed::{ComputedValueAsSpecified, Context, ToComputedValue}; pub use self::image::{AngleOrCorner, ColorStop, EndingShape as GradientEndingShape, Gradient}; @@ -515,31 +515,5 @@ impl ToCss for Opacity { } } -#[derive(PartialEq, Clone, Debug)] -#[cfg_attr(feature = "servo", derive(HeapSizeOf))] -pub enum UrlOrNone { - Url(SpecifiedUrl), - None, -} +pub type UrlOrNone = Either; -impl ComputedValueAsSpecified for UrlOrNone {} -impl NoViewportPercentage for UrlOrNone {} - -impl ToCss for UrlOrNone { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - UrlOrNone::Url(ref url) => url.to_css(dest), - UrlOrNone::None => dest.write_str("none"), - } - } -} - -impl UrlOrNone { - pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { - if input.try(|input| input.expect_ident_matching("none")).is_ok() { - return Ok(UrlOrNone::None); - } - - Ok(UrlOrNone::Url(try!(SpecifiedUrl::parse(context, input)))) - } -} diff --git a/components/style/values/specified/url.rs b/components/style/values/specified/url.rs index 08a13be04f6..d3100586c52 100644 --- a/components/style/values/specified/url.rs +++ b/components/style/values/specified/url.rs @@ -7,7 +7,7 @@ use cssparser::{CssStringWriter, Parser}; #[cfg(feature = "gecko")] use gecko_bindings::sugar::refptr::{GeckoArcPrincipal, GeckoArcURI}; -use parser::ParserContext; +use parser::{Parse, ParserContext}; #[cfg(feature = "gecko")] use parser::ParserContextExtraData; use servo_url::ServoUrl; @@ -15,6 +15,7 @@ use std::fmt::{self, Write}; use std::ptr; use std::sync::Arc; use style_traits::ToCss; +use values::NoViewportPercentage; use values::computed::ComputedValueAsSpecified; #[derive(PartialEq, Clone, Debug)] @@ -72,8 +73,8 @@ pub struct SpecifiedUrl { extra_data: UrlExtraData, } -impl SpecifiedUrl { - pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { +impl Parse for SpecifiedUrl { + fn parse(context: &ParserContext, input: &mut Parser) -> Result { let url = try!(input.expect_url()); let extra_data = match UrlExtraData::make_from(context) { @@ -95,7 +96,9 @@ impl SpecifiedUrl { extra_data: extra_data, }) } +} +impl SpecifiedUrl { pub fn extra_data(&self) -> &UrlExtraData { &self.extra_data } @@ -171,3 +174,5 @@ impl ToCss for SpecifiedUrl { // TODO(emilio): Maybe consider ComputedUrl to save a word in style structs? impl ComputedValueAsSpecified for SpecifiedUrl {} + +impl NoViewportPercentage for SpecifiedUrl {} diff --git a/tests/unit/style/properties/serialization.rs b/tests/unit/style/properties/serialization.rs index 96b0004b843..34a00deeb2b 100644 --- a/tests/unit/style/properties/serialization.rs +++ b/tests/unit/style/properties/serialization.rs @@ -418,9 +418,9 @@ mod shorthand_serialization { } mod list_style { - use style::properties::longhands::list_style_image::SpecifiedValue as ListStyleImage; use style::properties::longhands::list_style_position::computed_value::T as ListStylePosition; use style::properties::longhands::list_style_type::computed_value::T as ListStyleType; + use style::values::Either; use super::*; #[test] @@ -428,7 +428,7 @@ mod shorthand_serialization { let mut properties = Vec::new(); let position = DeclaredValue::Value(ListStylePosition::inside); - let image = DeclaredValue::Value(ListStyleImage::Url( + let image = DeclaredValue::Value(Either::First( SpecifiedUrl::new_for_testing("http://servo/test.png"))); let style_type = DeclaredValue::Value(ListStyleType::disc);