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 <LibGUI/Painter.h>
28#include <LibWeb/DOM/Document.h>
29#include <LibWeb/DOM/Element.h>
30#include <LibWeb/Frame.h>
31#include <LibWeb/Layout/LayoutBlock.h>
32#include <LibWeb/Layout/LayoutNode.h>
33
34namespace Web {
35
36LayoutNode::LayoutNode(const Node* node)
37 : m_node(node)
38{
39 if (m_node)
40 m_node->set_layout_node({}, this);
41}
42
43LayoutNode::~LayoutNode()
44{
45 if (m_node && m_node->layout_node() == this)
46 m_node->set_layout_node({}, nullptr);
47}
48
49void LayoutNode::layout()
50{
51 for_each_child([](auto& child) {
52 child.layout();
53 });
54}
55
56const LayoutBlock* LayoutNode::containing_block() const
57{
58 for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
59 if (is<LayoutBlock>(*ancestor))
60 return to<LayoutBlock>(ancestor);
61 }
62 return nullptr;
63}
64
65void LayoutNode::render(RenderingContext& context)
66{
67 if (!is_visible())
68 return;
69
70 // TODO: render our border
71 for_each_child([&](auto& child) {
72 child.render(context);
73 });
74}
75
76HitTestResult LayoutNode::hit_test(const Gfx::Point& position) const
77{
78 HitTestResult result;
79 for_each_child([&](auto& child) {
80 auto child_result = child.hit_test(position);
81 if (child_result.layout_node)
82 result = child_result;
83 });
84 return result;
85}
86
87const Document& LayoutNode::document() const
88{
89 if (is_anonymous())
90 return parent()->document();
91 return node()->document();
92}
93
94Document& LayoutNode::document()
95{
96 if (is_anonymous())
97 return parent()->document();
98 // FIXME: Remove this const_cast once we give up on the idea of a const link from layout tree to DOM tree.
99 return const_cast<Node*>(node())->document();
100}
101
102const LayoutDocument& LayoutNode::root() const
103{
104 ASSERT(document().layout_node());
105 return *document().layout_node();
106}
107
108LayoutDocument& LayoutNode::root()
109{
110 ASSERT(document().layout_node());
111 return *document().layout_node();
112}
113
114void LayoutNode::split_into_lines(LayoutBlock& container)
115{
116 for_each_child([&](auto& child) {
117 if (child.is_inline()) {
118 child.split_into_lines(container);
119 } else {
120 // FIXME: Support block children of inlines.
121 }
122 });
123}
124
125void LayoutNode::set_needs_display()
126{
127 auto* frame = document().frame();
128 ASSERT(frame);
129
130 if (auto* block = containing_block()) {
131 block->for_each_fragment([&](auto& fragment) {
132 if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
133 const_cast<Frame*>(frame)->set_needs_display(enclosing_int_rect(fragment.rect()));
134 }
135 return IterationDecision::Continue;
136 });
137 }
138}
139
140Gfx::FloatPoint LayoutNode::box_type_agnostic_position() const
141{
142 if (is_box())
143 return to<LayoutBox>(*this).position();
144 ASSERT(is_inline());
145 Gfx::FloatPoint position;
146 if (auto* block = containing_block()) {
147 block->for_each_fragment([&](auto& fragment) {
148 if (&fragment.layout_node() == this || is_ancestor_of(fragment.layout_node())) {
149 position = fragment.rect().location();
150 return IterationDecision::Break;
151 }
152 return IterationDecision::Continue;
153 });
154 }
155 return position;
156}
157
158}