layout_api/layout_node.rs
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5#![expect(unsafe_code)]
6#![deny(missing_docs)]
7
8use std::borrow::Cow;
9use std::fmt::Debug;
10
11use net_traits::image_cache::Image;
12use pixels::ImageMetadata;
13use servo_arc::Arc;
14use servo_base::id::{BrowsingContextId, PipelineId};
15use servo_url::ServoUrl;
16use style::context::SharedStyleContext;
17use style::dom::{NodeInfo, OpaqueNode, TNode};
18use style::properties::ComputedValues;
19use style::selector_parser::PseudoElement;
20
21use crate::layout_dom::{DangerousStyleNodeOf, LayoutElementOf, LayoutNodeOf};
22use crate::pseudo_element_chain::PseudoElementChain;
23use crate::{
24 GenericLayoutData, HTMLCanvasData, HTMLMediaData, LayoutDataTrait, LayoutDomTypeBundle,
25 LayoutNodeType, SVGElementData, SharedSelection,
26};
27
28/// A trait that exposes a DOM nodes to layout. Implementors of this trait must abide by certain
29/// safety requirements. Layout will only ever access and mutate each node from a single thread
30/// at a time, though children may be used in parallel from other threads. That is why this trait
31/// does not allow access to parent nodes, as it would make it easy to cause race conditions and
32/// memory errors.
33///
34/// Note that the related [`DangerousStyleNode`] trait *may* access parent nodes, which is why
35/// that API is marked as `unsafe` here. In general [`DangerousStyleNode`] should only be used
36/// when interfacing with the `stylo` and `selectors`.
37pub trait LayoutNode<'dom>: Copy + Debug + NodeInfo + Send + Sync {
38 /// The concrete implementation of [`LayoutDomTypeBundle`] implemented in `script`.
39 type ConcreteTypeBundle: LayoutDomTypeBundle<'dom>;
40
41 /// Creates a new `LayoutNode` for the same `LayoutNode` with a different pseudo-element type.
42 ///
43 /// Returns `None` if this pseudo doesn't apply to the given element for one of
44 /// the following reasons:
45 ///
46 /// 1. This node is not an element.
47 /// 2. `pseudo` is eager and is not defined in the stylesheet. In this case, there
48 /// is not reason to process the pseudo element at all.
49 /// 3. `pseudo` is for `::servo-details-content` and
50 /// it doesn't apply to this element, either because it isn't a details or is
51 /// in the wrong state.
52 fn with_pseudo(&self, pseudo_element_type: PseudoElement) -> Option<Self>;
53
54 /// Returns the [`PseudoElementChain`] for this [`LayoutElement`].
55 fn pseudo_element_chain(&self) -> PseudoElementChain;
56
57 /// Returns access to a version of this LayoutNode that can be used by stylo
58 /// and selectors. This is dangerous as it allows more access to ancestors nodes
59 /// than LayoutNode. This should *only* be used when handing a node to stylo
60 /// or selectors.
61 ///
62 /// # Safety
63 ///
64 /// This should only ever be called from the main script thread. It is never
65 /// okay to explicitly create a node for style while any layout worker threads
66 /// are running.
67 unsafe fn dangerous_style_node(self) -> DangerousStyleNodeOf<'dom, Self::ConcreteTypeBundle>;
68
69 /// Returns access to the DOM parent node of this node. This *does not* take
70 /// into account shadow tree children and slottables. For that use
71 /// [`Self::dangerous_flat_tree_parent`].
72 ///
73 /// # Safety
74 ///
75 /// This should only ever be called from the main script thread. It is never
76 /// okay to explicitly access the parent node while any layout worker threads
77 /// are running.
78 unsafe fn dangerous_dom_parent(self) -> Option<Self>;
79
80 /// Returns access to the flat tree parent node of this node. This takes
81 /// into account shadow tree children and slottables. For that use
82 /// [`Self::dangerous_flat_tree_parent`].
83 ///
84 /// # Safety
85 ///
86 /// This should only ever be called from the main script thread. It is never
87 /// okay to explicitly access the parent node while any layout worker threads
88 /// are running.
89 unsafe fn dangerous_flat_tree_parent(self) -> Option<Self>;
90
91 /// Get the layout data of this node, attempting to downcast it to the desired type.
92 /// Returns None if there is no layout data or it isn't of the desired type.
93 fn layout_data(&self) -> Option<&'dom GenericLayoutData>;
94
95 /// Returns whether the node is connected.
96 fn is_connected(&self) -> bool;
97
98 /// Converts self into an `OpaqueNode`.
99 fn opaque(&self) -> OpaqueNode;
100
101 /// Returns the type ID of this node. Returns `None` if this is a pseudo-element; otherwise,
102 /// returns `Some`.
103 fn type_id(&self) -> Option<LayoutNodeType>;
104
105 /// Initialize this node with empty opaque layout data.
106 fn initialize_layout_data<RequestedLayoutDataType: LayoutDataTrait>(&self);
107
108 /// Returns an iterator over this node's children in the [flat tree]. This
109 /// takes into account shadow tree children and slottables.
110 ///
111 /// [flat tree]: https://drafts.csswg.org/css-shadow-1/#flat-tree
112 fn flat_tree_children(&self) -> impl Iterator<Item = Self> + Sized;
113
114 /// Returns an iterator over this node's children in the DOM. This
115 /// *does not* take shadow roots and assigned slottables into account.
116 /// For that use [`Self::flat_tree_children`].
117 fn dom_children(&self) -> impl Iterator<Item = Self> + Sized;
118
119 /// Returns a [`LayoutElement`] if this is an element in the HTML namespace, None otherwise.
120 fn as_html_element(&self) -> Option<LayoutElementOf<'dom, Self::ConcreteTypeBundle>>;
121
122 /// Returns a [`LayoutElement`] if this is an element.
123 fn as_element(&self) -> Option<LayoutElementOf<'dom, Self::ConcreteTypeBundle>>;
124
125 /// Returns the computed style for the given node, properly handling pseudo-elements. For
126 /// elements this returns their style and for other nodes, this returns the style of the parent
127 /// element, if one exists.
128 ///
129 /// # Panics
130 ///
131 /// - Calling this method will panic it is an element has no style data, whether because
132 /// styling has not run yet or was not run for this element.
133 /// - Calling this method will panic if it is a non-element node without a parent element.
134 fn style(&self, context: &SharedStyleContext) -> Arc<ComputedValues>;
135
136 /// Returns the style for a text node. This is computed on the fly from the
137 /// parent style to avoid traversing text nodes in the style system.
138 ///
139 /// # Safety
140 ///
141 /// Note that this does require accessing the parent, which this interface
142 /// technically forbids. But accessing the parent is only unsafe insofar as
143 /// it can be used to reach siblings and cousins. A simple immutable borrow
144 /// of the parent data is fine, since the bottom-up traversal will not process
145 /// the parent until all the children have been processed.
146 ///
147 /// # Panics
148 ///
149 /// - Calling this method will panic if the parent element has no style data, whether
150 /// because styling has not run yet or was not run for this element.
151 /// - Calling this method will panic if it is a non-element node without a parent element.
152 fn parent_style(&self, context: &SharedStyleContext) -> Arc<ComputedValues>;
153
154 /// Returns the computed `:selected` style for the given node, properly handling
155 /// pseudo-elements. For elements this returns their style and for other nodes, this
156 /// returns the style of the parent element, if one exists.
157 ///
158 /// # Panics
159 ///
160 /// - Calling this method will panic it is an element has no style data, whether because
161 /// styling has not run yet or was not run for this element.
162 /// - Calling this method will panic if it is a non-element node without a parent element.
163 fn selected_style(&self, context: &SharedStyleContext) -> Arc<ComputedValues>;
164
165 /// Get the text content of this node, if it is a text node.
166 ///
167 /// # Panics
168 ///
169 /// This method will panic if called on a node that is not a DOM text node.
170 fn text_content(self) -> Cow<'dom, str>;
171
172 /// If this node manages a selection, this returns the shared selection for the node.
173 fn selection(&self) -> Option<SharedSelection>;
174
175 /// If this is an image element, returns its URL. If this is not an image element, fails.
176 fn image_url(&self) -> Option<ServoUrl>;
177
178 /// If this is an image element, returns its current-pixel-density. If this is not an image element, fails.
179 fn image_density(&self) -> Option<f64>;
180
181 /// If this is an image element, returns its image data. Otherwise, returns `None`.
182 fn image_data(&self) -> Option<(Option<Image>, Option<ImageMetadata>)>;
183
184 /// Whether or not this is an image element that is showing a broken image icon.
185 fn showing_broken_image_icon(&self) -> bool;
186
187 /// Return the [`HTMLCanvas`] data for this node, if it is a canvas.
188 fn canvas_data(&self) -> Option<HTMLCanvasData>;
189
190 /// Return the [`SVGElementData`] for this node, if it is an SVG subtree.
191 fn svg_data(&self) -> Option<SVGElementData<'dom>>;
192
193 /// Return the [`HTMLMediaData`] for this node, if it is a media element.
194 fn media_data(&self) -> Option<HTMLMediaData>;
195
196 /// If this node is an iframe element, returns its browsing context ID. If this node is
197 /// not an iframe element, fails. Returns None if there is no nested browsing context.
198 fn iframe_browsing_context_id(&self) -> Option<BrowsingContextId>;
199
200 /// If this node is an iframe element, returns its pipeline ID. If this node is
201 /// not an iframe element, fails. Returns None if there is no nested browsing context.
202 fn iframe_pipeline_id(&self) -> Option<PipelineId>;
203
204 /// Return the table span property if it is an element that supports it.
205 fn table_span(&self) -> Option<u32>;
206
207 /// Return the table colspan property if it is an element that supports it.
208 fn table_colspan(&self) -> Option<u32>;
209
210 /// Return the table rowspan property if it is an element that supports it.
211 fn table_rowspan(&self) -> Option<u32>;
212
213 /// Whether this is a container for the text within a single-line text input. This
214 /// is used to solve the special case of line height for a text entry widget.
215 /// <https://html.spec.whatwg.org/multipage/#the-input-element-as-a-text-entry-widget>
216 fn is_single_line_text_input(&self) -> bool;
217
218 /// Whether or not this [`LayoutNode`] is in a user agent widget shadow DOM.
219 fn is_root_of_user_agent_widget(&self) -> bool;
220
221 /// Set whether or not this node has an active pseudo-element style with a `content`
222 /// attribute that uses `attr`.
223 fn set_uses_content_attribute_with_attr(&self, _uses_content_attribute_with_attr: bool);
224}
225
226/// A node that can be passed to `stylo` and `selectors` that allows accessing the
227/// parent node. We consider this to be too dangerous for normal layout, so it is
228/// reserved only for using `stylo` and `selectors`.
229///
230/// If you are not interfacing with `stylo` and `selectors` you *should not* use this
231/// type, unless you know what you are doing.
232pub trait DangerousStyleNode<'dom>: TNode + Sized + NodeInfo + Send + Sync {
233 /// The concrete implementation of [`LayoutDomTypeBundle`] implemented in `script`.
234 type ConcreteTypeBundle: LayoutDomTypeBundle<'dom>;
235 /// Get a handle to the original "safe" version of this node, a [`LayoutNode`] implementation.
236 fn layout_node(&self) -> LayoutNodeOf<'dom, Self::ConcreteTypeBundle>;
237}