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
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
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>
|
2022-08-28 14:42:07 +03:00
|
|
|
#include <LibJS/Heap/Handle.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>
|
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 {
|
2022-07-09 16:17:47 +03:00
|
|
|
// Normal layout. No min-content or max-content constraints applied.
|
2022-03-19 17:44:02 +03:00
|
|
|
Normal,
|
|
|
|
|
2022-07-09 16:17:47 +03:00
|
|
|
// Intrinsic size determination.
|
2022-07-17 00:43:48 +03:00
|
|
|
// Boxes honor min-content and max-content constraints (set via LayoutState::UsedValues::{width,height}_constraint)
|
2022-07-09 16:17:47 +03:00
|
|
|
// by considering their containing block to be 0-sized or infinitely large in the relevant axis.
|
2022-07-20 19:12:12 +03:00
|
|
|
// https://drafts.csswg.org/css-sizing-3/#intrinsic-sizing
|
|
|
|
IntrinsicSizing,
|
2020-11-22 15:38:18 +03:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-07-11 18:12:58 +03:00
|
|
|
size_t serial_id() const { return m_serial_id; }
|
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
bool is_anonymous() const;
|
|
|
|
DOM::Node const* dom_node() const;
|
|
|
|
DOM::Node* dom_node();
|
2019-06-15 23:49:44 +03:00
|
|
|
|
2022-09-28 18:11:23 +03:00
|
|
|
bool is_generated() const { return m_generated; }
|
|
|
|
void set_generated(bool b) { m_generated = b; }
|
|
|
|
|
2022-03-11 00:38:08 +03:00
|
|
|
Painting::Paintable* paintable() { return m_paintable; }
|
2022-03-10 17:50:57 +03:00
|
|
|
Painting::Paintable const* paintable() const { return m_paintable; }
|
2022-03-11 00:38:08 +03:00
|
|
|
void set_paintable(RefPtr<Painting::Paintable>);
|
2022-03-10 17:50:57 +03:00
|
|
|
|
2022-03-11 00:38:08 +03:00
|
|
|
virtual RefPtr<Painting::Paintable> create_paintable() const;
|
2022-03-10 17:50:57 +03:00
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
DOM::Document& document();
|
|
|
|
DOM::Document const& document() const;
|
2019-09-29 12:43:33 +03:00
|
|
|
|
2021-11-18 17:01:28 +03:00
|
|
|
HTML::BrowsingContext const& browsing_context() const;
|
|
|
|
HTML::BrowsingContext& browsing_context();
|
2020-06-14 17:45:45 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
InitialContainingBlock const& root() const;
|
2021-09-08 12:27:46 +03:00
|
|
|
InitialContainingBlock& 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;
|
2022-02-19 18:39:32 +03:00
|
|
|
String debug_description() const;
|
2021-01-01 18:42:44 +03:00
|
|
|
|
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
|
|
|
|
2022-03-22 20:34:02 +03:00
|
|
|
bool is_out_of_flow(FormattingContext const&) const;
|
|
|
|
|
2021-01-17 11:34:01 +03:00
|
|
|
// These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
|
2021-01-07 19:31:26 +03:00
|
|
|
virtual bool is_box() const { return false; }
|
2021-10-06 21:07:13 +03:00
|
|
|
virtual bool is_block_container() const { return false; }
|
2021-10-26 17:15:53 +03:00
|
|
|
virtual bool is_break_node() const { return false; }
|
2021-01-17 11:34:01 +03:00
|
|
|
virtual bool is_text_node() const { return false; }
|
2021-08-18 12:44:05 +03:00
|
|
|
virtual bool is_initial_containing_block_box() const { return false; }
|
2021-10-18 17:26:05 +03:00
|
|
|
virtual bool is_svg_box() const { return false; }
|
2022-02-11 15:37:22 +03:00
|
|
|
virtual bool is_svg_geometry_box() const { return false; }
|
2021-10-27 18:58:19 +03:00
|
|
|
virtual bool is_label() const { return false; }
|
2022-03-13 19:21:27 +03:00
|
|
|
virtual bool is_replaced_box() const { return false; }
|
|
|
|
virtual bool is_list_item_marker_box() const { return false; }
|
2021-01-17 11:34:01 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool fast_is() const = delete;
|
2021-01-07 19:31:26 +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
|
|
|
|
2021-05-29 23:36:15 +03:00
|
|
|
bool is_flex_item() const { return m_is_flex_item; }
|
|
|
|
void set_flex_item(bool b) { m_is_flex_item = b; }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
BlockContainer const* containing_block() const;
|
|
|
|
BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<Node const*>(this)->containing_block()); }
|
2019-07-01 08:28:37 +03:00
|
|
|
|
2021-05-07 19:03:25 +03:00
|
|
|
bool establishes_stacking_context() const;
|
|
|
|
|
2020-06-05 17:54:28 +03:00
|
|
|
bool can_contain_boxes_with_position_absolute() const;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Gfx::Font const& font() const;
|
2021-01-06 13:07:02 +03:00
|
|
|
const CSS::ImmutableComputedValues& computed_values() const;
|
2022-03-24 19:37:24 +03:00
|
|
|
float line_height() const;
|
2019-10-07 10:50:31 +03:00
|
|
|
|
2020-11-22 17:53:01 +03:00
|
|
|
NodeWithStyle* parent();
|
2022-04-01 20:58:27 +03:00
|
|
|
NodeWithStyle const* 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
|
|
|
|
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-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
|
|
|
|
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
|
|
|
|
2022-08-28 14:42:07 +03:00
|
|
|
JS::Handle<DOM::Document> m_document;
|
|
|
|
JS::Handle<DOM::Node> m_dom_node;
|
2022-03-11 00:38:08 +03:00
|
|
|
RefPtr<Painting::Paintable> m_paintable;
|
2019-06-25 20:46:01 +03:00
|
|
|
|
2022-07-11 18:12:58 +03:00
|
|
|
size_t m_serial_id { 0 };
|
|
|
|
|
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 };
|
2021-05-29 23:36:15 +03:00
|
|
|
|
|
|
|
bool m_is_flex_item { false };
|
2022-09-28 18:11:23 +03:00
|
|
|
bool m_generated { false };
|
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:
|
2022-03-14 22:21:51 +03:00
|
|
|
virtual ~NodeWithStyle() override = default;
|
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
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Gfx::Font const& font() const { return *m_font; }
|
2021-01-06 13:31:19 +03:00
|
|
|
float line_height() const { return m_line_height; }
|
2021-11-12 15:11:01 +03:00
|
|
|
Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
|
2022-07-31 03:11:59 +03:00
|
|
|
const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; }
|
2021-01-06 13:05:23 +03:00
|
|
|
|
2021-01-06 16:10:53 +03:00
|
|
|
NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
|
|
|
|
|
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>);
|
2021-01-06 16:10:53 +03:00
|
|
|
NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
|
2019-10-07 10:50:31 +03:00
|
|
|
|
|
|
|
private:
|
2021-01-06 12:34:31 +03:00
|
|
|
CSS::ComputedValues m_computed_values;
|
2021-01-06 13:05:23 +03:00
|
|
|
RefPtr<Gfx::Font> m_font;
|
2021-01-06 13:31:19 +03:00
|
|
|
float m_line_height { 0 };
|
2022-07-31 03:11:59 +03:00
|
|
|
RefPtr<CSS::AbstractImageStyleValue> m_list_style_image;
|
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; }
|
2022-04-01 20:58:27 +03:00
|
|
|
BoxModelMetrics const& box_model() const { return m_box_model; }
|
2019-10-15 17:48:38 +03:00
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-06 16:10:53 +03:00
|
|
|
NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
|
|
|
|
: NodeWithStyle(document, node, move(computed_values))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:48:38 +03:00
|
|
|
private:
|
|
|
|
BoxModelMetrics m_box_model;
|
|
|
|
};
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
inline Gfx::Font const& Node::font() const
|
2021-01-06 13:05:23 +03:00
|
|
|
{
|
|
|
|
if (m_has_style)
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<NodeWithStyle const*>(this)->font();
|
2021-01-06 13:05:23 +03:00
|
|
|
return parent()->font();
|
|
|
|
}
|
|
|
|
|
2021-01-06 13:07:02 +03:00
|
|
|
inline const CSS::ImmutableComputedValues& Node::computed_values() const
|
2020-06-24 15:17:05 +03:00
|
|
|
{
|
|
|
|
if (m_has_style)
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<NodeWithStyle const*>(this)->computed_values();
|
2021-01-06 12:34:31 +03:00
|
|
|
return parent()->computed_values();
|
2020-06-24 15:17:05 +03:00
|
|
|
}
|
|
|
|
|
2022-03-24 19:37:24 +03:00
|
|
|
inline float Node::line_height() const
|
|
|
|
{
|
|
|
|
if (m_has_style)
|
|
|
|
return static_cast<NodeWithStyle const*>(this)->line_height();
|
|
|
|
return parent()->line_height();
|
|
|
|
}
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
inline NodeWithStyle const* Node::parent() const
|
2019-10-07 10:50:31 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
return static_cast<NodeWithStyle const*>(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
|
|
|
}
|