2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-01-01 18:08:19 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
* 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>
|
2020-07-26 18:16:18 +03:00
|
|
|
#include <AK/TypeCasts.h>
|
2019-06-15 23:49:44 +03:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:00 +03:00
|
|
|
#include <LibGfx/Rect.h>
|
2021-01-06 12:34:31 +03:00
|
|
|
#include <LibWeb/CSS/ComputedValues.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-11-22 17:53:01 +03:00
|
|
|
namespace Web::Layout {
|
2020-03-07 12:27:02 +03:00
|
|
|
|
2020-11-22 15:38:18 +03:00
|
|
|
enum class LayoutMode {
|
|
|
|
Default,
|
|
|
|
AllPossibleLineBreaks,
|
|
|
|
OnlyRequiredLineBreaks,
|
|
|
|
};
|
|
|
|
|
2020-12-03 21:15:27 +03:00
|
|
|
enum class PaintPhase {
|
|
|
|
Background,
|
|
|
|
Border,
|
|
|
|
Foreground,
|
|
|
|
FocusOutline,
|
|
|
|
Overlay,
|
|
|
|
};
|
|
|
|
|
2019-09-29 00:02:22 +03:00
|
|
|
struct HitTestResult {
|
2020-11-22 17:53:01 +03:00
|
|
|
RefPtr<Node> layout_node;
|
2019-11-06 00:13:26 +03:00
|
|
|
int index_in_node { 0 };
|
2020-08-21 18:50:41 +03:00
|
|
|
|
|
|
|
enum InternalPosition {
|
|
|
|
None,
|
|
|
|
Before,
|
|
|
|
Inside,
|
|
|
|
After,
|
|
|
|
};
|
|
|
|
InternalPosition internal_position { None };
|
2019-09-29 00:02:22 +03:00
|
|
|
};
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-08-05 17:55:56 +03:00
|
|
|
enum class HitTestType {
|
|
|
|
Exact, // Exact matches only
|
|
|
|
TextCursor, // Clicking past the right/bottom edge of text will still hit the text
|
|
|
|
};
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
class Node : public TreeNode<Node> {
|
2019-06-15 23:49:44 +03:00
|
|
|
public:
|
2020-11-22 17:53:01 +03:00
|
|
|
virtual ~Node();
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-08-05 17:55:56 +03:00
|
|
|
virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
|
2019-09-29 00:02:22 +03:00
|
|
|
|
2020-11-22 16:46:36 +03:00
|
|
|
bool is_anonymous() const { return !m_dom_node; }
|
|
|
|
const DOM::Node* dom_node() const { return m_dom_node; }
|
|
|
|
DOM::Node* dom_node() { return m_dom_node; }
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2020-07-26 20:37:56 +03:00
|
|
|
DOM::Document& document() { return m_document; }
|
|
|
|
const DOM::Document& document() const { return m_document; }
|
2019-09-29 12:43:33 +03:00
|
|
|
|
2020-06-14 17:45:45 +03:00
|
|
|
const Frame& frame() const;
|
|
|
|
Frame& frame();
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
const InitialContainingBlockBox& root() const;
|
|
|
|
InitialContainingBlockBox& root();
|
2019-11-04 21:37:52 +03:00
|
|
|
|
2020-12-05 22:10:02 +03:00
|
|
|
bool is_root_element() const;
|
|
|
|
|
2021-01-01 18:42:44 +03:00
|
|
|
String class_name() const;
|
|
|
|
|
2019-10-15 15:19:52 +03:00
|
|
|
bool has_style() const { return m_has_style; }
|
2019-10-05 23:07:45 +03:00
|
|
|
|
2020-11-29 17:29:10 +03:00
|
|
|
virtual bool can_have_children() const { return true; }
|
|
|
|
|
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
|
|
|
|
2021-01-01 20:55:47 +03:00
|
|
|
bool is_inline_block() const;
|
2020-05-05 17:06:22 +03:00
|
|
|
|
2020-09-11 19:15:47 +03:00
|
|
|
virtual bool wants_mouse_events() const { return false; }
|
|
|
|
|
|
|
|
virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
|
|
|
virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
|
|
|
|
virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
|
|
|
|
|
2020-10-06 02:11:33 +03:00
|
|
|
virtual void before_children_paint(PaintContext&, PaintPhase) {};
|
2020-06-18 22:35:44 +03:00
|
|
|
virtual void paint(PaintContext&, PaintPhase);
|
2020-12-03 21:30:48 +03:00
|
|
|
virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
|
2020-10-06 02:11:33 +03:00
|
|
|
virtual void after_children_paint(PaintContext&, PaintPhase) {};
|
2019-06-16 22:35:03 +03:00
|
|
|
|
2020-06-26 16:08:42 +03:00
|
|
|
bool is_floating() const;
|
2020-12-06 21:54:23 +03:00
|
|
|
bool is_positioned() const;
|
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
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
const BlockBox* containing_block() const;
|
2020-12-06 21:48:02 +03:00
|
|
|
BlockBox* containing_block() { return const_cast<BlockBox*>(const_cast<const Node*>(this)->containing_block()); }
|
2019-07-01 08:28:37 +03:00
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
bool can_contain_boxes_with_position_absolute() const;
|
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
const CSS::StyleProperties& specified_style() const;
|
2021-01-06 12:34:31 +03:00
|
|
|
const CSS::ImmutableComputedValues& style() const;
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
NodeWithStyle* parent();
|
|
|
|
const NodeWithStyle* parent() const;
|
2019-07-24 08:34:07 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
void inserted_into(Node&) { }
|
|
|
|
void removed_from(Node&) { }
|
2020-05-26 22:53:10 +03:00
|
|
|
void children_changed() { }
|
2019-09-29 18:40:39 +03:00
|
|
|
|
2020-12-05 22:10:39 +03:00
|
|
|
virtual void split_into_lines(InlineFormattingContext&, 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; }
|
|
|
|
|
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;
|
|
|
|
|
2020-08-21 18:50:41 +03:00
|
|
|
enum class SelectionState {
|
|
|
|
None, // No selection
|
2020-11-22 17:53:01 +03:00
|
|
|
Start, // Selection starts in this Node
|
|
|
|
End, // Selection ends in this Node
|
|
|
|
StartAndEnd, // Selection starts and ends in this Node
|
|
|
|
Full, // Selection starts before and ends after this Node
|
2020-08-21 18:50:41 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
SelectionState selection_state() const { return m_selection_state; }
|
|
|
|
void set_selection_state(SelectionState state) { m_selection_state = state; }
|
|
|
|
|
2020-12-06 21:59:28 +03:00
|
|
|
template<typename Callback>
|
|
|
|
void for_each_child_in_paint_order(Callback callback) const
|
|
|
|
{
|
|
|
|
for_each_child([&](auto& child) {
|
|
|
|
if (is<Box>(child) && downcast<Box>(child).stacking_context())
|
|
|
|
return;
|
|
|
|
if (!child.is_positioned())
|
|
|
|
callback(child);
|
|
|
|
});
|
|
|
|
for_each_child([&](auto& child) {
|
|
|
|
if (is<Box>(child) && downcast<Box>(child).stacking_context())
|
|
|
|
return;
|
|
|
|
if (child.is_positioned())
|
|
|
|
callback(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-15 23:49:44 +03:00
|
|
|
protected:
|
2020-11-22 17:53:01 +03:00
|
|
|
Node(DOM::Document&, DOM::Node*);
|
2019-06-15 23:49:44 +03:00
|
|
|
|
|
|
|
private:
|
2020-11-22 17:53:01 +03:00
|
|
|
friend class NodeWithStyle;
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-10-22 21:26:32 +03:00
|
|
|
NonnullRefPtr<DOM::Document> m_document;
|
2020-11-22 16:46:36 +03:00
|
|
|
RefPtr<DOM::Node> m_dom_node;
|
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 };
|
2020-08-21 18:50:41 +03:00
|
|
|
SelectionState m_selection_state { SelectionState::None };
|
2019-06-15 23:49:44 +03:00
|
|
|
};
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
class NodeWithStyle : public Node {
|
2019-10-07 10:50:31 +03:00
|
|
|
public:
|
2020-11-22 17:53:01 +03:00
|
|
|
virtual ~NodeWithStyle() override { }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
const CSS::StyleProperties& specified_style() const { return m_specified_style; }
|
|
|
|
void set_specified_style(const CSS::StyleProperties& style) { m_specified_style = style; }
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2021-01-06 12:34:31 +03:00
|
|
|
const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
|
2020-06-24 15:17:05 +03:00
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
void apply_style(const CSS::StyleProperties&);
|
2020-06-24 20:41:12 +03:00
|
|
|
|
2019-10-07 10:50:31 +03:00
|
|
|
protected:
|
2020-11-22 17:53:01 +03:00
|
|
|
NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
private:
|
2021-01-06 12:34:31 +03:00
|
|
|
CSS::ComputedValues m_computed_values;
|
2020-06-24 15:17:05 +03:00
|
|
|
|
2020-07-26 21:01:35 +03:00
|
|
|
NonnullRefPtr<CSS::StyleProperties> m_specified_style;
|
2020-06-24 00:15:23 +03:00
|
|
|
CSS::Position m_position;
|
2019-10-07 10:50:31 +03:00
|
|
|
};
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
|
2019-10-15 17:48:38 +03:00
|
|
|
public:
|
|
|
|
BoxModelMetrics& box_model() { return m_box_model; }
|
|
|
|
const BoxModelMetrics& box_model() const { return m_box_model; }
|
|
|
|
|
|
|
|
protected:
|
2020-11-22 17:53:01 +03:00
|
|
|
NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
|
|
|
|
: NodeWithStyle(document, node, move(style))
|
2019-10-15 17:48:38 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
BoxModelMetrics m_box_model;
|
|
|
|
};
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
inline const CSS::StyleProperties& Node::specified_style() const
|
2019-10-07 10:50:31 +03:00
|
|
|
{
|
|
|
|
if (m_has_style)
|
2020-11-22 17:53:01 +03:00
|
|
|
return static_cast<const NodeWithStyle*>(this)->specified_style();
|
2020-06-24 14:51:14 +03:00
|
|
|
return parent()->specified_style();
|
2019-10-07 10:50:31 +03:00
|
|
|
}
|
|
|
|
|
2021-01-06 12:34:31 +03:00
|
|
|
inline const CSS::ImmutableComputedValues& Node::style() const
|
2020-06-24 15:17:05 +03:00
|
|
|
{
|
|
|
|
if (m_has_style)
|
2021-01-06 12:34:31 +03:00
|
|
|
return static_cast<const NodeWithStyle*>(this)->computed_values();
|
|
|
|
return parent()->computed_values();
|
2020-06-24 15:17:05 +03:00
|
|
|
}
|
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
inline const NodeWithStyle* Node::parent() const
|
2019-10-07 10:50:31 +03:00
|
|
|
{
|
2020-11-22 17:53:01 +03:00
|
|
|
return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
|
2019-10-07 10:50:31 +03:00
|
|
|
}
|
2019-10-15 15:19:52 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
inline NodeWithStyle* Node::parent()
|
2019-10-18 11:16:33 +03:00
|
|
|
{
|
2020-11-22 17:53:01 +03:00
|
|
|
return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
|
2019-10-18 11:16:33 +03:00
|
|
|
}
|
|
|
|
|
2020-06-09 21:42:11 +03:00
|
|
|
}
|