Node.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2018-2020, 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/StyleProperties.h>
  32. #include <LibWeb/Forward.h>
  33. #include <LibWeb/Layout/BoxModelMetrics.h>
  34. #include <LibWeb/Layout/LayoutPosition.h>
  35. #include <LibWeb/Layout/LayoutStyle.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. struct HitTestResult {
  45. RefPtr<Node> layout_node;
  46. int index_in_node { 0 };
  47. enum InternalPosition {
  48. None,
  49. Before,
  50. Inside,
  51. After,
  52. };
  53. InternalPosition internal_position { None };
  54. };
  55. enum class HitTestType {
  56. Exact, // Exact matches only
  57. TextCursor, // Clicking past the right/bottom edge of text will still hit the text
  58. };
  59. class Node : public TreeNode<Node> {
  60. public:
  61. virtual ~Node();
  62. virtual HitTestResult hit_test(const Gfx::IntPoint&, HitTestType) const;
  63. bool is_anonymous() const { return !m_dom_node; }
  64. const DOM::Node* dom_node() const { return m_dom_node; }
  65. DOM::Node* dom_node() { return m_dom_node; }
  66. DOM::Document& document() { return m_document; }
  67. const DOM::Document& document() const { return m_document; }
  68. const Frame& frame() const;
  69. Frame& frame();
  70. const InitialContainingBlockBox& root() const;
  71. InitialContainingBlockBox& root();
  72. virtual const char* class_name() const = 0;
  73. virtual bool is_root() const { return false; }
  74. virtual bool is_text() const { return false; }
  75. virtual bool is_block() const { return false; }
  76. virtual bool is_replaced() const { return false; }
  77. virtual bool is_widget() const { return false; }
  78. virtual bool is_frame() const { return false; }
  79. virtual bool is_image() const { return false; }
  80. virtual bool is_canvas() const { return false; }
  81. virtual bool is_box() const { return false; }
  82. virtual bool is_table() const { return false; }
  83. virtual bool is_table_row() const { return false; }
  84. virtual bool is_table_cell() const { return false; }
  85. virtual bool is_table_row_group() const { return false; }
  86. virtual bool is_break() const { return false; }
  87. virtual bool is_check_box() const { return false; }
  88. virtual bool is_button() const { return false; }
  89. virtual bool is_list_item() const { return false; }
  90. bool has_style() const { return m_has_style; }
  91. bool is_inline() const { return m_inline; }
  92. void set_inline(bool b) { m_inline = b; }
  93. bool is_inline_block() const { return is_inline() && is_block(); }
  94. virtual bool wants_mouse_events() const { return false; }
  95. virtual void handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  96. virtual void handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned modifiers);
  97. virtual void handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint&, unsigned buttons, unsigned modifiers);
  98. enum class PaintPhase {
  99. Background,
  100. Border,
  101. Foreground,
  102. FocusOutline,
  103. Overlay,
  104. };
  105. virtual void before_children_paint(PaintContext&, PaintPhase) {};
  106. virtual void paint(PaintContext&, PaintPhase);
  107. virtual void after_children_paint(PaintContext&, PaintPhase) {};
  108. bool is_floating() const;
  109. bool is_absolutely_positioned() const;
  110. bool is_fixed_position() const;
  111. const BlockBox* containing_block() const;
  112. bool can_contain_boxes_with_position_absolute() const;
  113. const CSS::StyleProperties& specified_style() const;
  114. const ImmutableLayoutStyle& style() const;
  115. NodeWithStyle* parent();
  116. const NodeWithStyle* parent() const;
  117. void inserted_into(Node&) { }
  118. void removed_from(Node&) { }
  119. void children_changed() { }
  120. virtual void split_into_lines(BlockBox& container, LayoutMode);
  121. bool is_visible() const { return m_visible; }
  122. void set_visible(bool visible) { m_visible = visible; }
  123. virtual void set_needs_display();
  124. bool children_are_inline() const { return m_children_are_inline; }
  125. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  126. Gfx::FloatPoint box_type_agnostic_position() const;
  127. float font_size() const;
  128. enum class SelectionState {
  129. None, // No selection
  130. Start, // Selection starts in this Node
  131. End, // Selection ends in this Node
  132. StartAndEnd, // Selection starts and ends in this Node
  133. Full, // Selection starts before and ends after this Node
  134. };
  135. SelectionState selection_state() const { return m_selection_state; }
  136. void set_selection_state(SelectionState state) { m_selection_state = state; }
  137. protected:
  138. Node(DOM::Document&, DOM::Node*);
  139. private:
  140. friend class NodeWithStyle;
  141. NonnullRefPtr<DOM::Document> m_document;
  142. RefPtr<DOM::Node> m_dom_node;
  143. bool m_inline { false };
  144. bool m_has_style { false };
  145. bool m_visible { true };
  146. bool m_children_are_inline { false };
  147. SelectionState m_selection_state { SelectionState::None };
  148. };
  149. class NodeWithStyle : public Node {
  150. public:
  151. virtual ~NodeWithStyle() override { }
  152. const CSS::StyleProperties& specified_style() const { return m_specified_style; }
  153. void set_specified_style(const CSS::StyleProperties& style) { m_specified_style = style; }
  154. const ImmutableLayoutStyle& style() const { return static_cast<const ImmutableLayoutStyle&>(m_style); }
  155. void apply_style(const CSS::StyleProperties&);
  156. protected:
  157. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  158. private:
  159. LayoutStyle m_style;
  160. NonnullRefPtr<CSS::StyleProperties> m_specified_style;
  161. CSS::Position m_position;
  162. };
  163. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  164. public:
  165. BoxModelMetrics& box_model() { return m_box_model; }
  166. const BoxModelMetrics& box_model() const { return m_box_model; }
  167. protected:
  168. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  169. : NodeWithStyle(document, node, move(style))
  170. {
  171. }
  172. private:
  173. BoxModelMetrics m_box_model;
  174. };
  175. inline const CSS::StyleProperties& Node::specified_style() const
  176. {
  177. if (m_has_style)
  178. return static_cast<const NodeWithStyle*>(this)->specified_style();
  179. return parent()->specified_style();
  180. }
  181. inline const ImmutableLayoutStyle& Node::style() const
  182. {
  183. if (m_has_style)
  184. return static_cast<const NodeWithStyle*>(this)->style();
  185. return parent()->style();
  186. }
  187. inline const NodeWithStyle* Node::parent() const
  188. {
  189. return static_cast<const NodeWithStyle*>(TreeNode<Node>::parent());
  190. }
  191. inline NodeWithStyle* Node::parent()
  192. {
  193. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  194. }
  195. }
  196. AK_BEGIN_TYPE_TRAITS(Web::Layout::NodeWithStyle)
  197. static bool is_type(const Web::Layout::Node& node) { return node.has_style(); }
  198. AK_END_TYPE_TRAITS()