diff options
author | Andreas Kling <kling@serenityos.org> | 2021-01-02 03:30:04 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-02 03:48:35 +0100 |
commit | 5116b94114aef2c07f1a3553816c88739a0e6125 (patch) | |
tree | 6459fa8dd29c3c7992c7c53f7b453fce5de84ed4 /Libraries/LibWeb | |
parent | 9bdffc9eb460ac4068b5e889dfb15e9a85ef54b3 (diff) | |
download | serenity-5116b94114aef2c07f1a3553816c88739a0e6125.zip |
LibWeb: Fix unnecessary wrapping of block boxes in anonymous blocks
Outside of tables, we don't need to wrap block-level boxes in anymous
blocks. Only inline-level boxes need this treatment.
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r-- | Libraries/LibWeb/Dump.cpp | 4 | ||||
-rw-r--r-- | Libraries/LibWeb/Layout/TreeBuilder.cpp | 42 |
2 files changed, 26 insertions, 20 deletions
diff --git a/Libraries/LibWeb/Dump.cpp b/Libraries/LibWeb/Dump.cpp index 8256381b65..5b4cf9ca6f 100644 --- a/Libraries/LibWeb/Dump.cpp +++ b/Libraries/LibWeb/Dump.cpp @@ -144,7 +144,7 @@ void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool sho if (!is<Layout::Box>(layout_node)) { builder.appendff("{}{}{} <{}{}{}>", nonbox_color_on, - layout_node.class_name(), + layout_node.class_name().substring_view(13), color_off, tag_name, nonbox_color_on, @@ -157,7 +157,7 @@ void dump_tree(StringBuilder& builder, const Layout::Node& layout_node, bool sho auto& box = downcast<Layout::Box>(layout_node); builder.appendff("{}{}{} <{}{}{}{}> ", box_color_on, - box.class_name(), + box.class_name().substring_view(13), color_off, box_color_on, tag_name, diff --git a/Libraries/LibWeb/Layout/TreeBuilder.cpp b/Libraries/LibWeb/Layout/TreeBuilder.cpp index 17ab36d8f1..6ab00f549f 100644 --- a/Libraries/LibWeb/Layout/TreeBuilder.cpp +++ b/Libraries/LibWeb/Layout/TreeBuilder.cpp @@ -49,6 +49,9 @@ static NonnullRefPtr<CSS::StyleProperties> style_for_anonymous_block(Node& paren return new_style; } +// The insertion_parent_for_*() functions maintain the invariant that block-level boxes must have either +// only block-level children or only inline-level children. + static Layout::Node& insertion_parent_for_inline_node(Layout::Node& layout_parent, Layout::Node& layout_node) { if (layout_parent.is_inline()) @@ -57,7 +60,7 @@ static Layout::Node& insertion_parent_for_inline_node(Layout::Node& layout_paren if (!layout_parent.has_children() || layout_parent.children_are_inline()) return layout_parent; - // layout_parent has block children, insert into an anonymous wrapper block (and create it first if needed) + // Parent has block-level children, insert into an anonymous wrapper block (and create it first if needed) if (!layout_parent.last_child()->is_anonymous() || !layout_parent.last_child()->children_are_inline()) { layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent)))); } @@ -66,28 +69,31 @@ static Layout::Node& insertion_parent_for_inline_node(Layout::Node& layout_paren static Layout::Node& insertion_parent_for_block_node(Layout::Node& layout_parent, Layout::Node& layout_node) { - if (!layout_parent.has_children() || !layout_parent.children_are_inline()) + if (!layout_parent.has_children()) { + // Parent block has no children, insert this block into parent. return layout_parent; + } - // layout_parent has inline children, first move them into an anonymous wrapper block - if (layout_parent.children_are_inline()) { - NonnullRefPtrVector<Layout::Node> children; - while (RefPtr<Layout::Node> child = layout_parent.first_child()) { - layout_parent.remove_child(*child); - children.append(child.release_nonnull()); - } - layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent)))); - layout_parent.set_children_are_inline(false); - for (auto& child : children) { - layout_parent.last_child()->append_child(child); - } - layout_parent.last_child()->set_children_are_inline(true); + if (!layout_parent.children_are_inline()) { + // Parent block has block-level children, insert this block into parent. + return layout_parent; } - if (!layout_parent.last_child()->is_anonymous() || layout_parent.last_child()->children_are_inline()) { - layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent)))); + // Parent block has inline-level children (our siblings). + // First move these siblings into an anonymous wrapper block. + NonnullRefPtrVector<Layout::Node> children; + while (RefPtr<Layout::Node> child = layout_parent.first_child()) { + layout_parent.remove_child(*child); + children.append(child.release_nonnull()); } - return *layout_parent.last_child(); + layout_parent.append_child(adopt(*new BlockBox(layout_node.document(), nullptr, style_for_anonymous_block(layout_parent)))); + layout_parent.set_children_are_inline(false); + for (auto& child : children) { + layout_parent.last_child()->append_child(child); + } + layout_parent.last_child()->set_children_are_inline(true); + // Then it's safe to insert this block into parent. + return layout_parent; } void TreeBuilder::create_layout_tree(DOM::Node& dom_node) |