Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <LibHTML/DOM/Document.h>
28#include <LibHTML/DOM/ParentNode.h>
29#include <LibHTML/Layout/LayoutNode.h>
30#include <LibHTML/Layout/LayoutTable.h>
31#include <LibHTML/Layout/LayoutText.h>
32#include <LibHTML/Layout/LayoutTreeBuilder.h>
33
34LayoutTreeBuilder::LayoutTreeBuilder()
35{
36}
37
38static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties* parent_style)
39{
40 auto layout_node = node.create_layout_node(parent_style);
41 if (!layout_node)
42 return nullptr;
43
44 if (!node.has_children())
45 return layout_node;
46
47 NonnullRefPtrVector<LayoutNode> layout_children;
48 bool have_inline_children = false;
49 bool have_block_children = false;
50
51 to<ParentNode>(node).for_each_child([&](Node& child) {
52 auto layout_child = create_layout_tree(child, &layout_node->style());
53 if (!layout_child)
54 return;
55 if (layout_child->is_inline())
56 have_inline_children = true;
57 if (layout_child->is_block())
58 have_block_children = true;
59 layout_children.append(layout_child.release_nonnull());
60 });
61
62 for (auto& layout_child : layout_children) {
63 if (have_block_children && have_inline_children && layout_child.is_inline()) {
64 if (is<LayoutText>(layout_child) && to<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
65 continue;
66 layout_node->inline_wrapper().append_child(layout_child);
67 } else {
68 layout_node->append_child(layout_child);
69 }
70 }
71
72 if (have_inline_children && !have_block_children)
73 layout_node->set_children_are_inline(true);
74
75 // FIXME: This is really hackish. Some layout nodes don't care about inline children.
76 if (is<LayoutTable>(layout_node))
77 layout_node->set_children_are_inline(false);
78
79 return layout_node;
80}
81
82RefPtr<LayoutNode> LayoutTreeBuilder::build(Node& node)
83{
84 // FIXME: Support building partial trees.
85 ASSERT(is<Document>(node));
86 return create_layout_tree(node, nullptr);
87}