mirror of
https://github.com/servo/servo.git
synced 2025-10-18 17:29:18 +01:00
A specified position is now a struct made of two values of different types, the first one being PositionComponent<X>, and the second one PositionComponent<Y>. A position component is represented by the new enum PositionComponent<Side>, with the three values Center, Length(LengthOrPercentage), and Side(Side, Option<LengthOrPercentage>). Side keywords are represented by the X and Y enums, which don't include a value for the center keyword anymore. They are accompanied by the Side trait, which allows us to determine whether a side keyword is "left" or "top". This refactor simplified the parsing and serialisation code and exposed bugs in it, where it would reject valid <position> values followed by arbitrary tokens, and where it would fail to prefer "left" to "right" when serialising positions in basic shapes.
44 lines
1.3 KiB
Rust
44 lines
1.3 KiB
Rust
/* 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/. */
|
|
|
|
//! CSS handling for the computed value of
|
|
//! [`position`][position] values.
|
|
//!
|
|
//! [position]: https://drafts.csswg.org/css-backgrounds-3/#position
|
|
|
|
use std::fmt;
|
|
use style_traits::ToCss;
|
|
use values::computed::LengthOrPercentage;
|
|
use values::generics::position::Position as GenericPosition;
|
|
|
|
/// The computed value of a CSS `<position>`
|
|
pub type Position = GenericPosition<HorizontalPosition, VerticalPosition>;
|
|
|
|
/// The computed value of a CSS horizontal position.
|
|
pub type HorizontalPosition = LengthOrPercentage;
|
|
|
|
/// The computed value of a CSS vertical position.
|
|
pub type VerticalPosition = LengthOrPercentage;
|
|
|
|
impl Position {
|
|
/// `50% 50%`
|
|
#[inline]
|
|
pub fn center() -> Self {
|
|
Self::new(LengthOrPercentage::Percentage(0.5), LengthOrPercentage::Percentage(0.5))
|
|
}
|
|
|
|
/// `0% 0%`
|
|
#[inline]
|
|
pub fn zero() -> Self {
|
|
Self::new(LengthOrPercentage::zero(), LengthOrPercentage::zero())
|
|
}
|
|
}
|
|
|
|
impl ToCss for Position {
|
|
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
|
self.horizontal.to_css(dest)?;
|
|
dest.write_str(" ")?;
|
|
self.vertical.to_css(dest)
|
|
}
|
|
}
|