Skip to content

Commit

Permalink
style: add flex shorthand parser
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloamed committed Sep 16, 2024
1 parent dc72b3c commit 718af1e
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions src/web/vaev-style/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "base.h"
#include "computed.h"
#include "karm-base/res.h"
#include "karm-logger/logger.h"
#include "values.h"

// https://www.w3.org/TR/CSS22/propidx.html
Expand Down Expand Up @@ -870,6 +868,79 @@ struct FlexFlowProp {
}
};

// https://www.w3.org/TR/css-flexbox-1/#propdef-flex
struct FlexProp {
Tuple<FlexBasis, Number, Number> value = initial();

static Tuple<FlexBasis, Number, Number> initial() {
return {
Width{Width::AUTO},
0,
1,
};
}

static constexpr Str name() { return "flex"; }

void apply(Computed &c) const {
c.flex.cow().basis = value.v0;
c.flex.cow().grow = value.v1;
c.flex.cow().shrink = value.v2;
}

Res<> parse(Cursor<Css::Sst> &c) {
if (c.ended())
return Error::invalidData("unexpected end of input");

if (c.skip(Css::Token::ident("none"))) {
value.v0 = Width{Width::AUTO};
value.v1 = value.v2 = 0;
return Ok();
} else if (c.skip(Css::Token::ident("initial"))) {
value.v0 = Width{Width::AUTO};
value.v1 = 0, value.v2 = 1;
return Ok();
}

auto parseGrowShrink = [&c]() -> Res<Tuple<Number, Number>> {
auto grow = parseValue<Number>(c);
if (not grow)
return Error::invalidData("expected flex item grow");

Number growValue = grow.unwrap();

return Ok(Tuple{growValue, parseValue<Number>(c).unwrapOr(1)});
};

auto maybeGrowShrinkValues = parseGrowShrink();
if (maybeGrowShrinkValues) {
auto growShrinkValues = maybeGrowShrinkValues.unwrap();
value.v1 = growShrinkValues.v0, value.v2 = growShrinkValues.v1;

auto basis = parseValue<FlexBasis>(c);
if (basis)
value.v0 = basis.unwrap();
else
value.v0 = FlexBasis(Width(Length(0, Length::Unit::PX)));
} else {
auto basis = parseValue<FlexBasis>(c);
if (basis)
value.v0 = basis.unwrap();
else
return Error::invalidData("expected flex item grow or basis");

maybeGrowShrinkValues = parseGrowShrink();
if (maybeGrowShrinkValues) {
auto growShrinkValues = maybeGrowShrinkValues.unwrap();
value.v1 = growShrinkValues.v0, value.v2 = growShrinkValues.v1;
} else {
value.v1 = value.v2 = 1;
}
}
return Ok();
}
};

// MARK: Fonts -----------------------------------------------------------------

// https://www.w3.org/TR/css-fonts-4/#font-family-prop
Expand Down Expand Up @@ -1592,6 +1663,7 @@ using _StyleProp = Union<
FlexShrinkProp,
FlexWrapProp,
FlexFlowProp,
FlexProp,

// Font
FontFamilyProp,
Expand Down

0 comments on commit 718af1e

Please sign in to comment.