diff --git a/components/style/lib.rs b/components/style/lib.rs index 2b31dc0f779..53d4e97ee3f 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -182,15 +182,24 @@ pub fn arc_ptr_eq(a: &Arc, b: &Arc) -> bool { (a as *const T) == (b as *const T) } -pub fn serialize_comma_separated_list(dest: &mut W, list: &[T]) - -> fmt::Result where W: fmt::Write, T: ToCss { - if list.len() > 0 { - for item in &list[..list.len()-1] { - try!(item.to_css(dest)); - try!(write!(dest, ", ")); - } - list[list.len()-1].to_css(dest) - } else { - Ok(()) +/// Serializes as CSS a comma-separated list of any `T` that supports being +/// serialized as CSS. +pub fn serialize_comma_separated_list(dest: &mut W, + list: &[T]) + -> fmt::Result + where W: fmt::Write, + T: ToCss, +{ + if list.is_empty() { + return Ok(()); } + + try!(list[0].to_css(dest)); + + for item in list.iter().skip(1) { + try!(write!(dest, ", ")); + try!(item.to_css(dest)); + } + + Ok(()) } diff --git a/components/style/properties/declaration_block.rs b/components/style/properties/declaration_block.rs index de2d274280b..0cf96724dcb 100644 --- a/components/style/properties/declaration_block.rs +++ b/components/style/properties/declaration_block.rs @@ -405,9 +405,10 @@ pub fn append_serialization<'a, W, I, N>(dest: &mut W, importance: Importance, is_first_serialization: &mut bool) -> fmt::Result - where W: fmt::Write, - I: Iterator, - N: ToCss { + where W: fmt::Write, + I: Iterator, + N: ToCss +{ try!(handle_first_serialization(dest, is_first_serialization)); // Overflow does not behave like a normal shorthand. When overflow-x and overflow-y are not of equal @@ -525,4 +526,3 @@ pub fn parse_property_declaration_list(context: &ParserContext, input: &mut Pars super::deduplicate_property_declarations(&mut block); block } - diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index 5efeac958fe..83feda735c2 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -73,16 +73,21 @@ use values::{computed, specified}; ${caller.body()} } + + /// The definition of the computed value for ${name}. pub mod computed_value { pub use super::single_value::computed_value as single_value; pub use self::single_value::T as SingleComputedValue; + /// The computed value, effectively a list of single values. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct T(pub Vec); } impl ToCss for computed_value::T { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write, + { let mut iter = self.0.iter(); if let Some(val) = iter.next() { try!(val.to_css(dest)); @@ -101,12 +106,15 @@ } } + /// The specified value of ${name}. #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct SpecifiedValue(pub Vec); impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write, + { let mut iter = self.0.iter(); if let Some(val) = iter.next() { try!(val.to_css(dest)); @@ -405,7 +413,11 @@ } impl<'a> LonghandsToSerialize<'a> { - pub fn from_iter>(iter: I) -> Result { + /// Tries to get a serializable set of longhands given a set of + /// property declarations. + pub fn from_iter(iter: I) -> Result + where I: Iterator, + { // Define all of the expected variables that correspond to the shorthand % for sub_property in shorthand.sub_properties: let mut ${sub_property.ident} = None; @@ -446,7 +458,9 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write, + { let mut all_flags = SerializeFlags::all(); let mut with_variables = false; % for sub_property in shorthand.sub_properties: @@ -477,7 +491,10 @@ } - pub fn parse(context: &ParserContext, input: &mut Parser, + /// Parse the given shorthand and fill the result into the + /// `declarations` vector. + pub fn parse(context: &ParserContext, + input: &mut Parser, declarations: &mut Vec) -> Result<(), ()> { input.look_for_var_functions(); diff --git a/components/style/properties/longhand/background.mako.rs b/components/style/properties/longhand/background.mako.rs index 645e9666c63..26f611bd33b 100644 --- a/components/style/properties/longhand/background.mako.rs +++ b/components/style/properties/longhand/background.mako.rs @@ -87,89 +87,102 @@ ${helpers.predefined_type("background-color", "CSSColor", <%helpers:vector_longhand name="background-position-x" animatable="True"> - use std::fmt; - use style_traits::ToCss; - use values::HasViewportPercentage; - use values::specified::position::HorizontalPosition; + use std::fmt; + use style_traits::ToCss; + use values::HasViewportPercentage; + use values::specified::position::HorizontalPosition; - pub mod computed_value { - use values::computed::position::HorizontalPosition; - use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; + #[allow(missing_docs)] + pub mod computed_value { + use values::computed::position::HorizontalPosition; + use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; - pub type T = HorizontalPosition; - } + pub type T = HorizontalPosition; + } - pub type SpecifiedValue = HorizontalPosition; + #[allow(missing_docs)] + pub type SpecifiedValue = HorizontalPosition; - #[inline] - pub fn get_initial_value() -> computed_value::T { - use values::computed::position::HorizontalPosition; - HorizontalPosition(computed::LengthOrPercentage::Percentage(0.0)) + #[inline] + #[allow(missing_docs)] + pub fn get_initial_value() -> computed_value::T { + use values::computed::position::HorizontalPosition; + HorizontalPosition(computed::LengthOrPercentage::Percentage(0.0)) + } + #[inline] + #[allow(missing_docs)] + pub fn get_initial_specified_value() -> SpecifiedValue { + use values::specified::position::Keyword; + HorizontalPosition { + keyword: Some(Keyword::Left), + position: None, } - #[inline] - pub fn get_initial_specified_value() -> SpecifiedValue { - use values::specified::position::Keyword; - HorizontalPosition { - keyword: Some(Keyword::Left), - position: None, - } - } - #[inline] - pub fn get_initial_position_value() -> SpecifiedValue { - use values::specified::{LengthOrPercentage, Percentage}; - HorizontalPosition { - keyword: None, - position: Some(LengthOrPercentage::Percentage(Percentage(0.0))), - } + } + #[inline] + #[allow(missing_docs)] + pub fn get_initial_position_value() -> SpecifiedValue { + use values::specified::{LengthOrPercentage, Percentage}; + HorizontalPosition { + keyword: None, + position: Some(LengthOrPercentage::Percentage(Percentage(0.0))), } + } - pub fn parse(context: &ParserContext, input: &mut Parser) - -> Result { - HorizontalPosition::parse(context, input) - } + #[allow(missing_docs)] + pub fn parse(context: &ParserContext, input: &mut Parser) + -> Result { + HorizontalPosition::parse(context, input) + } <%helpers:vector_longhand name="background-position-y" animatable="True"> - use std::fmt; - use style_traits::ToCss; - use values::HasViewportPercentage; - use values::specified::position::VerticalPosition; + use std::fmt; + use style_traits::ToCss; + use values::HasViewportPercentage; + use values::specified::position::VerticalPosition; - pub mod computed_value { - use values::computed::position::VerticalPosition; - use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; + #[allow(missing_docs)] + pub mod computed_value { + use values::computed::position::VerticalPosition; + use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; - pub type T = VerticalPosition; - } + pub type T = VerticalPosition; + } - pub type SpecifiedValue = VerticalPosition; + #[allow(missing_docs)] + pub type SpecifiedValue = VerticalPosition; - #[inline] - pub fn get_initial_value() -> computed_value::T { - use values::computed::position::VerticalPosition; - VerticalPosition(computed::LengthOrPercentage::Percentage(0.0)) + #[inline] + #[allow(missing_docs)] + pub fn get_initial_value() -> computed_value::T { + use values::computed::position::VerticalPosition; + VerticalPosition(computed::LengthOrPercentage::Percentage(0.0)) + } + #[inline] + #[allow(missing_docs)] + pub fn get_initial_specified_value() -> SpecifiedValue { + use values::specified::position::Keyword; + VerticalPosition { + keyword: Some(Keyword::Top), + position: None, } - #[inline] - pub fn get_initial_specified_value() -> SpecifiedValue { - use values::specified::position::Keyword; - VerticalPosition { - keyword: Some(Keyword::Top), - position: None, - } - } - #[inline] - pub fn get_initial_position_value() -> SpecifiedValue { - use values::specified::{LengthOrPercentage, Percentage}; - VerticalPosition { - keyword: None, - position: Some(LengthOrPercentage::Percentage(Percentage(0.0))), - } + } + #[inline] + #[allow(missing_docs)] + pub fn get_initial_position_value() -> SpecifiedValue { + use values::specified::{LengthOrPercentage, Percentage}; + VerticalPosition { + keyword: None, + position: Some(LengthOrPercentage::Percentage(Percentage(0.0))), } + } - pub fn parse(context: &ParserContext, input: &mut Parser) - -> Result { - VerticalPosition::parse(context, input) - } + #[inline] + #[allow(missing_docs)] + pub fn parse(context: &ParserContext, input: &mut Parser) + -> Result { + VerticalPosition::parse(context, input) + } ${helpers.single_keyword("background-repeat", @@ -199,6 +212,7 @@ ${helpers.single_keyword("background-origin", use style_traits::ToCss; use values::HasViewportPercentage; + #[allow(missing_docs)] pub mod computed_value { use values::computed::LengthOrPercentageOrAuto; use properties::animated_properties::{Interpolate, RepeatableListInterpolate}; @@ -254,6 +268,7 @@ ${helpers.single_keyword("background-origin", #[derive(Clone, PartialEq, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + #[allow(missing_docs)] pub struct ExplicitSize { pub width: specified::LengthOrPercentageOrAuto, pub height: specified::LengthOrPercentageOrAuto, diff --git a/components/style/properties/longhand/box.mako.rs b/components/style/properties/longhand/box.mako.rs index c7782eeb0e6..3b5cd6bba4c 100644 --- a/components/style/properties/longhand/box.mako.rs +++ b/components/style/properties/longhand/box.mako.rs @@ -46,7 +46,8 @@ impl ToCss for T { fn to_css(&self, dest: &mut W) -> ::std::fmt::Result - where W: ::std::fmt::Write { + where W: ::std::fmt::Write, + { match *self { % for value in values: T::${to_rust_ident(value)} => dest.write_str("${value}"), @@ -55,9 +56,14 @@ } } } - #[inline] pub fn get_initial_value() -> computed_value::T { + + /// The initial display value. + #[inline] + pub fn get_initial_value() -> computed_value::T { computed_value::T::${to_rust_ident(values[0])} } + + /// Parse a display value. pub fn parse(_context: &ParserContext, input: &mut Parser) -> Result { match_ignore_ascii_case! { try!(input.expect_ident()), @@ -144,119 +150,128 @@ ${helpers.single_keyword("clear", "none left right both", -<%helpers:longhand name="vertical-align" - animatable="True"> - use std::fmt; - use style_traits::ToCss; - use values::HasViewportPercentage; +<%helpers:longhand name="vertical-align" animatable="True"> + use std::fmt; + use style_traits::ToCss; + use values::HasViewportPercentage; - <% vertical_align = data.longhands_by_name["vertical-align"] %> - <% vertical_align.keyword = Keyword("vertical-align", - "baseline sub super top text-top middle bottom text-bottom", - extra_gecko_values="middle-with-baseline") %> - <% vertical_align_keywords = vertical_align.keyword.values_for(product) %> + <% vertical_align = data.longhands_by_name["vertical-align"] %> + <% vertical_align.keyword = Keyword("vertical-align", + "baseline sub super top text-top middle bottom text-bottom", + extra_gecko_values="middle-with-baseline") %> + <% vertical_align_keywords = vertical_align.keyword.values_for(product) %> - impl HasViewportPercentage for SpecifiedValue { - fn has_viewport_percentage(&self) -> bool { - match *self { - SpecifiedValue::LengthOrPercentage(length) => length.has_viewport_percentage(), - _ => false - } - } - } + impl HasViewportPercentage for SpecifiedValue { + fn has_viewport_percentage(&self) -> bool { + match *self { + SpecifiedValue::LengthOrPercentage(length) => length.has_viewport_percentage(), + _ => false + } + } + } - #[allow(non_camel_case_types)] - #[derive(Debug, Clone, PartialEq, Copy)] - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub enum SpecifiedValue { - % for keyword in vertical_align_keywords: - ${to_rust_ident(keyword)}, - % endfor - LengthOrPercentage(specified::LengthOrPercentage), - } + /// The `vertical-align` value. + #[allow(non_camel_case_types)] + #[derive(Debug, Clone, PartialEq, Copy)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + pub enum SpecifiedValue { + % for keyword in vertical_align_keywords: + ${to_rust_ident(keyword)}, + % endfor + LengthOrPercentage(specified::LengthOrPercentage), + } - impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - % for keyword in vertical_align_keywords: - SpecifiedValue::${to_rust_ident(keyword)} => dest.write_str("${keyword}"), - % endfor - SpecifiedValue::LengthOrPercentage(value) => value.to_css(dest), - } - } - } - /// baseline | sub | super | top | text-top | middle | bottom | text-bottom - /// | | - pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { - input.try(|i| specified::LengthOrPercentage::parse(context, i)) - .map(SpecifiedValue::LengthOrPercentage) - .or_else(|()| { - match_ignore_ascii_case! { try!(input.expect_ident()), - % for keyword in vertical_align_keywords: - "${keyword}" => Ok(SpecifiedValue::${to_rust_ident(keyword)}), - % endfor - _ => Err(()) - } - }) - } - pub mod computed_value { - use app_units::Au; - use std::fmt; - use style_traits::ToCss; - use values::{CSSFloat, computed}; - #[allow(non_camel_case_types)] - #[derive(PartialEq, Copy, Clone, Debug)] - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub enum T { - % for keyword in vertical_align_keywords: - ${to_rust_ident(keyword)}, - % endfor - LengthOrPercentage(computed::LengthOrPercentage), - } - impl ToCss for T { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - match *self { - % for keyword in vertical_align_keywords: - T::${to_rust_ident(keyword)} => dest.write_str("${keyword}"), - % endfor - T::LengthOrPercentage(value) => value.to_css(dest), - } - } - } - } - #[inline] - pub fn get_initial_value() -> computed_value::T { computed_value::T::baseline } + impl ToCss for SpecifiedValue { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + % for keyword in vertical_align_keywords: + SpecifiedValue::${to_rust_ident(keyword)} => dest.write_str("${keyword}"), + % endfor + SpecifiedValue::LengthOrPercentage(value) => value.to_css(dest), + } + } + } + /// baseline | sub | super | top | text-top | middle | bottom | text-bottom + /// | | + pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { + input.try(|i| specified::LengthOrPercentage::parse(context, i)) + .map(SpecifiedValue::LengthOrPercentage) + .or_else(|_| { + match_ignore_ascii_case! { try!(input.expect_ident()), + % for keyword in vertical_align_keywords: + "${keyword}" => Ok(SpecifiedValue::${to_rust_ident(keyword)}), + % endfor + _ => Err(()) + } + }) + } - impl ToComputedValue for SpecifiedValue { - type ComputedValue = computed_value::T; + /// The computed value for `vertical-align`. + pub mod computed_value { + use app_units::Au; + use std::fmt; + use style_traits::ToCss; + use values::{CSSFloat, computed}; - #[inline] - fn to_computed_value(&self, context: &Context) -> computed_value::T { - match *self { - % for keyword in vertical_align_keywords: - SpecifiedValue::${to_rust_ident(keyword)} => { - computed_value::T::${to_rust_ident(keyword)} - } - % endfor - SpecifiedValue::LengthOrPercentage(value) => - computed_value::T::LengthOrPercentage(value.to_computed_value(context)), - } - } - #[inline] - fn from_computed_value(computed: &computed_value::T) -> Self { - match *computed { - % for keyword in vertical_align_keywords: - computed_value::T::${to_rust_ident(keyword)} => { - SpecifiedValue::${to_rust_ident(keyword)} - } - % endfor - computed_value::T::LengthOrPercentage(value) => - SpecifiedValue::LengthOrPercentage( - ToComputedValue::from_computed_value(&value) - ), - } - } - } + /// The keywords are the same, and the `LengthOrPercentage` is computed + /// here. + #[allow(non_camel_case_types)] + #[derive(PartialEq, Copy, Clone, Debug)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + pub enum T { + % for keyword in vertical_align_keywords: + ${to_rust_ident(keyword)}, + % endfor + LengthOrPercentage(computed::LengthOrPercentage), + } + impl ToCss for T { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + match *self { + % for keyword in vertical_align_keywords: + T::${to_rust_ident(keyword)} => dest.write_str("${keyword}"), + % endfor + T::LengthOrPercentage(value) => value.to_css(dest), + } + } + } + } + + /// The initial computed value for `vertical-align`. + #[inline] + pub fn get_initial_value() -> computed_value::T { + computed_value::T::baseline + } + + impl ToComputedValue for SpecifiedValue { + type ComputedValue = computed_value::T; + + #[inline] + fn to_computed_value(&self, context: &Context) -> computed_value::T { + match *self { + % for keyword in vertical_align_keywords: + SpecifiedValue::${to_rust_ident(keyword)} => { + computed_value::T::${to_rust_ident(keyword)} + } + % endfor + SpecifiedValue::LengthOrPercentage(value) => + computed_value::T::LengthOrPercentage(value.to_computed_value(context)), + } + } + #[inline] + fn from_computed_value(computed: &computed_value::T) -> Self { + match *computed { + % for keyword in vertical_align_keywords: + computed_value::T::${to_rust_ident(keyword)} => { + SpecifiedValue::${to_rust_ident(keyword)} + } + % endfor + computed_value::T::LengthOrPercentage(value) => + SpecifiedValue::LengthOrPercentage( + ToComputedValue::from_computed_value(&value) + ), + } + } + } @@ -275,44 +290,51 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto", gecko_constant_prefix="NS_STYLE_OVERFLOW")} // FIXME(pcwalton, #2742): Implement scrolling for `scroll` and `auto`. -<%helpers:longhand name="overflow-y" - need_clone="True" - animatable="False"> - use super::overflow_x; +<%helpers:longhand name="overflow-y" need_clone="True" animatable="False"> + use super::overflow_x; - use std::fmt; - use style_traits::ToCss; - use values::computed::ComputedValueAsSpecified; - use values::NoViewportPercentage; + use std::fmt; + use style_traits::ToCss; + use values::computed::ComputedValueAsSpecified; + use values::NoViewportPercentage; - pub use self::computed_value::T as SpecifiedValue; + pub use self::computed_value::T as SpecifiedValue; - impl NoViewportPercentage for SpecifiedValue {} + impl NoViewportPercentage for SpecifiedValue {} - impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - self.0.to_css(dest) - } - } + impl ToCss for SpecifiedValue { + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + self.0.to_css(dest) + } + } - pub mod computed_value { - #[derive(Debug, Clone, Copy, PartialEq)] - #[cfg_attr(feature = "servo", derive(HeapSizeOf))] - pub struct T(pub super::super::overflow_x::computed_value::T); - } - impl ComputedValueAsSpecified for SpecifiedValue {} + /// The specified and computed value for overflow-y is a wrapper on top of + /// `overflow-x`, so we re-use the logic, but prevent errors from mistakenly + /// assign one to other. + /// + /// TODO(Manishearth, emilio): We may want to just use the same value. + pub mod computed_value { + #[derive(Debug, Clone, Copy, PartialEq)] + #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + pub struct T(pub super::super::overflow_x::computed_value::T); + } - pub fn get_initial_value() -> computed_value::T { - computed_value::T(overflow_x::get_initial_value()) - } + impl ComputedValueAsSpecified for SpecifiedValue {} - pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { - overflow_x::parse(context, input).map(SpecifiedValue) - } + #[inline] + #[allow(missing_docs)] + pub fn get_initial_value() -> computed_value::T { + computed_value::T(overflow_x::get_initial_value()) + } + + #[inline] + #[allow(missing_docs)] + pub fn parse(context: &ParserContext, input: &mut Parser) -> Result { + overflow_x::parse(context, input).map(SpecifiedValue) + } -// TODO(pcwalton): Multiple transitions. <%helpers:longhand name="transition-duration" need_index="True" animatable="False"> @@ -369,7 +391,6 @@ ${helpers.single_keyword("overflow-x", "visible hidden scroll auto", // TODO(pcwalton): Lots more timing functions. -// TODO(pcwalton): Multiple transitions. <%helpers:longhand name="transition-timing-function" need_index="True" animatable="False"> diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index ee9ae6a4470..a247594ac58 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -315,7 +315,10 @@ ${helpers.single_keyword("font-variant-caps", use app_units::Au; pub type T = Au; } - #[inline] pub fn get_initial_value() -> computed_value::T { + + #[inline] + #[allow(missing_docs)] + pub fn get_initial_value() -> computed_value::T { Au::from_px(FONT_MEDIUM_PX) } diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index d9f55241623..5d1bf766c8a 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -160,8 +160,8 @@ pub mod shorthands { /// A module with all the code related to animated properties. /// -/// This needs to be loaded at least after all longhand modules, given they -/// populate the global data. +/// This needs to be "included" by mako at least after all longhand modules, +/// given they populate the global data. pub mod animated_properties { #![deny(missing_docs)] @@ -467,15 +467,21 @@ impl ShorthandId { } } - /// Serializes possible shorthand name with value to input buffer given a list of longhand declarations. - /// On success, returns true if shorthand value is written and false if no shorthand value is present. + /// Serializes the possible shorthand name with value to input buffer given + /// a list of longhand declarations. + /// + /// On success, returns true if the shorthand value is written, or false if + /// no shorthand value is present. pub fn serialize_shorthand_to_buffer<'a, W, I>(self, dest: &mut W, declarations: I, is_first_serialization: &mut bool, importance: Importance) -> Result - where W: Write, I: IntoIterator, I::IntoIter: Clone { + where W: Write, + I: IntoIterator, + I::IntoIter: Clone, + { match self.get_shorthand_appendable_value(declarations) { None => Ok(false), Some(appendable_value) => { @@ -490,60 +496,73 @@ impl ShorthandId { } } - fn get_shorthand_appendable_value<'a, I>(self, declarations: I) + fn get_shorthand_appendable_value<'a, I>(self, + declarations: I) -> Option> - where I: IntoIterator, I::IntoIter: Clone { - let declarations = declarations.into_iter(); + where I: IntoIterator, + I::IntoIter: Clone, + { + let declarations = declarations.into_iter(); - // Only cloning iterators (a few pointers each) not declarations. - let mut declarations2 = declarations.clone(); - let mut declarations3 = declarations.clone(); + // Only cloning iterators (a few pointers each) not declarations. + let mut declarations2 = declarations.clone(); + let mut declarations3 = declarations.clone(); - let first_declaration = match declarations2.next() { - Some(declaration) => declaration, - None => return None - }; + let first_declaration = match declarations2.next() { + Some(declaration) => declaration, + None => return None + }; - // https://drafts.csswg.org/css-variables/#variables-in-shorthands - if let Some(css) = first_declaration.with_variables_from_shorthand(self) { - if declarations2.all(|d| d.with_variables_from_shorthand(self) == Some(css)) { - return Some(AppendableValue::Css(css)); - } - else { - return None; - } - } + // https://drafts.csswg.org/css-variables/#variables-in-shorthands + if let Some(css) = first_declaration.with_variables_from_shorthand(self) { + if declarations2.all(|d| d.with_variables_from_shorthand(self) == Some(css)) { + return Some(AppendableValue::Css(css)); + } + return None; + } - if !declarations3.any(|d| d.with_variables()) { - return Some(AppendableValue::DeclarationsForShorthand(self, declarations)); - } + if !declarations3.any(|d| d.with_variables()) { + return Some(AppendableValue::DeclarationsForShorthand(self, declarations)); + } - None + None } } +/// Servo's representation of a declared value for a given `T`, which is the +/// declared value for that property. #[derive(Clone, PartialEq, Eq, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum DeclaredValue { + /// A known specified value from the stylesheet. Value(T), + /// A value that contained any css variables. WithVariables { + /// The css serialization for this value. css: String, + /// The first token type for this serialization. first_token_type: TokenSerializationType, + /// The base url. base_url: ServoUrl, + /// The shorthand this came from. from_shorthand: Option, }, + /// The `initial` keyword. Initial, + /// The `inherit` keyword. Inherit, + /// The `unset` keyword. Unset, } impl HasViewportPercentage for DeclaredValue { fn has_viewport_percentage(&self) -> bool { match *self { - DeclaredValue::Value(ref v) - => v.has_viewport_percentage(), - DeclaredValue::WithVariables { .. } - => panic!("DeclaredValue::has_viewport_percentage without resolving variables!"), + DeclaredValue::Value(ref v) => v.has_viewport_percentage(), + DeclaredValue::WithVariables { .. } => { + panic!("DeclaredValue::has_viewport_percentage without \ + resolving variables!") + }, DeclaredValue::Initial | DeclaredValue::Inherit | DeclaredValue::Unset => false, @@ -621,7 +640,9 @@ impl fmt::Debug for PropertyId { } impl ToCss for PropertyId { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write, + { match *self { PropertyId::Longhand(id) => dest.write_str(id.name()), PropertyId::Shorthand(id) => dest.write_str(id.name()), @@ -1156,19 +1177,22 @@ pub struct ComputedValues { % endfor custom_properties: Option>, shareable: bool, + /// The writing mode of this computed values struct. pub writing_mode: WritingMode, + /// The root element's computed font size. pub root_font_size: Au, } #[cfg(feature = "servo")] impl ComputedValues { + /// Construct a `ComputedValues` instance. pub fn new(custom_properties: Option>, - shareable: bool, - writing_mode: WritingMode, - root_font_size: Au, - % for style_struct in data.active_style_structs(): - ${style_struct.ident}: Arc, - % endfor + shareable: bool, + writing_mode: WritingMode, + root_font_size: Au, + % for style_struct in data.active_style_structs(): + ${style_struct.ident}: Arc, + % endfor ) -> Self { ComputedValues { custom_properties: custom_properties, @@ -1821,16 +1845,21 @@ pub fn apply_declarations<'a, F, I>(viewport_size: Size2D, } if seen.get_font_style() || seen.get_font_weight() || seen.get_font_stretch() || - seen.get_font_family() { + seen.get_font_family() { style.mutate_font().compute_font_hash(); } style } +/// Modifies the style for an anonymous flow so it resets all its non-inherited +/// style structs, and set their borders and outlines to zero. +/// +/// Also, it gets a new display value, which is honored except when it's +/// `inline`. #[cfg(feature = "servo")] pub fn modify_style_for_anonymous_flow(style: &mut Arc, - new_display_value: longhands::display::computed_value::T) { + new_display_value: longhands::display::computed_value::T) { // The 'align-self' property needs some special treatment since // its value depends on the 'align-items' value of its parent. % if "align-items" in data.longhands_by_name: @@ -1870,12 +1899,17 @@ pub fn modify_style_for_anonymous_flow(style: &mut Arc, outline.outline_width = Au(0); } -/// Alters the given style to accommodate replaced content. This is called in flow construction. It -/// handles cases like `
foo bar baz
` (in which `foo`, `bar`, -/// and `baz` must not be absolutely-positioned) and cases like `Foo` (in which the -/// `vertical-align: top` style of `sup` must not propagate down into `Foo`). +/// Alters the given style to accommodate replaced content. This is called in +/// flow construction. It handles cases like: /// -/// FIXME(#5625, pcwalton): It would probably be cleaner and faster to do this in the cascade. +///
foo bar baz
+/// +/// (in which `foo`, `bar`, and `baz` must not be absolutely-positioned) and +/// cases like `Foo` (in which the `vertical-align: top` style of +/// `sup` must not propagate down into `Foo`). +/// +/// FIXME(#5625, pcwalton): It would probably be cleaner and faster to do this +/// in the cascade. #[cfg(feature = "servo")] #[inline] pub fn modify_style_for_replaced_content(style: &mut Arc) { @@ -1908,11 +1942,11 @@ pub fn modify_style_for_replaced_content(style: &mut Arc) { } } -/// Adjusts borders as appropriate to account for a fragment's status as the first or last fragment -/// within the range of an element. +/// Adjusts borders as appropriate to account for a fragment's status as the +/// first or last fragment within the range of an element. /// -/// Specifically, this function sets border widths to zero on the sides for which the fragment is -/// not outermost. +/// Specifically, this function sets border widths to zero on the sides for +/// which the fragment is not outermost. #[cfg(feature = "servo")] #[inline] pub fn modify_border_style_for_inline_sides(style: &mut Arc, @@ -1964,7 +1998,8 @@ pub fn modify_border_style_for_inline_sides(style: &mut Arc, } } -/// Adjusts the `position` property as necessary for the outer fragment wrapper of an inline-block. +/// Adjusts the `position` property as necessary for the outer fragment wrapper +/// of an inline-block. #[cfg(feature = "servo")] #[inline] pub fn modify_style_for_outer_inline_block_fragment(style: &mut Arc) { @@ -1973,10 +2008,11 @@ pub fn modify_style_for_outer_inline_block_fragment(style: &mut Arc) { @@ -2010,8 +2046,8 @@ pub fn modify_style_for_text(style: &mut Arc) { } } -/// Adjusts the `clip` property so that an inline absolute hypothetical fragment doesn't clip its -/// children. +/// Adjusts the `clip` property so that an inline absolute hypothetical fragment +/// doesn't clip its children. #[cfg(feature = "servo")] pub fn modify_style_for_inline_absolute_hypothetical_fragment(style: &mut Arc) { if style.get_effects().clip.0.is_some() { diff --git a/components/style/properties/shorthand/serialize.mako.rs b/components/style/properties/shorthand/serialize.mako.rs index 3dbf39a8ac3..e8b7a76b0cf 100644 --- a/components/style/properties/shorthand/serialize.mako.rs +++ b/components/style/properties/shorthand/serialize.mako.rs @@ -7,8 +7,16 @@ use style_traits::ToCss; use values::specified::{BorderStyle, CSSColor}; use std::fmt; -pub fn serialize_four_sides(dest: &mut W, top: &I, right: &I, bottom: &I, left: &I) - -> fmt::Result where W: fmt::Write, I: ToCss + PartialEq { +#[allow(missing_docs)] +pub fn serialize_four_sides(dest: &mut W, + top: &I, + right: &I, + bottom: &I, + left: &I) + -> fmt::Result + where W: fmt::Write, + I: ToCss + PartialEq, +{ if left == right { let horizontal_value = left; @@ -85,8 +93,10 @@ fn serialize_directional_border(dest: &mut W, } +#[allow(missing_docs)] pub fn is_overflow_shorthand<'a, I>(appendable_value: &AppendableValue<'a, I>) -> bool - where I: Iterator { + where I: Iterator +{ if let AppendableValue::DeclarationsForShorthand(shorthand, _) = *appendable_value { if let ShorthandId::Overflow = shorthand { return true; diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index bc573eb4bfe..dcfa897bfde 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -268,7 +268,8 @@ impl CssRule { /// Note that only some types of rules can contain rules. An empty slice is /// used for others. pub fn with_nested_rules_and_mq(&self, mut f: F) -> R - where F: FnMut(&[CssRule], Option<&MediaList>) -> R { + where F: FnMut(&[CssRule], Option<&MediaList>) -> R + { match *self { CssRule::Import(ref lock) => { let rule = lock.read(); diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index a22a799206f..84743325d12 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -72,9 +72,13 @@ pub trait ToComputedValue { fn from_computed_value(computed: &Self::ComputedValue) -> Self; } +/// A marker trait to represent that the specified value is also the computed +/// value. pub trait ComputedValueAsSpecified {} -impl ToComputedValue for T where T: ComputedValueAsSpecified + Clone { +impl ToComputedValue for T + where T: ComputedValueAsSpecified + Clone, +{ type ComputedValue = T; #[inline] diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 3be4d742eff..ea073b86f11 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -34,7 +34,8 @@ macro_rules! define_numbered_css_keyword_enum { impl ToCss for $name { fn to_css(&self, dest: &mut W) -> ::std::fmt::Result - where W: ::std::fmt::Write { + where W: ::std::fmt::Write, + { match *self { $( $name::$variant => dest.write_str($css) ),+ } @@ -46,17 +47,26 @@ macro_rules! define_numbered_css_keyword_enum { pub mod computed; pub mod specified; +/// A CSS float value. pub type CSSFloat = f32; +/// The default font size. pub const FONT_MEDIUM_PX: i32 = 16; +/// A trait used to represent whether this value has viewport units. pub trait HasViewportPercentage { + /// Returns true if this value has viewport units. fn has_viewport_percentage(&self) -> bool; } +/// A trait used as a marker to represent that a given value has no viewport +/// units. pub trait NoViewportPercentage {} -impl HasViewportPercentage for T where T: NoViewportPercentage { +impl HasViewportPercentage for T + where T: NoViewportPercentage, +{ + #[inline] fn has_viewport_percentage(&self) -> bool { false } diff --git a/components/style/values/specified/length.rs b/components/style/values/specified/length.rs index 40fb57a4963..e20b79ad82f 100644 --- a/components/style/values/specified/length.rs +++ b/components/style/values/specified/length.rs @@ -23,15 +23,22 @@ pub use super::image::{SizeKeyword, VerticalDirection}; #[derive(Clone, PartialEq, Copy, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] +/// A font relative length. pub enum FontRelativeLength { + /// A "em" value: https://drafts.csswg.org/css-values/#em Em(CSSFloat), + /// A "ex" value: https://drafts.csswg.org/css-values/#ex Ex(CSSFloat), + /// A "ch" value: https://drafts.csswg.org/css-values/#ch Ch(CSSFloat), + /// A "rem" value: https://drafts.csswg.org/css-values/#rem Rem(CSSFloat) } impl ToCss for FontRelativeLength { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut W) -> fmt::Result + where W: fmt::Write + { match *self { FontRelativeLength::Em(length) => write!(dest, "{}em", length), FontRelativeLength::Ex(length) => write!(dest, "{}ex", length), diff --git a/components/style/viewport.rs b/components/style/viewport.rs index 2e965da4e54..646a80398a4 100644 --- a/components/style/viewport.rs +++ b/components/style/viewport.rs @@ -127,7 +127,7 @@ pub enum ViewportLength { impl ToCss for ViewportLength { fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write + where W: fmt::Write, { match *self { ViewportLength::Specified(length) => length.to_css(dest), @@ -545,7 +545,9 @@ impl Cascade { } pub fn from_stylesheets<'a, I>(stylesheets: I, device: &Device) -> Self - where I: IntoIterator, I::Item: AsRef { + where I: IntoIterator, + I::Item: AsRef, + { let mut cascade = Self::new(); for stylesheet in stylesheets { stylesheet.as_ref().effective_viewport_rules(device, |rule| { @@ -581,10 +583,13 @@ impl Cascade { } } +/// Just a helper trait to be able to implement methods on ViewportConstraints. pub trait MaybeNew { + /// Create a ViewportConstraints from a viewport size and a `@viewport` + /// rule. fn maybe_new(initial_viewport: TypedSize2D, - rule: &ViewportRule) - -> Option; + rule: &ViewportRule) + -> Option; } impl MaybeNew for ViewportConstraints { diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs index b737b6e9b54..f89f442df57 100644 --- a/components/style_traits/values.rs +++ b/components/style_traits/values.rs @@ -95,11 +95,12 @@ macro_rules! __define_css_keyword_enum__actual { impl ToCss for $name { fn to_css(&self, dest: &mut W) -> ::std::fmt::Result - where W: ::std::fmt::Write { - match *self { - $( $name::$variant => dest.write_str($css) ),+ - } + where W: ::std::fmt::Write + { + match *self { + $( $name::$variant => dest.write_str($css) ),+ } + } } } }