1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 //! CSS handling for the computed value of 6 //! [`position`][position] values. 7 //! 8 //! [position]: https://drafts.csswg.org/css-backgrounds-3/#position 9 10 use crate::values::computed::{Integer, LengthPercentage, NonNegativeNumber, Percentage}; 11 use crate::values::generics::position::AspectRatio as GenericAspectRatio; 12 use crate::values::generics::position::Position as GenericPosition; 13 use crate::values::generics::position::PositionComponent as GenericPositionComponent; 14 use crate::values::generics::position::PositionOrAuto as GenericPositionOrAuto; 15 use crate::values::generics::position::ZIndex as GenericZIndex; 16 pub use crate::values::specified::position::{GridAutoFlow, GridTemplateAreas, MasonryAutoFlow}; 17 use crate::Zero; 18 use std::fmt::{self, Write}; 19 use style_traits::{CssWriter, ToCss}; 20 21 /// The computed value of a CSS `<position>` 22 pub type Position = GenericPosition<HorizontalPosition, VerticalPosition>; 23 24 /// The computed value of an `auto | <position>` 25 pub type PositionOrAuto = GenericPositionOrAuto<Position>; 26 27 /// The computed value of a CSS horizontal position. 28 pub type HorizontalPosition = LengthPercentage; 29 30 /// The computed value of a CSS vertical position. 31 pub type VerticalPosition = LengthPercentage; 32 33 impl Position { 34 /// `50% 50%` 35 #[inline] center() -> Self36 pub fn center() -> Self { 37 Self::new( 38 LengthPercentage::new_percent(Percentage(0.5)), 39 LengthPercentage::new_percent(Percentage(0.5)), 40 ) 41 } 42 43 /// `0% 0%` 44 #[inline] zero() -> Self45 pub fn zero() -> Self { 46 Self::new(LengthPercentage::zero(), LengthPercentage::zero()) 47 } 48 } 49 50 impl ToCss for Position { to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write,51 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result 52 where 53 W: Write, 54 { 55 self.horizontal.to_css(dest)?; 56 dest.write_str(" ")?; 57 self.vertical.to_css(dest) 58 } 59 } 60 61 impl GenericPositionComponent for LengthPercentage { is_center(&self) -> bool62 fn is_center(&self) -> bool { 63 match self.to_percentage() { 64 Some(Percentage(per)) => per == 0.5, 65 _ => false, 66 } 67 } 68 } 69 70 /// A computed value for the `z-index` property. 71 pub type ZIndex = GenericZIndex<Integer>; 72 73 /// A computed value for the `aspect-ratio` property. 74 pub type AspectRatio = GenericAspectRatio<NonNegativeNumber>; 75