/* 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 values. //! //! TODO(emilio): Enhance docs. use Namespace; use context::QuirksMode; use cssparser::{Parser, Token, serialize_identifier, BasicParseError}; use parser::{ParserContext, Parse}; use self::url::SpecifiedUrl; use std::ascii::AsciiExt; use std::borrow::Cow; use std::f32; use std::fmt; use style_traits::{ToCss, ParseError, StyleParseError}; use style_traits::values::specified::AllowedNumericType; use super::{Auto, CSSFloat, CSSInteger, Either, None_}; use super::computed::{self, Context, ToComputedValue}; use super::generics::grid::{TrackBreadth as GenericTrackBreadth, TrackSize as GenericTrackSize}; use super::generics::grid::TrackList as GenericTrackList; use values::computed::ComputedValueAsSpecified; use values::specified::calc::CalcNode; pub use properties::animated_properties::TransitionProperty; #[cfg(feature = "gecko")] pub use self::align::{AlignItems, AlignJustifyContent, AlignJustifySelf, JustifyItems}; pub use self::background::BackgroundSize; pub use self::border::{BorderCornerRadius, BorderImageSlice, BorderImageWidth}; pub use self::border::{BorderImageSideWidth, BorderRadius, BorderSideWidth}; pub use self::color::{Color, RGBAColor}; pub use self::effects::{BoxShadow, Filter, SimpleShadow}; pub use self::flex::FlexBasis; #[cfg(feature = "gecko")] pub use self::gecko::ScrollSnapPoint; pub use self::image::{ColorStop, EndingShape as GradientEndingShape, Gradient}; pub use self::image::{GradientItem, GradientKind, Image, ImageLayer, MozImageRect}; pub use self::length::{AbsoluteLength, CalcLengthOrPercentage, CharacterWidth}; pub use self::length::{FontRelativeLength, Length, LengthOrNone, LengthOrNumber}; pub use self::length::{LengthOrPercentage, LengthOrPercentageOrAuto}; pub use self::length::{LengthOrPercentageOrNone, MaxLength, MozLength}; pub use self::length::{NoCalcLength, Percentage, ViewportPercentageLength}; pub use self::rect::LengthOrNumberRect; pub use self::position::{Position, PositionComponent}; pub use self::text::{InitialLetter, LetterSpacing, LineHeight, WordSpacing}; pub use self::transform::{TimingFunction, TransformOrigin}; pub use super::generics::grid::GridLine; pub use super::generics::grid::GridTemplateComponent as GenericGridTemplateComponent; #[cfg(feature = "gecko")] pub mod align; pub mod background; pub mod basic_shape; pub mod border; pub mod calc; pub mod color; pub mod effects; pub mod flex; #[cfg(feature = "gecko")] pub mod gecko; pub mod grid; pub mod image; pub mod length; pub mod position; pub mod rect; pub mod text; pub mod transform; /// Common handling for the specified value CSS url() values. pub mod url { use cssparser::Parser; use parser::{Parse, ParserContext}; use style_traits::ParseError; use values::computed::ComputedValueAsSpecified; #[cfg(feature = "servo")] pub use ::servo::url::*; #[cfg(feature = "gecko")] pub use ::gecko::url::*; impl Parse for SpecifiedUrl { fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { let url = input.expect_url()?; Self::parse_from_string(url.into_owned(), context) } } impl Eq for SpecifiedUrl {} // TODO(emilio): Maybe consider ComputedUrl to save a word in style structs? impl ComputedValueAsSpecified for SpecifiedUrl {} no_viewport_percentage!(SpecifiedUrl); } /// Parse an `` value, handling `calc()` correctly. pub fn parse_integer<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { match input.next()? { Token::Number { int_value: Some(v), .. } => Ok(Integer::new(v)), Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { let result = input.parse_nested_block(|i| { CalcNode::parse_integer(context, i) })?; Ok(Integer::from_calc(result)) } t => Err(BasicParseError::UnexpectedToken(t).into()) } } /// Parse a `` value, handling `calc()` correctly, and without length /// limitations. pub fn parse_number<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { parse_number_with_clamping_mode(context, input, AllowedNumericType::All) } /// Parse a `` value, with a given clamping mode. pub fn parse_number_with_clamping_mode<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType) -> Result> { match input.next()? { Token::Number { value, .. } if clamping_mode.is_ok(context.parsing_mode, value) => { Ok(Number { value: value.min(f32::MAX).max(f32::MIN), calc_clamping_mode: None, }) }, Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { let result = input.parse_nested_block(|i| { CalcNode::parse_number(context, i) })?; Ok(Number { value: result.min(f32::MAX).max(f32::MIN), calc_clamping_mode: Some(clamping_mode), }) } t => Err(BasicParseError::UnexpectedToken(t).into()) } } #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf, Deserialize, Serialize))] /// An angle consisting of a value and a unit. /// /// Computed Angle is essentially same as specified angle except calc /// value serialization. Therefore we are using computed Angle enum /// to hold the value and unit type. pub struct Angle { value: computed::Angle, was_calc: bool, } impl ToCss for Angle { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { if self.was_calc { dest.write_str("calc(")?; } self.value.to_css(dest)?; if self.was_calc { dest.write_str(")")?; } Ok(()) } } impl ToComputedValue for Angle { type ComputedValue = computed::Angle; fn to_computed_value(&self, _context: &Context) -> Self::ComputedValue { self.value } fn from_computed_value(computed: &Self::ComputedValue) -> Self { Angle { value: *computed, was_calc: false, } } } impl Angle { /// Returns an angle with the given value in degrees. pub fn from_degrees(value: CSSFloat, was_calc: bool) -> Self { Angle { value: computed::Angle::Degree(value), was_calc: was_calc } } /// Returns an angle with the given value in gradians. pub fn from_gradians(value: CSSFloat, was_calc: bool) -> Self { Angle { value: computed::Angle::Gradian(value), was_calc: was_calc } } /// Returns an angle with the given value in turns. pub fn from_turns(value: CSSFloat, was_calc: bool) -> Self { Angle { value: computed::Angle::Turn(value), was_calc: was_calc } } /// Returns an angle with the given value in radians. pub fn from_radians(value: CSSFloat, was_calc: bool) -> Self { Angle { value: computed::Angle::Radian(value), was_calc: was_calc } } #[inline] #[allow(missing_docs)] pub fn radians(self) -> f32 { self.value.radians() } /// Returns an angle value that represents zero. pub fn zero() -> Self { Self::from_degrees(0.0, false) } /// Returns an `Angle` parsed from a `calc()` expression. pub fn from_calc(radians: CSSFloat) -> Self { Angle { value: computed::Angle::Radian(radians), was_calc: true, } } } impl Parse for Angle { /// Parses an angle according to CSS-VALUES § 6.1. fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { let token = input.next()?; match token { Token::Dimension { value, ref unit, .. } => { Angle::parse_dimension(value, unit, /* from_calc = */ false) } Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { return input.parse_nested_block(|i| CalcNode::parse_angle(context, i)) } _ => Err(()) }.map_err(|()| BasicParseError::UnexpectedToken(token).into()) } } impl Angle { /// Parse an `` value given a value and an unit. pub fn parse_dimension( value: CSSFloat, unit: &str, from_calc: bool) -> Result { let angle = match_ignore_ascii_case! { unit, "deg" => Angle::from_degrees(value, from_calc), "grad" => Angle::from_gradians(value, from_calc), "turn" => Angle::from_turns(value, from_calc), "rad" => Angle::from_radians(value, from_calc), _ => return Err(()) }; Ok(angle) } /// Parse an angle, including unitless 0 degree. /// /// Note that numbers without any AngleUnit, including unitless 0 angle, /// should be invalid. However, some properties still accept unitless 0 /// angle and stores it as '0deg'. /// /// We can remove this and get back to the unified version Angle::parse once /// https://github.com/w3c/csswg-drafts/issues/1162 is resolved. pub fn parse_with_unitless<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result> { let token = input.next()?; match token { Token::Dimension { value, ref unit, .. } => { Angle::parse_dimension(value, unit, /* from_calc = */ false) } Token::Number { value, .. } if value == 0. => Ok(Angle::zero()), Token::Function(ref name) if name.eq_ignore_ascii_case("calc") => { return input.parse_nested_block(|i| CalcNode::parse_angle(context, i)) } _ => Err(()) }.map_err(|()| BasicParseError::UnexpectedToken(token).into()) } } // The integer values here correspond to the border conflict resolution rules in CSS 2.1 § // 17.6.2.1. Higher values override lower values. define_numbered_css_keyword_enum! { BorderStyle: "none" => none = -1, "solid" => solid = 6, "double" => double = 7, "dotted" => dotted = 4, "dashed" => dashed = 5, "hidden" => hidden = -2, "groove" => groove = 1, "ridge" => ridge = 3, "inset" => inset = 0, "outset" => outset = 2, } no_viewport_percentage!(BorderStyle); impl BorderStyle { /// Whether this border style is either none or hidden. pub fn none_or_hidden(&self) -> bool { matches!(*self, BorderStyle::none | BorderStyle::hidden) } } /// A time in seconds according to CSS-VALUES § 6.2. #[derive(Clone, Copy, Debug, HasViewportPercentage, PartialEq, PartialOrd)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub struct Time { seconds: CSSFloat, was_calc: bool, } impl Time { /// Return a `