LayoutNode.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/Vector.h>
  29. #include <LibGfx/FloatRect.h>
  30. #include <LibGfx/Rect.h>
  31. #include <LibHTML/CSS/StyleProperties.h>
  32. #include <LibHTML/Layout/BoxModelMetrics.h>
  33. #include <LibHTML/Layout/LayoutPosition.h>
  34. #include <LibHTML/RenderingContext.h>
  35. #include <LibHTML/TreeNode.h>
  36. class Document;
  37. class Element;
  38. class LayoutBlock;
  39. class LayoutDocument;
  40. class LayoutNode;
  41. class LayoutNodeWithStyle;
  42. class LineBoxFragment;
  43. class Node;
  44. struct HitTestResult {
  45. RefPtr<LayoutNode> layout_node;
  46. int index_in_node { 0 };
  47. };
  48. class LayoutNode : public TreeNode<LayoutNode> {
  49. public:
  50. virtual ~LayoutNode();
  51. virtual HitTestResult hit_test(const Gfx::Point&) const;
  52. bool is_anonymous() const { return !m_node; }
  53. const Node* node() const { return m_node; }
  54. Document& document();
  55. const Document& document() const;
  56. const LayoutDocument& root() const;
  57. LayoutDocument& root();
  58. template<typename Callback>
  59. inline void for_each_child(Callback callback) const
  60. {
  61. for (auto* node = first_child(); node; node = node->next_sibling())
  62. callback(*node);
  63. }
  64. template<typename Callback>
  65. inline void for_each_child(Callback callback)
  66. {
  67. for (auto* node = first_child(); node; node = node->next_sibling())
  68. callback(*node);
  69. }
  70. virtual const char* class_name() const { return "LayoutNode"; }
  71. virtual bool is_text() const { return false; }
  72. virtual bool is_block() const { return false; }
  73. virtual bool is_replaced() const { return false; }
  74. virtual bool is_widget() const { return false; }
  75. virtual bool is_image() const { return false; }
  76. virtual bool is_box() const { return false; }
  77. virtual bool is_table() const { return false; }
  78. virtual bool is_table_row() const { return false; }
  79. virtual bool is_table_cell() const { return false; }
  80. bool has_style() const { return m_has_style; }
  81. bool is_inline() const { return m_inline; }
  82. void set_inline(bool b) { m_inline = b; }
  83. virtual void layout();
  84. virtual void render(RenderingContext&);
  85. const LayoutBlock* containing_block() const;
  86. virtual LayoutNode& inline_wrapper() { return *this; }
  87. const StyleProperties& style() const;
  88. LayoutNodeWithStyle* parent();
  89. const LayoutNodeWithStyle* parent() const;
  90. void inserted_into(LayoutNode&) {}
  91. void removed_from(LayoutNode&) {}
  92. virtual void split_into_lines(LayoutBlock& container);
  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. template<typename U>
  99. const U* next_sibling_of_type() const;
  100. template<typename U>
  101. U* next_sibling_of_type();
  102. template<typename T>
  103. const T* first_child_of_type() const;
  104. template<typename T>
  105. T* first_child_of_type();
  106. template<typename T>
  107. const T* first_ancestor_of_type() const;
  108. template<typename T>
  109. T* first_ancestor_of_type();
  110. Gfx::FloatPoint box_type_agnostic_position() const;
  111. protected:
  112. explicit LayoutNode(const Node*);
  113. private:
  114. friend class LayoutNodeWithStyle;
  115. const Node* m_node { nullptr };
  116. bool m_inline { false };
  117. bool m_has_style { false };
  118. bool m_visible { true };
  119. bool m_children_are_inline { false };
  120. };
  121. class LayoutNodeWithStyle : public LayoutNode {
  122. public:
  123. virtual ~LayoutNodeWithStyle() override {}
  124. const StyleProperties& style() const { return m_style; }
  125. void set_style(const StyleProperties& style) { m_style = style; }
  126. protected:
  127. explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
  128. : LayoutNode(node)
  129. , m_style(move(style))
  130. {
  131. m_has_style = true;
  132. }
  133. private:
  134. NonnullRefPtr<StyleProperties> m_style;
  135. };
  136. class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
  137. public:
  138. BoxModelMetrics& box_model() { return m_box_model; }
  139. const BoxModelMetrics& box_model() const { return m_box_model; }
  140. protected:
  141. LayoutNodeWithStyleAndBoxModelMetrics(const Node* node, NonnullRefPtr<StyleProperties> style)
  142. : LayoutNodeWithStyle(node, move(style))
  143. {
  144. }
  145. private:
  146. BoxModelMetrics m_box_model;
  147. };
  148. inline const StyleProperties& LayoutNode::style() const
  149. {
  150. if (m_has_style)
  151. return static_cast<const LayoutNodeWithStyle*>(this)->style();
  152. return parent()->style();
  153. }
  154. inline const LayoutNodeWithStyle* LayoutNode::parent() const
  155. {
  156. return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  157. }
  158. inline LayoutNodeWithStyle* LayoutNode::parent()
  159. {
  160. return static_cast<LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  161. }
  162. template<typename T>
  163. inline bool is(const LayoutNode&)
  164. {
  165. return false;
  166. }
  167. template<typename T>
  168. inline bool is(const LayoutNode* node)
  169. {
  170. return !node || is<T>(*node);
  171. }
  172. template<>
  173. inline bool is<LayoutNode>(const LayoutNode&)
  174. {
  175. return true;
  176. }
  177. template<>
  178. inline bool is<LayoutNodeWithStyle>(const LayoutNode& node)
  179. {
  180. return node.has_style();
  181. }
  182. template<typename T>
  183. inline const T& to(const LayoutNode& node)
  184. {
  185. ASSERT(is<T>(node));
  186. return static_cast<const T&>(node);
  187. }
  188. template<typename T>
  189. inline T* to(LayoutNode* node)
  190. {
  191. ASSERT(is<T>(node));
  192. return static_cast<T*>(node);
  193. }
  194. template<typename T>
  195. inline const T* to(const LayoutNode* node)
  196. {
  197. ASSERT(is<T>(node));
  198. return static_cast<const T*>(node);
  199. }
  200. template<typename T>
  201. inline T& to(LayoutNode& node)
  202. {
  203. ASSERT(is<T>(node));
  204. return static_cast<T&>(node);
  205. }
  206. template<typename T>
  207. inline const T* LayoutNode::next_sibling_of_type() const
  208. {
  209. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  210. if (is<T>(*sibling))
  211. return &to<T>(*sibling);
  212. }
  213. return nullptr;
  214. }
  215. template<typename T>
  216. inline T* LayoutNode::next_sibling_of_type()
  217. {
  218. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  219. if (is<T>(*sibling))
  220. return &to<T>(*sibling);
  221. }
  222. return nullptr;
  223. }
  224. template<typename T>
  225. inline const T* LayoutNode::first_child_of_type() const
  226. {
  227. for (auto* child = first_child(); child; child = child->next_sibling()) {
  228. if (is<T>(*child))
  229. return &to<T>(*child);
  230. }
  231. return nullptr;
  232. }
  233. template<typename T>
  234. inline T* LayoutNode::first_child_of_type()
  235. {
  236. for (auto* child = first_child(); child; child = child->next_sibling()) {
  237. if (is<T>(*child))
  238. return &to<T>(*child);
  239. }
  240. return nullptr;
  241. }
  242. template<typename T>
  243. inline const T* LayoutNode::first_ancestor_of_type() const
  244. {
  245. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  246. if (is<T>(*ancestor))
  247. return &to<T>(*ancestor);
  248. }
  249. return nullptr;
  250. }
  251. template<typename T>
  252. inline T* LayoutNode::first_ancestor_of_type()
  253. {
  254. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  255. if (is<T>(*ancestor))
  256. return &to<T>(*ancestor);
  257. }
  258. return nullptr;
  259. }