Node.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/NonnullRefPtr.h>
  28. #include <AK/TypeCasts.h>
  29. #include <AK/Vector.h>
  30. #include <LibGfx/Rect.h>
  31. #include <LibWeb/CSS/ComputedValues.h>
  32. #include <LibWeb/CSS/StyleProperties.h>
  33. #include <LibWeb/Forward.h>
  34. #include <LibWeb/Layout/BoxModelMetrics.h>
  35. #include <LibWeb/Layout/LayoutPosition.h>
  36. #include <LibWeb/Painting/PaintContext.h>
  37. #include <LibWeb/TreeNode.h>
  38. namespace Web::Layout {
  39. enum class LayoutMode {
  40. Default,
  41. AllPossibleLineBreaks,
  42. OnlyRequiredLineBreaks,
  43. };
  44. enum class PaintPhase {
  45. Background,
  46. Border,
  47. Foreground,
  48. FocusOutline,
  49. Overlay,
  50. };
  51. struct HitTestResult {
  52. RefPtr<Node> layout_node;
  53. int index_in_node { 0 };
  54. enum InternalPosition {
  55. None,
  56. Before,
  57. Inside,
  58. After,
  59. };
  60. InternalPosition internal_position { None };
  61. };
  62. enum class HitTestType {
  63. Exact, // Exact matches only
  64. TextCursor, // Clicking past the right/bottom edge of text will still hit the text
  65. };
  66. class Node : public TreeNode<Node> {
  67. public:
  68. virtual ~Node();
  69. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
  70. bool is_anonymous() const { return !m_dom_node; }
  71. const DOM::Node* dom_node() const { return m_dom_node; }
  72. DOM::Node* dom_node() { return m_dom_node; }
  73. DOM::Document& document() { return m_document; }
  74. const DOM::Document& document() const { return m_document; }
  75. const Frame& frame() const;
  76. Frame& frame();
  77. const InitialContainingBlockBox& root() const;
  78. InitialContainingBlockBox& root();
  79. bool is_root_element() const;
  80. String class_name() const;
  81. bool has_style() const { return m_has_style; }
  82. virtual bool can_have_children() const { return true; }
  83. bool is_inline() const { return m_inline; }
  84. void set_inline(bool b) { m_inline = b; }
  85. bool is_inline_block() const;
  86. virtual bool wants_mouse_events() const { return false; }
  87. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  88. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  89. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
  90. virtual bool handle_mousewheel(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers, int wheel_delta);
  91. virtual void before_children_paint(PaintContext&, PaintPhase) {};
  92. virtual void paint(PaintContext&, PaintPhase);
  93. virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
  94. virtual void after_children_paint(PaintContext&, PaintPhase) {};
  95. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  96. virtual bool is_box() const { return false; }
  97. virtual bool is_block_box() const { return false; }
  98. virtual bool is_text_node() const { return false; }
  99. template<typename T>
  100. bool fast_is() const = delete;
  101. bool is_floating() const;
  102. bool is_positioned() const;
  103. bool is_absolutely_positioned() const;
  104. bool is_fixed_position() const;
  105. const BlockBox* containing_block() const;
  106. BlockBox* containing_block() { return const_cast<BlockBox*>(const_cast<const Node*>(this)->containing_block()); }
  107. bool can_contain_boxes_with_position_absolute() const;
  108. const Gfx::Font& font() const;
  109. const CSS::ImmutableComputedValues& computed_values() const;
  110. NodeWithStyle* parent();
  111. const NodeWithStyle* parent() const;
  112. void inserted_into(Node&) { }
  113. void removed_from(Node&) { }
  114. void children_changed() { }
  115. virtual void split_into_lines(InlineFormattingContext&, LayoutMode);
  116. bool is_visible() const { return m_visible; }
  117. void set_visible(bool visible) { m_visible = visible; }
  118. virtual void set_needs_display();
  119. bool children_are_inline() const { return m_children_are_inline; }
  120. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  121. Gfx::FloatPoint box_type_agnostic_position() const;
  122. float font_size() const;
  123. enum class SelectionState {
  124. None, // No selection
  125. Start, // Selection starts in this Node
  126. End, // Selection ends in this Node
  127. StartAndEnd, // Selection starts and ends in this Node
  128. Full, // Selection starts before and ends after this Node
  129. };
  130. SelectionState selection_state() const { return m_selection_state; }
  131. void set_selection_state(SelectionState state) { m_selection_state = state; }
  132. template<typename Callback>
  133. void for_each_child_in_paint_order(Callback callback) const
  134. {
  135. for_each_child([&](auto& child) {
  136. if (is<Box>(child) && downcast<Box>(child).stacking_context())
  137. return;
  138. if (!child.is_positioned())
  139. callback(child);
  140. });
  141. for_each_child([&](auto& child) {
  142. if (is<Box>(child) && downcast<Box>(child).stacking_context())
  143. return;
  144. if (child.is_positioned())
  145. callback(child);
  146. });
  147. }
  148. protected:
  149. Node(DOM::Document&, DOM::Node*);
  150. private:
  151. friend class NodeWithStyle;
  152. NonnullRefPtr<DOM::Document> m_document;
  153. RefPtr<DOM::Node> m_dom_node;
  154. bool m_inline { false };
  155. bool m_has_style { false };
  156. bool m_visible { true };
  157. bool m_children_are_inline { false };
  158. SelectionState m_selection_state { SelectionState::None };
  159. };
  160. class NodeWithStyle : public Node {
  161. public:
  162. virtual ~NodeWithStyle() override { }
  163. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  164. void apply_style(const CSS::StyleProperties&);
  165. const Gfx::Font& font() const { return *m_font; }
  166. float line_height() const { return m_line_height; }
  167. float font_size() const { return m_font_size; }
  168. const CSS::ImageStyleValue* background_image() const { return m_background_image; }
  169. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  170. protected:
  171. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  172. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  173. private:
  174. CSS::ComputedValues m_computed_values;
  175. RefPtr<Gfx::Font> m_font;
  176. float m_line_height { 0 };
  177. float m_font_size { 0 };
  178. RefPtr<CSS::ImageStyleValue> m_background_image;
  179. CSS::Position m_position;
  180. };
  181. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  182. public:
  183. BoxModelMetrics& box_model() { return m_box_model; }
  184. const BoxModelMetrics& box_model() const { return m_box_model; }
  185. protected:
  186. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  187. : NodeWithStyle(document, node, move(style))
  188. {
  189. }
  190. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  191. : NodeWithStyle(document, node, move(computed_values))
  192. {
  193. }
  194. private:
  195. BoxModelMetrics m_box_model;
  196. };
  197. inline const Gfx::Font& Node::font() const
  198. {
  199. if (m_has_style)
  200. return static_cast<const NodeWithStyle*>(this)->font();
  201. return parent()->font();
  202. }
  203. inline float Node::font_size() const
  204. {
  205. if (m_has_style)
  206. return static_cast<const NodeWithStyle*>(this)->font_size();
  207. return parent()->font_size();
  208. }
  209. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  210. {
  211. if (m_has_style)
  212. return static_cast<const NodeWithStyle*>(this)->computed_values();
  213. return parent()->computed_values();
  214. }
  215. inline const NodeWithStyle* Node::parent() const
  216. {
  217. return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
  218. }
  219. inline NodeWithStyle* Node::parent()
  220. {
  221. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  222. }
  223. }