Skip to content

Commit

Permalink
style: Implement 'align-self' and 'justify-self'
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrubeck committed Feb 16, 2017
1 parent 34fb10b commit 4de480e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 11 deletions.
14 changes: 13 additions & 1 deletion components/style/properties/gecko.mako.rs
Expand Up @@ -864,7 +864,7 @@ fn static_assert() {
<% skip_position_longhands = " ".join(x.ident for x in SIDES + GRID_LINES) %>
<%self:impl_trait style_struct_name="Position"
skip_longhands="${skip_position_longhands} z-index box-sizing order align-content
justify-content">
justify-content align-self justify-self">
% for side in SIDES:
<% impl_split_style_coord("%s" % side.ident,
"mOffset",
Expand Down Expand Up @@ -914,6 +914,18 @@ fn static_assert() {

${impl_simple_copy('justify_content', 'mJustifyContent')}

pub fn set_align_self(&mut self, v: longhands::align_self::computed_value::T) {
self.gecko.mAlignSelf = v.0.bits()
}

${impl_simple_copy('align_self', 'mAlignSelf')}

pub fn set_justify_self(&mut self, v: longhands::justify_self::computed_value::T) {
self.gecko.mJustifySelf = v.0.bits()
}

${impl_simple_copy('justify_self', 'mJustifySelf')}

pub fn set_box_sizing(&mut self, v: longhands::box_sizing::computed_value::T) {
use computed_values::box_sizing::T;
use gecko_bindings::structs::StyleBoxSizing;
Expand Down
29 changes: 21 additions & 8 deletions components/style/properties/longhand/position.mako.rs
Expand Up @@ -140,14 +140,27 @@ ${helpers.predefined_type("flex-shrink", "Number",
animatable=True)}

// https://drafts.csswg.org/css-align/#align-self-property
// FIXME: We don't support the Gecko value 'normal' yet.
${helpers.single_keyword("align-self", "auto stretch flex-start flex-end center baseline",
need_clone=True,
extra_prefixes="webkit",
extra_gecko_values="normal",
gecko_constant_prefix="NS_STYLE_ALIGN",
spec="https://drafts.csswg.org/css-flexbox/#propdef-align-self",
animatable=False)}
% if product == "servo":
// FIXME: Update Servo to support the same syntax as Gecko.
${helpers.single_keyword("align-self", "auto stretch flex-start flex-end center baseline",
need_clone=True,
extra_prefixes="webkit",
spec="https://drafts.csswg.org/css-flexbox/#propdef-align-self",
animatable=False)}
% else:
${helpers.predefined_type(name="align-self",
type="AlignJustifySelf",
initial_value="specified::AlignJustifySelf::auto()",
spec="https://drafts.csswg.org/css-align/#align-self-property",
extra_prefixes="webkit",
animatable=False)}

${helpers.predefined_type(name="justify-self",
type="AlignJustifySelf",
initial_value="specified::AlignJustifySelf::auto()",
spec="https://drafts.csswg.org/css-align/#justify-self-property",
animatable=False)}
% endif

// https://drafts.csswg.org/css-flexbox/#propdef-order
<%helpers:longhand name="order" animatable="True" extra_prefixes="webkit"
Expand Down
4 changes: 3 additions & 1 deletion components/style/values/computed/mod.rs
Expand Up @@ -17,7 +17,7 @@ pub use self::image::{AngleOrCorner, EndingShape as GradientShape, Gradient, Gra
pub use self::image::{LengthOrKeyword, LengthOrPercentageOrKeyword};
pub use super::{Auto, Either, None_};
#[cfg(feature = "gecko")]
pub use super::specified::AlignJustifyContent;
pub use super::specified::{AlignJustifyContent, AlignJustifySelf};
pub use super::specified::{Angle, BorderStyle, GridLine, Time, UrlOrNone};
pub use super::specified::url::UrlExtraData;
pub use self::length::{CalcLengthOrPercentage, Length, LengthOrNumber, LengthOrPercentage, LengthOrPercentageOrAuto};
Expand Down Expand Up @@ -124,6 +124,8 @@ impl ToComputedValue for specified::CSSColor {

#[cfg(feature = "gecko")]
impl ComputedValueAsSpecified for specified::AlignJustifyContent {}
#[cfg(feature = "gecko")]
impl ComputedValueAsSpecified for specified::AlignJustifySelf {}
impl ComputedValueAsSpecified for specified::BorderStyle {}

#[derive(Debug, PartialEq, Clone, Copy)]
Expand Down
86 changes: 86 additions & 0 deletions components/style/values/specified/align.rs
Expand Up @@ -200,6 +200,57 @@ impl Parse for AlignJustifyContent {
}
}

/// Value of the `align-self` or `justify-self` property.
///
/// https://drafts.csswg.org/css-align/#self-alignment
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct AlignJustifySelf(pub AlignFlags);

impl AlignJustifySelf {
/// The initial value 'auto'
#[inline]
pub fn auto() -> Self {
AlignJustifySelf(ALIGN_AUTO)
}
}

no_viewport_percentage!(AlignJustifySelf);

impl ToCss for AlignJustifySelf {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
self.0.to_css(dest)
}
}

impl Parse for AlignJustifySelf {
// auto | normal | stretch | <baseline-position> |
// [ <overflow-position>? && <self-position> ]
fn parse(_: &ParserContext, input: &mut Parser) -> Result<Self, ()> {
// auto | normal | stretch | <baseline-position>
if let Ok(value) = input.try(parse_auto_normal_stretch_baseline) {
return Ok(AlignJustifySelf(value))
}
// [ <overflow-position>? && <self-position> ]
if let Ok(value) = input.try(parse_overflow_self_position) {
return Ok(AlignJustifySelf(value))
}
Err(())
}
}


// auto | normal | stretch | <baseline-position>
fn parse_auto_normal_stretch_baseline(input: &mut Parser) -> Result<AlignFlags, ()> {
let ident = input.expect_ident()?;
match_ignore_ascii_case! { ident,
"auto" => Ok(ALIGN_AUTO),
"normal" => Ok(ALIGN_NORMAL),
"stretch" => Ok(ALIGN_STRETCH),
"baseline" => Ok(ALIGN_BASELINE),
_ => Err(())
}
}

// normal | <baseline-position>
fn parse_normal_or_baseline(input: &mut Parser) -> Result<AlignFlags, ()> {
let ident = input.expect_ident()?;
Expand Down Expand Up @@ -264,3 +315,38 @@ fn parse_overflow_position(input: &mut Parser) -> Result<AlignFlags, ()> {
_ => Err(())
}
}

// [ <overflow-position>? && <self-position> ]
fn parse_overflow_self_position(input: &mut Parser) -> Result<AlignFlags, ()> {
// <self-position> followed by optional <overflow-position>
if let Ok(mut self_position) = input.try(|input| parse_self_position(input)) {
if let Ok(overflow) = input.try(|input| parse_overflow_position(input)) {
self_position |= overflow;
}
return Ok(self_position)
}
// <overflow-position> followed by required <self-position>
if let Ok(overflow) = input.try(|input| parse_overflow_position(input)) {
if let Ok(self_position) = input.try(|input| parse_self_position(input)) {
return Ok(overflow | self_position)
}
}
return Err(())
}

// <self-position>
fn parse_self_position(input: &mut Parser) -> Result<AlignFlags, ()> {
let ident = input.expect_ident()?;
match_ignore_ascii_case! { ident,
"start" => Ok(ALIGN_START),
"end" => Ok(ALIGN_END),
"flex-start" => Ok(ALIGN_FLEX_START),
"flex-end" => Ok(ALIGN_FLEX_END),
"center" => Ok(ALIGN_CENTER),
"left" => Ok(ALIGN_LEFT),
"right" => Ok(ALIGN_RIGHT),
"self-start" => Ok(ALIGN_SELF_START),
"self-end" => Ok(ALIGN_SELF_END),
_ => Err(())
}
}
2 changes: 1 addition & 1 deletion components/style/values/specified/mod.rs
Expand Up @@ -21,7 +21,7 @@ use super::computed::{ComputedValueAsSpecified, Context, ToComputedValue};
use super::computed::Shadow as ComputedShadow;

#[cfg(feature = "gecko")]
pub use self::align::AlignJustifyContent;
pub use self::align::{AlignJustifyContent, AlignJustifySelf};
pub use self::grid::GridLine;
pub use self::image::{AngleOrCorner, ColorStop, EndingShape as GradientEndingShape, Gradient};
pub use self::image::{GradientKind, HorizontalDirection, Image, LengthOrKeyword, LengthOrPercentageOrKeyword};
Expand Down

0 comments on commit 4de480e

Please sign in to comment.