Commit Graph

917 Commits

Author SHA1 Message Date
Ben Wiederhake
8deced39a8 LibWeb: Resolve cyclic declaration/definitions involving Length
This remained undetected for a long time as HeaderCheck is disabled by
default. This commit makes the following file compile again:

    // file: compile_me.cpp
    #include <LibWeb/CSS/GridTrackSize.h>
    // That's it, this was enough to cause a compilation error.
2022-09-15 14:45:38 +01:00
Andreas Kling
34025442ff LibWeb: Account for float's container offsets in BFC root auto height
When calculating the automatic height of a BFC root, we stretch it to
contain the bottommost margin edge of floating boxes.

Before this change, we assumed that floating boxes had coordinates
relative to the BFC root, when they're actually relative to the floating
box's containing block. This may or may not be the BFC root, so we have
to use margin_box_in_ancestor_coordinate_space() to apply offsets from
all boxes in the containing block chain (up to the BFC root).
2022-09-14 20:18:49 +02:00
Andreas Kling
35a9a2fbb2 LibWeb: Account for containing block padding when placing abspos boxes
Unlike the other positioning schemes, absolute positioning is relative
to the *padding* edge of the containing block.
2022-09-14 14:43:17 +02:00
Andreas Kling
f25203f245 LibWeb: Don't re-resolve "auto" flex item sizes after definitizing them
This is rather subtle and points to our architecture around definite
sizes not being exactly right, but...

At some points during flexbox layout, the spec tells us that the sizes
of certain flex items are considered definite from this point on.
We implement this by marking each item's associated UsedValues as
"has-definite-width/height".

However, this breaks code that tries to resolve computed "auto" sizes
by taking the corresponding size from the containing block. The end
result was that the 1st sizing pass in flexbox would find the right size
for an "auto" sized item, but the 2nd pass would override the correct
size with the containing block's content size in that axis instead.

To work around the issue, FFC now remembers when it "definitizes" an
item, and future attempts to resolve an "auto" computed size for that
value will bypass the computed-auto-is-resolved-against-containing-block
step of the algorithm. It's not perfect, and we'll need to think more
about how to really represent these intermediate states relating to
box sizes being definite..
2022-09-14 14:43:17 +02:00
Andreas Kling
b8aa6a4453 LibWeb: Use PercentageOr<T>::contains_percentage() in CSS layout
By asking if the value *contains* a percentage rather than whether it
*is* one, we cover many more cases where e.g `width: calc(100% - 10px)`
should be "treated as auto" etc.
2022-09-14 14:43:17 +02:00
Andreas Kling
f941b7aefe LibWeb: Create stacking context for flex/grid items with z-index != auto 2022-09-14 00:09:49 +02:00
Andreas Kling
fb5879fdcc LibWeb: Don't include abspos children in containing block's auto width 2022-09-14 00:09:49 +02:00
martinfalisse
df22f38feb LibWeb: Inform the parent context of the grid's size 2022-09-14 00:09:14 +02:00
martinfalisse
214329b81c LibWeb: Layout grid items after calculation
Now that the positions of each grid item have been calculated, and the
sizes of the individual rows and columns ascertained, can actually
layout the different items.
2022-09-14 00:09:14 +02:00
martinfalisse
58078c1f40 LibWeb: Calculate sizes of Auto grid tracks 2022-09-14 00:09:14 +02:00
martinfalisse
f558fd93b7 LibWeb: Calculate sizes of FlexibleLength grid tracks 2022-09-14 00:09:14 +02:00
martinfalisse
fbe703e2bd LibWeb: Follow spec for calculating intrinsic track sizes 2022-09-14 00:09:14 +02:00
martinfalisse
b7d053421e LibWeb: Calculate sizes of known grid track sizes
Calculate the sizes of grid tracks for which there is a clear size
(length or percentage has been specified).
2022-09-14 00:09:14 +02:00
martinfalisse
6b649b6343 LibWeb: Start sizing grid tracks
The second part of formatting the display grid is calculating the sizes
of the columns and rows.
2022-09-14 00:09:14 +02:00
martinfalisse
902bdc8f15 LibWeb: Position remaining grid items 2022-09-14 00:09:14 +02:00
martinfalisse
707b76ed41 LibWeb: Position grid items with fixed row positions 2022-09-14 00:09:14 +02:00
martinfalisse
f143fefca2 LibWeb: Position grid items with fixed position values 2022-09-14 00:09:14 +02:00
martinfalisse
e0cd63f531 LibWeb: Add array to keep track of grid boxes left to place
This structure will be useful to subtract from after placing different
boxes into the grid.
2022-09-14 00:09:14 +02:00
martinfalisse
a2ecb903af LibWeb: New start at layouting out the grid
According to the spec it's necessary to:
1. Layout the cells in the grid
2. Find the sizes of the rows and columns

Since I had started to do this backwards, and as I expand in future
commits, I take here the opportunity to start with a clean state.

The occupation_grid keeps track of which cells in the grid have been
filled out.
2022-09-14 00:09:14 +02:00
Andreas Kling
54e0e85581 LibWeb: Treat percentage width/height as "auto" more consistently in BFC
Since we can't resolve percentage sizes against an indefinitely-sized
containing block, treat the sizes as "auto" in all these cases.
2022-09-13 17:03:31 +02:00
Andreas Kling
71ca857b67 LibWeb: Break lines until we have enough space between floats
Before this change, we'd always insert one line box fragment, even when
a float was taking up too much space on the line, and the fragment
didn't actually fit.

We now perform line breaks until we have enough space between floats.
This fixes many page layouts where we'd previously see small fragments
of inline content outside the right edge of the containing block.
2022-09-13 17:03:31 +02:00
Andreas Kling
c9a7853fef LibWeb: Include all floating descendants in BFC root's automatic height
Before this change, we were only considering floating boxes that were
immediate children of the BFC root.
2022-09-13 17:03:31 +02:00
Andreas Kling
bfcb4d88cf LibWeb: Treat percentage heights as "auto" if unresolvable
If the containing block has indefinite height, we can't resolve
percentage heights against it. Instead of treating it as 0 by accident,
let's treat it as "auto" on purpose.

This brings back the cheek borders on Acid2.
2022-09-09 15:20:10 +02:00
Andreas Kling
3f960781fd LibWeb: Don't collapse vertical margins between floats
Just stack floats at their vertical margin edge instead of letting their
border boxes rub up against each other.
2022-09-09 15:20:10 +02:00
Andreas Kling
7ba6eb37fc LibWeb: Rename confusing parameter to layout_block_level_box()
It wasn't the content height, but rather the the bottom edge of the
lowest margin box.
2022-09-08 15:03:55 +02:00
Andreas Kling
af73a5d921 LibWeb: Use correct box edge when looking for space between floats 2022-09-08 15:03:55 +02:00
Andreas Kling
26544f559c LibWeb: Make anonymous wrapper blocks actually have "display: block"
We didn't set their display at all before, and since CSS display is not
inherited, anonymous block wrappers were actually "display: inline", but
it kinda worked anyway because we positioned blocks based on their C++
class (BlockContainer) rather than their CSS display value.

Now that we position based on CSS display value, this broke positioning
of anonymous wrapper blocks, and this fixes that.
2022-09-08 11:54:13 +02:00
Andreas Kling
9e39f51fd3 LibWeb: Position blocks after previous block-level box, ignoring type
Before this change, block-level boxes were laid out vertically by
placing them after the nearest previous BlockContainer sibling. This
only worked if the preceding block-level box happened to be a
BlockContainer.

This fixes an issue where the screenshot on netsurf-browser.org was not
being laid out properly (it was `img { display: block }` which creates
a block-level ImageBox, and ImageBox is not a BlockContainer but a
ReplacedBox, so the following block-level box was skipping over the
ImageBox and being placed next to whatever was before the ImageBox..)
2022-09-08 01:42:25 +02:00
Andreas Kling
faf9746244 LibWeb: Cache width of "alt" text in ImageBox
We were constantly measuring and re-measuring the "alt" attribute text
of ImageBox layout nodes, even when the alt text didn't change. By
caching this, we avoid a *lot* of repeated text measurement work.
2022-09-07 17:47:33 +02:00
Andreas Kling
514fa83708 LibWeb: Improve float: right behavior
- Use the border box of the floated element when testing if something
  needs to flow around it.
- Take the floated element's containing block size into account (instead
  of the BFC root) when calculating available space on a line where a
  right-side float intrudes.
2022-09-07 17:47:33 +02:00
Andreas Kling
a42506c8b9 LibWeb: Fix three accidental float truncations
We were doing this:

    max(0, some_floating_point_value)

This returns an `int` result based on the type of `0`, oops!
2022-09-07 17:47:33 +02:00
Andreas Kling
6f433c8656 LibWeb+LibJS: Make the EventTarget hierarchy (incl. DOM) GC-allocated
This is a monster patch that turns all EventTargets into GC-allocated
PlatformObjects. Their C++ wrapper classes are removed, and the LibJS
garbage collector is now responsible for their lifetimes.

There's a fair amount of hacks and band-aids in this patch, and we'll
have a lot of cleanup to do after this.
2022-09-06 00:27:09 +02:00
Andreas Kling
bb547ce1c4 LibWeb: Make AbstractRange and subclasses GC-allocated 2022-09-06 00:27:09 +02:00
Andreas Kling
e65990f7a3 LibWeb: Treat cyclic percentage max-size as "auto" on the flex container
This allows things like `max-width: 100%` to work on flex containers
whose containing blocks themselves have automatic width.
2022-08-27 12:19:37 +02:00
martinfalisse
79a630adef LibWeb: Begin formatting the grid
This is a really starter attempt at formatting the grid. It doesn't yet
take into account the computed_values of grid-template-rows, nor the
values in grid-column-start and like CSS properties.

But these changes are a start and make it so the examples in
display-grid.html work.

To be fleshed out further..
2022-08-25 13:47:48 +02:00
martinfalisse
44d08b81b7 LibWeb: Parse grid-column-start and related CSS properties
Parse grid-column-start, end, and the equivalent for rows.
2022-08-25 13:47:48 +02:00
martinfalisse
92a00648b1 LibWeb: Parse grid-template-columns and grid-template-rows
Add functionality to begin parsing grid-template-columns and
grid-template-rows. There are still things to be added, like parsing
functions, but I would say a couple of the major points are already
adressed like length, percentage, and flexible-length.
2022-08-25 13:47:48 +02:00
martinfalisse
e4c5799026 LibWeb: Add GridFormattingContext 2022-08-25 13:47:48 +02:00
watkinsr
35dbeb064a LibWeb: Render flex line items RTL (reversed items) for FlexEnd
This is a basic attempt at trying to handle parent container case
justify-content: flex-end.

Test-scenario:
Head to https://ryanwatkins.me and note that now the header nav is on
the right as opposed to the left in-line with how Chrome/Firefox would
respectively handle it also, i.e. 'flex-end'

Implementation:
Move cursor to the end and render in reverse backwards shifting the
cursor leftwards.
2022-08-16 09:55:49 +01:00
Andreas Kling
ab6af4c9a0 LibWeb: Remove unused member LineBuilder::m_layout_mode 2022-08-14 23:33:28 +02:00
martinfalisse
872b6369c4 LibWeb: Use parent and sibling positions for absolutely positioned div
If absolutely positioned divs do not have a fixed position, then their
position must be calculated based off of the position of their parent
and their siblings.
2022-08-14 11:22:52 +02:00
martinfalisse
65e7126c48 LibWeb: Factor out compute_y_position
Factor out the code that computes the vertical position of a Box with
respect to its siblings so that it can be used when computing the
absolutely positioned divs as well.
2022-08-14 11:22:52 +02:00
MacDue
6a6475673f LibWeb: Convert images to common AbstractImageStyleValue base
This commit moves both the ImageStyleValue and LinearGradientStyleValue
to a common base class of AbstractImageStyleValue. This abstracts
getting the natural_width/height, loading/resolving, and painting
the image.

Now for 'free' you get:

 - Linear gradients working with the various background sizing/repeat
   properties.
 - Linear gradients working as list-markers :^) -- best feature ever!

P.s. This commit is a little large as it's tricky to make this change
incrementally without breaking things.
2022-08-08 22:39:06 +02:00
MacDue
a6505f6e6d LibWeb: Allow % height of a % height parent in block-formatted elements
With this you can start to see Francine's face in the CSS oil painting
(https://diana-adrianne.com/purecss-francine/)
2022-08-07 22:52:22 +02:00
MacDue
0e8dafcbb8 LibWeb: Allow absolute boxes to have a % height of a % height parent
Previously absolutely positioned boxes could only have a % height if
their parent had a absolute height (a height in pixels, em, etc).

This broke some websites/demos such as the "Francine CSS oil painting",
which starts to appear after this commit.

Francine: https://diana-adrianne.com/purecss-francine/
2022-08-07 22:52:22 +02:00
Tom
8163ee1500 LibWeb: Use rect value in CSS clip property
When a rect value is passed to the clip property via CSS, keep it in
ComputedValues so that at a later stage can make use of it.
2022-08-07 22:40:11 +02:00
Andreas Kling
5f34c8ab03 LibWeb: Make automatic heights for abspos non-replaced behave better
Previously we were checking if values were "auto" after resolving the
"auto"-ness out of them, which didn't work. There's still a bunch of
work to do on this algorithm, but now we can at least resolve some basic
automatic height scenarios.
2022-07-26 01:53:41 +02:00
Andreas Kling
0258fd8043 LibWeb: Remove FFC helpers is_{main,cross}_axis_margin_first_auto()
We already cache these values with each flex item, so let's just use
those instead.
2022-07-26 01:53:41 +02:00
Andreas Kling
71ec46b8cc LibWeb: Don't treat calc() in used flex basis as definite for now
We'll eventually need some way to determine if a calc() value results
in a definite size.
2022-07-26 01:53:41 +02:00
Andreas Kling
7200b3a16c LibWeb: Make two passes in FFC where to support percentage min/max sizes
Percentage sizes of flex items are relative to the flex container, but
even when the flex container is automatically sized, we still have to
support them.

To make this work, we first do a pass where percentage sizes are ignored
(treated as "auto", basically) in order to get a "reference" value.

Then we do a second pass where percentages can be resolved against this
reference value.
2022-07-26 01:53:41 +02:00
Andreas Kling
3def9e47f6 LibWeb: Improve min/max content contribution calculations for flex items
Follow the spec more closely.
2022-07-26 01:53:41 +02:00
Andreas Kling
4c89252f6a LibWeb: Clamp flex items to min/max main size during intrinsic sizing
We were neglecting to clamp flex items when calculating the intrinsic
main size of the flex container. This was covered by a FIXME, which we
can now remove. :^)
2022-07-26 01:53:41 +02:00
Andreas Kling
dc66a3cad9 LibWeb: Simplify flex container main size determination
We had an older incomplete implementation of flex container max-content
sizing that we used for unconstrained main size determination.

This patch replaces the old implementation with a call to the new
max-content sizing code.

Note that this isn't a complete implementation of flex container sizing
still, but at least we've deduplicated some code.
2022-07-26 01:53:41 +02:00
Andreas Kling
18b1854aea LibWeb: Add FFC helpers for getting the computed main/cross size of box 2022-07-26 01:53:41 +02:00
Andreas Kling
a4d7dc40bf LibWeb: Move get_pixel_{width,height} helpers into FFC class 2022-07-26 01:53:41 +02:00
Andreas Kling
60ac258a48 LibWeb: Mark flex item cross sizes as definite when spec asks us to
The CSS-FLEXBOX-1 spec gives us two situations in which flex item cross
sizes should be considered definite. Both of them happen *during* flex
layout, which is super finicky but it is what it is.
2022-07-26 01:53:41 +02:00
Andreas Kling
83bb16ede3 LibWeb: Avoid some unnecessary inside layouts during intrinsic sizing
When calculating intrinsic sizes, we don't need to recurse into *every*
box and layout its insides. IIUC, we can skip any unconstrained box with
definite sizes in both axes. So this patch does exactly that.
2022-07-26 01:53:41 +02:00
Andreas Kling
3ede8dbffb LibWeb: Rename IntrinsicSizeDetermination to IntrinsicSizing
This matches the exact terminology used in CSS-SIZING-3:
https://drafts.csswg.org/css-sizing-3/#intrinsic-sizing
2022-07-26 01:53:41 +02:00
Andreas Kling
97f53de8a2 LibWeb: Take size constraints into account in fit-content calculations
Also avoid calculating both min-content and max-content sizes when only
one of them is needed.
2022-07-26 01:53:41 +02:00
Andreas Kling
8f35c9a037 LibWeb: Resolve flexbox percent heights against containing block height
Percentage values for `min-height` and `max-height` should be resolved
against the containing block height, not its width.
2022-07-26 01:53:41 +02:00
Andreas Kling
7cc6d1da2d LibWeb: Resolve definite sizes when instantiating UsedValues
When we decide that a box has definite width or height based on its
containing block's corresponding size, we'll want to resolve the
current box's size as well. Otherwise anyone querying the size on
this box will get the bogus message of "yes, this definite, and its
value is zero."
2022-07-26 01:53:41 +02:00
Andreas Kling
4cbec00c44 LibWeb: Actually check if percentage used flex basis is definite
Previously, we considered all LengthPercentage values for used flex
basis to be definite. This is not accurate, as percentages should only
be considered definite if the reference value they resolve against is
a definite size.

Fix this by checking the flex container's main definite size flag.
2022-07-26 01:53:41 +02:00
Andreas Kling
8bdc6f5390 LibWeb: Don't override main size during flex item cross sizing
There's no need to override the sizes before calculating the cross size.

Besides, by the time we're calculating the hypothetical cross size of
flex items, we may already have established a definite main size anyway,
so overriding it would be wrong.
2022-07-26 01:53:41 +02:00
Andreas Kling
69243947d5 LibWeb: Some flex items have definite size after flexing
This patch implements two of the special "definite size" rules from
the CSS-FLEXBOX-1 spec.

https://drafts.csswg.org/css-flexbox-1/#definite-sizes
2022-07-26 01:53:41 +02:00
Andreas Kling
02c59fe8c9 LibWeb: Containing block always has definite width during abspos layout
From CSS-SIZING-3:

"...the size of the containing block of an absolutely positioned element
is always definite with respect to that element."
2022-07-26 01:53:41 +02:00
Andreas Kling
71a707480c LibWeb: Move "has-definite-width/height" flags to UsedValues
This state is less static than we originally assumed, and there are
special formatting context-specific rules that say certain sizes are
definite in special circumstances.

To be able to support this, we move the has-definite-size flags from
the layout node to the UsedValues struct instead.
2022-07-26 01:53:41 +02:00
Andreas Kling
bd48d9521a LibWeb: Simplify more code with CSS::LengthPercentage::is_auto() 2022-07-26 00:04:21 +02:00
Andreas Kling
fac4529082 LibWeb: Honor flex container sizing constraints in available space 2022-07-25 15:03:18 +02:00
Andreas Kling
050e70cc7e LibWeb: Position abspos children of flex container after parent layout
If we wait until after the parent context has laid out the flex
container, abspos children are able to use the final results of the
parent sizing the flex container.

This makes `height:auto` work on abspos children of a flex container.
2022-07-25 15:03:18 +02:00
Andreas Kling
7aa9e03e85 LibWeb: Simplify some code with CSS::LengthPercentage::is_auto() 2022-07-25 15:03:18 +02:00
Andreas Kling
3145f92bc0 LibWeb: Count flex item's *outer* size towards container intrinsic size
This isn't in the spec, but it's visually obvious that we need to count
item margin boxes towards the main intrinsic size of the flex container.
2022-07-25 15:03:18 +02:00
Andreas Kling
b71f747453 LibWeb: Add helpers for adding margin box to flex item's content size 2022-07-25 15:03:18 +02:00
MacDue
57c6792458 LibWeb: Support appearance: none for <progress> elements
This disables this system progress bar, and instead creates one
out of pseudo elements, that can be selected and styled with the
::-webkit-progress-bar/value selectors.
2022-07-23 01:45:49 +02:00
MacDue
d7d34d88e5 LibWeb: Implement appearance CSS property
This includes the "compat" values even though they are not strictly
necessary.
2022-07-23 01:45:49 +02:00
Andreas Kling
237fbe4d54 LibWeb: Remember the used flex basis for each flex item
This will be consulted later on in the flex layout algorithm.
2022-07-21 01:46:26 +02:00
Andreas Kling
b7003194d2 LibWeb: Use right offset for justify-content: flex-end
Offsets in this algorithm are relative to the starting position, so it
should be 0 whether its `flex-start` or `flex-end`.
2022-07-21 01:46:25 +02:00
Andreas Kling
e095b9c6f9 LibWeb: Resolve auto containing block sizes before intrinsic sizing
Before performing intrinsic sizing layout on a box, we now check if its
containing block has automatic size in the relevant axis, and if so,
we fetch the size of the nearest containing block ancestor with a size.
2022-07-21 01:46:25 +02:00
Andreas Kling
da0cc9d401 LibWeb: Floor hypothetical main size at 0
The spec asks us to do this. It doesn't become relevant until we
implement box-sizing, but might as well do it anyway.
2022-07-21 01:46:25 +02:00
Andreas Kling
ed8930fff5 LibWeb: Add accessors for UsedValues::computed_{width,height}
This is preparation for doing some more work when assigning to these
values.
2022-07-19 15:40:41 +02:00
Andreas Kling
c517dfde12 LibWeb: Honor the flexbox spec's "automatic minimum size" everywhere
`min-width: auto` and `min-height: auto` have special behavior on flex
items. We already handled it in many cases but there were two places
where it was incorrectly treated as 0. This fixes that.
2022-07-19 12:45:50 +02:00
MacDue
ee7e9e7c86 LibWeb: Allow having a linear-gradient() as a background-image 2022-07-17 20:11:38 +01:00
Andreas Kling
9b46091f38 LibWeb: Rename LayoutState::NodeState => LayoutState::UsedValues
This object contains all the CSS "used values" as seen during the layout
process, so calling it "used values" seems appropriate. :^)
2022-07-17 14:11:37 +02:00
Andreas Kling
52862c72d0 LibWeb: Rename FormattingState to LayoutState
This seems a bit more descriptive (and also a bit shorter).
2022-07-17 14:11:36 +02:00
Andreas Kling
0d8f9019c8 LibWeb: Implement basic stretch alignment for flex-wrap: wrap
We currently ignore the `align-content` property, that's a FIXME
for our future selves.
2022-07-17 14:11:36 +02:00
Andreas Kling
9fc43d5766 LibWeb: Update flex container intrinsic size algorithm to draft spec
The CSSWG draft Flexbox spec is a bit clearer on some details, so let's
update our implementation to match the latest version.
2022-07-17 14:11:36 +02:00
Andreas Kling
884b7fad48 LibWeb: Remove weird is_undefined_or_auto() helper in FFC
This was a leftover from when ComputedValues stored sizes in Optionals.
Now that we've gotten rid of the "undefined" state, there's no need for
this helper, we can just access the size values directly.
2022-07-15 14:11:19 +02:00
Andreas Kling
0636e1db61 LibWeb: Update flex line "remaining free space" when finished
Since we re-use this value later in the layout algorithm, we have to
update it before leaving the "resolve flexible lengths" step.
2022-07-15 14:11:19 +02:00
Andreas Kling
d2b887a793 LibWeb: Only create one wrapper for inline content inside flex container
Due to a missing `return` statement, we were creating two anonymous
wrapper blocks around each piece of inline content inside a flex
container.

This had no visual impact, since they ended up with 0x0 dimensions,
but we were wasting a fair bit of time running layout on them.
2022-07-13 01:26:29 +02:00
Andreas Kling
7d7d5f0b1b LibWeb: Use fit-content size for flex items with indefinite cross size 2022-07-12 23:12:11 +02:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
sin-ack
c70f45ff44 Everywhere: Explicitly specify the size in StringView constructors
This commit moves the length calculations out to be directly on the
StringView users. This is an important step towards the goal of removing
StringView(char const*), as it moves the responsibility of calculating
the size of the string to the user of the StringView (which will prevent
naive uses causing OOB access).
2022-07-12 23:11:35 +02:00
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
31fc1990fa LibWeb: Set up both containing block sizes before intrinsic sizing
When calculating e.g the min-content height of some box, we would only
set its containing block's height to 0 in the temporary formatting
state. The containing block width was not touched at all.

This patch aims to do a bit better by resolving indefinite containing
block sizes to INFINITY before performing intrinsic sizing. We should
probably also find a way to resolve definite containing block sizes,
but I'll leave that for our future selves.
2022-07-12 02:46:21 +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
96c9ca502b LibWeb: Add safety mechanism to guard against non-finite layout sizes
Due to some yet-to-be-found bug(s) in intrinsic sizing, we can sometimes
end up deciding that some box has a non-finite intrinsic size.

This will create unpleasant results, and may lead to some layout
algorithms looping infinitely, so this patch adds a safeguard where
we simply turn non-finite intrinsic sizes into zero (and complain a
little bit in the debug log.)
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
0cacaf025d LibWeb: Stop putting the FormattingState nodes in a slow hash map
Instead, put them in a Vector<OwnPtr<NodeState>>. Each layout node
has a unique index into the vector. It's a simple serial ID assigned
during layout tree construction. Every new layout restarts the sequence
at 0 for the next ICB.

This is a huge layout speed improvement on all content.
2022-07-11 18:57:45 +02:00
Andreas Kling
327938c945 LibWeb: Bring sizing of replaced elements closer to spec
We were prematurely resolving the computed size values, which meant
that `auto` values were swallowed before we could resolve them via
the intrinsic aspect ratio (if present)
2022-07-11 18:57:45 +02:00
Andreas Kling
17a6fcfde3 LibWeb: Try to work out the intrinsic size of <svg> elements
If the `width` and `height` attributes are provided, we derive the
intrinsic size and ratio from them.

Otherwise, we trace a rectangle around the geometry elements inside
the SVG and use the size of that as the intrinsic size.

This is definitely far from correct, but is still a much better guess
at the intrinsic size than nothing.
2022-07-11 18:57:45 +02:00
Andreas Kling
7dfd5afd37 LibWeb: Set 1:1 intrinsic aspect ratio for radio buttons 2022-07-11 18:57:45 +02:00
Andreas Kling
557006f5be LibWeb: Cache reference to <svg> element in SVGFormattingContext 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
61c27815e4 LibWeb: More specialization of intrinsic sizing layout
This patch adds a separate entry point for this kind of layout.
We override it in BFC to set up initial width/height values for the
BFC root block.

Resulting dimensions are assigned as content_width and content_height
at the end of intrinsic sizing, for each axis, if it's either "auto"
or there's a min-content or max-content constraint in effect.
2022-07-11 18:57:45 +02:00
Andreas Kling
28eec22c83 LibWeb: Don't iterate over text content inside replaced elements
This fixes an issue where whitespace inside embedded <svg> elements
would create unexpected whitespace text content on the page.

When combined with something like `white-space: pre-wrap`, it ended
up generating a lot of surprising vertical offsets.
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
e78282aebb LibWeb: Make sure we always apply size constraints in IFC
Pre-compute the effective containing block width in the IFC constructor
and use that throughout.
2022-07-11 18:57:45 +02:00
Andreas Kling
7713198e9c LibWeb: Cache a pointer to the containing block state in IFC 2022-07-11 18:57:45 +02:00
Andreas Kling
9784a567d2 LibWeb: Factor out BFC "layout this block-level box" to a function 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
66d08d2417 LibWeb: Move IFC result measurement from IFC to BFC
Before this change, IFC would first generate all of its line boxes
and then measure them. It would then write some of the values into
the state of the IFC's containing block.

We now move that up to BFC::layout_inline_children() instead, which
is a much more natural place to decide metrics for the block.
2022-07-11 18:57:45 +02:00
Andreas Kling
fd68be29ab LibWeb: Make BFC always drive IFC
Instead of allowing FormattingContext to instantiate an IFC for anything
that has inline children, move this logic to BFC.

This is fine, since only BFC deals with blocks having inline children
anyway.
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
MacDue
61a703816c LibWeb: Base marker size on font height rather than line height
This fixes the oversized markers on GitHub
2022-07-09 09:28:31 +01:00
Andreas Kling
8abbbdf6fa LibWeb: Cache a pointer to the IFC root in InlineLevelIterator 2022-07-06 20:31:27 +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
83a6be593c LibWeb: Add a 1-entry lookup cache to FormattingState
This makes repeated lookups of the state for the same box much faster
by bypassing the HashMap.
2022-07-06 20:31:22 +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
MacDue
28c78b45ca LibWeb: Keep both horizontal and vertical border radii till painting 2022-06-13 09:43:45 +01:00
Karol Kosek
a0bea0b86f LibWeb: Calculate floating elements width using min- and max-width
Previously, floating elements computed the width by only using the
`width` property. Now, they will also use the `min-width` and
`max-width` properties. :^)

The new steps are from "10.4. Minimum and maximum widths: 'min-width'
and 'max-width'" in the CSS 2 spec.

Found it when looking at curl.se.
2022-06-06 09:14:42 +01:00
Sam Atkins
7c91fda088 LibWeb: Allow multiple text-decoration-lines
The spec grammar for `text-decoration-line` is:

`none | [ underline || overline || line-through || blink ]`

Which means that it's either `none`, or any combination of the other
values. This patch makes that parse for `text-decoration-line` and
`text-decoration`, stores the results as a Vector, and adjusts
`paint_text_decoration()` to run as a loop over all the values that are
provided.

As noted, storing a Vector of values is a bit wasteful, as they could be
stored as flags in a single `u8`. But I was getting too confused trying
to do that in a nice way.
2022-04-14 21:54:10 +02:00
Sam Atkins
e941f07931 LibWeb: Make StyleProperties::property() always return a value
By the time that property() gets called, we've already given every
single property a value, so we can just return it. This simplifies a
lot of places that were manually handling a lack of value
unnecessarily.
2022-04-14 14:54:06 +02:00
Sam Atkins
a797a92990 LibWeb: Stop including StyleValue.h in a few places
I don't know how much this will actually help compile times, but it's
something!
2022-04-14 14:54:06 +02:00
Sam Atkins
4d42885327 LibWeb: Return Optional from StyleProperties::box_sizing()
This function was written as if it returned `Optional<CSS::BoxSizing>`
but actually returned a plain `CSS::BoxSizing`, meaning if the property
was not set or was invalid, it would return whichever enum value was
first. This wasn't visible because we don't yet pay any attention to
the `box-sizing` property.
2022-04-14 14:54:06 +02:00
Andreas Kling
6712bbc0ee LibWeb: Tidy up Layout::TreeBuilder ancestor stack a little bit
- Make it a vector of references since we never put null pointers on
  the ancestor stack.

- Use Vector::in_reverse() to iterate backwards.
2022-04-13 19:52:25 +02:00
Andreas Kling
de5de4d99a LibWeb: Fix typo in CSS::Display::is_outside_and_inside() name 2022-04-13 16:40:46 +02:00
Andreas Kling
b3d85afd92 LibWeb: Make Layout::Box::set_needs_display() work for all boxes
For inline-blocks and inline replaced elements, we previously fell into
a code path that tried to find a corresponding line box fragment to
invalidate. However, we don't need to do any of that, all we need to do
is get the absolute rect from our paintable, and invalidate that.

This makes CRC2D invalidations happen immediately instead of as a side
effect of some other invalidation.
2022-04-12 00:11:48 +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
fc8c4ea23f LibWeb: Let BFC compute width for block-level replaced elements
We never really hit this code path before, but now that IMG elements can
be block-level, we need to get them hooked up with box model metrics.
2022-04-11 01:04:09 +02:00
Andreas Kling
edfa4508a5 LibWeb: Create a no-op formatting context for childless replaced boxes
This is a hack that allows block-level replaced elements to be flex
items. Flexbox layout currently assumes (in many places) that it's
always possible to create an independent formatting context for each of
its items.
2022-04-11 01:03:47 +02:00
Andreas Kling
ca2807ba42 AK: Honor box's own intrinsic size in calculate_intrinsic_sizes()
If a layout box claims to have both intrinsic width and height, we can
simply return those instead of performing any kind of layout.
2022-04-11 00:11:53 +02:00
Andreas Kling
5e35167e05 LibWeb: Don't mark non-painted images as being inside the viewport 2022-04-10 20:16:09 +02:00
Andreas Kling
20c65e4298 LibWeb: Don't include children of overflow:hidden in scrollable overflow
This fixes an issue where Google Maps was getting page scrollbars
when it shouldn't have.
2022-04-10 20:02:53 +02:00
Simon Wanner
206d6ece55 LibGfx: Move other font-related files to LibGfx/Font/ 2022-04-09 23:48:18 +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
45db35ad04 LibWeb: Fix double-sized box model metrics on inline replaced elements
We were mistakenly treating inline replaced elements as if they are the
start of a regular display:inline element. This meant that we collected
the horizontal start and end metrics from the box model, and then added
those to the inline-level item produced by InlineLevelIterator.

This effectively meant that <img>, <svg> and other replaced elements got
double-sized values for margin/border/padding on the left and right
sides. (Which manifested as a mysterious margin around the element.)
2022-04-08 23:12:04 +02:00
Simon Wanner
a57bfc2f8c LibWeb: Take already computed height for flex container's auto height
Similar to BlockFormattingContext, FlexFormatting context already
handles height:auto sizing correctly and sets the content_height
correspondingly.
2022-04-08 20:44:23 +02:00
Andreas Kling
85dd891811 LibWeb: Ignore non-painting layout nodes when building stacking contexts 2022-04-07 17:06:02 +02:00
Andreas Kling
be26818448 LibWeb: Rename compute_intrinsic_height() => calculate_auto_height()
Change "compute" to "calculate" to make clearer that this is unrelated
to the CSS "computed height" concept.

Change "intrinsic" to "auto" to make clearer that this is not the same
as the intrinsic min-content and max-content sizing calculations.
2022-04-06 14:43:00 +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
33887917e4 LibWeb: Determine intrinsic flex container size from content-size
When running the min-content and max-content sizing algorithms and the
target box creates a flex formatting context, we don't need to measure
its children.

FFC has already assigned the content_width and content_height values,
so we just need to pick those up from the container's formatting state.
2022-04-06 11:58:32 +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
Simon Wanner
509362c103 LibWeb: Include all row-groups in column width calculations
This was noticeable for example on fonts.serenityos.net, where the
thead and tfoot would not get the same column widths as the tbody.
2022-04-03 23:12:13 +02:00
Enver Balalic
7381474835 LibWeb: Don't break text lines if white-space: nowrap
If white-space is nowrap then we don't want to break a text_node
into multiple line boxes. This fixes the width calculation in the
min-content case for white-space: nowrap elements. Before this
the min-width would be the width of the biggest line box.
2022-04-03 21:51:15 +02:00
Enver Balalic
d8ec9a5501 LibWeb: Fix computing_width of a block when LayoutMode::MaxContent
We had an issue with computing a width of a block in MaxContent
mode because we would set the width of the containing block to
infinity which would make everything in the try_compute_width
block infinity as well.

This adds a special case for width = 'auto' when
width_of_containing_block is infinity and removes the width of
containing block from the equation entirely.
2022-04-03 12:58: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
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01: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
74d8e201eb LibWeb: Fix calculating the intrinsic height of a box
For computing height in FormattingContext::calculate_intrinsic_sizes
we were calling into BlockFormattingContext::compute_theoretical_height
which will check if the CSS height property was defined and calculate
the height based on that instead of calculating the intrinsic height

This patch adds a new function calculate_intrinsic_height, which will
call into compute_auto_height_for_block_level_element for a block
element, or into compute_height_for_replaced_element for a replaced
element.
2022-03-30 21:16:47 +02:00
Andreas Kling
6a4247bee9 LibWeb: Use more precise font metrics when doing inline layout
We now position inline-level boxes based on ascent and descent metrics
from the font in use. This makes our basic text layouts look a lot more
like those produced by other browsers. :^)

I've tried to match the terminology used by the CSS Inline Layout spec.

This will regress Acid2 a little bit, and probably various other sites,
but on the whole it's the direction we should be heading, so let's go.
2022-03-30 01:12:44 +02:00
Andreas Kling
9b0d158e69 LibWeb: Only set children-are-not-inline when inserting in-flow child
We were marking block boxes as having non-inline children when inserting
any child box, even if the child was out-of-flow.
2022-03-30 00:57:15 +02:00
Andreas Kling
1c88536298 LibWeb: Use the new Gfx::Painter::draw_text_run() API for drawing text
This avoids a bunch of unnecessary work in Painter which not only took
time, but sometimes also led to alignment issues. draw_text_run() will
draw the text where we tell it, and that's it.
2022-03-30 00:57:15 +02:00
Andreas Kling
0fd51ae811 LibWeb: Ignore empty anonymous block children in height:auto calculation
This fixes a regression on GitHub from
5da7ebb806.

Thanks to Simon for reporting it! :^)
2022-03-29 22:30:58 +02:00
Andreas Kling
e7eb6241c2 LibWeb: Ignore list-item marker boxes in height:auto calculation 2022-03-29 21:50:31 +02:00
Andreas Kling
5da7ebb806 LibWeb: Make height:auto for non-BFC-root blocks more correct
Unlike BFC root blocks with height:auto, when the block *isn't* a BFC
root, we don't have to look for the "bottommost" block-level child and
determine the width from that.

Instead, we should just look at the last in-flow block-level child.
This was already indicated in the spec comment next to the code, but
the code itself was wrong.

This makes the body element on Acid3 have the correct height. It also
introduces a small regression on Acid2 that we'll have to track down.
2022-03-29 18:53:27 +02:00
Andreas Kling
f4ed4b2806 LibWeb: Swallow whitespace when it causes us to break the line
This fixes an issue seen on Acid3 where the instruction text would shift
around when moving from "white-space:pre-wrap" to "white-space:normal".
2022-03-29 13:37:54 +02:00
Andreas Kling
9711e1b303 LibWeb: Make floating boxes in IFC occupy horizontal margin box
Previously, we only allowed floats to take up its own border box's worth
of horizontal space when laid out inside an IFC.

We should instead consume the full margin box horizonally. This fixes an
issue where a floated box on Acid3 had {width:20px; margin-right:-20px;}
but still consumed 20px of the previously available space, despite being
moved out of the way by its own negative margin.
2022-03-29 12:35:31 +02:00
Andreas Kling
5def3b0150 LibWeb: Use LineBox::height() when determining IFC auto heights
We don't need to loop through all the fragments on the line to work out
how tall it is. Just ask for the height. :^)
2022-03-29 12:25:34 +02:00
Andreas Kling
03008d9c9c LibWeb: Ensure that TextNode::ChunkIterator emits preserved newlines
When doing max-content layout, we were not committing newlines even
though we were supposed to due to white-space:pre*.

This broke the WPT harness due to a VERIFY() in ChunkIterator where we
were assuming the commit would always succeed.

Thanks to Orphis for reporting this! :^)
2022-03-29 03:05:11 +02:00
Andreas Kling
a6eb031058 LibWeb: Use correct top content edge when calculating auto block heights
When the spec tells us to measure from the top content edge of a block,
that just means we should measure from Y=0. We don't need to go looking
for a child box with a negative top offset and measure from there.
2022-03-29 02:59:00 +02:00
Simon Wanner
7e4793df63 LibWeb: Size table cells using a combination of min- and max-widths
This gets us a bit closer to the recommended algorithms in CSS 2.2 and
CSS Table Module 3.

A couple of table heavy websites (e.g. news.ycombinator.com,
html5test.com, etc.) now look quite okay. :^)
2022-03-29 00:39:57 +02:00
Simon Wanner
b92cc3670b LibWeb: Only size width: auto table-cells by min-content 2022-03-29 00:39:57 +02:00
Simon Wanner
304dbb0242 LibWeb: Set a cell's content width based on the column(s) it's in 2022-03-28 15:25:25 +02:00
Simon Wanner
6dbd00970d LibWeb: Calculate a cell's width using min-content 2022-03-28 15:25:25 +02:00
Simon Wanner
e5a779aecf LibWeb: Apply the layout_mode argument in BFC::compute_width 2022-03-28 15:25:25 +02:00
Simon Wanner
9b3229da17 LibWeb: Distribute the width of a table cell spanning multiple columns 2022-03-28 15:25:25 +02:00
Simon Wanner
4fe154bd6a LibWeb: Expand the last cell in a row to the right edge 2022-03-28 15:25:25 +02:00
Simon Wanner
5f265eebf5 LibWeb: Apply a table row's height to all cells 2022-03-28 15:25:25 +02:00
Andreas Kling
3ac9ea369d LibWeb: Don't choke on ICB with inline children
Let's relax our assumption about what kind of children the ICB has.
This is preparation for loading XHTML documents.
2022-03-27 23:38:27 +02:00
Andreas Kling
4575ab558b LibWeb: Make whitespace collapsing stateless
Previously, the whitespace collapsing code had a parameter telling it
whether the previous text node ended in whitespace. This was not
actually necessary, so let's get rid of it.
2022-03-27 21:56:21 +02:00
Andreas Kling
f9e8f02451 LibWeb: Don't crash in IFC if very first chunk is collapsible whitespace 2022-03-27 21:06:02 +02:00
Andreas Kling
27c68624b6 LibWeb: Resolve top/bottom inset properties for position:relative
This patch reimplements inset property resolution based on the new
CSS Positioned Layout specification. Nothing should change for
left/right insets, but we gain support for top/bottom. :^)
2022-03-27 18:16:09 +02:00
Andreas Kling
fa71401bec LibWeb: Rename ComputedValues::offset() => inset() 2022-03-27 18:16:08 +02:00
Andreas Kling
d77dfc6b48 LibWeb: Rename FormattingContext::compute_position() => compute_inset()
This function computes the used inset properties, not the position of a
box per se, so let's call it something more accurate.
2022-03-27 18:16:08 +02:00
Andreas Kling
c49c036c84 LibWeb: Stop allowing position:relative to affect layout
Relatively positioned boxes should not affect the *layout* of their
siblings. So instead of applying relative inset as a layout-time
translation on the box, we now perform the adjustment at the paintable
level instead.

This makes position:relative actually work as expected, and exposes some
new bugs we need to take care of for Acid2. :^)
2022-03-27 18:16:08 +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
b0208f38f6 LibWeb: Use Gfx::Font::pixel_size() when we want pixel metrics
This gives us consistent results with both bitmap and scalable fonts.
2022-03-27 01:14:56 +01:00
Andreas Kling
83a2aa1832 LibWeb: Include negative margins in height:auto computation for BFC root
...but never allow the resulting height to become negative. This solves
an issue seen on Acid3 where elements with negative vertical margins
expanded the size of their height:auto container instead of shrinking
it, which is the correct behavior. This now works :^)
2022-03-26 22:51:10 +01:00
Andreas Kling
0d9c28add9 LibWeb: Don't collapse horizontal margins between floating boxes
CSS 2.2 says "Horizontal margins never collapse."

So instead of collapsing them, we now add them together, which makes
negative margins between floating boxes work beautifully.
2022-03-26 22:12:21 +01:00
Andreas Kling
fa99259412 LibWeb: Simplify text chunk iteration a little bit
Instead of TextNode::ChunkIterator having two bool members to remember
things across calls to next(), this patch reorganizes the loop in next()
so that preserved newline/whitespace chunks are emitted right away
instead of in an awkward deferred way.
2022-03-26 20:04:56 +01:00
Andreas Kling
dd6a0dd0f7 LibWeb: Remove unused declarations from Layout::TextNode 2022-03-26 20:04:56 +01:00
Andreas Kling
aefe1727fc LibWeb: Make text newlines in "pre" mode emit a ForcedBreak item
Instead of emitting a Text item with the "should_force_break" flag set
to true, newlines in newline-preserving text content now timply turn
into ForcedBreak items. This makes the <pre> element work again.
2022-03-26 20:04:56 +01:00
Andreas Kling
d32630e17b LibWeb: Don't append collapsible whitespace to start of new line
After performing a required line break, and the next text chunk is
all collapsible whitespace, simply discard the whitespace.
2022-03-26 20:04:56 +01:00
Andreas Kling
fe908e7db2 LibWeb: Rename "offset" in box model metrics to "inset"
The CSS Positioned Layout spec refers to the top/left/bottom/right
properties as "inset" properties, so let's use the same terminology.
2022-03-26 17:31:01 +01: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
Andreas Kling
925c34cf43 LibWeb: Include floats in height:auto for BFC root with inline children
BFC roots with children_are_inline()==true can still have floating boxes
as well. children_are_inline() is only concerned with in-flow children.

For this reason, we have to always consider floats when calculating
height:auto for BFC roots.
2022-03-26 00:15:25 +01:00
Andreas Kling
c02e6f991a LibWeb: Improve vertical margin collapse between adjacent blocks
Collect all the preceding block-level siblings whose vertical margins
are collapsible. Both margin-top and margin-bottom now (previously,
we only considered the margin-bottom of siblings.)

Use the right margin in part-negative and all-negative situations.
2022-03-25 00:10:09 +01:00
Andreas Kling
6cffabef03 LibWeb: Support CSS vertical-align values "top" and "bottom" 2022-03-24 22:57:01 +01:00
Andreas Kling
195ef5e26f LibWeb: Bring CSS line-height implementation closer to spec
We now distribute the line-height evenly between the space above and
below inline-level boxes. This noticeably improves our baseline
alignment in many cases.

Note that the "vertical-align: <length>" case is quite awkward, as the
extra height added by the offset baseline must count towards the line
box height.

There's a lot of room for improvement here, but this makes the buckets
container on Acid3 show up in the right place, with the right size.
2022-03-24 22:52:44 +01:00
Andreas Kling
ed1576eea8 LibWeb: Align baseline of inline-block with non-zero top border/padding
We were not adjusting the fragment baseline for inline-blocks that had
some border and/or padding size up top.
2022-03-24 18:30:56 +01:00
Andreas Kling
54ffe79d70 LibWeb: Fix missing line-height & font-size on some anonymous blocks
Make sure we use the create_anonymous_wrapper() helper function whenever
wrapping inline content in anonymous wrapper blocks. We were forgetting
to do this in one case, which led to some wrapper blocks having 0px
font-size and line-height.
2022-03-24 18:30:56 +01:00
Andreas Kling
30b1772eeb LibWeb: Rename "specified_style" to "computed_style" in Layout::Node
The specified style was turned into computed style long before it
arrived here (StyleComputer took care of that.)
2022-03-24 18:14:01 +01:00
Andreas Kling
40af665a49 LibWeb: Add margin box helpers to Layout::FormattingState 2022-03-24 18:14:01 +01:00
Andreas Kling
329f06d59a LibWeb: Add Layout::Node::line_height()
This allows you to call line_height() on any layout node, even if it's a
text node (in which case we'll ask the parent node for its line-height.)
2022-03-24 18:14:01 +01:00
Sam Atkins
03daa4653f LibWeb: Parse and compute text-shadow property 2022-03-24 18:08:34 +01:00
Timothy Flynn
5608bc4eaf LibWeb: Remove inheritance of FormAssociatedElement from HTMLElement
HTMLObjectElement will need to be both a FormAssociatedElement and a
BrowsingContextContainer. Currently, both of these classes inherit from
HTMLElement. This can work in C++, but is generally frowned upon, and
doesn't play particularly well with the rest of LibWeb.

Instead, we can essentially revert commit 3bb5c62 to remove HTMLElement
from FormAssociatedElement's hierarchy. This means that objects such as
HTMLObjectElement individually inherit from FormAssociatedElement and
HTMLElement now.

Some caveats are:

* FormAssociatedElement still needs to know when the HTMLElement is
  inserted into and removed from the DOM. This hook is automatically
  injected via a macro now, while still allowing classes like
  HTMLInputElement to also know when the element is inserted.

* Casting from a DOM::Element to a FormAssociatedElement is now a
  sideways cast, rather than directly following an inheritance chain.
  This means static_cast cannot be used here; but we can safely use
  dynamic_cast since the only 2 instances of this already use RTTI to
  verify the cast.
2022-03-24 03:35:11 +01:00
Andreas Kling
e0c7727934 LibWeb: Fill the whole viewport with the correct background color
CSS2 tells us to use the HTML element's background color if not
transparent. Otherwise, the BODY element's background color.
2022-03-23 17:38:00 +01:00
Andreas Kling
fb9ee26c43 LibWeb: Resolve numeric line-heights against element's own font size
For things like "line-height: 2" to work, the font size must be assigned
before resolving the line height. Previously, the line-height was
resolved first, which meant that numeric and other relative units were
resolved against the default font-size instead.
2022-03-23 15:57:05 +01:00
Andreas Kling
5118a4c1e7 LibWeb: Parse CSS "font-variant" as part of "font"
This allows us to parse CSS "font" values that contain e.g "small-caps"
or "normal", as used on Acid3.
2022-03-23 14:54:21 +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
de6f7f0029 LibWeb: Support CSS floats in inline flow
CSS floats are now emitted by the InlineLevelIterator. When this
happens, IFC coordinates with the parent BFC to float the box to the
side, using the current LineBuilder state for vertical placement.

This makes the "instructions" text on Acid3 render as a single
contiguous flow of inline content.
2022-03-22 19:26:51 +01:00
Andreas Kling
fa64a7f6cc LibWeb: Relax "all children must be either inline or block" invariant
This was implemented too rigidly, which made it impossible to place
floats correctly when they occurred in inline flow.

The new invariant is "all in-flow children must be either inline or
block". Out-of-flow children like floating and absolutely positioned
boxes are ignored when deciding when to generate anonymous boxes.
2022-03-22 19:26:51 +01:00
Andreas Kling
74927fd218 LibWeb: Move Layout::Box::is_out_of_flow() to Layout::Node
I want to use this function in inline layout, where we're not just
dealing with boxes.
2022-03-22 19:26:51 +01:00
Simon Wanner
145efbe07a LibWeb: Apply the CSS transform-origin property
We don't have transform-box yet, so this applies to the border-box
for now.

This also makes us pass a couple Web Platform Tests as well :^)
For example:
https://wpt.live/css/css-transforms/css3-transform-scale-002.html
2022-03-22 02:06:21 +01:00
Karol Kosek
686507a38f LibWeb: Translate table cells by their top left border 2022-03-21 21:55:21 +01:00
Karol Kosek
20730c164c LibWeb: Include table cell border widths when calculating cell rects
Previously, table cells would overlap when they had CSS border or
padding properties defined.
2022-03-21 21:55:21 +01:00
Andreas Kling
df8ef03957 LibWeb: Pick up the CSS "visibility" property an honor it when painting 2022-03-21 15:42:57 +01:00
Andreas Kling
59afdb959f LibWeb: Build stacking context tree lazily
There's no actual need to build the stacking context tree before
performing layout. Instead, make it lazy and build the tree when it's
actually needed for something.

This avoids a bunch of work in situations where multiple synchronous
layouts are forced (typically by JavaScript) without painting or hit
testing taking place in between.

It also opens up for style invalidations that only target the stacking
context tree.
2022-03-21 13:03:33 +01:00
Andreas Kling
159d4d772a LibWeb: Assign the used top/bottom border widths to inline-block boxes
We were forgetting to update these values, which led to missing top and
bottom borders for elements with display:inline-block.
2022-03-20 13:36:45 +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
83afc1154c LibWeb: Fix IFC over-shrinking the available space for line boxes
After accounting for left-side floats, we have to subtract the offset of
the IFC's containing block again, to get the real starting X offset
for the current line.

This was done correctly in leftmost_x_offset_at() but incorrectly in
available_space_for_line(), causing IFC to break lines too early in
cases where the containing block had a non-zero X offset from the BFC
root block.
2022-03-19 12:42:10 +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
Andreas Kling
28b771560a LibWeb: Make SVG <svg> elements behave as CSS replaced elements
This makes SVG-in-HTML behave quite a bit better by following general
replaced layout rules. It also turns <svg> elements into inline-level
boxes instead of block-level boxes.
2022-03-19 12:42:10 +01:00
Andreas Kling
a19b9b727d LibWeb: Place right-side floats relative to their containing block
We were incorrectly placing them relative to the BFC root, but CSS2
says they are relative to their own containing block.
2022-03-18 19:28:58 +01:00
Simon Wanner
a2331e8dd3 LibWeb: Implement CSS transforms on stacking contexts
Since there is currently no easy way to handle rotations and skews
with LibGfx this only implements translation and scaling by first
constructing a general 4x4 transformation matrix like outlined in
the css-transforms-1 specification. This is then downgraded to a
Gfx::AffineTransform in order to transform the destination rectangle
used with draw_scaled_bitmap()

While rotation would be nice this already looks pretty good :^)
2022-03-18 18:51:42 +01:00
Simon Wanner
7c79fc209f LibWeb: Establish a new stacking context for elements with transform 2022-03-18 18:51:42 +01:00
Andreas Kling
39b7fbfeb9 LibWeb: Rewrite CSS float implementation to use offset-from-edge
The previous implementation used relative X offsets for both left and
right-side floats. This made right-side floats super awkward, since we
could only determine their X position once the width of the BFC root was
known, and for BFC roots with automatic width, this was not even working
at all most of the time.

This patch changes the way we deal with floats so that BFC keeps track
of the offset-from-edge for each float. The offset is the distance from
the BFC root edge (left or right, depending on float direction) to the
"innermost" margin edge of the floating box.

Floating box are now laid out in two passes: while going through the
normal flow layout, we put floats in their *static* position (i.e the
position they would have occupied if they weren't floating) and then
update the Y position value to the final one.

The second pass occurs later on, when the BFC root has had its width
assigned by the parent context. Once we know the root width, we can
set the X position value of floating boxes. (Because the X position of
right-side floats is relative to the right edge of the BFC root.)
2022-03-18 15:18:48 +01:00
Andreas Kling
28642de6ed LibWeb: Make LineBuilder aware of the current LayoutMode
This will allow us to override the available space correctly when doing
intrinsic sizing.
2022-03-18 15:18:48 +01:00
Andreas Kling
915ee66bd6 LibWeb: Implement shrink-to-fit layout on top of intrinsic size cache
Using the intrinsic size cache means we only perform the nested layout
to determine intrinsic size *once* per root layout pass.

Furthermore, by using a throwaway FormattingState, details of the nested
layout can't leak into and mutate the outer layout.
2022-03-18 15:18:48 +01:00
Andreas Kling
39ca39204b LibWeb: Cache intrinsic sizes on the root FormattingState
Instead of caching them with the current state, we can cache them at the
root of the state tree. Since intrinsic sizes are immutable during the
same layout, this allows layout to take advantage of intrinsic sizes
discovered during nested layout (and avoids a *lot* of duplicate work.)
2022-03-18 15:18:48 +01:00
Andreas Kling
d3932b5880 LibWeb: Give FormattingState a reference to its root state
FormattingStates can have parents, in case we're performing nested
layouts to determine something's intrinsic size. In those cases, it will
soon be useful to find the outermost (root) state.
2022-03-18 15:18:48 +01:00
Andreas Kling
60c781ebc7 LibWeb: Simplify Layout::Node::containing_block()
Use first_ancestor_of_type<BlockContainer>() instead of implementing a
custom traversal lambda.
2022-03-18 15:18:48 +01:00
Andreas Kling
ef8a72ff3f LibWeb: Move available_space_for_line() from IFC to BFC
This is preparation for allowing blocks with their own internal BFC to
flow around floating boxes in the parent BFC.

Note that IFC still has the available_space_for_line() API, which
returns space available within the IFC's own containing block, while the
BFC available_space_for_line() returns space available within its root.
2022-03-18 15:18:48 +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
be5f0b5ac4 LibWeb: Move text fragment painting to PaintableWithLines
All the other painting code has moved to paintables already.
2022-03-16 23:13:05 +01:00
sin-ack
29583104d2 LibWeb: Refactor all LabelableNode subclasses + input event handling :^)
This commit is messy due to the Paintable and Layout classes being
tangled together.

The RadioButton, CheckBox and ButtonBox classes are now subclasses of
FormAssociatedLabelableNode. This subclass separates these layout nodes
from LabelableNode, which is also the superclass of non-form associated
labelable nodes (Progress).

ButtonPaintable, CheckBoxPaintable and RadioButtonPaintable no longer
call events on DOM nodes directly from their mouse event handlers;
instead, all the functionality is now directly in EventHandler, which
dispatches the related events. handle_mousedown and related methods
return a bool indicating whether the event handling should proceed.

Paintable classes can now return an alternative DOM::Node which should
be the target of the mouse event. Labels use this to indicate that the
labeled control should be the target of the mouse events.

HTMLInputElement put its activation behavior on run_activation_behavior,
which wasn't actually called anywhere and had to be manually called by
other places. We now use activation_behavior which is used by
EventDispatcher.

This commit also brings HTMLInputElement closer to spec by removing the
did_foo functions that did ad-hoc event dispatching and unifies the
behavior under run_input_activation_behavior.
2022-03-16 00:38:31 +01:00
Andreas Kling
b8ee4dfda8 LibWeb: Don't compute style *again* for elements in Layout::TreeBuilder
TreeBuilder wasn't taking advantage of the fact that we already have
computed style cached on each DOM::Element by the time we're
constructing a layout tree.

So instead of using the cached style, we recomputed it from scratch for
every element. This was done because invalidation was broken in many
places, but now that it's more or less trustworthy, stop recomputing
style on the fly in TreeBuilder and use what the preceding style update
pass gave us instead.

This basically cuts style computation work in half. :^)
2022-03-15 22:43:44 +01:00
Andreas Kling
e31fe3eeb8 LibWeb: Rename Element::specified_css_values() => computed_css_values()
Let's make it very clear that these are *computed* values, and not at
all the specified values. The specified values are currently discarded
by the CSS cascade algorithm.
2022-03-15 19:48:19 +01:00