LayoutNode.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 <LibWeb/CSS/StyleProperties.h>
  32. #include <LibWeb/Forward.h>
  33. #include <LibWeb/Layout/BoxModelMetrics.h>
  34. #include <LibWeb/Layout/LayoutPosition.h>
  35. #include <LibWeb/RenderingContext.h>
  36. #include <LibWeb/TreeNode.h>
  37. namespace Web {
  38. template<typename T>
  39. inline bool is(const LayoutNode&)
  40. {
  41. return false;
  42. }
  43. template<typename T>
  44. inline bool is(const LayoutNode* node)
  45. {
  46. return !node || is<T>(*node);
  47. }
  48. template<>
  49. inline bool is<LayoutNode>(const LayoutNode&)
  50. {
  51. return true;
  52. }
  53. template<typename T>
  54. inline const T& to(const LayoutNode& node)
  55. {
  56. ASSERT(is<T>(node));
  57. return static_cast<const T&>(node);
  58. }
  59. template<typename T>
  60. inline T* to(LayoutNode* node)
  61. {
  62. ASSERT(is<T>(node));
  63. return static_cast<T*>(node);
  64. }
  65. template<typename T>
  66. inline const T* to(const LayoutNode* node)
  67. {
  68. ASSERT(is<T>(node));
  69. return static_cast<const T*>(node);
  70. }
  71. template<typename T>
  72. inline T& to(LayoutNode& node)
  73. {
  74. ASSERT(is<T>(node));
  75. return static_cast<T&>(node);
  76. }
  77. struct HitTestResult {
  78. RefPtr<LayoutNode> layout_node;
  79. int index_in_node { 0 };
  80. };
  81. class LayoutNode : public TreeNode<LayoutNode> {
  82. public:
  83. virtual ~LayoutNode();
  84. virtual HitTestResult hit_test(const Gfx::IntPoint&) const;
  85. bool is_anonymous() const { return !m_node; }
  86. const Node* node() const { return m_node; }
  87. Node* node() { return const_cast<Node*>(m_node); }
  88. Document& document();
  89. const Document& document() const;
  90. const LayoutDocument& root() const;
  91. LayoutDocument& root();
  92. template<typename Callback>
  93. inline void for_each_child(Callback callback) const
  94. {
  95. for (auto* node = first_child(); node; node = node->next_sibling())
  96. callback(*node);
  97. }
  98. template<typename Callback>
  99. inline void for_each_child(Callback callback)
  100. {
  101. for (auto* node = first_child(); node; node = node->next_sibling())
  102. callback(*node);
  103. }
  104. template<typename T, typename Callback>
  105. inline void for_each_child_of_type(Callback callback)
  106. {
  107. for (auto* node = first_child(); node; node = node->next_sibling()) {
  108. if (!is<T>(node))
  109. continue;
  110. callback(to<T>(*node));
  111. }
  112. }
  113. template<typename T, typename Callback>
  114. inline void for_each_child_of_type(Callback callback) const
  115. {
  116. for (auto* node = first_child(); node; node = node->next_sibling()) {
  117. if (!is<T>(node))
  118. continue;
  119. callback(to<T>(*node));
  120. }
  121. }
  122. virtual const char* class_name() const = 0;
  123. virtual bool is_root() const { return false; }
  124. virtual bool is_text() const { return false; }
  125. virtual bool is_block() const { return false; }
  126. virtual bool is_replaced() const { return false; }
  127. virtual bool is_widget() const { return false; }
  128. virtual bool is_frame() const { return false; }
  129. virtual bool is_image() const { return false; }
  130. virtual bool is_canvas() const { return false; }
  131. virtual bool is_box() const { return false; }
  132. virtual bool is_table() const { return false; }
  133. virtual bool is_table_row() const { return false; }
  134. virtual bool is_table_cell() const { return false; }
  135. virtual bool is_table_row_group() const { return false; }
  136. bool has_style() const { return m_has_style; }
  137. bool is_inline() const { return m_inline; }
  138. void set_inline(bool b) { m_inline = b; }
  139. bool is_inline_block() const { return is_inline() && is_block(); }
  140. enum class LayoutMode {
  141. Default,
  142. AllPossibleLineBreaks,
  143. OnlyRequiredLineBreaks,
  144. };
  145. virtual void layout(LayoutMode);
  146. virtual void render(RenderingContext&);
  147. bool is_absolutely_positioned() const;
  148. const LayoutBlock* containing_block() const;
  149. bool can_contain_boxes_with_position_absolute() const;
  150. virtual LayoutNode& inline_wrapper() { return *this; }
  151. const StyleProperties& style() const;
  152. LayoutNodeWithStyle* parent();
  153. const LayoutNodeWithStyle* parent() const;
  154. void inserted_into(LayoutNode&) { }
  155. void removed_from(LayoutNode&) { }
  156. void children_changed() { }
  157. virtual void split_into_lines(LayoutBlock& container, LayoutMode);
  158. bool is_visible() const { return m_visible; }
  159. void set_visible(bool visible) { m_visible = visible; }
  160. virtual void set_needs_display();
  161. bool children_are_inline() const { return m_children_are_inline; }
  162. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  163. template<typename U>
  164. const U* next_sibling_of_type() const;
  165. template<typename U>
  166. U* next_sibling_of_type();
  167. template<typename U>
  168. const U* previous_sibling_of_type() const;
  169. template<typename U>
  170. U* previous_sibling_of_type();
  171. template<typename T>
  172. const T* first_child_of_type() const;
  173. template<typename T>
  174. T* first_child_of_type();
  175. template<typename T>
  176. const T* first_ancestor_of_type() const;
  177. template<typename T>
  178. T* first_ancestor_of_type();
  179. Gfx::FloatPoint box_type_agnostic_position() const;
  180. float font_size() const;
  181. protected:
  182. explicit LayoutNode(const Node*);
  183. private:
  184. friend class LayoutNodeWithStyle;
  185. const Node* m_node { nullptr };
  186. bool m_inline { false };
  187. bool m_has_style { false };
  188. bool m_visible { true };
  189. bool m_children_are_inline { false };
  190. };
  191. class LayoutNodeWithStyle : public LayoutNode {
  192. public:
  193. virtual ~LayoutNodeWithStyle() override { }
  194. const StyleProperties& style() const { return m_style; }
  195. void set_style(const StyleProperties& style) { m_style = style; }
  196. protected:
  197. explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr<StyleProperties> style)
  198. : LayoutNode(node)
  199. , m_style(move(style))
  200. {
  201. m_has_style = true;
  202. }
  203. private:
  204. NonnullRefPtr<StyleProperties> m_style;
  205. };
  206. class LayoutNodeWithStyleAndBoxModelMetrics : public LayoutNodeWithStyle {
  207. public:
  208. BoxModelMetrics& box_model() { return m_box_model; }
  209. const BoxModelMetrics& box_model() const { return m_box_model; }
  210. protected:
  211. LayoutNodeWithStyleAndBoxModelMetrics(const Node* node, NonnullRefPtr<StyleProperties> style)
  212. : LayoutNodeWithStyle(node, move(style))
  213. {
  214. }
  215. private:
  216. BoxModelMetrics m_box_model;
  217. };
  218. inline const StyleProperties& LayoutNode::style() const
  219. {
  220. if (m_has_style)
  221. return static_cast<const LayoutNodeWithStyle*>(this)->style();
  222. return parent()->style();
  223. }
  224. inline const LayoutNodeWithStyle* LayoutNode::parent() const
  225. {
  226. return static_cast<const LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  227. }
  228. inline LayoutNodeWithStyle* LayoutNode::parent()
  229. {
  230. return static_cast<LayoutNodeWithStyle*>(TreeNode<LayoutNode>::parent());
  231. }
  232. template<typename T>
  233. inline const T* LayoutNode::next_sibling_of_type() const
  234. {
  235. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  236. if (is<T>(*sibling))
  237. return &to<T>(*sibling);
  238. }
  239. return nullptr;
  240. }
  241. template<typename T>
  242. inline T* LayoutNode::next_sibling_of_type()
  243. {
  244. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  245. if (is<T>(*sibling))
  246. return &to<T>(*sibling);
  247. }
  248. return nullptr;
  249. }
  250. template<typename T>
  251. inline const T* LayoutNode::previous_sibling_of_type() const
  252. {
  253. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  254. if (is<T>(*sibling))
  255. return &to<T>(*sibling);
  256. }
  257. return nullptr;
  258. }
  259. template<typename T>
  260. inline T* LayoutNode::previous_sibling_of_type()
  261. {
  262. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  263. if (is<T>(*sibling))
  264. return &to<T>(*sibling);
  265. }
  266. return nullptr;
  267. }
  268. template<typename T>
  269. inline const T* LayoutNode::first_child_of_type() const
  270. {
  271. for (auto* child = first_child(); child; child = child->next_sibling()) {
  272. if (is<T>(*child))
  273. return &to<T>(*child);
  274. }
  275. return nullptr;
  276. }
  277. template<typename T>
  278. inline T* LayoutNode::first_child_of_type()
  279. {
  280. for (auto* child = first_child(); child; child = child->next_sibling()) {
  281. if (is<T>(*child))
  282. return &to<T>(*child);
  283. }
  284. return nullptr;
  285. }
  286. template<typename T>
  287. inline const T* LayoutNode::first_ancestor_of_type() const
  288. {
  289. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  290. if (is<T>(*ancestor))
  291. return &to<T>(*ancestor);
  292. }
  293. return nullptr;
  294. }
  295. template<typename T>
  296. inline T* LayoutNode::first_ancestor_of_type()
  297. {
  298. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  299. if (is<T>(*ancestor))
  300. return &to<T>(*ancestor);
  301. }
  302. return nullptr;
  303. }
  304. template<>
  305. inline bool is<LayoutNodeWithStyle>(const LayoutNode& node)
  306. {
  307. return node.has_style();
  308. }
  309. }