mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 03:55:24 +00:00
LibHTML: Move layout tree building to a LayoutTreeBuilder class
Building a whole layout tree shouldn't be a concern of Node, so this patch moves it to a separate class.
This commit is contained in:
parent
d14b60533f
commit
f7cd5662ef
Notes:
sideshowbarker
2024-07-19 11:41:18 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f7cd5662efd
6 changed files with 70 additions and 39 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <LibHTML/DOM/HTMLTitleElement.h>
|
||||
#include <LibHTML/Frame.h>
|
||||
#include <LibHTML/Layout/LayoutDocument.h>
|
||||
#include <LibHTML/Layout/LayoutTreeBuilder.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Document::Document()
|
||||
|
@ -153,8 +154,10 @@ URL Document::complete_url(const String& string) const
|
|||
|
||||
void Document::layout()
|
||||
{
|
||||
if (!m_layout_root)
|
||||
m_layout_root = create_layout_tree(style_resolver(), nullptr);
|
||||
if (!m_layout_root) {
|
||||
LayoutTreeBuilder tree_builder;
|
||||
m_layout_root = tree_builder.build(*this);
|
||||
}
|
||||
m_layout_root->layout();
|
||||
}
|
||||
|
||||
|
@ -209,4 +212,3 @@ void Document::set_hovered_node(Node* node)
|
|||
if (m_hovered_node)
|
||||
m_hovered_node->invalidate_style();
|
||||
}
|
||||
|
||||
|
|
|
@ -19,41 +19,6 @@ Node::~Node()
|
|||
{
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Node::create_layout_tree(const StyleResolver& resolver, const StyleProperties* parent_style) const
|
||||
{
|
||||
auto layout_node = create_layout_node(resolver, parent_style);
|
||||
if (!layout_node)
|
||||
return nullptr;
|
||||
|
||||
if (!has_children())
|
||||
return layout_node;
|
||||
|
||||
Vector<RefPtr<LayoutNode>> layout_children;
|
||||
bool have_inline_children = false;
|
||||
bool have_block_children = false;
|
||||
|
||||
static_cast<const ParentNode&>(*this).for_each_child([&](const Node& child) {
|
||||
auto layout_child = child.create_layout_tree(resolver, &layout_node->style());
|
||||
if (!layout_child)
|
||||
return;
|
||||
if (!layout_child->is_block())
|
||||
have_inline_children = true;
|
||||
if (layout_child->is_block())
|
||||
have_block_children = true;
|
||||
layout_children.append(move(layout_child));
|
||||
});
|
||||
|
||||
for (auto layout_child : layout_children)
|
||||
if (have_block_children && have_inline_children && !layout_child->is_block()) {
|
||||
if (layout_child->is_text() && static_cast<const LayoutText&>(*layout_child).text_for_style(*parent_style) == " ")
|
||||
continue;
|
||||
layout_node->inline_wrapper().append_child(*layout_child);
|
||||
} else {
|
||||
layout_node->append_child(*layout_child);
|
||||
}
|
||||
return layout_node;
|
||||
}
|
||||
|
||||
const HTMLAnchorElement* Node::enclosing_link_element() const
|
||||
{
|
||||
return first_ancestor_of_type<HTMLAnchorElement>();
|
||||
|
|
|
@ -38,7 +38,6 @@ public:
|
|||
bool is_parent_node() const { return is_element() || is_document(); }
|
||||
|
||||
virtual RefPtr<LayoutNode> create_layout_node(const StyleResolver&, const StyleProperties* parent_style) const;
|
||||
RefPtr<LayoutNode> create_layout_tree(const StyleResolver&, const StyleProperties* parent_style) const;
|
||||
|
||||
virtual String tag_name() const = 0;
|
||||
|
||||
|
|
51
Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp
Normal file
51
Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include <LibHTML/DOM/Document.h>
|
||||
#include <LibHTML/DOM/ParentNode.h>
|
||||
#include <LibHTML/Layout/LayoutNode.h>
|
||||
#include <LibHTML/Layout/LayoutText.h>
|
||||
#include <LibHTML/Layout/LayoutTreeBuilder.h>
|
||||
|
||||
LayoutTreeBuilder::LayoutTreeBuilder()
|
||||
{
|
||||
}
|
||||
|
||||
static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties* parent_style)
|
||||
{
|
||||
auto layout_node = node.create_layout_node(node.document().style_resolver(), parent_style);
|
||||
if (!layout_node)
|
||||
return nullptr;
|
||||
|
||||
if (!node.has_children())
|
||||
return layout_node;
|
||||
|
||||
NonnullRefPtrVector<LayoutNode> layout_children;
|
||||
bool have_inline_children = false;
|
||||
bool have_block_children = false;
|
||||
|
||||
to<ParentNode>(node).for_each_child([&](Node& child) {
|
||||
auto layout_child = create_layout_tree(child, &layout_node->style());
|
||||
if (!layout_child)
|
||||
return;
|
||||
if (layout_child->is_inline())
|
||||
have_inline_children = true;
|
||||
if (layout_child->is_block())
|
||||
have_block_children = true;
|
||||
layout_children.append(layout_child.release_nonnull());
|
||||
});
|
||||
|
||||
for (auto& layout_child : layout_children)
|
||||
if (have_block_children && have_inline_children && !layout_child.is_block()) {
|
||||
if (is<LayoutText>(layout_child) && to<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
|
||||
continue;
|
||||
layout_node->inline_wrapper().append_child(layout_child);
|
||||
} else {
|
||||
layout_node->append_child(layout_child);
|
||||
}
|
||||
return layout_node;
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> LayoutTreeBuilder::build(Node& node)
|
||||
{
|
||||
// FIXME: Support building partial trees.
|
||||
ASSERT(is<Document>(node));
|
||||
return create_layout_tree(node, nullptr);
|
||||
}
|
13
Libraries/LibHTML/Layout/LayoutTreeBuilder.h
Normal file
13
Libraries/LibHTML/Layout/LayoutTreeBuilder.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/RefPtr.h>
|
||||
|
||||
class Node;
|
||||
class LayoutNode;
|
||||
|
||||
class LayoutTreeBuilder {
|
||||
public:
|
||||
LayoutTreeBuilder();
|
||||
|
||||
RefPtr<LayoutNode> build(Node&);
|
||||
};
|
|
@ -46,6 +46,7 @@ LIBHTML_OBJS = \
|
|||
Layout/BoxModelMetrics.o \
|
||||
Layout/LineBox.o \
|
||||
Layout/LineBoxFragment.o \
|
||||
Layout/LayoutTreeBuilder.o \
|
||||
ResourceLoader.o \
|
||||
HtmlView.o \
|
||||
Frame.o \
|
||||
|
|
Loading…
Add table
Reference in a new issue