Node.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/TypeCasts.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Rect.h>
  11. #include <LibWeb/CSS/ComputedValues.h>
  12. #include <LibWeb/CSS/StyleProperties.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/Layout/BoxModelMetrics.h>
  15. #include <LibWeb/Layout/LayoutPosition.h>
  16. #include <LibWeb/Painting/PaintContext.h>
  17. #include <LibWeb/TreeNode.h>
  18. namespace Web::Layout {
  19. enum class LayoutMode {
  20. // Normal layout.
  21. // - We use the containing block's used width.
  22. // - Content flows into the available space, line breaks inserted where necessary.
  23. Normal,
  24. // MinContent layout is used for discovering the min-content intrinsic size of a box.
  25. // - We act as if the containing block has 0 used width.
  26. // - Every line-breaking opportunity is taken.
  27. MinContent,
  28. // MaxContent layout is used for discovering the max-content intrinsic size of a box.
  29. // - We act as if the containing block has infinite used width.
  30. // - Only forced line-breaking opportunities are taken.
  31. MaxContent,
  32. };
  33. class Node : public TreeNode<Node> {
  34. public:
  35. virtual ~Node();
  36. bool is_anonymous() const { return !m_dom_node; }
  37. const DOM::Node* dom_node() const { return m_dom_node; }
  38. DOM::Node* dom_node() { return m_dom_node; }
  39. Painting::Paintable* paintable() { return m_paintable; }
  40. Painting::Paintable const* paintable() const { return m_paintable; }
  41. void set_paintable(RefPtr<Painting::Paintable>);
  42. virtual RefPtr<Painting::Paintable> create_paintable() const;
  43. DOM::Document& document() { return m_document; }
  44. const DOM::Document& document() const { return m_document; }
  45. HTML::BrowsingContext const& browsing_context() const;
  46. HTML::BrowsingContext& browsing_context();
  47. const InitialContainingBlock& root() const;
  48. InitialContainingBlock& root();
  49. bool is_root_element() const;
  50. String class_name() const;
  51. String debug_description() const;
  52. bool has_style() const { return m_has_style; }
  53. virtual bool can_have_children() const { return true; }
  54. bool is_inline() const { return m_inline; }
  55. void set_inline(bool b) { m_inline = b; }
  56. bool is_inline_block() const;
  57. bool is_out_of_flow(FormattingContext const&) const;
  58. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  59. virtual bool is_box() const { return false; }
  60. virtual bool is_block_container() const { return false; }
  61. virtual bool is_break_node() const { return false; }
  62. virtual bool is_text_node() const { return false; }
  63. virtual bool is_initial_containing_block_box() const { return false; }
  64. virtual bool is_svg_box() const { return false; }
  65. virtual bool is_svg_geometry_box() const { return false; }
  66. virtual bool is_label() const { return false; }
  67. virtual bool is_replaced_box() const { return false; }
  68. virtual bool is_list_item_marker_box() const { return false; }
  69. template<typename T>
  70. bool fast_is() const = delete;
  71. bool is_floating() const;
  72. bool is_positioned() const;
  73. bool is_absolutely_positioned() const;
  74. bool is_fixed_position() const;
  75. bool is_flex_item() const { return m_is_flex_item; }
  76. void set_flex_item(bool b) { m_is_flex_item = b; }
  77. const BlockContainer* containing_block() const;
  78. BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<const Node*>(this)->containing_block()); }
  79. bool establishes_stacking_context() const;
  80. bool can_contain_boxes_with_position_absolute() const;
  81. const Gfx::Font& font() const;
  82. const CSS::ImmutableComputedValues& computed_values() const;
  83. NodeWithStyle* parent();
  84. const NodeWithStyle* parent() const;
  85. void inserted_into(Node&) { }
  86. void removed_from(Node&) { }
  87. void children_changed() { }
  88. bool is_visible() const { return m_visible; }
  89. void set_visible(bool visible) { m_visible = visible; }
  90. virtual void set_needs_display();
  91. bool children_are_inline() const { return m_children_are_inline; }
  92. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  93. Gfx::FloatPoint box_type_agnostic_position() const;
  94. enum class SelectionState {
  95. None, // No selection
  96. Start, // Selection starts in this Node
  97. End, // Selection ends in this Node
  98. StartAndEnd, // Selection starts and ends in this Node
  99. Full, // Selection starts before and ends after this Node
  100. };
  101. SelectionState selection_state() const { return m_selection_state; }
  102. void set_selection_state(SelectionState state) { m_selection_state = state; }
  103. protected:
  104. Node(DOM::Document&, DOM::Node*);
  105. private:
  106. friend class NodeWithStyle;
  107. NonnullRefPtr<DOM::Document> m_document;
  108. RefPtr<DOM::Node> m_dom_node;
  109. RefPtr<Painting::Paintable> m_paintable;
  110. bool m_inline { false };
  111. bool m_has_style { false };
  112. bool m_visible { true };
  113. bool m_children_are_inline { false };
  114. SelectionState m_selection_state { SelectionState::None };
  115. bool m_is_flex_item { false };
  116. };
  117. class NodeWithStyle : public Node {
  118. public:
  119. virtual ~NodeWithStyle() override = default;
  120. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  121. void apply_style(const CSS::StyleProperties&);
  122. const Gfx::Font& font() const { return *m_font; }
  123. float line_height() const { return m_line_height; }
  124. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  125. const CSS::ImageStyleValue* list_style_image() const { return m_list_style_image; }
  126. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  127. bool has_definite_height() const { return m_has_definite_height; }
  128. bool has_definite_width() const { return m_has_definite_width; }
  129. void set_has_definite_height(bool b) { m_has_definite_height = b; }
  130. void set_has_definite_width(bool b) { m_has_definite_width = b; }
  131. void did_insert_into_layout_tree(CSS::StyleProperties const&);
  132. protected:
  133. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  134. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  135. private:
  136. CSS::ComputedValues m_computed_values;
  137. RefPtr<Gfx::Font> m_font;
  138. float m_line_height { 0 };
  139. RefPtr<CSS::ImageStyleValue> m_list_style_image;
  140. bool m_has_definite_height { false };
  141. bool m_has_definite_width { false };
  142. };
  143. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  144. public:
  145. BoxModelMetrics& box_model() { return m_box_model; }
  146. const BoxModelMetrics& box_model() const { return m_box_model; }
  147. protected:
  148. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  149. : NodeWithStyle(document, node, move(style))
  150. {
  151. }
  152. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  153. : NodeWithStyle(document, node, move(computed_values))
  154. {
  155. }
  156. private:
  157. BoxModelMetrics m_box_model;
  158. };
  159. inline const Gfx::Font& Node::font() const
  160. {
  161. if (m_has_style)
  162. return static_cast<const NodeWithStyle*>(this)->font();
  163. return parent()->font();
  164. }
  165. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  166. {
  167. if (m_has_style)
  168. return static_cast<const NodeWithStyle*>(this)->computed_values();
  169. return parent()->computed_values();
  170. }
  171. inline const NodeWithStyle* Node::parent() const
  172. {
  173. return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
  174. }
  175. inline NodeWithStyle* Node::parent()
  176. {
  177. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  178. }
  179. }