Skip to content

Commit

Permalink
LibWeb: Refactor out-of-flow and in-flow into functions
Browse files Browse the repository at this point in the history
The concept of out-of-flow and in-flow elements is used in a few places
in the layout code. This change refactors these concepts into functions.
  • Loading branch information
arthurc authored and awesomekling committed Jul 6, 2024
1 parent 9c80326 commit 196922a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
8 changes: 8 additions & 0 deletions Userland/Libraries/LibWeb/Layout/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ class Node
u32 initial_quote_nesting_level() const { return m_initial_quote_nesting_level; }
void set_initial_quote_nesting_level(u32 value) { m_initial_quote_nesting_level = value; }

// An element is called out of flow if it is floated, absolutely positioned, or is the root element.
// https://www.w3.org/TR/CSS22/visuren.html#positioning-scheme
bool is_out_of_flow() const { return is_floating() || is_absolutely_positioned(); }

// An element is called in-flow if it is not out-of-flow.
// https://www.w3.org/TR/CSS22/visuren.html#positioning-scheme
bool is_in_flow() const { return !is_out_of_flow(); }

protected:
Node(DOM::Document&, DOM::Node*);

Expand Down
14 changes: 5 additions & 9 deletions Userland/Libraries/LibWeb/Layout/TreeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ TreeBuilder::TreeBuilder() = default;
static bool has_inline_or_in_flow_block_children(Layout::Node const& layout_node)
{
for (auto child = layout_node.first_child(); child; child = child->next_sibling()) {
if (child->is_inline())
return true;
if (!child->is_floating() && !child->is_absolutely_positioned())
if (child->is_inline() || child->is_in_flow())
return true;
}
return false;
Expand All @@ -57,7 +55,7 @@ static bool has_in_flow_block_children(Layout::Node const& layout_node)
for (auto child = layout_node.first_child(); child; child = child->next_sibling()) {
if (child->is_inline())
continue;
if (!child->is_floating() && !child->is_absolutely_positioned())
if (child->is_in_flow())
return true;
}
return false;
Expand Down Expand Up @@ -98,9 +96,7 @@ static Layout::Node& insertion_parent_for_block_node(Layout::NodeWithStyle& layo
return layout_parent;
}

bool is_out_of_flow = layout_node.is_absolutely_positioned() || layout_node.is_floating();

if (is_out_of_flow
if (layout_node.is_out_of_flow()
&& !layout_parent.display().is_flex_inside()
&& !layout_parent.display().is_grid_inside()
&& layout_parent.last_child()->is_anonymous()
Expand All @@ -115,7 +111,7 @@ static Layout::Node& insertion_parent_for_block_node(Layout::NodeWithStyle& layo
return layout_parent;
}

if (is_out_of_flow) {
if (layout_node.is_out_of_flow()) {
// Block is out-of-flow, it can have inline siblings if necessary.
return layout_parent;
}
Expand All @@ -128,7 +124,7 @@ static Layout::Node& insertion_parent_for_block_node(Layout::NodeWithStyle& layo
for (JS::GCPtr<Layout::Node> child = layout_parent.first_child(); child; child = next) {
next = child->next_sibling();
// NOTE: We let out-of-flow children stay in the parent, to preserve tree structure.
if (child->is_floating() || child->is_absolutely_positioned())
if (child->is_out_of_flow())
continue;
layout_parent.remove_child(*child);
children.append(*child);
Expand Down

0 comments on commit 196922a

Please sign in to comment.