Node.h 11 KB

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