Node.h 8.3 KB

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