Commit graph

233 commits

Author SHA1 Message Date
Idan Horowitz
e39514721e LibWeb: Add missing break to avoid fallthrough in FlexFormattingContext
This silences a Clang warning.
2022-07-12 18:11:21 +03:00
Idan Horowitz
c70359ac04 LibWeb: Remove unused variable in FlexFormattingContext 2022-07-12 17:54:55 +03:00
Andreas Kling
dc13ced1a7 LibWeb: Express flex item cross axis alignment as offsets-from-center
This allows us to perform cross axis alignment without knowing the final
cross size of the flex container.
2022-07-12 15:55:43 +02:00
Andreas Kling
5f78e780f5 LibWeb: Actually clamp flex line cross size to min/max-size
We were dropping the result of css_clamp() on the floor, oops!
Let's also mark it [[nodiscard]] so it won't happen again.
2022-07-12 15:55:43 +02:00
Andreas Kling
c81e5c9d82 LibWeb: Floor scaled flex shrink factor at 1 when spec asks us to 2022-07-12 02:46:21 +02:00
Andreas Kling
6ecf7db87b LibWeb: Take margin box into account when justifying flex items
Before this patch, we were justifying based on the content box only,
which led to misalignments along the main axis when items had non-zero
padding, borders or margins.
2022-07-12 02:46:21 +02:00
Andreas Kling
2f0657739b LibWeb: Honor align-self over align-items when non-auto on flex item 2022-07-12 02:46:21 +02:00
Andreas Kling
4493702fb2 LibWeb: Honor preferred size as intrinsic cross size of flex items
I'm not 100% sure this is on-spec, but it seems to me that flex items
that have a specified non-auto cross size should honor that value in
its min-content and max-contribution.
2022-07-11 18:57:45 +02:00
Andreas Kling
9cae06de09 LibWeb: Implement step 9.2.3 of the flexbox layout algorithm
When sizing the flex container under a min-content or a max-content
constraint, flex items with a used flex basis of "content" should be
sized under the same constraint.
2022-07-11 18:57:45 +02:00
Andreas Kling
e1dab7b63d LibWeb: Use the *outer* flex base size in intrinsic size calculation 2022-07-11 18:57:45 +02:00
Andreas Kling
aee3d79ad1 LibWeb: Use the "scaled flex shrink factor" where noted by the spec 2022-07-11 18:57:45 +02:00
Andreas Kling
f7750985fa LibWeb: Treat "flex-basis: 0px" like any other definite basis value 2022-07-11 18:57:45 +02:00
Andreas Kling
7b4a86ab80 LibWeb: Use max-content main size for flex items w/ definite cross size
If the main size is indefinite, that is.
2022-07-11 18:57:45 +02:00
Andreas Kling
64959a8504 LibWeb: Express intrinsic size layout via size constraints
Previously, we had three layout modes:

- Normal:
    - Everything uses the computed values from CSS.

- MinContent:
    - Containing blocks act as if they have 0 width.
    - All line breaking opportunities are taken.

- MaxContent:
    - Containing blocks act as if they have infinite width.
    - Only forced line breaks are accepted.

The above was based on a set of misunderstandings of CSS sizing.
A major problem with the above was that *all* containing blocks
behaved differently during intrinsic size layout, not just the
relevant one.

With this patch there are only two layout modes:

- Normal:
    - Everything uses the computed values from CSS.

- IntrinsicSizeDetermination:
    - One or more boxes have size constraints applied.

There are two size constraints per layout box, set here:

- FormattingState::NodeState::width_constraint
- FormattingState::NodeState::height_constraint

They are of type SizeConstraint and can be one of None, MinContent,
or MaxContent. The default is None.

When performing an IntrinsicSizeDetermination layout, we now assign
a size constraint to the box we're trying to determine the intrinsic
size of, which is then honored by using two new helpers to query
the dimensions of containing blocks:

- FormattingContext::containing_block_width_for(Box)
- FormattingContext::containing_block_height_for(Box)

If there's a relevant constraint in effect on the Box, the size of
its containing block is adjusted accordingly.

This is essentially an implementation of the "available space"
constraints from CSS-SIZING-3. I'm sure some things will break from
this, and we'll have to deal with that separately.

Spec: https://drafts.csswg.org/css-sizing-3/#available
2022-07-11 18:57:45 +02:00
Andreas Kling
db2d62671f LibWeb: Add FFC helpers for getting intrinsic size of flex items 2022-07-11 18:57:45 +02:00
Andreas Kling
4f6fc3d3a6 LibWeb: Only perform the requested form of intrinsic size calculation
Before, querying any of the four intrinsic sizes would cause us to
calculate all of them (the four being min-content width/height, and
max-content width/height).

Now, the helper functions only calculate the specific intrinsic size
requested. It's then cached at the root formatting context level,
so that it's never calculated twice within the same layout pass.
2022-07-11 18:57:44 +02:00
Andreas Kling
496cf39cf5 LibWeb: Make separate functions for calculating min/max content sizes
At first, these are just wrappers around calculate_intrinsic_sizes().
Eventually, we'll make them do only the work necessary for their
specific size.
2022-07-11 18:57:44 +02:00
Andreas Kling
1690d88887 LibWeb: Clamp intrinsic flex item main size contributions to min/max 2022-07-11 18:57:44 +02:00
Andreas Kling
89e65e0ea1 LibWeb: Support flex item cross sizing based on intrinsic aspect ratio
This patch implements case B from the "determine the flex base size
and hypothetical main size of each item" from the flexbox algorithm.

https://drafts.csswg.org/css-flexbox-1/#algo-main-item
2022-07-10 00:47:46 +02:00
Andreas Kling
8eb022a57d LibWeb: Don't include flex line margins in inner flex item cross sizes
In cases where flex item cross size is based on the flex line cross
size, the spec specifically says to transfer the *outer* cross size of
the line. We were ignoring the "outer" part.

This patch fixes that by subtracting the cross margins from the size.
2022-07-10 00:04:41 +02:00
Andreas Kling
030dbfd2a9 LibWeb: Improve support for flex-start/end with reversed flex-direction
We now pass the "flex-direction" test from the CSSWG flexbox test suite.
2022-07-10 00:04:41 +02:00
Andreas Kling
cefc931347 LibWeb: Make sure CSS::ComputedValues has initial size values
Instead of using Optional<LengthPercentage>, we now use LengthPercentage
for these values. The initial values are all `auto`.

This avoids having to check `has_value()` in a ton of places.
2022-07-09 22:16:35 +02:00
Andreas Kling
4935055407 LibWeb: Keep the "remaining free space" across flexbox algo steps
Instead of recomputing the "remaining free space" per flex line,
remember it after calculating it during the "resolve flexible lengths"
step so we can reuse it later.
2022-07-06 20:31:24 +02:00
Andreas Kling
d2b7d2440f LibWeb: Cache flex item main sizes to avoid relayout during same cycle
This makes twitter.com actually load & render (although not very well.)
2022-07-06 00:22:42 +02:00
Andreas Kling
13b4d91f66 LibWeb: Always apply min/max cross size constraints to flex items
We were neglecting to apply min-size and max-size constraints in the
fast path for flex items with a definite cross size.
2022-07-04 22:30:59 +02:00
Andreas Kling
7953bd8391 LibWeb: Implement "transferred size suggestion" for flex items 2022-06-22 18:36:17 +02:00
Andreas Kling
75e8b1305d LibWeb: Actually distribute free space to flex items with auto margins
We were not applying the distributed space to the used offset of flex
items, as we were only assigning the margins to the layout state of the
box, not the internal FlexItem::margins.
2022-06-21 21:18:00 +02:00
Andreas Kling
99e96f951f LibWeb: Implement (some of) "automatic minimum size" for flex items 2022-06-21 21:06:19 +02:00
Andreas Kling
967b257518 LibWeb: Make FFC prepare replaced child boxes for layout
Before we ask a replaced box about its intrinsic dimensions, we have
to "prepare" the box, which tells it to go and work out what its
intrinsic dimensions are.

I've added a FIXME about how this is silly (and clearly bug-prone)
but this patch only patches it locally in FFC for now.
2022-06-21 20:40:26 +02:00
Andreas Kling
85f2f8ab3f LibWeb: Make sure we layout absolutely positioned children of FFC 2022-06-21 20:17:28 +02:00
Andreas Kling
57feb0f3ec LibWeb: Add missing null check of independent formatting context in FFC
When calling layout_inside() on a flex item that can't have children of
its own, layout_inside() will not return an independent formatting
context, so we need to handle that case here.
2022-04-11 01:20:24 +02:00
Andreas Kling
cc77c82383 LibWeb: Follow relaxed CSS clamping rules in flexbox layout
AFAICT, css-values-4 tells us that clamping numbers to a range where
min>max is okay. That means we can't use AK::clamp() since it will
VERIFY that max>=min.

This patch adds a css_clamp() helper (locally in FFC for now).

This fixes an issue where a bunch of sites were crashing due to the
VERIFY in AK::clamp().
2022-04-09 14:50:05 +02:00
Andreas Kling
922509c1a5 LibWeb: Remove incorrect comment from FFC flex item cross sizing 2022-04-06 14:43:00 +02:00
Andreas Kling
c8240e31a1 LibWeb: Implement basic intrinsic sizing algorithms for flex containers
This patch adds support for MinContent and MaxContent layout to FFC.
This means that an FFC can now calculate intrinsic sizes for the flex
container, to be used by the parent formatting context.

There are some FIXME's as usual, but this already works on basic things.
2022-04-06 11:31:08 +02:00
Enver Balalic
747f347b75 LibWeb: Implement flex reverse layouts
This builds on the work done by implementing the flex order CSS
property and implements flex reverse layouts by just reversing
the order and the items within each order bucket.
2022-04-02 19:01:14 +02:00
Enver Balalic
58398b1e12 LibWeb: Implement the flex order CSS property
Adds support for the flex order property and a test page for it
on the browser welcome page.
2022-04-02 18:40:32 +02:00
Enver Balalic
7be57c322e LibWeb: Flex fix minimum main size in min/max violations calculation
While calculating the minimum size for main min/max size violations
we were flooring the min size to 0 if the item doesn't have a min
main size. Instead of that determine the intrinsic min main size
of that element.

This fixes the flex: 80% 0 4/1/0 test case in the flex.html
test page.

This case was missed in a previous commit that added the
determine_min_main_size_of_child function
2022-03-31 21:09:41 +02:00
Enver Balalic
8b4d09932a LibWeb: Fix sizing of flex child that has flex-basis 0
Before if an element didn't have a main min size we would clamp
it to a literal zero. If that element also had a flex-basis 0
it's width would end up being 0.
This patch adds a determine_min_main_size_of_child function that
will calculate the minimum main size for the box based on the
content of the box.
We use the result of that function now instead of clamping
the element main min size to 0.

This also adds one more box to the flex.html test page, which is
the same flex: 0 0 0 box but with flex-direction: column.
2022-03-30 21:16:47 +02:00
Enver Balalic
4ecc695a65 LibWeb: Take borders and padding into account when doing Flex layout
Before this the flex layout didn't take into account the applied
borders or padding while laying out the items.

The child's top and left borders would get painted over the
parent's borders, also due to it not taking borders into account,
children with borders would overlap each other.

Due to it not taking padding into account, the children would get
drawn outside the parent element.
2022-03-27 17:43:47 +02:00
Andreas Kling
2e6f5fddef LibWeb: Transfer flex item box model metrics to paintable
Until now, flex items has 0/0/0 margin/border/padding values.
2022-03-26 17:31:01 +01:00
Johannes Laudenberg
eb1d7e8076 LibWeb: Make margins in the main dimension work for flex items
The spec at https://www.w3.org/TR/css-flexbox-1/ states that when
calculating specific spaces and sizes inside a flex container, the outer
size of a flex item needs to be taken into account.

This patch adds the margins in the main dimension of a flex item to
these calculations such that their margins are actually painted in a lot
of common cases.

It makes our Github page look marginally better.
2022-03-23 11:59:07 +01:00
Andreas Kling
c1f0d21bbe LibWeb: Rename the LayoutMode enum values and explain them
The old mode names, while mechanically accurate, didn't really reflect
their relationship to the CSS specifications.

This patch renames them as follows:

    Default => Normal
    AllPossibleLineBreaks => MinContent
    OnlyRequiredLineBreaks => MaxContent

There's also now an explainer comment with the LayoutMode enum about the
specific implications of layout in each mode.
2022-03-19 15:46:15 +01:00
Andreas Kling
8d5768b103 LibWeb: Don't treat inline-level children of flex items as whitespace
This was causing us to collapse some children of flex items as if they
were useless whitespace text nodes.
2022-03-19 12:42:10 +01:00
Lenny Maiorani
c37820b898 Libraries: Use default constructors/destructors in LibWeb
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-17 17:23:49 +00:00
Andreas Kling
ab9d9709b9 LibWeb: Give flex containers with percentage cross size some dimensions
Instead of making them zero-sized on the cross axis, just treat
percentage sizes like auto for now. Better to have *some* size than none
at all.
2022-03-13 00:04:51 +01:00
Andreas Kling
bd94f2c4c8 LibWeb: Add a debug helper to dump current state of an FFC 2022-03-13 00:04:51 +01:00
Andreas Kling
ad8d65fd6e LibWeb: Fix calculation of hypothetical cross size in column flex layout
Simplify automatic cross sizing of items in flex-direction:column by
using the fit-content width directly.

There's obviously a lot more nuance to this, but for now this makes
flex items with both width:auto and height:auto actually get some height
in column flex layouts.
2022-03-13 00:04:51 +01:00
Andreas Kling
515db5fc1b LibWeb: Make Layout::FormattingState copies shallow
Previously, each NodeState in a FormattingState was shared with the
parent FormattingState, but the HashMap of NodeState had to be copied
when making FormattingState copies.

This patch makes copying instant by keeping a pointer to the parent
FormattingState instead. When fetching immutable state via get(), we may
now return a reference to a NodeState owned by a parent FormattingState.

get_mutable() will copy any NodeState found in the ancestor chain before
making a brand new one.
2022-03-13 00:04:51 +01:00
Andreas Kling
8c91e8016c LibWeb: Handle many more cases with intrinsic/auto sizing in flex layout
The flexbox specification barely even handwaves about automatically
sized items, but there's actually a lot of work to do in order for them
to get the correct size.

This patch is messy, but does make significant progress on supporting
flex items with indefinite width and/or height.

There's a fair amount of nested layout going on here, but do note that
we'll be hitting the intrinsic sizes cache whenever possible.
2022-03-13 00:04:51 +01:00
Andreas Kling
e4eb6d4f1f LibWeb: Add FFC helpers for resolving definite main/cross sizes
Although something has a definite size, we may still have to "resolve"
it, since FFC is quite liberal in what it considers to be definite.

Let's put that logic in a set of helper functions.
2022-03-13 00:04:51 +01:00