LayoutNode.h 11 KB

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