mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-19 16:51:52 +00:00
LibHTML: Move layout tree building to Node
This also fixes another bug with inline wrappers. Namely, we should only add inline wrappers if a block node has both non-block (inline or text) and block children.
This commit is contained in:
parent
841ae44392
commit
599edba7a3
Notes:
sideshowbarker
2024-07-19 11:56:25 +09:00
Author: https://github.com/bugaevc
Commit: 599edba7a3
Pull-request: https://github.com/SerenityOS/serenity/pull/609
Reviewed-by: https://github.com/awesomekling ✅
3 changed files with 70 additions and 43 deletions
|
@ -22,54 +22,12 @@ void Frame::set_document(Document* document)
|
|||
m_document = document;
|
||||
}
|
||||
|
||||
RefPtr<LayoutNode> Frame::generate_layout_tree()
|
||||
{
|
||||
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 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";
|
||||
|
||||
if (display == "none")
|
||||
return nullptr;
|
||||
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 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 (!node.has_children())
|
||||
return layout_node;
|
||||
static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
|
||||
auto layout_child = build_layout_tree(child);
|
||||
if (!layout_child)
|
||||
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(*m_document);
|
||||
}
|
||||
|
||||
void Frame::layout()
|
||||
{
|
||||
if (!m_document)
|
||||
return;
|
||||
|
||||
auto layout_root = generate_layout_tree();
|
||||
auto layout_root = m_document->create_layout_tree(m_document->style_resolver(), nullptr);
|
||||
|
||||
layout_root->style().size().set_width(m_size.width());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue