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 <LibWeb/DOM/Document.h>
28#include <LibWeb/DOM/ParentNode.h>
29#include <LibWeb/Layout/LayoutNode.h>
30#include <LibWeb/Layout/LayoutTable.h>
31#include <LibWeb/Layout/LayoutText.h>
32#include <LibWeb/Layout/LayoutTreeBuilder.h>
33
34namespace Web {
35
36LayoutTreeBuilder::LayoutTreeBuilder()
37{
38}
39
40static RefPtr<LayoutNode> create_layout_tree(Node& node, const StyleProperties* parent_style)
41{
42 auto layout_node = node.create_layout_node(parent_style);
43 if (!layout_node)
44 return nullptr;
45
46 if (!node.has_children())
47 return layout_node;
48
49 NonnullRefPtrVector<LayoutNode> layout_children;
50 bool have_inline_children = false;
51 bool have_block_children = false;
52
53 to<ParentNode>(node).for_each_child([&](Node& child) {
54 auto layout_child = create_layout_tree(child, &layout_node->style());
55 if (!layout_child)
56 return;
57 if (layout_child->is_inline())
58 have_inline_children = true;
59 if (layout_child->is_block())
60 have_block_children = true;
61 layout_children.append(layout_child.release_nonnull());
62 });
63
64 for (auto& layout_child : layout_children) {
65 if (have_block_children && have_inline_children && layout_child.is_inline()) {
66 if (is<LayoutText>(layout_child) && to<LayoutText>(layout_child).text_for_style(*parent_style) == " ")
67 continue;
68 layout_node->inline_wrapper().append_child(layout_child);
69 } else {
70 layout_node->append_child(layout_child);
71 }
72 }
73
74 if (have_inline_children && !have_block_children)
75 layout_node->set_children_are_inline(true);
76
77 // FIXME: This is really hackish. Some layout nodes don't care about inline children.
78 if (is<LayoutTable>(layout_node))
79 layout_node->set_children_are_inline(false);
80
81 return layout_node;
82}
83
84RefPtr<LayoutNode> LayoutTreeBuilder::build(Node& node)
85{
86 // FIXME: Support building partial trees.
87 ASSERT(is<Document>(node));
88 return create_layout_tree(node, nullptr);
89}
90
91}