Node.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.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 <LibGC/Root.h>
  11. #include <LibGfx/Rect.h>
  12. #include <LibJS/Heap/Cell.h>
  13. #include <LibWeb/CSS/ComputedValues.h>
  14. #include <LibWeb/CSS/StyleComputer.h>
  15. #include <LibWeb/CSS/StyleProperties.h>
  16. #include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
  17. #include <LibWeb/DOM/Document.h>
  18. #include <LibWeb/Forward.h>
  19. #include <LibWeb/Layout/BoxModelMetrics.h>
  20. #include <LibWeb/Painting/PaintContext.h>
  21. #include <LibWeb/Painting/Paintable.h>
  22. #include <LibWeb/TreeNode.h>
  23. namespace Web::Layout {
  24. enum class LayoutMode {
  25. // Normal layout. No min-content or max-content constraints applied.
  26. Normal,
  27. // Intrinsic size determination.
  28. // Boxes honor min-content and max-content constraints (set via LayoutState::UsedValues::{width,height}_constraint)
  29. // by considering their containing block to be 0-sized or infinitely large in the relevant axis.
  30. // https://drafts.csswg.org/css-sizing-3/#intrinsic-sizing
  31. IntrinsicSizing,
  32. };
  33. class Node
  34. : public JS::Cell
  35. , public TreeNode<Node> {
  36. GC_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. using PaintableList = IntrusiveList<&Painting::Paintable::m_list_node>;
  58. Painting::Paintable* first_paintable() { return m_paintable.first(); }
  59. Painting::Paintable const* first_paintable() const { return m_paintable.first(); }
  60. PaintableList& paintables() { return m_paintable; }
  61. PaintableList const& paintables() const { return m_paintable; }
  62. void add_paintable(GC::Ptr<Painting::Paintable>);
  63. void clear_paintables();
  64. virtual GC::Ptr<Painting::Paintable> create_paintable() const;
  65. DOM::Document& document();
  66. DOM::Document const& document() const;
  67. GC::Ptr<HTML::Navigable> navigable() const;
  68. Viewport const& root() const;
  69. Viewport& root();
  70. bool is_root_element() const;
  71. String debug_description() const;
  72. bool has_style() const { return m_has_style; }
  73. bool has_style_or_parent_with_style() const;
  74. virtual bool can_have_children() const { return true; }
  75. CSS::Display display() const;
  76. bool is_inline() const;
  77. bool is_inline_block() const;
  78. bool is_inline_table() const;
  79. bool is_out_of_flow(FormattingContext const&) const;
  80. // These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
  81. virtual bool is_box() const { return false; }
  82. virtual bool is_block_container() const { return false; }
  83. virtual bool is_break_node() const { return false; }
  84. virtual bool is_text_node() const { return false; }
  85. virtual bool is_viewport() const { return false; }
  86. virtual bool is_svg_box() const { return false; }
  87. virtual bool is_svg_geometry_box() const { return false; }
  88. virtual bool is_svg_mask_box() const { return false; }
  89. virtual bool is_svg_svg_box() const { return false; }
  90. virtual bool is_label() const { return false; }
  91. virtual bool is_replaced_box() const { return false; }
  92. virtual bool is_list_item_box() const { return false; }
  93. virtual bool is_list_item_marker_box() const { return false; }
  94. virtual bool is_fieldset_box() const { return false; }
  95. virtual bool is_legend_box() const { return false; }
  96. virtual bool is_table_wrapper() const { return false; }
  97. virtual bool is_node_with_style_and_box_model_metrics() const { return false; }
  98. template<typename T>
  99. bool fast_is() const = delete;
  100. bool is_floating() const;
  101. bool is_positioned() const;
  102. bool is_absolutely_positioned() const;
  103. bool is_fixed_position() const;
  104. bool is_sticky_position() const;
  105. bool is_flex_item() const { return m_is_flex_item; }
  106. void set_flex_item(bool b) { m_is_flex_item = b; }
  107. bool is_grid_item() const { return m_is_grid_item; }
  108. void set_grid_item(bool b) { m_is_grid_item = b; }
  109. Box const* containing_block() const;
  110. Box* containing_block() { return const_cast<Box*>(const_cast<Node const*>(this)->containing_block()); }
  111. [[nodiscard]] Box const* static_position_containing_block() const;
  112. [[nodiscard]] Box* static_position_containing_block() { return const_cast<Box*>(const_cast<Node const*>(this)->static_position_containing_block()); }
  113. // Closest non-anonymous ancestor box, to be used when resolving percentage values.
  114. // Anonymous block boxes are ignored when resolving percentage values that would refer to it:
  115. // the closest non-anonymous ancestor box is used instead.
  116. // https://www.w3.org/TR/CSS22/visuren.html#anonymous-block-level
  117. Box const* non_anonymous_containing_block() const;
  118. bool establishes_stacking_context() const;
  119. bool can_contain_boxes_with_position_absolute() const;
  120. Gfx::Font const& first_available_font() const;
  121. Gfx::Font const& scaled_font(PaintContext&) const;
  122. Gfx::Font const& scaled_font(float scale_factor) const;
  123. CSS::ImmutableComputedValues const& computed_values() const;
  124. NodeWithStyle* parent();
  125. NodeWithStyle const* parent() const;
  126. void inserted_into(Node&) { }
  127. void removed_from(Node&) { }
  128. void children_changed() { }
  129. bool children_are_inline() const { return m_children_are_inline; }
  130. void set_children_are_inline(bool value) { m_children_are_inline = value; }
  131. u32 initial_quote_nesting_level() const { return m_initial_quote_nesting_level; }
  132. void set_initial_quote_nesting_level(u32 value) { m_initial_quote_nesting_level = value; }
  133. // An element is called out of flow if it is floated, absolutely positioned, or is the root element.
  134. // https://www.w3.org/TR/CSS22/visuren.html#positioning-scheme
  135. bool is_out_of_flow() const { return is_floating() || is_absolutely_positioned(); }
  136. // An element is called in-flow if it is not out-of-flow.
  137. // https://www.w3.org/TR/CSS22/visuren.html#positioning-scheme
  138. bool is_in_flow() const { return !is_out_of_flow(); }
  139. [[nodiscard]] bool has_css_transform() const
  140. {
  141. if (!computed_values().transformations().is_empty())
  142. return true;
  143. if (computed_values().rotate().has_value())
  144. return true;
  145. if (computed_values().translate().has_value())
  146. return true;
  147. return false;
  148. }
  149. protected:
  150. Node(DOM::Document&, DOM::Node*);
  151. virtual void visit_edges(Cell::Visitor&) override;
  152. private:
  153. friend class NodeWithStyle;
  154. GC::Ref<DOM::Node> m_dom_node;
  155. PaintableList m_paintable;
  156. GC::Ptr<DOM::Element> m_pseudo_element_generator;
  157. bool m_anonymous { false };
  158. bool m_has_style { false };
  159. bool m_children_are_inline { false };
  160. bool m_is_flex_item { false };
  161. bool m_is_grid_item { false };
  162. GeneratedFor m_generated_for { GeneratedFor::NotGenerated };
  163. u32 m_initial_quote_nesting_level { 0 };
  164. };
  165. class NodeWithStyle : public Node {
  166. GC_CELL(NodeWithStyle, Node);
  167. public:
  168. virtual ~NodeWithStyle() override = default;
  169. CSS::ImmutableComputedValues const& computed_values() const { return static_cast<CSS::ImmutableComputedValues const&>(*m_computed_values); }
  170. CSS::MutableComputedValues& mutable_computed_values() { return static_cast<CSS::MutableComputedValues&>(*m_computed_values); }
  171. void apply_style(const CSS::StyleProperties&);
  172. Gfx::Font const& first_available_font() const;
  173. Vector<CSS::BackgroundLayerData> const& background_layers() const { return computed_values().background_layers(); }
  174. const CSS::AbstractImageStyleValue* list_style_image() const { return m_list_style_image; }
  175. GC::Ref<NodeWithStyle> create_anonymous_wrapper() const;
  176. void transfer_table_box_computed_values_to_wrapper_computed_values(CSS::ComputedValues& wrapper_computed_values);
  177. bool is_body() const;
  178. bool is_scroll_container() const;
  179. virtual void visit_edges(Cell::Visitor& visitor) override;
  180. protected:
  181. NodeWithStyle(DOM::Document&, DOM::Node*, CSS::StyleProperties);
  182. NodeWithStyle(DOM::Document&, DOM::Node*, NonnullOwnPtr<CSS::ComputedValues>);
  183. private:
  184. void reset_table_box_computed_values_used_by_wrapper_to_init_values();
  185. void propagate_style_to_anonymous_wrappers();
  186. NonnullOwnPtr<CSS::ComputedValues> m_computed_values;
  187. RefPtr<CSS::AbstractImageStyleValue const> m_list_style_image;
  188. };
  189. class NodeWithStyleAndBoxModelMetrics : public NodeWithStyle {
  190. GC_CELL(NodeWithStyleAndBoxModelMetrics, NodeWithStyle);
  191. public:
  192. BoxModelMetrics& box_model() { return m_box_model; }
  193. BoxModelMetrics const& box_model() const { return m_box_model; }
  194. protected:
  195. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, CSS::StyleProperties style)
  196. : NodeWithStyle(document, node, move(style))
  197. {
  198. }
  199. NodeWithStyleAndBoxModelMetrics(DOM::Document& document, DOM::Node* node, NonnullOwnPtr<CSS::ComputedValues> computed_values)
  200. : NodeWithStyle(document, node, move(computed_values))
  201. {
  202. }
  203. private:
  204. virtual bool is_node_with_style_and_box_model_metrics() const final { return true; }
  205. BoxModelMetrics m_box_model;
  206. };
  207. template<>
  208. inline bool Node::fast_is<NodeWithStyleAndBoxModelMetrics>() const { return is_node_with_style_and_box_model_metrics(); }
  209. inline bool Node::has_style_or_parent_with_style() const
  210. {
  211. return m_has_style || (parent() != nullptr && parent()->has_style_or_parent_with_style());
  212. }
  213. inline Gfx::Font const& Node::first_available_font() const
  214. {
  215. VERIFY(has_style_or_parent_with_style());
  216. if (m_has_style)
  217. return static_cast<NodeWithStyle const*>(this)->first_available_font();
  218. return parent()->first_available_font();
  219. }
  220. inline Gfx::Font const& Node::scaled_font(PaintContext& context) const
  221. {
  222. return scaled_font(context.device_pixels_per_css_pixel());
  223. }
  224. inline Gfx::Font const& Node::scaled_font(float scale_factor) const
  225. {
  226. auto const& font = first_available_font();
  227. return font.with_size(font.point_size() * scale_factor);
  228. }
  229. inline const CSS::ImmutableComputedValues& Node::computed_values() const
  230. {
  231. VERIFY(has_style_or_parent_with_style());
  232. if (m_has_style)
  233. return static_cast<NodeWithStyle const*>(this)->computed_values();
  234. return parent()->computed_values();
  235. }
  236. inline NodeWithStyle const* Node::parent() const
  237. {
  238. return static_cast<NodeWithStyle const*>(TreeNode<Node>::parent());
  239. }
  240. inline NodeWithStyle* Node::parent()
  241. {
  242. return static_cast<NodeWithStyle*>(TreeNode<Node>::parent());
  243. }
  244. inline Gfx::Font const& NodeWithStyle::first_available_font() const
  245. {
  246. // https://drafts.csswg.org/css-fonts/#first-available-font
  247. // First font for which the character U+0020 (space) is not excluded by a unicode-range
  248. return computed_values().font_list().font_for_code_point(' ');
  249. }
  250. }