mirror of
https://github.com/servo/servo.git
synced 2025-10-04 02:29:12 +01:00
Refactor Position
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.
This commit is contained in:
parent
0040160b38
commit
70ec61cf01
22 changed files with 484 additions and 887 deletions
|
@ -5,231 +5,12 @@
|
|||
//! Generic types for CSS handling of specified and computed values of
|
||||
//! [`position`](https://drafts.csswg.org/css-backgrounds-3/#position)
|
||||
|
||||
use cssparser::Parser;
|
||||
use parser::{Parse, ParserContext};
|
||||
use std::fmt;
|
||||
use style_traits::ToCss;
|
||||
use values::HasViewportPercentage;
|
||||
use values::computed::{ComputedValueAsSpecified, Context, ToComputedValue};
|
||||
use values::specified::{LengthOrPercentage, Percentage};
|
||||
use values::computed::{Context, ToComputedValue};
|
||||
|
||||
define_css_keyword_enum!{ Keyword:
|
||||
"center" => Center,
|
||||
"left" => Left,
|
||||
"right" => Right,
|
||||
"top" => Top,
|
||||
"bottom" => Bottom,
|
||||
"x-start" => XStart,
|
||||
"x-end" => XEnd,
|
||||
"y-start" => YStart,
|
||||
"y-end" => YEnd
|
||||
}
|
||||
|
||||
add_impls_for_keyword_enum!(Keyword);
|
||||
|
||||
impl Keyword {
|
||||
#[inline]
|
||||
/// The defaults for position keywords are `left` and `top` (`x-start` and `y-start` for logical).
|
||||
/// This method checks whether this keyword indicates their opposite sides. See the
|
||||
/// `ToComputedValue` impl on `HorizontalPosition` and `VerticalPosition` for its use case.
|
||||
pub fn is_other_side(&self) -> bool {
|
||||
if self.is_horizontal() || self.is_logical_x() {
|
||||
matches!(*self, Keyword::Right | Keyword::XEnd)
|
||||
} else {
|
||||
matches!(*self, Keyword::Bottom | Keyword::YEnd)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check whether this is a keyword for horizontal position.
|
||||
pub fn is_horizontal(&self) -> bool {
|
||||
matches!(*self, Keyword::Left | Keyword::Right)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check whether this is a keyword for vertical position.
|
||||
pub fn is_vertical(&self) -> bool {
|
||||
matches!(*self, Keyword::Top | Keyword::Bottom)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check whether this is a horizontal logical keyword.
|
||||
pub fn is_logical_x(&self) -> bool {
|
||||
matches!(*self, Keyword::XStart | Keyword::XEnd)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check whether this is a vertical logical keyword.
|
||||
pub fn is_logical_y(&self) -> bool {
|
||||
matches!(*self, Keyword::YStart | Keyword::YEnd)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Check whether this is a logical keyword.
|
||||
pub fn is_logical(&self) -> bool {
|
||||
self.is_logical_x() || self.is_logical_y()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Keyword> for LengthOrPercentage {
|
||||
fn from(val: Keyword) -> LengthOrPercentage {
|
||||
match val {
|
||||
Keyword::Center => LengthOrPercentage::Percentage(Percentage(0.5)),
|
||||
Keyword::Left | Keyword::Top => LengthOrPercentage::Percentage(Percentage(0.0)),
|
||||
Keyword::Right | Keyword::Bottom => LengthOrPercentage::Percentage(Percentage(1.0)),
|
||||
// FIXME(canaltinova): Support logical keywords
|
||||
Keyword::XStart | Keyword::YStart => LengthOrPercentage::Percentage(Percentage(0.0)),
|
||||
Keyword::XEnd | Keyword::YEnd => LengthOrPercentage::Percentage(Percentage(1.0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
/// A generic type for representing horizontal or vertical `<position>` value.
|
||||
pub struct PositionValue<L> {
|
||||
/// Even though this is generic, it's always a `<length-percentage>` value.
|
||||
pub position: Option<L>,
|
||||
/// A position keyword.
|
||||
pub keyword: Option<Keyword>,
|
||||
}
|
||||
|
||||
impl<L: HasViewportPercentage> HasViewportPercentage for PositionValue<L> {
|
||||
#[inline]
|
||||
fn has_viewport_percentage(&self) -> bool {
|
||||
self.position.as_ref().map_or(false, |pos| pos.has_viewport_percentage())
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Parse> PositionValue<L> {
|
||||
/// Internal parsing function which (after parsing) checks the keyword with the
|
||||
/// given function.
|
||||
pub fn parse_internal<F>(context: &ParserContext, input: &mut Parser,
|
||||
mut is_allowed_keyword: F) -> Result<PositionValue<L>, ()>
|
||||
where F: FnMut(Keyword) -> bool
|
||||
{
|
||||
let (mut pos, mut keyword) = (None, None);
|
||||
for _ in 0..2 {
|
||||
if let Ok(l) = input.try(|i| L::parse(context, i)) {
|
||||
if pos.is_some() {
|
||||
return Err(())
|
||||
}
|
||||
|
||||
pos = Some(l);
|
||||
}
|
||||
|
||||
if let Ok(k) = input.try(Keyword::parse) {
|
||||
if keyword.is_some() || !is_allowed_keyword(k) {
|
||||
return Err(())
|
||||
}
|
||||
|
||||
keyword = Some(k);
|
||||
}
|
||||
}
|
||||
|
||||
if pos.is_some() {
|
||||
if let Some(Keyword::Center) = keyword {
|
||||
return Err(()) // "center" and <length> is not allowed
|
||||
}
|
||||
} else if keyword.is_none() {
|
||||
return Err(()) // at least one value is necessary
|
||||
}
|
||||
|
||||
Ok(PositionValue {
|
||||
position: pos,
|
||||
keyword: keyword,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: ToCss> ToCss for PositionValue<L> {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
if let Some(keyword) = self.keyword {
|
||||
keyword.to_css(dest)?;
|
||||
}
|
||||
|
||||
if let Some(ref position) = self.position {
|
||||
if self.keyword.is_some() {
|
||||
dest.write_str(" ")?;
|
||||
}
|
||||
|
||||
position.to_css(dest)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
/// A generic type for representing horizontal `<position>`
|
||||
pub struct HorizontalPosition<L>(pub L);
|
||||
|
||||
impl<L: ToCss> ToCss for HorizontalPosition<L> {
|
||||
#[inline]
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
self.0.to_css(dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: HasViewportPercentage> HasViewportPercentage for HorizontalPosition<L> {
|
||||
#[inline]
|
||||
fn has_viewport_percentage(&self) -> bool {
|
||||
self.0.has_viewport_percentage()
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Parse> Parse for HorizontalPosition<PositionValue<L>> {
|
||||
#[inline]
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
PositionValue::parse_internal(context, input, |keyword| {
|
||||
matches!{ keyword,
|
||||
Keyword::Left | Keyword::Right | Keyword::Center |
|
||||
Keyword::XStart | Keyword::XEnd
|
||||
}
|
||||
}).map(HorizontalPosition)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
/// A generic type for representing vertical `<position>`
|
||||
pub struct VerticalPosition<L>(pub L);
|
||||
|
||||
impl<L: ToCss> ToCss for VerticalPosition<L> {
|
||||
#[inline]
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
self.0.to_css(dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: HasViewportPercentage> HasViewportPercentage for VerticalPosition<L> {
|
||||
#[inline]
|
||||
fn has_viewport_percentage(&self) -> bool {
|
||||
self.0.has_viewport_percentage()
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: Parse> Parse for VerticalPosition<PositionValue<L>> {
|
||||
#[inline]
|
||||
fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
|
||||
PositionValue::parse_internal(context, input, |keyword| {
|
||||
matches!{ keyword,
|
||||
Keyword::Top | Keyword::Bottom | Keyword::Center |
|
||||
Keyword::YStart | Keyword::YEnd
|
||||
}
|
||||
}).map(VerticalPosition)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||
/// A generic type for representing a CSS [position](https://drafts.csswg.org/css-values/#position).
|
||||
///
|
||||
/// Note that the horizontal and vertical positions aren't really different types.
|
||||
/// They're just unit struct wrappers over `LengthOrPercentage`. They should be different
|
||||
/// because they allow different keywords (for e.g., vertical position doesn't allow
|
||||
/// `right` or `left` keywords and vice versa).
|
||||
pub struct Position<H, V> {
|
||||
/// The horizontal component of position.
|
||||
pub horizontal: H,
|
||||
|
@ -237,8 +18,15 @@ pub struct Position<H, V> {
|
|||
pub vertical: V,
|
||||
}
|
||||
|
||||
/// A generic type for representing positions with keywords.
|
||||
pub type PositionWithKeyword<L> = Position<HorizontalPosition<L>, VerticalPosition<L>>;
|
||||
impl<H, V> Position<H, V> {
|
||||
/// Returns a new position.
|
||||
pub fn new(horizontal: H, vertical: V) -> Self {
|
||||
Self {
|
||||
horizontal: horizontal,
|
||||
vertical: vertical,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: HasViewportPercentage, V: HasViewportPercentage> HasViewportPercentage for Position<H, V> {
|
||||
#[inline]
|
||||
|
@ -247,42 +35,9 @@ impl<H: HasViewportPercentage, V: HasViewportPercentage> HasViewportPercentage f
|
|||
}
|
||||
}
|
||||
|
||||
impl<L: ToCss> ToCss for Position<L, L> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<L: ToCss> ToCss for PositionWithKeyword<PositionValue<L>> {
|
||||
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
|
||||
macro_rules! to_css_with_keyword {
|
||||
($pos:expr, $default:expr) => {
|
||||
$pos.keyword.unwrap_or($default).to_css(dest)?;
|
||||
if let Some(ref position) = $pos.position {
|
||||
dest.write_str(" ")?;
|
||||
position.to_css(dest)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (self.horizontal.0.keyword.is_some() && self.horizontal.0.position.is_some()) ||
|
||||
(self.vertical.0.keyword.is_some() && self.vertical.0.position.is_some()) {
|
||||
to_css_with_keyword!(self.horizontal.0, Keyword::Left);
|
||||
dest.write_str(" ")?;
|
||||
to_css_with_keyword!(self.vertical.0, Keyword::Top);
|
||||
return Ok(())
|
||||
}
|
||||
|
||||
self.horizontal.to_css(dest)?;
|
||||
dest.write_str(" ")?;
|
||||
self.vertical.to_css(dest)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: ToComputedValue, V: ToComputedValue> ToComputedValue for Position<H, V> {
|
||||
type ComputedValue = Position<H::ComputedValue, V::ComputedValue>;
|
||||
type ComputedValue = Position<<H as ToComputedValue>::ComputedValue,
|
||||
<V as ToComputedValue>::ComputedValue>;
|
||||
|
||||
#[inline]
|
||||
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
|
||||
|
@ -294,7 +49,7 @@ impl<H: ToComputedValue, V: ToComputedValue> ToComputedValue for Position<H, V>
|
|||
|
||||
#[inline]
|
||||
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
|
||||
Position {
|
||||
Self {
|
||||
horizontal: ToComputedValue::from_computed_value(&computed.horizontal),
|
||||
vertical: ToComputedValue::from_computed_value(&computed.vertical),
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue