Skip to content

Commit

Permalink
style: Don't restrict alignment shorthand parsing now that's not ambi…
Browse files Browse the repository at this point in the history
…guous.

Bug: 1339656
Reviewed-by: mats
MozReview-Commit-ID: AnYBt6zwnPl
  • Loading branch information
emilio committed Apr 6, 2018
1 parent 7f3b9ca commit 725943f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 67 deletions.
47 changes: 17 additions & 30 deletions components/style/properties/shorthand/position.mako.rs
Expand Up @@ -637,19 +637,22 @@

let justify_content = match justify_content {
Ok(v) => v,
Err(err) => {
if !align_content.is_valid_on_both_axes() {
return Err(err);
Err(..) => {
// https://drafts.csswg.org/css-align-3/#place-content:
//
// The second value is assigned to justify-content; if
// omitted, it is copied from the first value, unless that
// value is a <baseline-position> in which case it is
// defaulted to start.
//
if !align_content.is_baseline_position() {
align_content
} else {
ContentDistribution::start()
}

align_content
}
};

if align_content.has_extra_flags() || justify_content.has_extra_flags() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}

Ok(expanded! {
align_content: AlignContent(align_content),
justify_content: JustifyContent(justify_content),
Expand Down Expand Up @@ -682,18 +685,12 @@

let justify = match justify {
Ok(v) => v,
Err(err) => {
if !align.is_valid_on_both_axes() {
return Err(err);
}
Err(..) => {
debug_assert!(align.is_valid_on_both_axes());
align
}
};

if justify.has_extra_flags() || align.has_extra_flags() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}

Ok(expanded! {
align_self: AlignSelf(align),
justify_self: JustifySelf(justify),
Expand Down Expand Up @@ -729,14 +726,9 @@
input: &mut Parser<'i, 't>,
) -> Result<Longhands, ParseError<'i>> {
let align = AlignItems::parse(context, input)?;
if align.has_extra_flags() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
let justify = input.try(|input| JustifyItems::parse(context, input))
.unwrap_or(JustifyItems::from(align));
if justify.has_extra_flags() {
return Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError));
}
let justify =
input.try(|input| JustifyItems::parse(context, input))
.unwrap_or_else(|_| JustifyItems::from(align));

Ok(expanded! {
align_items: align,
Expand All @@ -746,11 +738,6 @@

impl<'a> ToCss for LonghandsToSerialize<'a> {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: fmt::Write {
if self.align_items.has_extra_flags() ||
self.justify_items.has_extra_flags() {
return Ok(());
}

self.align_items.to_css(dest)?;
if self.align_items.0 != self.justify_items.0 {
dest.write_str(" ")?;
Expand Down
49 changes: 12 additions & 37 deletions components/style/values/specified/align.rs
Expand Up @@ -139,6 +139,12 @@ impl ContentDistribution {
Self::new(AlignFlags::NORMAL)
}

/// `start`
#[inline]
pub fn start() -> Self {
Self::new(AlignFlags::START)
}

/// The initial value 'normal'
#[inline]
pub fn new(primary: AlignFlags) -> Self {
Expand All @@ -155,19 +161,12 @@ impl ContentDistribution {
self.primary.bits() as u16
}

/// Returns whether this value is valid for both axis directions.
pub fn is_valid_on_both_axes(&self) -> bool {
match self.primary.value() {
// <baseline-position> is only allowed on the block axis.
AlignFlags::BASELINE |
AlignFlags::LAST_BASELINE => false,

// left | right are only allowed on the inline axis.
AlignFlags::LEFT |
AlignFlags::RIGHT => false,

_ => true,
}
/// Returns whether this value is a <baseline-position>.
pub fn is_baseline_position(&self) -> bool {
matches!(
self.primary.value(),
AlignFlags::BASELINE | AlignFlags::LAST_BASELINE
)
}

/// The primary alignment
Expand All @@ -176,12 +175,6 @@ impl ContentDistribution {
self.primary
}

/// Whether this value has extra flags.
#[inline]
pub fn has_extra_flags(self) -> bool {
self.primary().intersects(AlignFlags::FLAG_BITS)
}

/// Parse a value for align-content / justify-content.
pub fn parse<'i, 't>(
input: &mut Parser<'i, 't>,
Expand Down Expand Up @@ -309,12 +302,6 @@ impl SelfAlignment {
}
}

/// Whether this value has extra flags.
#[inline]
pub fn has_extra_flags(self) -> bool {
self.0.intersects(AlignFlags::FLAG_BITS)
}

/// Parse a self-alignment value on one of the axis.
pub fn parse<'i, 't>(
input: &mut Parser<'i, 't>,
Expand Down Expand Up @@ -402,12 +389,6 @@ impl AlignItems {
pub fn normal() -> Self {
AlignItems(AlignFlags::NORMAL)
}

/// Whether this value has extra flags.
#[inline]
pub fn has_extra_flags(self) -> bool {
self.0.intersects(AlignFlags::FLAG_BITS)
}
}


Expand Down Expand Up @@ -449,12 +430,6 @@ impl JustifyItems {
pub fn normal() -> Self {
JustifyItems(AlignFlags::NORMAL)
}

/// Whether this value has extra flags.
#[inline]
pub fn has_extra_flags(self) -> bool {
self.0.intersects(AlignFlags::FLAG_BITS)
}
}

impl Parse for JustifyItems {
Expand Down

0 comments on commit 725943f

Please sign in to comment.