Node.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtr.h>
  8. #include <AK/TypeCasts.h>
  9. #include <AK/Vector.h>
  10. #include <LibGfx/Rect.h>
  11. #include <LibWeb/CSS/ComputedValues.h>
  12. #include <LibWeb/CSS/StyleProperties.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/Layout/BoxModelMetrics.h>
  15. #include <LibWeb/Layout/LayoutPosition.h>
  16. #include <LibWeb/Painting/PaintContext.h>
  17. #include <LibWeb/TreeNode.h>
  18. namespace Web::Layout {
  19. enum class LayoutMode {
  20. // Normal layout.
  21. // - We use the containing block's used width.
  22. // - Content flows into the available space, line breaks inserted where necessary.
  23. Normal,
  24. // MinContent layout is used for discovering the min-content intrinsic size of a box.
  25. // - We act as if the containing block has 0 used width.
  26. // - Every line-breaking opportunity is taken.
  27. MinContent,
  28. // MaxContent layout is used for discovering the max-content intrinsic size of a box.
  29. // - We act as if the containing block has infinite used width.
  30. // - Only forced line-breaking opportunities are taken.
  31. MaxContent,
  32. };
  33. class Node : public TreeNode<Node> {
  34. public:
  35. virtual ~Node();
  36. bool is_anonymous() const { return !m_dom_node; }
  37. const DOM::Node* dom_node() const { return m_dom_node; }
  38. DOM::Node* dom_node() { return m_dom_node; }
  39. Painting::Paintable* paintable() { return m_paintable; }
  40. Painting::Paintable const* paintable() const { return m_paintable; }
  41. void set_paintable(RefPtr<Painting::Paintable>);
  42. virtual RefPtr<Painting::Paintable> create_paintable() const;
  43. DOM::Document& document() { return m_document; }
  44. const DOM::Document& document() const { return m_document; }
  45. HTML::BrowsingContext const& browsing_context() const;
  46. HTML::BrowsingContext& browsing_context();
  47. InitialContainingBlock const& root() const;
  48. InitialContainingBlock& root();
  49. bool is_root_element() const;
  50. String class_name() const;
  51. String debug_description() const;
  52. bool has_style() const { return m_has_style; }
  53. virtual bool can_have_children() const { return true; }
  54. bool is_inline() const { return m_inline; }
  55. void set_inline(bool b) { m_inline = b; }
  56. bool is_inline_block() const;
  57. bool is_out_of_flow(FormattingContext const&) const;
  58. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  59. virtual bool is_box() const { return false; }
  60. virtual bool is_block_container() const { return false; }
  61. virtual bool is_break_node() const { return false; }
  62. virtual bool is_text_node() const { return false; }
  63. virtual bool is_initial_containing_block_box() const { return false; }
  64. virtual bool is_svg_box() const { return false; }
  65. virtual bool is_svg_geometry_box() const { return false; }
  66. virtual bool is_label() const { return false; }
  67. virtual bool is_replaced_box() const { return false; }
  68. virtual bool is_list_item_marker_box() const { return false; }
  69. template<typename T>
  70. bool fast_is() const = delete;
  71. bool is_floating() const;
  72. bool is_positioned() const;
  73. bool is_absolutely_positioned() const;
  74. bool is_fixed_position() const;
  75. bool is_flex_item() const { return m_is_flex_item; }
  76. void set_flex_item(bool b) { m_is_flex_item = b; }
  77. BlockContainer const* containing_block() const;
  78. BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<Node const*>(this)->containing_block()); }
  79. bool establishes_stacking_context() const;
  80. bool can_contain_boxes_with_position_absolute() const;
  81. Gfx::Font const& font() const;
  82. const CSS::ImmutableComputedValues& computed_values() const;
  83. float line_height() const;
  84. NodeWithStyle* parent();
  85. NodeWithStyle const* parent() const;
  86. void inserted_into(Node&) { }
  87. void removed_from(Node&) { }
  88. void children_changed() { }
  89. bool is_visible() const { return m_visible; }
  90. void set_visible(bool visible) { m_visible = visible; }
  91. virtual void set_needs_display();
  92. bool children_are_inline() const { return m_children_are_inline; }
  93. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  94. Gfx::FloatPoint box_type_agnostic_position() const;
  95. enum class SelectionState {
  96. None, // No selection
  97. Start, // Selection starts in this Node
  98. End, // Selection ends in this Node
  99. StartAndEnd, // Selection starts and ends in this Node
  100. Full, // Selection starts before and ends after this Node
  101. };
  102. SelectionState selection_state() const { return m_selection_state; }
  103. void set_selection_state(SelectionState state) { m_selection_state = state; }
  104. protected:
  105. Node(DOM::Document&, DOM::Node*);
  106. private:
  107. friend class NodeWithStyle;
  108. NonnullRefPtr<DOM::Document> m_document;
  109. RefPtr<DOM::Node> m_dom_node;
  110. RefPtr<Painting::Paintable> m_paintable;
  111. bool m_inline { false };
  112. bool m_has_style { false };
  113. bool m_visible { true };
  114. bool m_children_are_inline { false };
  115. SelectionState m_selection_state { SelectionState::None };
  116. bool m_is_flex_item { false };
  117. };
  118. class NodeWithStyle : public Node {
  119. public:
  120. virtual ~NodeWithStyle() override = default;
  121. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  122. void apply_style(const CSS::StyleProperties&);
  123. Gfx::Font const& font() const { return *m_font; }
  124. float line_height() const { return m_line_height; }
  125. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  126. const CSS::ImageStyleValue* list_style_image() const { return m_list_style_image; }
  127. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  128. bool has_definite_height() const { return m_has_definite_height; }
  129. bool has_definite_width() const { return m_has_definite_width; }
  130. void set_has_definite_height(bool b) { m_has_definite_height = b; }
  131. void set_has_definite_width(bool b) { m_has_definite_width = b; }
  132. void did_insert_into_layout_tree(CSS::StyleProperties const&);
  133. protected:
  134. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  135. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  136. private:
  137. CSS::ComputedValues m_computed_values;
  138. RefPtr<Gfx::Font> m_font;
  139. float m_line_height { 0 };
  140. RefPtr<CSS::ImageStyleValue> m_list_style_image;
  141. bool m_has_definite_height { false };
  142. bool m_has_definite_width { false };
  143. };
  144. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  145. public:
  146. BoxModelMetrics& box_model() { return m_box_model; }
  147. BoxModelMetrics const& box_model() const { return m_box_model; }
  148. protected:
  149. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  150. : NodeWithStyle(document, node, move(style))
  151. {
  152. }
  153. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  154. : NodeWithStyle(document, node, move(computed_values))
  155. {
  156. }
  157. private:
  158. BoxModelMetrics m_box_model;
  159. };
  160. inline Gfx::Font const& Node::font() const
  161. {
  162. if (m_has_style)
  163. return static_cast<NodeWithStyle const*>(this)->font();
  164. return parent()->font();
  165. }
  166. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  167. {
  168. if (m_has_style)
  169. return static_cast<NodeWithStyle const*>(this)->computed_values();
  170. return parent()->computed_values();
  171. }
  172. inline float Node::line_height() const
  173. {
  174. if (m_has_style)
  175. return static_cast<NodeWithStyle const*>(this)->line_height();
  176. return parent()->line_height();
  177. }
  178. inline NodeWithStyle const* Node::parent() const
  179. {
  180. return static_cast<NodeWithStyle const*>(TreeNode<Node>::parent());
  181. }
  182. inline NodeWithStyle* Node::parent()
  183. {
  184. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  185. }
  186. }