1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:48:11 +00:00

LibHTML: Refactor to go from DOM -> styled tree -> layout tree.

Frame::layout() drives everything now, it takes the DOM contained in the
frame and puts it through the tree transformations.
This commit is contained in:
Andreas Kling 2019-06-29 21:42:07 +02:00
parent 6e95b11395
commit 7eef69ad4b
20 changed files with 132 additions and 131 deletions

View file

@ -1,3 +1,4 @@
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutDocument.h>
@ -12,31 +13,9 @@ Document::~Document()
{
}
static void create_layout_tree_for_node(Node& node)
StyleResolver& Document::style_resolver()
{
if (auto layout_node = node.create_layout_node()) {
node.set_layout_node(*layout_node);
#ifdef DEBUG_LAYOUT_TREE_BUILD
if (node.is_element()) {
printf("created layout node for <%s>, parent is %p, parent ln is %p\n", static_cast<const Element&>(node).tag_name().characters(), node.parent_node(), node.parent_node()->layout_node());
}
#endif
if (node.parent() && node.parent()->layout_node())
node.parent()->layout_node()->append_child(*layout_node);
}
if (node.is_parent_node()) {
static_cast<ParentNode&>(node).for_each_child([&](auto& child) {
create_layout_tree_for_node(child);
});
}
}
void Document::build_layout_tree()
{
create_layout_tree_for_node(*this);
}
RefPtr<LayoutNode> Document::create_layout_node()
{
return adopt(*new LayoutDocument(*this));
if (!m_style_resolver)
m_style_resolver = make<StyleResolver>(*this);
return *m_style_resolver;
}

View file

@ -1,18 +1,27 @@
#pragma once
#include <AK/AKString.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/DOM/ParentNode.h>
class LayoutNode;
class StyleResolver;
class StyleSheet;
class Document : public ParentNode {
public:
Document();
virtual ~Document() override;
virtual RefPtr<LayoutNode> create_layout_node() override;
StyleResolver& style_resolver();
void build_layout_tree();
void add_sheet(const StyleSheet& sheet) { m_sheets.append(sheet); }
const NonnullRefPtrVector<StyleSheet>& stylesheets() const { return m_sheets; }
private:
OwnPtr<StyleResolver> m_style_resolver;
NonnullRefPtrVector<StyleSheet> m_sheets;
};

View file

@ -62,18 +62,3 @@ bool Element::has_class(const StringView& class_name) const
}
return false;
}
RefPtr<LayoutNode> Element::create_layout_node()
{
if (m_tag_name == "html")
return adopt(*new LayoutBlock(*this));
if (m_tag_name == "body")
return adopt(*new LayoutBlock(*this));
if (m_tag_name == "h1")
return adopt(*new LayoutBlock(*this));
if (m_tag_name == "p")
return adopt(*new LayoutBlock(*this));
if (m_tag_name == "b")
return adopt(*new LayoutInline(*this));
return nullptr;
}

View file

@ -42,8 +42,6 @@ public:
bool has_class(const StringView&) const;
virtual RefPtr<LayoutNode> create_layout_node() override;
private:
Attribute* find_attribute(const String& name);
const Attribute* find_attribute(const String& name) const;

View file

@ -9,13 +9,3 @@ Node::Node(NodeType type)
Node::~Node()
{
}
RefPtr<LayoutNode> Node::create_layout_node()
{
return nullptr;
}
void Node::set_layout_node(NonnullRefPtr<LayoutNode> layout_node)
{
m_layout_node = move(layout_node);
}

View file

@ -12,7 +12,6 @@ enum class NodeType : unsigned {
DOCUMENT_NODE = 9,
};
class LayoutNode;
class ParentNode;
class Node : public TreeNode<Node> {
@ -25,16 +24,8 @@ public:
bool is_document() const { return type() == NodeType::DOCUMENT_NODE; }
bool is_parent_node() const { return is_element() || is_document(); }
virtual RefPtr<LayoutNode> create_layout_node();
const LayoutNode* layout_node() const { return m_layout_node; }
LayoutNode* layout_node() { return m_layout_node; }
void set_layout_node(NonnullRefPtr<LayoutNode>);
protected:
explicit Node(NodeType);
NodeType m_type { NodeType::INVALID };
RefPtr<LayoutNode> m_layout_node;
};

View file

@ -10,8 +10,3 @@ Text::Text(const String& data)
Text::~Text()
{
}
RefPtr<LayoutNode> Text::create_layout_node()
{
return adopt(*new LayoutText(*this));
}

View file

@ -10,8 +10,6 @@ public:
const String& data() const { return m_data; }
virtual RefPtr<LayoutNode> create_layout_node() override;
private:
String m_data;
};