From fd0aa5dd43bdf9b3184a891481157113496de4cb Mon Sep 17 00:00:00 2001 From: Sergey Bugaev Date: Sat, 21 Sep 2019 15:32:17 +0300 Subject: 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 ^) --- Libraries/LibHTML/Frame.cpp | 77 +++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 52 deletions(-) (limited to 'Libraries/LibHTML/Frame.cpp') 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 #include -#include #include #include #include @@ -23,71 +22,46 @@ void Frame::set_document(Document* document) m_document = document; } -RefPtr Frame::generate_style_tree() +RefPtr Frame::generate_layout_tree() { - if (!m_document) - return nullptr; + auto resolver = m_document->style_resolver(); + auto create_layout_node = [&](const Node& node) -> RefPtr { + if (node.is_document()) + return adopt(*new LayoutDocument(static_cast(node), {})); - auto& resolver = m_document->style_resolver(); - Function(const Node&, StyledNode*)> resolve_style = [&](const Node& node, StyledNode* parent_styled_node) -> RefPtr { - RefPtr styled_node; - if (node.is_element()) - styled_node = resolver.create_styled_node(static_cast(node)); - else if (node.is_document()) - styled_node = resolver.create_styled_node(static_cast(node)); - if (!styled_node) - return nullptr; - if (parent_styled_node) - parent_styled_node->append_child(*styled_node); - static_cast(node).for_each_child([&](const Node& child) { - if (!child.is_element()) - return; - auto styled_child_node = resolve_style(static_cast(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(node)); + auto display_property = style_properties.property("display"); + String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline"; -RefPtr Frame::generate_layout_tree(const StyledNode& styled_root) -{ - auto create_layout_node = [](const StyledNode& styled_node) -> RefPtr { - if (styled_node.node() && styled_node.node()->is_document()) - return adopt(*new LayoutDocument(static_cast(*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(const StyledNode&)> build_layout_tree; - build_layout_tree = [&](const StyledNode& styled_node) -> RefPtr { - auto layout_node = create_layout_node(styled_node); + Function(const Node&)> build_layout_tree; + build_layout_tree = [&](const Node& node) -> RefPtr { + 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(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()); -- cgit v1.2.3