Node.h 11 KB

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