Node.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. Default,
  21. AllPossibleLineBreaks,
  22. OnlyRequiredLineBreaks,
  23. };
  24. enum class PaintPhase {
  25. Background,
  26. Border,
  27. Foreground,
  28. FocusOutline,
  29. Overlay,
  30. };
  31. struct HitTestResult {
  32. RefPtr<Node> layout_node;
  33. int index_in_node { 0 };
  34. enum InternalPosition {
  35. None,
  36. Before,
  37. Inside,
  38. After,
  39. };
  40. InternalPosition internal_position { None };
  41. };
  42. enum class HitTestType {
  43. Exact, // Exact matches only
  44. TextCursor, // Clicking past the right/bottom edge of text will still hit the text
  45. };
  46. class Node : public TreeNode<Node> {
  47. public:
  48. virtual ~Node();
  49. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
  50. bool is_anonymous() const { return !m_dom_node; }
  51. const DOM::Node* dom_node() const { return m_dom_node; }
  52. DOM::Node* dom_node() { return m_dom_node; }
  53. DOM::Document& document() { return m_document; }
  54. const DOM::Document& document() const { return m_document; }
  55. const Frame& frame() const;
  56. Frame& frame();
  57. const InitialContainingBlockBox& root() const;
  58. InitialContainingBlockBox& root();
  59. bool is_root_element() const;
  60. String class_name() const;
  61. bool has_style() const { return m_has_style; }
  62. virtual bool can_have_children() const { return true; }
  63. bool is_inline() const { return m_inline; }
  64. void set_inline(bool b) { m_inline = b; }
  65. bool is_inline_block() const;
  66. virtual bool wants_mouse_events() const { return false; }
  67. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  68. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  69. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
  70. virtual bool handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers, int wheel_delta);
  71. virtual void before_children_paint(PaintContext&, PaintPhase) {};
  72. virtual void paint(PaintContext&, PaintPhase);
  73. virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
  74. virtual void after_children_paint(PaintContext&, PaintPhase) {};
  75. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  76. virtual bool is_box() const { return false; }
  77. virtual bool is_block_box() const { return false; }
  78. virtual bool is_text_node() const { return false; }
  79. template<typename T>
  80. bool fast_is() const = delete;
  81. bool is_floating() const;
  82. bool is_positioned() const;
  83. bool is_absolutely_positioned() const;
  84. bool is_fixed_position() const;
  85. const BlockBox* containing_block() const;
  86. BlockBox* containing_block() { return const_cast<BlockBox*>(const_cast<const Node*>(this)->containing_block()); }
  87. bool establishes_stacking_context() const;
  88. bool can_contain_boxes_with_position_absolute() const;
  89. const Gfx::Font& font() const;
  90. const CSS::ImmutableComputedValues& computed_values() const;
  91. NodeWithStyle* parent();
  92. const NodeWithStyle* parent() const;
  93. void inserted_into(Node&) { }
  94. void removed_from(Node&) { }
  95. void children_changed() { }
  96. virtual void split_into_lines(InlineFormattingContext&, LayoutMode);
  97. bool is_visible() const { return m_visible; }
  98. void set_visible(bool visible) { m_visible = visible; }
  99. virtual void set_needs_display();
  100. bool children_are_inline() const { return m_children_are_inline; }
  101. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  102. Gfx::FloatPoint box_type_agnostic_position() const;
  103. float font_size() const;
  104. enum class SelectionState {
  105. None, // No selection
  106. Start, // Selection starts in this Node
  107. End, // Selection ends in this Node
  108. StartAndEnd, // Selection starts and ends in this Node
  109. Full, // Selection starts before and ends after this Node
  110. };
  111. SelectionState selection_state() const { return m_selection_state; }
  112. void set_selection_state(SelectionState state) { m_selection_state = state; }
  113. template<typename Callback>
  114. void for_each_child_in_paint_order(Callback callback) const
  115. {
  116. for_each_child([&](auto& child) {
  117. if (is<Box>(child) && downcast<Box>(child).stacking_context())
  118. return;
  119. if (!child.is_positioned())
  120. callback(child);
  121. });
  122. for_each_child([&](auto& child) {
  123. if (is<Box>(child) && downcast<Box>(child).stacking_context())
  124. return;
  125. if (child.is_positioned())
  126. callback(child);
  127. });
  128. }
  129. protected:
  130. Node(DOM::Document&, DOM::Node*);
  131. private:
  132. friend class NodeWithStyle;
  133. NonnullRefPtr<DOM::Document> m_document;
  134. RefPtr<DOM::Node> m_dom_node;
  135. bool m_inline { false };
  136. bool m_has_style { false };
  137. bool m_visible { true };
  138. bool m_children_are_inline { false };
  139. SelectionState m_selection_state { SelectionState::None };
  140. };
  141. class NodeWithStyle : public Node {
  142. public:
  143. virtual ~NodeWithStyle() override { }
  144. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  145. void apply_style(const CSS::StyleProperties&);
  146. const Gfx::Font& font() const { return *m_font; }
  147. float line_height() const { return m_line_height; }
  148. float font_size() const { return m_font_size; }
  149. const CSS::ImageStyleValue* background_image() const { return m_background_image; }
  150. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  151. protected:
  152. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  153. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  154. private:
  155. CSS::ComputedValues m_computed_values;
  156. RefPtr<Gfx::Font> m_font;
  157. float m_line_height { 0 };
  158. float m_font_size { 0 };
  159. RefPtr<CSS::ImageStyleValue> m_background_image;
  160. CSS::Position m_position;
  161. };
  162. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  163. public:
  164. BoxModelMetrics& box_model() { return m_box_model; }
  165. const BoxModelMetrics& box_model() const { return m_box_model; }
  166. protected:
  167. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  168. : NodeWithStyle(document, node, move(style))
  169. {
  170. }
  171. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  172. : NodeWithStyle(document, node, move(computed_values))
  173. {
  174. }
  175. private:
  176. BoxModelMetrics m_box_model;
  177. };
  178. inline const Gfx::Font& Node::font() const
  179. {
  180. if (m_has_style)
  181. return static_cast<const NodeWithStyle*>(this)->font();
  182. return parent()->font();
  183. }
  184. inline float Node::font_size() const
  185. {
  186. if (m_has_style)
  187. return static_cast<const NodeWithStyle*>(this)->font_size();
  188. return parent()->font_size();
  189. }
  190. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  191. {
  192. if (m_has_style)
  193. return static_cast<const NodeWithStyle*>(this)->computed_values();
  194. return parent()->computed_values();
  195. }
  196. inline const NodeWithStyle* Node::parent() const
  197. {
  198. return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
  199. }
  200. inline NodeWithStyle* Node::parent()
  201. {
  202. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  203. }
  204. }