2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-21 19:45:35 +03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/FloatRect.h>
|
|
|
|
#include <LibGfx/Rect.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/CSS/StyleProperties.h>
|
2020-06-09 21:42:11 +03:00
|
|
|
#include <LibWeb/Forward.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/Layout/BoxModelMetrics.h>
|
|
|
|
#include <LibWeb/Layout/LayoutPosition.h>
|
2020-06-18 22:35:44 +03:00
|
|
|
#include <LibWeb/Painting/PaintContext.h>
|
2020-03-07 12:32:51 +03:00
|
|
|
#include <LibWeb/TreeNode.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
namespace Web {
|
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
template<typename T>
|
|
|
|
inline bool is(const LayoutNode&)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool is(const LayoutNode* node)
|
|
|
|
{
|
|
|
|
return !node || is<T>(*node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline bool is<LayoutNode>(const LayoutNode&)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline const T& to(const LayoutNode& node)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(node));
|
|
|
|
return static_cast<const T&>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T* to(LayoutNode* node)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(node));
|
|
|
|
return static_cast<T*>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline const T* to(const LayoutNode* node)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(node));
|
|
|
|
return static_cast<const T*>(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T& to(LayoutNode& node)
|
|
|
|
{
|
|
|
|
ASSERT(is<T>(node));
|
|
|
|
return static_cast<T&>(node);
|
|
|
|
}
|
2019-09-29 00:02:22 +03:00
|
|
|
|
|
|
|
struct HitTestResult {
|
|
|
|
RefPtr<LayoutNode> layout_node;
|
2019-11-06 00:13:26 +03:00
|
|
|
int index_in_node { 0 };
|
2019-09-29 00:02:22 +03:00
|
|
|
};
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2019-06-25 20:46:01 +03:00
|
|
|
class LayoutNode : public TreeNode<LayoutNode> {
|
2019-06-15 23:49:44 +03:00
|
|
|
public:
|
|
|
|
virtual ~LayoutNode();
|
|
|
|
|
2020-06-10 11:57:59 +03:00
|
|
|
virtual HitTestResult hit_test(const Gfx::IntPoint&) const;
|
2019-09-29 00:02:22 +03:00
|
|
|
|
2019-06-16 12:28:47 +03:00
|
|
|
bool is_anonymous() const { return !m_node; }
|
2019-06-15 23:49:44 +03:00
|
|
|
const Node* node() const { return m_node; }
|
2020-06-06 00:36:02 +03:00
|
|
|
Node* node() { return const_cast<Node*>(m_node); }
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2019-11-04 21:37:52 +03:00
|
|
|
Document& document();
|
2019-09-29 12:43:33 +03:00
|
|
|
const Document& document() const;
|
|
|
|
|
2020-06-14 17:45:45 +03:00
|
|
|
const Frame& frame() const;
|
|
|
|
Frame& frame();
|
|
|
|
|
2019-11-04 21:37:52 +03:00
|
|
|
const LayoutDocument& root() const;
|
|
|
|
LayoutDocument& root();
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
template<typename Callback>
|
|
|
|
inline void for_each_child(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling())
|
|
|
|
callback(*node);
|
|
|
|
}
|
|
|
|
|
2019-06-16 22:35:03 +03:00
|
|
|
template<typename Callback>
|
|
|
|
inline void for_each_child(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling())
|
|
|
|
callback(*node);
|
|
|
|
}
|
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
template<typename T, typename Callback>
|
|
|
|
inline void for_each_child_of_type(Callback callback)
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (!is<T>(node))
|
|
|
|
continue;
|
|
|
|
callback(to<T>(*node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename Callback>
|
|
|
|
inline void for_each_child_of_type(Callback callback) const
|
|
|
|
{
|
|
|
|
for (auto* node = first_child(); node; node = node->next_sibling()) {
|
|
|
|
if (!is<T>(node))
|
|
|
|
continue;
|
|
|
|
callback(to<T>(*node));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
virtual const char* class_name() const = 0;
|
|
|
|
virtual bool is_root() const { return false; }
|
2019-06-15 23:49:44 +03:00
|
|
|
virtual bool is_text() const { return false; }
|
2019-07-01 08:28:37 +03:00
|
|
|
virtual bool is_block() const { return false; }
|
2019-10-06 00:20:35 +03:00
|
|
|
virtual bool is_replaced() const { return false; }
|
2019-11-25 23:21:55 +03:00
|
|
|
virtual bool is_widget() const { return false; }
|
2020-06-06 00:36:02 +03:00
|
|
|
virtual bool is_frame() const { return false; }
|
2019-12-18 22:52:36 +03:00
|
|
|
virtual bool is_image() const { return false; }
|
2020-03-19 21:07:56 +03:00
|
|
|
virtual bool is_canvas() const { return false; }
|
2019-10-15 17:48:38 +03:00
|
|
|
virtual bool is_box() const { return false; }
|
2019-10-18 00:34:12 +03:00
|
|
|
virtual bool is_table() const { return false; }
|
|
|
|
virtual bool is_table_row() const { return false; }
|
|
|
|
virtual bool is_table_cell() const { return false; }
|
2020-06-09 22:08:15 +03:00
|
|
|
virtual bool is_table_row_group() const { return false; }
|
2019-10-15 15:19:52 +03:00
|
|
|
bool has_style() const { return m_has_style; }
|
2019-10-05 23:07:45 +03:00
|
|
|
|
|
|
|
bool is_inline() const { return m_inline; }
|
|
|
|
void set_inline(bool b) { m_inline = b; }
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-05-05 17:06:22 +03:00
|
|
|
bool is_inline_block() const { return is_inline() && is_block(); }
|
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
enum class LayoutMode {
|
|
|
|
Default,
|
|
|
|
AllPossibleLineBreaks,
|
|
|
|
OnlyRequiredLineBreaks,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void layout(LayoutMode);
|
2020-06-18 19:57:35 +03:00
|
|
|
|
|
|
|
enum class PaintPhase {
|
|
|
|
Background,
|
|
|
|
Border,
|
|
|
|
Foreground,
|
|
|
|
Overlay,
|
|
|
|
};
|
2020-06-18 22:35:44 +03:00
|
|
|
virtual void paint(PaintContext&, PaintPhase);
|
2019-06-16 22:35:03 +03:00
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
bool is_absolutely_positioned() const;
|
2020-06-12 15:19:03 +03:00
|
|
|
bool is_fixed_position() const;
|
2020-06-05 17:54:28 +03:00
|
|
|
|
2019-07-01 08:28:37 +03:00
|
|
|
const LayoutBlock* containing_block() const;
|
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
bool can_contain_boxes_with_position_absolute() const;
|
|
|
|
|
2019-07-08 18:42:23 +03:00
|
|
|
virtual LayoutNode& inline_wrapper() { return *this; }
|
|
|
|
|
2019-10-07 10:50:31 +03:00
|
|
|
const StyleProperties& style() const;
|
|
|
|
|
2019-10-18 11:16:33 +03:00
|
|
|
LayoutNodeWithStyle* parent();
|
2019-10-07 10:50:31 +03:00
|
|
|
const LayoutNodeWithStyle* parent() const;
|
2019-07-24 08:34:07 +03:00
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
void inserted_into(LayoutNode&) { }
|
|
|
|
void removed_from(LayoutNode&) { }
|
|
|
|
void children_changed() { }
|
2019-09-29 18:40:39 +03:00
|
|
|
|
2020-05-26 22:53:10 +03:00
|
|
|
virtual void split_into_lines(LayoutBlock& container, LayoutMode);
|
2019-10-06 00:20:35 +03:00
|
|
|
|
2019-10-09 22:25:29 +03:00
|
|
|
bool is_visible() const { return m_visible; }
|
|
|
|
void set_visible(bool visible) { m_visible = visible; }
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
virtual void set_needs_display();
|
2019-10-09 22:25:29 +03:00
|
|
|
|
2019-10-18 00:32:08 +03:00
|
|
|
bool children_are_inline() const { return m_children_are_inline; }
|
|
|
|
void set_children_are_inline(bool value) { m_children_are_inline = value; }
|
|
|
|
|
2019-10-18 10:36:46 +03:00
|
|
|
template<typename U>
|
|
|
|
const U* next_sibling_of_type() const;
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* next_sibling_of_type();
|
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
template<typename U>
|
|
|
|
const U* previous_sibling_of_type() const;
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
U* previous_sibling_of_type();
|
|
|
|
|
2019-10-18 10:36:46 +03:00
|
|
|
template<typename T>
|
|
|
|
const T* first_child_of_type() const;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* first_child_of_type();
|
|
|
|
|
2019-10-18 11:16:33 +03:00
|
|
|
template<typename T>
|
|
|
|
const T* first_ancestor_of_type() const;
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T* first_ancestor_of_type();
|
|
|
|
|
2020-02-06 16:35:54 +03:00
|
|
|
Gfx::FloatPoint box_type_agnostic_position() const;
|
2019-10-20 10:14:12 +03:00
|
|
|
|
2020-06-07 18:55:46 +03:00
|
|
|
float font_size() const;
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
protected:
|
2019-10-07 10:50:31 +03:00
|
|
|
explicit LayoutNode(const Node*);
|
2019-06-15 23:49:44 +03:00
|
|
|
|
|
|
|
private:
|
2019-10-07 10:50:31 +03:00
|
|
|
friend class LayoutNodeWithStyle;
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
const Node* m_node { nullptr };
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2019-10-05 23:07:45 +03:00
|
|
|
bool m_inline { false };
|
2019-10-07 10:50:31 +03:00
|
|
|
bool m_has_style { false };
|
2019-10-09 22:25:29 +03:00
|
|
|
bool m_visible { true };
|
2019-10-18 00:32:08 +03:00
|
|
|
bool m_children_are_inline { false };
|
2019-06-15 23:49:44 +03:00
|
|
|
};
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
class LayoutNodeWithStyle : public LayoutNode {
|
|
|
|
public:
|
2020-05-26 22:53:10 +03:00
|
|
|
virtual ~LayoutNodeWithStyle() override { }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
const StyleProperties& style() const { return m_style; }
|
2019-10-14 19:32:02 +03:00
|
|
|
void set_style(const StyleProperties& style) { m_style = style; }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
|
|
|
|
: LayoutNode(node)
|
|
|
|
, m_style(move(style))
|
|
|
|
{
|
|
|
|
m_has_style = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
NonnullRefPtr<StyleProperties> m_style;
|
|
|
|
};
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
|
|
|
|
public:
|
|
|
|
BoxModelMetrics& box_model() { return m_box_model; }
|
|
|
|
const BoxModelMetrics& box_model() const { return m_box_model; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
LayoutNodeWithStyleAndBoxModelMetrics(const Node* node, NonnullRefPtr<StyleProperties> style)
|
|
|
|
: LayoutNodeWithStyle(node, move(style))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BoxModelMetrics m_box_model;
|
|
|
|
};
|
|
|
|
|
2019-10-07 10:50:31 +03:00
|
|
|
inline const StyleProperties& LayoutNode::style() const
|
|
|
|
{
|
|
|
|
if (m_has_style)
|
|
|
|
return static_cast<const LayoutNodeWithStyle*>(this)->style();
|
|
|
|
return parent()->style();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const LayoutNodeWithStyle* LayoutNode::parent() const
|
|
|
|
{
|
|
|
|
return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
|
|
|
|
}
|
2019-10-15 15:19:52 +03:00
|
|
|
|
2019-10-18 11:16:33 +03:00
|
|
|
inline LayoutNodeWithStyle* LayoutNode::parent()
|
|
|
|
{
|
|
|
|
return static_cast<LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
|
|
|
|
}
|
|
|
|
|
2019-10-15 15:19:52 +03:00
|
|
|
template<typename T>
|
2020-06-09 21:42:11 +03:00
|
|
|
inline const T* LayoutNode::next_sibling_of_type() const
|
2019-10-15 15:19:52 +03:00
|
|
|
{
|
2020-06-09 21:42:11 +03:00
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
if (is<T>(*sibling))
|
|
|
|
return &to<T>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2019-10-15 15:19:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-06-09 21:42:11 +03:00
|
|
|
inline T* LayoutNode::next_sibling_of_type()
|
2019-10-15 15:19:52 +03:00
|
|
|
{
|
2020-06-09 21:42:11 +03:00
|
|
|
for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
|
|
|
|
if (is<T>(*sibling))
|
|
|
|
return &to<T>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
2019-10-15 15:19:52 +03:00
|
|
|
}
|
2019-10-18 10:36:46 +03:00
|
|
|
|
|
|
|
template<typename T>
|
2020-06-09 21:42:11 +03:00
|
|
|
inline const T* LayoutNode::previous_sibling_of_type() const
|
2019-10-18 10:36:46 +03:00
|
|
|
{
|
2020-06-09 21:42:11 +03:00
|
|
|
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
|
2019-10-18 10:36:46 +03:00
|
|
|
if (is<T>(*sibling))
|
|
|
|
return &to<T>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-06-09 21:42:11 +03:00
|
|
|
inline T* LayoutNode::previous_sibling_of_type()
|
2019-10-18 10:36:46 +03:00
|
|
|
{
|
2020-06-09 21:42:11 +03:00
|
|
|
for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
|
2019-10-18 10:36:46 +03:00
|
|
|
if (is<T>(*sibling))
|
|
|
|
return &to<T>(*sibling);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline const T* LayoutNode::first_child_of_type() const
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (is<T>(*child))
|
|
|
|
return &to<T>(*child);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T* LayoutNode::first_child_of_type()
|
|
|
|
{
|
|
|
|
for (auto* child = first_child(); child; child = child->next_sibling()) {
|
|
|
|
if (is<T>(*child))
|
|
|
|
return &to<T>(*child);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-10-18 11:16:33 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline const T* LayoutNode::first_ancestor_of_type() const
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (is<T>(*ancestor))
|
|
|
|
return &to<T>(*ancestor);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T* LayoutNode::first_ancestor_of_type()
|
|
|
|
{
|
|
|
|
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
|
|
|
|
if (is<T>(*ancestor))
|
|
|
|
return &to<T>(*ancestor);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
template<>
|
|
|
|
inline bool is<LayoutNodeWithStyle>(const LayoutNode& node)
|
|
|
|
{
|
|
|
|
return node.has_style();
|
|
|
|
}
|
|
|
|
|
2020-03-07 12:27:02 +03:00
|
|
|
}
|