Node.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/StyleComputer.h>
  15. #include <LibWeb/CSS/StyleProperties.h>
  16. #include <LibWeb/DOM/Document.h>
  17. #include <LibWeb/FontCache.h>
  18. #include <LibWeb/Forward.h>
  19. #include <LibWeb/Layout/BoxModelMetrics.h>
  20. #include <LibWeb/Painting/PaintContext.h>
  21. #include <LibWeb/TreeNode.h>
  22. namespace Web::Layout {
  23. enum class LayoutMode {
  24. // Normal layout. No min-content or max-content constraints applied.
  25. Normal,
  26. // Intrinsic size determination.
  27. // Boxes honor min-content and max-content constraints (set via LayoutState::UsedValues::{width,height}_constraint)
  28. // by considering their containing block to be 0-sized or infinitely large in the relevant axis.
  29. // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizing
  30. IntrinsicSizing,
  31. };
  32. class Node
  33. : public JS::Cell
  34. , public TreeNode<Node>
  35. , public Weakable<Node> {
  36. JS_CELL(Node, JS::Cell);
  37. public:
  38. virtual ~Node();
  39. bool is_anonymous() const;
  40. DOM::Node const* dom_node() const;
  41. DOM::Node* dom_node();
  42. DOM::Element const* pseudo_element_generator() const;
  43. DOM::Element* pseudo_element_generator();
  44. enum class GeneratedFor {
  45. NotGenerated,
  46. PseudoBefore,
  47. PseudoAfter
  48. };
  49. bool is_generated() const { return m_generated_for != GeneratedFor::NotGenerated; }
  50. bool is_generated_for_before_pseudo_element() const { return m_generated_for == GeneratedFor::PseudoBefore; }
  51. bool is_generated_for_after_pseudo_element() const { return m_generated_for == GeneratedFor::PseudoAfter; }
  52. void set_generated_for(GeneratedFor type, DOM::Element& element)
  53. {
  54. m_generated_for = type;
  55. m_pseudo_element_generator = &element;
  56. }
  57. Painting::Paintable* paintable() { return m_paintable; }
  58. Painting::Paintable const* paintable() const { return m_paintable; }
  59. void set_paintable(JS::GCPtr<Painting::Paintable>);
  60. virtual JS::GCPtr<Painting::Paintable> create_paintable() const;
  61. DOM::Document& document();
  62. DOM::Document const& document() const;
  63. HTML::BrowsingContext const& browsing_context() const;
  64. HTML::BrowsingContext& browsing_context();
  65. JS::GCPtr<HTML::Navigable> navigable() const;
  66. Viewport const& root() const;
  67. Viewport& root();
  68. bool is_root_element() const;
  69. DeprecatedString debug_description() const;
  70. bool has_style() const { return m_has_style; }
  71. bool has_style_or_parent_with_style() const;
  72. virtual bool can_have_children() const { return true; }
  73. CSS::Display display() const;
  74. bool is_inline() const;
  75. bool is_inline_block() const;
  76. bool is_inline_table() const;
  77. bool is_out_of_flow(FormattingContext const&) const;
  78. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  79. virtual bool is_box() const { return false; }
  80. virtual bool is_block_container() const { return false; }
  81. virtual bool is_break_node() const { return false; }
  82. virtual bool is_text_node() const { return false; }
  83. virtual bool is_viewport() const { return false; }
  84. virtual bool is_svg_box() const { return false; }
  85. virtual bool is_svg_geometry_box() const { return false; }
  86. virtual bool is_svg_svg_box() const { return false; }
  87. virtual bool is_label() const { return false; }
  88. virtual bool is_replaced_box() const { return false; }
  89. virtual bool is_list_item_box() const { return false; }
  90. virtual bool is_list_item_marker_box() const { return false; }
  91. virtual bool is_table_wrapper() const { return false; }
  92. virtual bool is_node_with_style_and_box_model_metrics() const { return false; }
  93. template<typename T>
  94. bool fast_is() const = delete;
  95. bool is_floating() const;
  96. bool is_positioned() const;
  97. bool is_absolutely_positioned() const;
  98. bool is_fixed_position() const;
  99. bool is_flex_item() const { return m_is_flex_item; }
  100. void set_flex_item(bool b) { m_is_flex_item = b; }
  101. bool is_grid_item() const { return m_is_grid_item; }
  102. void set_grid_item(bool b) { m_is_grid_item = b; }
  103. Box const* containing_block() const;
  104. Box* containing_block() { return const_cast<Box*>(const_cast<Node const*>(this)->containing_block()); }
  105. // Closest non-anonymous ancestor box, to be used when resolving percentage values.
  106. // Anonymous block boxes are ignored when resolving percentage values that would refer to it:
  107. // the closest non-anonymous ancestor box is used instead.
  108. // https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level
  109. Box const* non_anonymous_containing_block() const;
  110. bool establishes_stacking_context() const;
  111. bool can_contain_boxes_with_position_absolute() const;
  112. Gfx::Font const& font() const;
  113. Gfx::Font const& scaled_font(PaintContext&) const;
  114. Gfx::Font const& scaled_font(float scale_factor) const;
  115. CSS::ImmutableComputedValues const& computed_values() const;
  116. CSSPixels line_height() const;
  117. NodeWithStyle* parent();
  118. NodeWithStyle const* parent() const;
  119. void inserted_into(Node&) { }
  120. void removed_from(Node&) { }
  121. void children_changed() { }
  122. virtual void set_needs_display();
  123. bool children_are_inline() const { return m_children_are_inline; }
  124. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  125. CSSPixelPoint box_type_agnostic_position() const;
  126. enum class SelectionState {
  127. None, // No selection
  128. Start, // Selection starts in this Node
  129. End, // Selection ends in this Node
  130. StartAndEnd, // Selection starts and ends in this Node
  131. Full, // Selection starts before and ends after this Node
  132. };
  133. SelectionState selection_state() const { return m_selection_state; }
  134. void set_selection_state(SelectionState state) { m_selection_state = state; }
  135. protected:
  136. Node(DOM::Document&, DOM::Node*);
  137. virtual void visit_edges(Cell::Visitor&) override;
  138. private:
  139. friend class NodeWithStyle;
  140. JS::NonnullGCPtr<DOM::Node> m_dom_node;
  141. JS::GCPtr<Painting::Paintable> m_paintable;
  142. JS::NonnullGCPtr<HTML::BrowsingContext> m_browsing_context;
  143. JS::GCPtr<DOM::Element> m_pseudo_element_generator;
  144. bool m_anonymous { false };
  145. bool m_has_style { false };
  146. bool m_children_are_inline { false };
  147. SelectionState m_selection_state { SelectionState::None };
  148. bool m_is_flex_item { false };
  149. bool m_is_grid_item { false };
  150. GeneratedFor m_generated_for { GeneratedFor::NotGenerated };
  151. };
  152. class NodeWithStyle : public Node {
  153. JS_CELL(NodeWithStyle, Node);
  154. public:
  155. virtual ~NodeWithStyle() override = default;
  156. const CSS::ImmutableComputedValues& computed_values() const { return static_cast<const CSS::ImmutableComputedValues&>(m_computed_values); }
  157. CSS::MutableComputedValues& mutable_computed_values() { return static_cast<CSS::MutableComputedValues&>(m_computed_values); }
  158. void apply_style(const CSS::StyleProperties&);
  159. Gfx::Font const& font() const { return *m_font; }
  160. CSSPixels line_height() const { return m_line_height; }
  161. void set_line_height(CSSPixels line_height) { m_line_height = line_height; }
  162. void set_font(Gfx::Font const& font) { m_font = font; }
  163. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  164. const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; }
  165. JS::NonnullGCPtr<NodeWithStyle> create_anonymous_wrapper() const;
  166. void transfer_table_box_computed_values_to_wrapper_computed_values(CSS::ComputedValues& wrapper_computed_values);
  167. protected:
  168. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullRefPtr<CSS::StyleProperties>);
  169. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::ComputedValues);
  170. private:
  171. void reset_table_box_computed_values_used_by_wrapper_to_init_values();
  172. CSS::ComputedValues m_computed_values;
  173. RefPtr<Gfx::Font const> m_font;
  174. CSSPixels m_line_height { 0 };
  175. RefPtr<CSS::AbstractImageStyleValue const> m_list_style_image;
  176. };
  177. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  178. JS_CELL(NodeWithStyleAndBoxModelMetrics, NodeWithStyle);
  179. public:
  180. BoxModelMetrics& box_model() { return m_box_model; }
  181. BoxModelMetrics const& box_model() const { return m_box_model; }
  182. protected:
  183. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullRefPtr<CSS::StyleProperties> style)
  184. : NodeWithStyle(document, node, move(style))
  185. {
  186. }
  187. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::ComputedValues computed_values)
  188. : NodeWithStyle(document, node, move(computed_values))
  189. {
  190. }
  191. private:
  192. virtual bool is_node_with_style_and_box_model_metrics() const final { return true; }
  193. BoxModelMetrics m_box_model;
  194. };
  195. template<>
  196. inline bool Node::fast_is<NodeWithStyleAndBoxModelMetrics>() const { return is_node_with_style_and_box_model_metrics(); }
  197. inline bool Node::has_style_or_parent_with_style() const
  198. {
  199. return m_has_style || (parent() != nullptr && parent()->has_style_or_parent_with_style());
  200. }
  201. inline Gfx::Font const& Node::font() const
  202. {
  203. VERIFY(has_style_or_parent_with_style());
  204. if (m_has_style)
  205. return static_cast<NodeWithStyle const*>(this)->font();
  206. return parent()->font();
  207. }
  208. inline Gfx::Font const& Node::scaled_font(PaintContext& context) const
  209. {
  210. return scaled_font(context.device_pixels_per_css_pixel());
  211. }
  212. inline Gfx::Font const& Node::scaled_font(float scale_factor) const
  213. {
  214. return document().style_computer().font_cache().scaled_font(font(), scale_factor);
  215. }
  216. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  217. {
  218. VERIFY(has_style_or_parent_with_style());
  219. if (m_has_style)
  220. return static_cast<NodeWithStyle const*>(this)->computed_values();
  221. return parent()->computed_values();
  222. }
  223. inline CSSPixels Node::line_height() const
  224. {
  225. VERIFY(has_style_or_parent_with_style());
  226. if (m_has_style)
  227. return static_cast<NodeWithStyle const*>(this)->line_height();
  228. return parent()->line_height();
  229. }
  230. inline NodeWithStyle const* Node::parent() const
  231. {
  232. return static_cast<NodeWithStyle const*>(TreeNode<Node>::parent());
  233. }
  234. inline NodeWithStyle* Node::parent()
  235. {
  236. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  237. }
  238. }