Node.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. HTML::BrowsingContext const& browsing_context() const;
  56. HTML::BrowsingContext& browsing_context();
  57. const InitialContainingBlock& root() const;
  58. InitialContainingBlock& root();
  59. bool is_root_element() const;
  60. String class_name() const;
  61. String debug_description() const;
  62. bool has_style() const { return m_has_style; }
  63. virtual bool can_have_children() const { return true; }
  64. bool is_inline() const { return m_inline; }
  65. void set_inline(bool b) { m_inline = b; }
  66. bool is_inline_block() const;
  67. virtual bool wants_mouse_events() const { return false; }
  68. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  69. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  70. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
  71. virtual bool handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y);
  72. virtual void before_children_paint(PaintContext&, PaintPhase) {};
  73. virtual void paint(PaintContext&, PaintPhase) = 0;
  74. virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
  75. virtual void after_children_paint(PaintContext&, PaintPhase) {};
  76. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  77. virtual bool is_box() const { return false; }
  78. virtual bool is_block_container() const { return false; }
  79. virtual bool is_break_node() const { return false; }
  80. virtual bool is_text_node() const { return false; }
  81. virtual bool is_initial_containing_block_box() const { return false; }
  82. virtual bool is_svg_box() const { return false; }
  83. virtual bool is_svg_geometry_box() const { return false; }
  84. virtual bool is_label() const { return false; }
  85. template<typename T>
  86. bool fast_is() const = delete;
  87. bool is_floating() const;
  88. bool is_positioned() const;
  89. bool is_absolutely_positioned() const;
  90. bool is_fixed_position() const;
  91. bool is_flex_item() const { return m_is_flex_item; }
  92. void set_flex_item(bool b) { m_is_flex_item = b; }
  93. const BlockContainer* containing_block() const;
  94. BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<const Node*>(this)->containing_block()); }
  95. bool establishes_stacking_context() const;
  96. bool can_contain_boxes_with_position_absolute() const;
  97. const Gfx::Font& font() const;
  98. const CSS::ImmutableComputedValues& computed_values() const;
  99. NodeWithStyle* parent();
  100. const NodeWithStyle* parent() const;
  101. void inserted_into(Node&) { }
  102. void removed_from(Node&) { }
  103. void children_changed() { }
  104. bool is_visible() const { return m_visible; }
  105. void set_visible(bool visible) { m_visible = visible; }
  106. virtual void set_needs_display();
  107. bool children_are_inline() const { return m_children_are_inline; }
  108. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  109. Gfx::FloatPoint box_type_agnostic_position() const;
  110. enum class SelectionState {
  111. None, // No selection
  112. Start, // Selection starts in this Node
  113. End, // Selection ends in this Node
  114. StartAndEnd, // Selection starts and ends in this Node
  115. Full, // Selection starts before and ends after this Node
  116. };
  117. SelectionState selection_state() const { return m_selection_state; }
  118. void set_selection_state(SelectionState state) { m_selection_state = state; }
  119. template<typename Callback>
  120. void for_each_child_in_paint_order(Callback callback) const
  121. {
  122. // Element traversal using the order defined in https://www.w3.org/TR/CSS2/zindex.html#painting-order.
  123. // Note: Some steps are skipped because they are not relevant to node traversal.
  124. // 3. Stacking contexts formed by positioned descendants with negative z-indices (excluding 0) in z-index order
  125. // (most negative first) then tree order.
  126. // FIXME: This does not retrieve elements in the z-index order.
  127. for_each_child([&](auto& child) {
  128. if (!child.is_positioned() || !is<Box>(child))
  129. return;
  130. auto& box_child = verify_cast<Box>(child);
  131. auto* stacking_context = box_child.stacking_context();
  132. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() < 0)
  133. callback(child);
  134. });
  135. // 4. For all its in-flow, non-positioned, block-level descendants in tree order: If the element is a block, list-item,
  136. // or other block equivalent:
  137. for_each_child([&](auto& child) {
  138. if (is<Box>(child) && verify_cast<Box>(child).stacking_context())
  139. return;
  140. if (!child.is_positioned())
  141. callback(child);
  142. });
  143. // 5. All non-positioned floating descendants, in tree order. For each one of these, treat the element as if it created
  144. // a new stacking context, but any positioned descendants and descendants which actually create a new stacking context
  145. // should be considered part of the parent stacking context, not this new one.
  146. for_each_child([&](auto& child) {
  147. if (is<Box>(child) && verify_cast<Box>(child).stacking_context())
  148. return;
  149. if (child.is_positioned())
  150. callback(child);
  151. });
  152. // 8. All positioned descendants with 'z-index: auto' or 'z-index: 0', in tree order. For those with 'z-index: auto', treat
  153. // the element as if it created a new stacking context, but any positioned descendants and descendants which actually
  154. // create a new stacking context should be considered part of the parent stacking context, not this new one. For those
  155. // with 'z-index: 0', treat the stacking context generated atomically.
  156. for_each_child([&](auto& child) {
  157. if (!child.is_positioned() || !is<Box>(child))
  158. return;
  159. auto& box_child = verify_cast<Box>(child);
  160. auto* stacking_context = box_child.stacking_context();
  161. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() == 0)
  162. callback(child);
  163. });
  164. // 9. Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order
  165. // (smallest first) then tree order.
  166. // FIXME: This does not retrieve elements in the z-index order.
  167. for_each_child([&](auto& child) {
  168. if (!child.is_positioned() || !is<Box>(child))
  169. return;
  170. auto& box_child = verify_cast<Box>(child);
  171. auto* stacking_context = box_child.stacking_context();
  172. if (stacking_context && box_child.computed_values().z_index().has_value() && box_child.computed_values().z_index().value() > 0)
  173. callback(child);
  174. });
  175. }
  176. protected:
  177. Node(DOM::Document&, DOM::Node*);
  178. private:
  179. friend class NodeWithStyle;
  180. NonnullRefPtr<DOM::Document> m_document;
  181. RefPtr<DOM::Node> m_dom_node;
  182. bool m_inline { false };
  183. bool m_has_style { false };
  184. bool m_visible { true };
  185. bool m_children_are_inline { false };
  186. SelectionState m_selection_state { SelectionState::None };
  187. bool m_is_flex_item { false };
  188. };
  189. class NodeWithStyle : public Node {
  190. public:
  191. virtual ~NodeWithStyle() override { }
  192. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  193. void apply_style(const CSS::StyleProperties&);
  194. const Gfx::Font& font() const { return *m_font; }
  195. float line_height() const { return m_line_height; }
  196. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  197. const CSS::ImageStyleValue* list_style_image() const { return m_list_style_image; }
  198. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  199. bool has_definite_height() const { return m_has_definite_height; }
  200. bool has_definite_width() const { return m_has_definite_width; }
  201. protected:
  202. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  203. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  204. private:
  205. CSS::ComputedValues m_computed_values;
  206. RefPtr<Gfx::Font> m_font;
  207. float m_line_height { 0 };
  208. RefPtr<CSS::ImageStyleValue> m_list_style_image;
  209. bool m_has_definite_height { false };
  210. bool m_has_definite_width { false };
  211. };
  212. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  213. public:
  214. BoxModelMetrics& box_model() { return m_box_model; }
  215. const BoxModelMetrics& box_model() const { return m_box_model; }
  216. protected:
  217. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  218. : NodeWithStyle(document, node, move(style))
  219. {
  220. }
  221. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  222. : NodeWithStyle(document, node, move(computed_values))
  223. {
  224. }
  225. private:
  226. BoxModelMetrics m_box_model;
  227. };
  228. inline const Gfx::Font& Node::font() const
  229. {
  230. if (m_has_style)
  231. return static_cast<const NodeWithStyle*>(this)->font();
  232. return parent()->font();
  233. }
  234. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  235. {
  236. if (m_has_style)
  237. return static_cast<const NodeWithStyle*>(this)->computed_values();
  238. return parent()->computed_values();
  239. }
  240. inline const NodeWithStyle* Node::parent() const
  241. {
  242. return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
  243. }
  244. inline NodeWithStyle* Node::parent()
  245. {
  246. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  247. }
  248. }