summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-10-15 12:22:41 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-10-15 14:24:26 +0200
commitf7cd5662efd7d653a0f043305d2655030eda55c2 (patch)
tree4483389062b7fdf66837625ff6977a76b746c675 /Libraries
parentd14b60533f4db342ab84cfe090cc6fda953d3927 (diff)
downloadserenity-f7cd5662efd7d653a0f043305d2655030eda55c2.zip
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.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibHTML/DOM/Document.cpp8
-rw-r--r--Libraries/LibHTML/DOM/Node.cpp35
-rw-r--r--Libraries/LibHTML/DOM/Node.h1
-rw-r--r--Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp51
-rw-r--r--Libraries/LibHTML/Layout/LayoutTreeBuilder.h13
-rw-r--r--Libraries/LibHTML/Makefile.shared1
6 files changed, 70 insertions, 39 deletions
diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp
index cd3c97715b..a08230fa00 100644
--- a/Libraries/LibHTML/DOM/Document.cpp
+++ b/Libraries/LibHTML/DOM/Document.cpp
@@ -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();
}
-
diff --git a/Libraries/LibHTML/DOM/Node.cpp b/Libraries/LibHTML/DOM/Node.cpp
index 54857808eb..cfd48d0c20 100644
--- a/Libraries/LibHTML/DOM/Node.cpp
+++ b/Libraries/LibHTML/DOM/Node.cpp
@@ -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>();
diff --git a/Libraries/LibHTML/DOM/Node.h b/Libraries/LibHTML/DOM/Node.h
index 1154d0466d..ca71a5fb2f 100644
--- a/Libraries/LibHTML/DOM/Node.h
+++ b/Libraries/LibHTML/DOM/Node.h
@@ -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;
diff --git a/Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp b/Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp
new file mode 100644
index 0000000000..4345d8be5d
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutTreeBuilder.cpp
@@ -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);
+}
diff --git a/Libraries/LibHTML/Layout/LayoutTreeBuilder.h b/Libraries/LibHTML/Layout/LayoutTreeBuilder.h
new file mode 100644
index 0000000000..e703fb5ed1
--- /dev/null
+++ b/Libraries/LibHTML/Layout/LayoutTreeBuilder.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <AK/RefPtr.h>
+
+class Node;
+class LayoutNode;
+
+class LayoutTreeBuilder {
+public:
+ LayoutTreeBuilder();
+
+ RefPtr<LayoutNode> build(Node&);
+};
diff --git a/Libraries/LibHTML/Makefile.shared b/Libraries/LibHTML/Makefile.shared
index a3af6d90d2..feac259fb3 100644
--- a/Libraries/LibHTML/Makefile.shared
+++ b/Libraries/LibHTML/Makefile.shared
@@ -46,6 +46,7 @@ LIBHTML_OBJS = \
Layout/BoxModelMetrics.o \
Layout/LineBox.o \
Layout/LineBoxFragment.o \
+ Layout/LayoutTreeBuilder.o \
ResourceLoader.o \
HtmlView.o \
Frame.o \