Skip to content

Commit

Permalink
vaev-style: Add break properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Nov 9, 2024
1 parent 588ced5 commit 0621ac8
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/vaev-style/styles.h
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,62 @@ struct BorderSpacingProp {
}
};

// MARK: Breaks ----------------------------------------------------------------

// https://www.w3.org/TR/css-break-3/#propdef-break-after
struct BreakAfterProp {
BreakBetween value = initial();

static constexpr Str name() { return "break-after"; }

static constexpr BreakBetween initial() { return BreakBetween::AUTO; }

void apply(Computed &c) const {
c.break_.cow().after = value;
}

Res<> parse(Cursor<Css::Sst> &c) {
value = try$(parseValue<BreakBetween>(c));
return Ok();
}
};

// https://www.w3.org/TR/css-break-3/#propdef-break-before
struct BreakBeforeProp {
BreakBetween value = initial();

static constexpr Str name() { return "break-before"; }

static constexpr BreakBetween initial() { return BreakBetween::AUTO; }

void apply(Computed &c) const {
c.break_.cow().before = value;
}

Res<> parse(Cursor<Css::Sst> &c) {
value = try$(parseValue<BreakBetween>(c));
return Ok();
}
};

// https://www.w3.org/TR/css-break-3/#break-within
struct BreakInsideProp {
BreakInside value = initial();

static constexpr Str name() { return "break-inside"; }

static constexpr BreakInside initial() { return BreakInside::AUTO; }

void apply(Computed &c) const {
c.break_.cow().inside = value;
}

Res<> parse(Cursor<Css::Sst> &c) {
value = try$(parseValue<BreakInside>(c));
return Ok();
}
};

// MARK: Flex ------------------------------------------------------------------

// https://www.w3.org/TR/css-flexbox-1/#flex-basis-property
Expand Down Expand Up @@ -2216,6 +2272,11 @@ using _StyleProp = Union<
BorderCollapseProp,
BorderSpacingProp,

// Breaks
BreakAfterProp,
BreakBeforeProp,
BreakInsideProp,

// Flex
FlexBasisProp,
FlexDirectionProp,
Expand Down
57 changes: 57 additions & 0 deletions src/vaev-style/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,63 @@ Res<BorderSpacing> ValueParser<BorderSpacing>::parse(Cursor<Css::Sst> &c) {
return Error::invalidData("expected border spacing value");
}

// MARK: BreakAfter & BreakBefore
// https://www.w3.org/TR/css-break-3/#propdef-break-after
// https://www.w3.org/TR/css-break-3/#propdef-break-before
Res<BreakBetween> ValueParser<BreakBetween>::parse(Cursor<Css::Sst> &c) {
if (c.ended())
return Error::invalidData("unexpected end of input");

if (c.skip(Css::Token::ident("auto"))) {
return Ok(BreakBetween::AUTO);
} else if (c.skip(Css::Token::ident("avoid"))) {
return Ok(BreakBetween::AVOID);
} else if (c.skip(Css::Token::ident("avoid-page"))) {
return Ok(BreakBetween::AVOID_PAGE);
} else if (c.skip(Css::Token::ident("page"))) {
return Ok(BreakBetween::PAGE);
} else if (c.skip(Css::Token::ident("left"))) {
return Ok(BreakBetween::LEFT);
} else if (c.skip(Css::Token::ident("right"))) {
return Ok(BreakBetween::RIGHT);
} else if (c.skip(Css::Token::ident("recto"))) {
return Ok(BreakBetween::RECTO);
} else if (c.skip(Css::Token::ident("verso"))) {
return Ok(BreakBetween::VERSO);
} else if (c.skip(Css::Token::ident("avoid-column"))) {
return Ok(BreakBetween::AVOID_COLUMN);
} else if (c.skip(Css::Token::ident("column"))) {
return Ok(BreakBetween::COLUMN);
} else if (c.skip(Css::Token::ident("avoid-region"))) {
return Ok(BreakBetween::AVOID_REGION);
} else if (c.skip(Css::Token::ident("region"))) {
return Ok(BreakBetween::REGION);
}

return Error::invalidData("expected break between value");
}

// MARK: BreakInside
// https://www.w3.org/TR/css-break-3/#break-within
Res<BreakInside> ValueParser<BreakInside>::parse(Cursor<Css::Sst> &c) {
if (c.ended())
return Error::invalidData("unexpected end of input");

if (c.skip(Css::Token::ident("auto"))) {
return Ok(BreakInside::AUTO);
} else if (c.skip(Css::Token::ident("avoid"))) {
return Ok(BreakInside::AVOID);
} else if (c.skip(Css::Token::ident("avoid-page"))) {
return Ok(BreakInside::AVOID_PAGE);
} else if (c.skip(Css::Token::ident("avoid-column"))) {
return Ok(BreakInside::AVOID_COLUMN);
} else if (c.skip(Css::Token::ident("avoid-region"))) {
return Ok(BreakInside::AVOID_REGION);
}

return Error::invalidData("expected break between value");
}

// MARK: Color
// https://drafts.csswg.org/css-color

Expand Down
11 changes: 11 additions & 0 deletions src/vaev-style/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <vaev-base/align.h>
#include <vaev-base/borders.h>
#include <vaev-base/break.h>
#include <vaev-base/calc.h>
#include <vaev-base/color.h>
#include <vaev-base/display.h>
Expand Down Expand Up @@ -64,6 +65,16 @@ struct ValueParser<BorderSpacing> {
static Res<BorderSpacing> parse(Cursor<Css::Sst> &c);
};

template <>
struct ValueParser<BreakBetween> {
static Res<BreakBetween> parse(Cursor<Css::Sst> &c);
};

template <>
struct ValueParser<BreakInside> {
static Res<BreakInside> parse(Cursor<Css::Sst> &c);
};

template <typename T>
struct ValueParser<CalcValue<T>> {
static Res<CalcValue<T>> parse(Cursor<Css::Sst> &c) {
Expand Down

0 comments on commit 0621ac8

Please sign in to comment.