Node.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <LibJS/Heap/Handle.h>
  12. #include <LibWeb/CSS/ComputedValues.h>
  13. #include <LibWeb/CSS/StyleProperties.h>
  14. #include <LibWeb/Forward.h>
  15. #include <LibWeb/Layout/BoxModelMetrics.h>
  16. #include <LibWeb/Painting/PaintContext.h>
  17. #include <LibWeb/TreeNode.h>
  18. namespace Web::Layout {
  19. enum class LayoutMode {
  20. // Normal layout. No min-content or max-content constraints applied.
  21. Normal,
  22. // Intrinsic size determination.
  23. // Boxes honor min-content and max-content constraints (set via LayoutState::UsedValues::{width,height}_constraint)
  24. // by considering their containing block to be 0-sized or infinitely large in the relevant axis.
  25. // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizing
  26. IntrinsicSizing,
  27. };
  28. class Node : public TreeNode<Node> {
  29. public:
  30. virtual ~Node();
  31. size_t serial_id() const { return m_serial_id; }
  32. bool is_anonymous() const;
  33. DOM::Node const* dom_node() const;
  34. DOM::Node* dom_node();
  35. Painting::Paintable* paintable() { return m_paintable; }
  36. Painting::Paintable const* paintable() const { return m_paintable; }
  37. void set_paintable(RefPtr<Painting::Paintable>);
  38. virtual RefPtr<Painting::Paintable> create_paintable() const;
  39. DOM::Document& document();
  40. DOM::Document const& document() const;
  41. HTML::BrowsingContext const& browsing_context() const;
  42. HTML::BrowsingContext& browsing_context();
  43. InitialContainingBlock const& root() const;
  44. InitialContainingBlock& root();
  45. bool is_root_element() const;
  46. String class_name() const;
  47. String debug_description() const;
  48. bool has_style() const { return m_has_style; }
  49. virtual bool can_have_children() const { return true; }
  50. bool is_inline() const { return m_inline; }
  51. void set_inline(bool b) { m_inline = b; }
  52. bool is_inline_block() const;
  53. bool is_out_of_flow(FormattingContext const&) const;
  54. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  55. virtual bool is_box() const { return false; }
  56. virtual bool is_block_container() const { return false; }
  57. virtual bool is_break_node() const { return false; }
  58. virtual bool is_text_node() const { return false; }
  59. virtual bool is_initial_containing_block_box() const { return false; }
  60. virtual bool is_svg_box() const { return false; }
  61. virtual bool is_svg_geometry_box() const { return false; }
  62. virtual bool is_label() const { return false; }
  63. virtual bool is_replaced_box() const { return false; }
  64. virtual bool is_list_item_marker_box() const { return false; }
  65. template<typename T>
  66. bool fast_is() const = delete;
  67. bool is_floating() const;
  68. bool is_positioned() const;
  69. bool is_absolutely_positioned() const;
  70. bool is_fixed_position() const;
  71. bool is_flex_item() const { return m_is_flex_item; }
  72. void set_flex_item(bool b) { m_is_flex_item = b; }
  73. BlockContainer const* containing_block() const;
  74. BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<Node const*>(this)->containing_block()); }
  75. bool establishes_stacking_context() const;
  76. bool can_contain_boxes_with_position_absolute() const;
  77. Gfx::Font const& font() const;
  78. const CSS::ImmutableComputedValues& computed_values() const;
  79. float line_height() const;
  80. NodeWithStyle* parent();
  81. NodeWithStyle const* parent() const;
  82. void inserted_into(Node&) { }
  83. void removed_from(Node&) { }
  84. void children_changed() { }
  85. bool is_visible() const { return m_visible; }
  86. void set_visible(bool visible) { m_visible = visible; }
  87. virtual void set_needs_display();
  88. bool children_are_inline() const { return m_children_are_inline; }
  89. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  90. Gfx::FloatPoint box_type_agnostic_position() const;
  91. enum class SelectionState {
  92. None, // No selection
  93. Start, // Selection starts in this Node
  94. End, // Selection ends in this Node
  95. StartAndEnd, // Selection starts and ends in this Node
  96. Full, // Selection starts before and ends after this Node
  97. };
  98. SelectionState selection_state() const { return m_selection_state; }
  99. void set_selection_state(SelectionState state) { m_selection_state = state; }
  100. protected:
  101. Node(DOM::Document&, DOM::Node*);
  102. private:
  103. friend class NodeWithStyle;
  104. JS::Handle<DOM::Document> m_document;
  105. JS::Handle<DOM::Node> m_dom_node;
  106. RefPtr<Painting::Paintable> m_paintable;
  107. size_t m_serial_id { 0 };
  108. bool m_inline { false };
  109. bool m_has_style { false };
  110. bool m_visible { true };
  111. bool m_children_are_inline { false };
  112. SelectionState m_selection_state { SelectionState::None };
  113. bool m_is_flex_item { false };
  114. };
  115. class NodeWithStyle : public Node {
  116. public:
  117. virtual ~NodeWithStyle() override = default;
  118. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  119. void apply_style(const CSS::StyleProperties&);
  120. Gfx::Font const& font() const { return *m_font; }
  121. float line_height() const { return m_line_height; }
  122. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  123. const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; }
  124. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  125. void did_insert_into_layout_tree(CSS::StyleProperties const&);
  126. protected:
  127. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  128. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  129. private:
  130. CSS::ComputedValues m_computed_values;
  131. RefPtr<Gfx::Font> m_font;
  132. float m_line_height { 0 };
  133. RefPtr<CSS::AbstractImageStyleValue> m_list_style_image;
  134. };
  135. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  136. public:
  137. BoxModelMetrics& box_model() { return m_box_model; }
  138. BoxModelMetrics const& box_model() const { return m_box_model; }
  139. protected:
  140. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  141. : NodeWithStyle(document, node, move(style))
  142. {
  143. }
  144. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  145. : NodeWithStyle(document, node, move(computed_values))
  146. {
  147. }
  148. private:
  149. BoxModelMetrics m_box_model;
  150. };
  151. inline Gfx::Font const& Node::font() const
  152. {
  153. if (m_has_style)
  154. return static_cast<NodeWithStyle const*>(this)->font();
  155. return parent()->font();
  156. }
  157. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  158. {
  159. if (m_has_style)
  160. return static_cast<NodeWithStyle const*>(this)->computed_values();
  161. return parent()->computed_values();
  162. }
  163. inline float Node::line_height() const
  164. {
  165. if (m_has_style)
  166. return static_cast<NodeWithStyle const*>(this)->line_height();
  167. return parent()->line_height();
  168. }
  169. inline NodeWithStyle const* Node::parent() const
  170. {
  171. return static_cast<NodeWithStyle const*>(TreeNode<Node>::parent());
  172. }
  173. inline NodeWithStyle* Node::parent()
  174. {
  175. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  176. }
  177. }