Move Position::parse_legacy logic to LegacyPosition type

This commit is contained in:
Nazım Can Altınova 2017-06-23 16:17:26 -07:00
parent d589645ee9
commit b33545ca3b
4 changed files with 220 additions and 82 deletions

View file

@ -15,6 +15,7 @@ use values::computed::{CalcLengthOrPercentage, LengthOrPercentage as ComputedLen
use values::computed::{Context, ToComputedValue};
use values::generics::position::Position as GenericPosition;
use values::specified::{AllowQuirks, LengthOrPercentage, Percentage};
use values::specified::transform::OriginComponent;
/// The specified value of a CSS `<position>`
pub type Position = GenericPosition<HorizontalPosition, VerticalPosition>;
@ -49,46 +50,26 @@ define_css_keyword_enum! { Y:
}
add_impls_for_keyword_enum!(Y);
/// Modern position syntax supports 3 and 4-value syntax. That means:
/// If three or four values are given, then each <percentage> or <length> represents an offset
/// and must be preceded by a keyword, which specifies from which edge the offset is given.
/// For example, `bottom 10px right 20px` represents a `10px` vertical
/// offset up from the bottom edge and a `20px` horizontal offset leftward from the right edge.
/// If three values are given, the missing offset is assumed to be zero.
/// But for some historical reasons we need to keep CSS Level 2 syntax which only supports up to
/// 2-value. This enum represents whether position should parse modern syntax or legacy syntax.
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PositionSyntax {
/// Modern syntax
Modern,
/// Legacy syntax
Legacy,
}
impl Parse for Position {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Self::parse_quirky(context, input, AllowQuirks::No, PositionSyntax::Modern)
Self::parse_quirky(context, input, AllowQuirks::No)
}
}
impl Position {
/// Parses a `<position>`, with quirks.
/// Syntax parameter uses PositionSyntax to determine whether the syntax parsing
/// mode is modern or legacy.
pub fn parse_quirky<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks,
syntax: PositionSyntax)
allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> {
match input.try(|i| PositionComponent::parse_quirky(context, i, allow_quirks, syntax)) {
match input.try(|i| PositionComponent::parse_quirky(context, i, allow_quirks)) {
Ok(x_pos @ PositionComponent::Center) => {
if let Ok(y_pos) = input.try(|i|
PositionComponent::parse_quirky(context, i, allow_quirks, syntax)) {
PositionComponent::parse_quirky(context, i, allow_quirks)) {
return Ok(Self::new(x_pos, y_pos));
}
let x_pos = input
.try(|i| PositionComponent::parse_quirky(context, i, allow_quirks, syntax))
.try(|i| PositionComponent::parse_quirky(context, i, allow_quirks))
.unwrap_or(x_pos);
let y_pos = PositionComponent::Center;
return Ok(Self::new(x_pos, y_pos));
@ -100,22 +81,12 @@ impl Position {
return Ok(Self::new(x_pos, y_pos));
}
if let Ok(y_keyword) = input.try(Y::parse) {
let y_lop = match syntax {
PositionSyntax::Modern => {
input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok()
},
PositionSyntax::Legacy => None,
};
let y_lop = input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok();
let x_pos = PositionComponent::Side(x_keyword, lop);
let y_pos = PositionComponent::Side(y_keyword, y_lop);
return Ok(Self::new(x_pos, y_pos));
}
let x_pos = PositionComponent::Side(x_keyword, None);
if syntax == PositionSyntax::Legacy {
if let Ok(y_lop) = input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)) {
return Ok(Self::new(x_pos, PositionComponent::Length(y_lop)))
}
}
let y_pos = lop.map_or(PositionComponent::Center, PositionComponent::Length);
return Ok(Self::new(x_pos, y_pos));
},
@ -136,19 +107,9 @@ impl Position {
}
let y_keyword = Y::parse(input)?;
let lop_and_x_pos: Result<_, ParseError> = input.try(|i| {
let y_lop = match syntax {
PositionSyntax::Modern => {
i.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok()
},
PositionSyntax::Legacy => None,
};
let y_lop = i.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok();
if let Ok(x_keyword) = i.try(X::parse) {
let x_lop = match syntax {
PositionSyntax::Modern => {
i.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok()
},
PositionSyntax::Legacy => None,
};
let x_lop = i.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok();
let x_pos = PositionComponent::Side(x_keyword, x_lop);
return Ok((y_lop, x_pos));
};
@ -165,13 +126,6 @@ impl Position {
Ok(Self::new(x_pos, y_pos))
}
/// Parses legacy Position syntax.
pub fn parse_legacy<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>)
-> Result<Self, ParseError<'i>> {
Self::parse_quirky(context, input, AllowQuirks::No, PositionSyntax::Legacy)
}
/// `center center`
#[inline]
pub fn center() -> Self {
@ -216,7 +170,7 @@ impl<S> HasViewportPercentage for PositionComponent<S> {
impl<S: Parse> Parse for PositionComponent<S> {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Self::parse_quirky(context, input, AllowQuirks::No, PositionSyntax::Modern)
Self::parse_quirky(context, input, AllowQuirks::No)
}
}
@ -224,8 +178,7 @@ impl<S: Parse> PositionComponent<S> {
/// Parses a component of a CSS position, with quirks.
pub fn parse_quirky<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks,
syntax: PositionSyntax)
allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> {
if input.try(|i| i.expect_ident_matching("center")).is_ok() {
return Ok(PositionComponent::Center);
@ -234,9 +187,6 @@ impl<S: Parse> PositionComponent<S> {
return Ok(PositionComponent::Length(lop));
}
let keyword = S::parse(context, input)?;
if syntax == PositionSyntax::Legacy {
return Ok(PositionComponent::Side(keyword, None));
}
let lop = input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)).ok();
Ok(PositionComponent::Side(keyword, lop))
}
@ -326,3 +276,108 @@ impl Side for Y {
*self == Y::Top
}
}
/// The specified value of a legacy CSS `<position>`
/// Modern position syntax supports 3 and 4-value syntax. That means:
/// If three or four values are given, then each <percentage> or <length> represents an offset
/// and must be preceded by a keyword, which specifies from which edge the offset is given.
/// For example, `bottom 10px right 20px` represents a `10px` vertical
/// offset up from the bottom edge and a `20px` horizontal offset leftward from the right edge.
/// If three values are given, the missing offset is assumed to be zero.
/// But for some historical reasons we need to keep CSS Level 2 syntax which only supports up to
/// 2-value. This type represents this 2-value syntax.
pub type LegacyPosition = GenericPosition<LegacyHPosition, LegacyVPosition>;
/// The specified value of a horizontal position.
pub type LegacyHPosition = OriginComponent<X>;
/// The specified value of a vertical position.
pub type LegacyVPosition = OriginComponent<Y>;
impl Parse for LegacyPosition {
fn parse<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
Self::parse_quirky(context, input, AllowQuirks::No)
}
}
impl LegacyPosition {
/// Parses a `<position>`, with quirks.
pub fn parse_quirky<'i, 't>(context: &ParserContext,
input: &mut Parser<'i, 't>,
allow_quirks: AllowQuirks)
-> Result<Self, ParseError<'i>> {
match input.try(|i| OriginComponent::parse(context, i)) {
Ok(x_pos @ OriginComponent::Center) => {
if let Ok(y_pos) = input.try(|i|
OriginComponent::parse(context, i)) {
return Ok(Self::new(x_pos, y_pos));
}
let x_pos = input
.try(|i| OriginComponent::parse(context, i))
.unwrap_or(x_pos);
let y_pos = OriginComponent::Center;
return Ok(Self::new(x_pos, y_pos));
},
Ok(OriginComponent::Side(x_keyword)) => {
if input.try(|i| i.expect_ident_matching("center")).is_ok() {
let x_pos = OriginComponent::Side(x_keyword);
let y_pos = OriginComponent::Center;
return Ok(Self::new(x_pos, y_pos));
}
if let Ok(y_keyword) = input.try(Y::parse) {
let x_pos = OriginComponent::Side(x_keyword);
let y_pos = OriginComponent::Side(y_keyword);
return Ok(Self::new(x_pos, y_pos));
}
let x_pos = OriginComponent::Side(x_keyword);
if let Ok(y_lop) = input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)) {
return Ok(Self::new(x_pos, OriginComponent::Length(y_lop)))
}
},
Ok(x_pos @ OriginComponent::Length(_)) => {
if let Ok(y_keyword) = input.try(Y::parse) {
let y_pos = OriginComponent::Side(y_keyword);
return Ok(Self::new(x_pos, y_pos));
}
if let Ok(y_lop) = input.try(|i| LengthOrPercentage::parse_quirky(context, i, allow_quirks)) {
let y_pos = OriginComponent::Length(y_lop);
return Ok(Self::new(x_pos, y_pos));
}
let y_pos = OriginComponent::Center;
let _ = input.try(|i| i.expect_ident_matching("center"));
return Ok(Self::new(x_pos, y_pos));
},
Err(_) => {},
}
let y_keyword = Y::parse(input)?;
let x_pos: Result<_, ParseError> = input.try(|i| {
if let Ok(x_keyword) = i.try(X::parse) {
let x_pos = OriginComponent::Side(x_keyword);
return Ok(x_pos);
}
i.expect_ident_matching("center")?;
Ok(OriginComponent::Center)
});
if let Ok(x_pos) = x_pos {
let y_pos = OriginComponent::Side(y_keyword);
return Ok(Self::new(x_pos, y_pos));
}
let x_pos = OriginComponent::Center;
let y_pos = OriginComponent::Side(y_keyword);
Ok(Self::new(x_pos, y_pos))
}
/// `center center`
#[inline]
pub fn center() -> Self {
Self::new(OriginComponent::Center, OriginComponent::Center)
}
}
impl ToCss for LegacyPosition {
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)
}
}