Skip to content

Commit

Permalink
identifying percentages for flex basis correct computation
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloamed committed Dec 23, 2024
1 parent 0b32c0a commit 7cb8f17
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/web/vaev-layout/flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ struct FlexItem {
}

// https://www.w3.org/TR/css-flexbox-1/#valdef-flex-basis-auto
void computeFlexBaseSize(Tree &tree, Px mainContainerSize, IntrinsicSize containerSizing) {
void computeFlexBaseSize(Tree &tree, Opt<Px> mainContainerDefiniteSize, IntrinsicSize containerSizing) {
// A NONE return here indicates a CONTENT case for the flex basis
auto getDefiniteFlexBasisSize = [](FlexProps &flexItemProps, FlexAxis &fa, Box *box) -> Opt<CalcValue<PercentOr<Length>>> {
if (flexItemProps.basis.type != FlexBasis::WIDTH)
Expand All @@ -255,13 +255,18 @@ struct FlexItem {
};

if (auto flexBasisDefiniteSize = getDefiniteFlexBasisSize(flexItemProps, fa, box)) {
flexBaseSize = resolve(
tree,
*box,
flexBasisDefiniteSize.unwrap(),
mainContainerSize
);
return;
// https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis#width
// TODO: if we have the information it isnt a percentage, we wouldnt need a relative width as an argument
// once the isPercentage API is properly implemented, we shouldnt be calling resolve with this useless arg
if (not isPurePercentage(flexBasisDefiniteSize.unwrap()) or mainContainerDefiniteSize) {
flexBaseSize = resolve(
tree,
*box,
flexBasisDefiniteSize.unwrap(),
mainContainerDefiniteSize.unwrapOr(0_px)
);
return;
}
}

if (isMinMaxIntrinsicSize(containerSizing)) {
Expand Down Expand Up @@ -674,12 +679,22 @@ struct FlexFormatingContext {
// 3. MARK: Flex base size and hypothetical main size of each item ---------
// https://www.w3.org/TR/css-flexbox-1/#algo-main-item

void _determineFlexBaseSizeAndHypotheticalMainSize(Tree &tree, IntrinsicSize containerSize) {
void _determineFlexBaseSizeAndHypotheticalMainSize(Tree &tree, Box &box, Input input) {
Opt<Px> containerDefiniteMainSize;
if (fa.mainAxis(box.style->sizing) == Size::LENGTH) {
containerDefiniteMainSize = resolve(
tree,
box,
fa.mainAxis(box.style->sizing).value,
input.containingBlock
);
}

for (auto &i : _items) {
i.computeFlexBaseSize(
tree,
fa.mainAxis(availableSpace),
containerSize
containerDefiniteMainSize,
input.intrinsic
);

i.computeHypotheticalMainSize(tree, availableSpace);
Expand Down Expand Up @@ -1504,7 +1519,7 @@ struct FlexFormatingContext {
_determineAvailableMainAndCrossSpace(tree, input);

// 3. Determine the flex base size and hypothetical main size of each item
_determineFlexBaseSizeAndHypotheticalMainSize(tree, input.intrinsic);
_determineFlexBaseSizeAndHypotheticalMainSize(tree, box, input);

// 4. Determine the main size of the flex container
_determineMainSize(tree, input, box);
Expand Down
9 changes: 9 additions & 0 deletions src/web/vaev-layout/values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ Px Resolver::resolve(FontSize value) {

// MARK: Resolve during layout -------------------------------------------------

bool isPurePercentage(CalcValue<PercentOr<Length>> value) {
if (value.type == CalcValue<PercentOr<Length>>::OpType::FIXED or
value.type == CalcValue<PercentOr<Length>>::OpType::SINGLE) {
return (value.lhs.template unwrap<PercentOr<Length>>())._type == PercentOr<Length>::PERCENT;
} else {
return false;
}
}

Px resolve(Tree const &tree, Box const &box, Length value) {
return Resolver::from(tree, box).resolve(value);
}
Expand Down
2 changes: 2 additions & 0 deletions src/web/vaev-layout/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ struct Resolver {

// MARK: Resolve during layout -------------------------------------------------

bool isPurePercentage(CalcValue<PercentOr<Length>>);

Px resolve(Tree const &tree, Box const &box, Length value);

Px resolve(Tree const &tree, Box const &box, PercentOr<Length> value, Px relative);
Expand Down

0 comments on commit 7cb8f17

Please sign in to comment.