Node.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. bool is_generated() const { return m_generated; }
  36. void set_generated(bool b) { m_generated = b; }
  37. Painting::Paintable* paintable() { return m_paintable; }
  38. Painting::Paintable const* paintable() const { return m_paintable; }
  39. void set_paintable(RefPtr<Painting::Paintable>);
  40. virtual RefPtr<Painting::Paintable> create_paintable() const;
  41. DOM::Document& document();
  42. DOM::Document const& document() const;
  43. HTML::BrowsingContext const& browsing_context() const;
  44. HTML::BrowsingContext& browsing_context();
  45. InitialContainingBlock const& root() const;
  46. InitialContainingBlock& root();
  47. bool is_root_element() const;
  48. String class_name() const;
  49. String debug_description() const;
  50. bool has_style() const { return m_has_style; }
  51. virtual bool can_have_children() const { return true; }
  52. CSS::Display display() const;
  53. bool is_inline() const;
  54. bool is_inline_block() const;
  55. bool is_out_of_flow(FormattingContext const&) const;
  56. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  57. virtual bool is_box() const { return false; }
  58. virtual bool is_block_container() const { return false; }
  59. virtual bool is_break_node() const { return false; }
  60. virtual bool is_text_node() const { return false; }
  61. virtual bool is_initial_containing_block_box() const { return false; }
  62. virtual bool is_svg_box() const { return false; }
  63. virtual bool is_svg_geometry_box() const { return false; }
  64. virtual bool is_label() const { return false; }
  65. virtual bool is_replaced_box() const { return false; }
  66. virtual bool is_list_item_marker_box() const { return false; }
  67. template<typename T>
  68. bool fast_is() const = delete;
  69. bool is_floating() const;
  70. bool is_positioned() const;
  71. bool is_absolutely_positioned() const;
  72. bool is_fixed_position() const;
  73. bool is_flex_item() const { return m_is_flex_item; }
  74. void set_flex_item(bool b) { m_is_flex_item = b; }
  75. BlockContainer const* containing_block() const;
  76. BlockContainer* containing_block() { return const_cast<BlockContainer*>(const_cast<Node const*>(this)->containing_block()); }
  77. bool establishes_stacking_context() const;
  78. bool can_contain_boxes_with_position_absolute() const;
  79. Gfx::Font const& font() const;
  80. const CSS::ImmutableComputedValues& computed_values() const;
  81. float line_height() const;
  82. NodeWithStyle* parent();
  83. NodeWithStyle const* parent() const;
  84. void inserted_into(Node&) { }
  85. void removed_from(Node&) { }
  86. void children_changed() { }
  87. bool is_visible() const { return m_visible; }
  88. void set_visible(bool visible) { m_visible = visible; }
  89. virtual void set_needs_display();
  90. bool children_are_inline() const { return m_children_are_inline; }
  91. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  92. Gfx::FloatPoint box_type_agnostic_position() const;
  93. enum class SelectionState {
  94. None, // No selection
  95. Start, // Selection starts in this Node
  96. End, // Selection ends in this Node
  97. StartAndEnd, // Selection starts and ends in this Node
  98. Full, // Selection starts before and ends after this Node
  99. };
  100. SelectionState selection_state() const { return m_selection_state; }
  101. void set_selection_state(SelectionState state) { m_selection_state = state; }
  102. protected:
  103. Node(DOM::Document&, DOM::Node*);
  104. private:
  105. friend class NodeWithStyle;
  106. JS::Handle<DOM::Document> m_document;
  107. JS::Handle<DOM::Node> m_dom_node;
  108. RefPtr<Painting::Paintable> m_paintable;
  109. size_t m_serial_id { 0 };
  110. bool m_has_style { false };
  111. bool m_visible { true };
  112. bool m_children_are_inline { false };
  113. SelectionState m_selection_state { SelectionState::None };
  114. bool m_is_flex_item { false };
  115. bool m_generated { false };
  116. };
  117. class NodeWithStyle : public Node {
  118. public:
  119. virtual ~NodeWithStyle() override = default;
  120. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  121. void apply_style(const CSS::StyleProperties&);
  122. Gfx::Font const& font() const { return *m_font; }
  123. float line_height() const { return m_line_height; }
  124. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  125. const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; }
  126. NonnullRefPtr<NodeWithStyle> create_anonymous_wrapper() const;
  127. protected:
  128. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  129. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  130. private:
  131. CSS::ComputedValues m_computed_values;
  132. RefPtr<Gfx::Font> m_font;
  133. float m_line_height { 0 };
  134. RefPtr<CSS::AbstractImageStyleValue> m_list_style_image;
  135. };
  136. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  137. public:
  138. BoxModelMetrics& box_model() { return m_box_model; }
  139. BoxModelMetrics const& box_model() const { return m_box_model; }
  140. protected:
  141. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  142. : NodeWithStyle(document, node, move(style))
  143. {
  144. }
  145. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  146. : NodeWithStyle(document, node, move(computed_values))
  147. {
  148. }
  149. private:
  150. BoxModelMetrics m_box_model;
  151. };
  152. inline Gfx::Font const& Node::font() const
  153. {
  154. if (m_has_style)
  155. return static_cast<NodeWithStyle const*>(this)->font();
  156. return parent()->font();
  157. }
  158. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  159. {
  160. if (m_has_style)
  161. return static_cast<NodeWithStyle const*>(this)->computed_values();
  162. return parent()->computed_values();
  163. }
  164. inline float Node::line_height() const
  165. {
  166. if (m_has_style)
  167. return static_cast<NodeWithStyle const*>(this)->line_height();
  168. return parent()->line_height();
  169. }
  170. inline NodeWithStyle const* Node::parent() const
  171. {
  172. return static_cast<NodeWithStyle const*>(TreeNode<Node>::parent());
  173. }
  174. inline NodeWithStyle* Node::parent()
  175. {
  176. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  177. }
  178. }