summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/Frame.cpp
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@gmail.com>2019-09-21 15:32:17 +0300
committerAndreas Kling <awesomekling@gmail.com>2019-09-28 18:29:42 +0200
commitfd0aa5dd43bdf9b3184a891481157113496de4cb (patch)
tree6aa9442e2e68c78d794e6d93f8b7ac3a9e0c6e47 /Libraries/LibHTML/Frame.cpp
parenta9ebd676e5446f2d1da1c93c5dd4e2dc98f2b1ba (diff)
downloadserenity-fd0aa5dd43bdf9b3184a891481157113496de4cb.zip
LibHTML: Get rid of the style tree
We now create a layout tree directly from the DOM tree. This way we don't actually lose text nodes ^)
Diffstat (limited to 'Libraries/LibHTML/Frame.cpp')
-rw-r--r--Libraries/LibHTML/Frame.cpp77
1 files changed, 25 insertions, 52 deletions
diff --git a/Libraries/LibHTML/Frame.cpp b/Libraries/LibHTML/Frame.cpp
index f4d26da501..78005562b3 100644
--- a/Libraries/LibHTML/Frame.cpp
+++ b/Libraries/LibHTML/Frame.cpp
@@ -1,6 +1,5 @@
#include <AK/Function.h>
#include <LibHTML/CSS/StyleResolver.h>
-#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>
@@ -23,71 +22,46 @@ void Frame::set_document(Document* document)
m_document = document;
}
-RefPtr<StyledNode> Frame::generate_style_tree()
+RefPtr<LayoutNode> Frame::generate_layout_tree()
{
- if (!m_document)
- return nullptr;
+ auto resolver = m_document->style_resolver();
+ auto create_layout_node = [&](const Node& node) -> RefPtr<LayoutNode> {
+ if (node.is_document())
+ return adopt(*new LayoutDocument(static_cast<const Document&>(node), {}));
- auto& resolver = m_document->style_resolver();
- Function<RefPtr<StyledNode>(const Node&, StyledNode*)> resolve_style = [&](const Node& node, StyledNode* parent_styled_node) -> RefPtr<StyledNode> {
- RefPtr<StyledNode> styled_node;
- if (node.is_element())
- styled_node = resolver.create_styled_node(static_cast<const Element&>(node));
- else if (node.is_document())
- styled_node = resolver.create_styled_node(static_cast<const Document&>(node));
- if (!styled_node)
- return nullptr;
- if (parent_styled_node)
- parent_styled_node->append_child(*styled_node);
- static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
- if (!child.is_element())
- return;
- auto styled_child_node = resolve_style(static_cast<const Element&>(child), styled_node.ptr());
- printf("Created StyledNode{%p} for Element{%p}\n", styled_child_node.ptr(), &node);
- });
- return styled_node;
- };
- auto styled_root = resolve_style(*m_document, nullptr);
- dump_tree(*styled_root);
- return styled_root;
-}
+ auto style_properties = resolver.resolve_style(static_cast<const Element&>(node));
+ auto display_property = style_properties.property("display");
+ String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
-RefPtr<LayoutNode> Frame::generate_layout_tree(const StyledNode& styled_root)
-{
- auto create_layout_node = [](const StyledNode& styled_node) -> RefPtr<LayoutNode> {
- if (styled_node.node() && styled_node.node()->is_document())
- return adopt(*new LayoutDocument(static_cast<const Document&>(*styled_node.node()), styled_node));
- switch (styled_node.display()) {
- case Display::None:
+ if (display == "none")
return nullptr;
- case Display::Block:
- return adopt(*new LayoutBlock(styled_node.node(), &styled_node));
- case Display::Inline:
- return adopt(*new LayoutInline(*styled_node.node(), styled_node));
- default:
- ASSERT_NOT_REACHED();
- }
+ if (display == "block")
+ return adopt(*new LayoutBlock(&node, move(style_properties)));
+ if (display == "inline")
+ return adopt(*new LayoutInline(node, move(style_properties)));
+
+ ASSERT_NOT_REACHED();
};
- Function<RefPtr<LayoutNode>(const StyledNode&)> build_layout_tree;
- build_layout_tree = [&](const StyledNode& styled_node) -> RefPtr<LayoutNode> {
- auto layout_node = create_layout_node(styled_node);
+ Function<RefPtr<LayoutNode>(const Node&)> build_layout_tree;
+ build_layout_tree = [&](const Node& node) -> RefPtr<LayoutNode> {
+ auto layout_node = create_layout_node(node);
if (!layout_node)
return nullptr;
- if (!styled_node.has_children())
+ if (!node.has_children())
return layout_node;
- for (auto* styled_child = styled_node.first_child(); styled_child; styled_child = styled_child->next_sibling()) {
- auto layout_child = build_layout_tree(*styled_child);
+ static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
+ auto layout_child = build_layout_tree(child);
if (!layout_child)
- continue;
+ return;
if (layout_child->is_inline())
layout_node->inline_wrapper().append_child(*layout_child);
else
layout_node->append_child(*layout_child);
- }
+ });
return layout_node;
};
- return build_layout_tree(styled_root);
+ return build_layout_tree(*m_document);
}
void Frame::layout()
@@ -95,8 +69,7 @@ void Frame::layout()
if (!m_document)
return;
- auto styled_root = generate_style_tree();
- auto layout_root = generate_layout_tree(*styled_root);
+ auto layout_root = generate_layout_tree();
layout_root->style().size().set_width(m_size.width());