script/dom/node/
layout_dom.rs1use atomic_refcell::AtomicRef;
8use layout_api::{
9 GenericLayoutData, HTMLCanvasData, HTMLMediaData, LayoutElementType, LayoutNodeType,
10 SVGElementData,
11};
12use net_traits::image_cache::Image;
13use pixels::ImageMetadata;
14use script_bindings::codegen::InheritTypes::{
15 ElementTypeId, HTMLElementTypeId, SVGElementTypeId, SVGGraphicsElementTypeId,
16};
17use servo_base::cross_process_instant::CrossProcessInstant;
18use servo_base::id::{BrowsingContextId, PipelineId};
19use servo_base::text::{RangeAny, Utf16CodeUnits, Utf32CodeUnits};
20use servo_url::ServoUrl;
21use style::dom::OpaqueNode;
22use style::selector_parser::PseudoElement;
23
24use crate::dom::bindings::inheritance::{CharacterDataTypeId, NodeTypeId};
25use crate::dom::bindings::root::{LayoutDom, ToLayout, ToLayoutOptional};
26use crate::dom::document::Document;
27use crate::dom::element::Element;
28use crate::dom::html::form_controls::htmlinputelement::HTMLInputElement;
29use crate::dom::html::htmlcanvaselement::HTMLCanvasElement;
30use crate::dom::html::htmliframeelement::HTMLIFrameElement;
31use crate::dom::html::htmlimageelement::HTMLImageElement;
32use crate::dom::html::htmlslotelement::HTMLSlotElement;
33use crate::dom::html::htmltextareaelement::HTMLTextAreaElement;
34use crate::dom::html::htmlvideoelement::HTMLVideoElement;
35use crate::dom::shadowroot::shadowroot::ShadowRoot;
36use crate::dom::svg::svgsvgelement::SVGSVGElement;
37use crate::dom::text::Text;
38use crate::dom::{Node, NodeFlags};
39
40impl<'dom> LayoutDom<'dom, Node> {
41 #[inline]
42 #[expect(unsafe_code)]
43 pub(crate) fn parent_node_ref(self) -> Option<LayoutDom<'dom, Node>> {
44 unsafe { self.unsafe_get().parent_node().to_layout() }
45 }
46
47 #[inline]
48 pub(crate) fn type_id_for_layout(self) -> NodeTypeId {
49 self.unsafe_get().type_id()
50 }
51
52 #[inline]
53 pub(crate) fn is_element_for_layout(&self) -> bool {
54 (*self).is::<Element>()
55 }
56
57 pub(crate) fn is_text_node_for_layout(&self) -> bool {
58 matches!(
59 self.type_id_for_layout(),
60 NodeTypeId::CharacterData(CharacterDataTypeId::Text(..))
61 )
62 }
63
64 #[inline]
65 pub(crate) fn composed_parent_node_ref(self) -> Option<LayoutDom<'dom, Node>> {
66 let parent = self.parent_node_ref();
67 if let Some(parent) = parent &&
68 let Some(shadow_root) = parent.downcast::<ShadowRoot>()
69 {
70 return Some(shadow_root.get_host_for_layout().upcast());
71 }
72 parent
73 }
74
75 #[inline]
76 pub(crate) fn traversal_parent(self) -> Option<LayoutDom<'dom, Element>> {
77 if let Some(assigned_slot) = self.assigned_slot_for_layout() {
78 return Some(assigned_slot.upcast());
79 }
80 let parent = self.parent_node_ref()?;
81 if let Some(shadow) = parent.downcast::<ShadowRoot>() {
82 return Some(shadow.get_host_for_layout());
83 };
84 parent.downcast()
85 }
86
87 #[inline]
88 #[expect(unsafe_code)]
89 pub(crate) fn first_child_ref(self) -> Option<LayoutDom<'dom, Node>> {
90 unsafe { self.unsafe_get().first_child().to_layout() }
91 }
92
93 #[inline]
94 #[expect(unsafe_code)]
95 pub(crate) fn last_child_ref(self) -> Option<LayoutDom<'dom, Node>> {
96 unsafe { self.unsafe_get().last_child().to_layout() }
97 }
98
99 #[inline]
100 #[expect(unsafe_code)]
101 pub(crate) fn prev_sibling_ref(self) -> Option<LayoutDom<'dom, Node>> {
102 unsafe { self.unsafe_get().prev_sibling().to_layout() }
103 }
104
105 #[inline]
106 #[expect(unsafe_code)]
107 pub(crate) fn next_sibling_ref(self) -> Option<LayoutDom<'dom, Node>> {
108 unsafe { self.unsafe_get().next_sibling().to_layout() }
109 }
110
111 #[inline]
112 #[expect(unsafe_code)]
113 pub(crate) fn owner_doc_for_layout(self) -> LayoutDom<'dom, Document> {
114 unsafe { self.unsafe_get().get_owner_doc().to_layout().unwrap() }
115 }
116
117 #[inline]
118 #[expect(unsafe_code)]
119 pub(crate) fn containing_shadow_root_for_layout(self) -> Option<LayoutDom<'dom, ShadowRoot>> {
120 unsafe {
121 self.unsafe_get()
122 .get_rare_data()
123 .borrow_for_layout()
124 .as_ref()?
125 .containing_shadow_root
126 .as_ref()
127 .map(|sr| sr.to_layout())
128 }
129 }
130
131 #[inline]
132 #[expect(unsafe_code)]
133 pub(crate) fn assigned_slot_for_layout(self) -> Option<LayoutDom<'dom, HTMLSlotElement>> {
134 unsafe {
135 self.unsafe_get()
136 .get_rare_data()
137 .borrow_for_layout()
138 .as_ref()?
139 .slottable_data
140 .assigned_slot
141 .as_ref()
142 .map(|assigned_slot| assigned_slot.to_layout())
143 }
144 }
145
146 #[inline]
151 #[expect(unsafe_code)]
152 pub(crate) unsafe fn get_flag(self, flag: NodeFlags) -> bool {
153 (self.unsafe_get()).flags().get().contains(flag)
154 }
155
156 #[inline]
157 #[expect(unsafe_code)]
158 pub(crate) unsafe fn set_flag(self, flag: NodeFlags, value: bool) {
159 let this = self.unsafe_get();
160 let mut flags = (this).flags().get();
161
162 if value {
163 flags.insert(flag);
164 } else {
165 flags.remove(flag);
166 }
167
168 (this).flags().set(flags);
169 }
170
171 #[inline]
172 #[expect(unsafe_code)]
173 pub(crate) fn layout_data(self) -> Option<&'dom GenericLayoutData> {
174 unsafe {
175 self.unsafe_get()
176 .layout_data()
177 .borrow_for_layout()
178 .as_deref()
179 }
180 }
181
182 #[inline]
190 #[expect(unsafe_code)]
191 pub(crate) unsafe fn initialize_layout_data(self, new_data: Box<GenericLayoutData>) {
192 let data = unsafe { self.unsafe_get().layout_data().borrow_mut_for_layout() };
193 debug_assert!(data.is_none());
194 *data = Some(new_data);
195 }
196
197 #[inline]
205 #[expect(unsafe_code)]
206 pub(crate) unsafe fn clear_layout_data(self) {
207 unsafe {
208 self.unsafe_get()
209 .layout_data()
210 .borrow_mut_for_layout()
211 .take();
212 }
213 }
214
215 pub(crate) fn is_single_line_text_inner_editor(&self) -> bool {
218 matches!(
219 self.implemented_pseudo_element(),
220 Some(PseudoElement::ServoTextControlInnerEditor)
221 )
222 }
223
224 pub(crate) fn is_text_container_of_single_line_input(&self) -> bool {
227 let is_single_line_text_inner_placeholder = matches!(
228 self.implemented_pseudo_element(),
229 Some(PseudoElement::Placeholder)
230 );
231 debug_assert!(
233 !is_single_line_text_inner_placeholder ||
234 self.containing_shadow_root_for_layout()
235 .map(|root| root.get_host_for_layout())
236 .map(|host| host.downcast::<HTMLInputElement>())
237 .is_some()
238 );
239
240 self.is_single_line_text_inner_editor() || is_single_line_text_inner_placeholder
241 }
242
243 pub(crate) fn text_content(self) -> AtomicRef<'dom, str> {
244 self.downcast::<Text>()
245 .expect("Called LayoutDom::text_content on non-Text node!")
246 .upcast()
247 .data_for_layout()
248 }
249
250 #[expect(unsafe_code)]
251 pub(crate) fn selection_for_text_node(&self) -> Option<RangeAny<Utf32CodeUnits>> {
252 if let Some(shadow_root) = self.containing_shadow_root_for_layout() {
253 let host = shadow_root.get_host_for_layout();
254 if let Some(input) = host.downcast::<HTMLInputElement>() {
255 return input.selection_for_layout();
256 }
257 if let Some(textarea) = host.downcast::<HTMLTextAreaElement>() {
258 return textarea.selection_for_layout();
259 }
260 }
261
262 let unsafe_self = self.unsafe_get();
263 if !unsafe_self.get_flag(NodeFlags::OVERLAPS_DOCUMENT_SELECTION) ||
264 unsafe_self.get_flag(NodeFlags::SELECTION_INHIBITED)
265 {
266 return None;
267 }
268
269 let text_node = self.downcast::<Text>()?;
270 let text = text_node.upcast().data_for_layout();
271 let selection = self.owner_doc_for_layout().visible_selection_for_layout()?;
272 let range = selection.range_for_layout()?;
273 let is_start_node = unsafe { range.start.container.to_layout() } == *self;
274 let is_end_node = unsafe { range.end.container.to_layout() } == *self;
275
276 let start =
277 is_start_node.then(|| Utf16CodeUnits(range.start.offset).to_utf32_code_units_in(&text));
278 let end =
279 is_end_node.then(|| Utf16CodeUnits(range.end.offset).to_utf32_code_units_in(&text));
280 Some(RangeAny::new(start, end))
281 }
282
283 pub(crate) fn text_node_paints_caret(&self) -> bool {
284 self.parent_node_ref()
286 .is_some_and(|parent| parent.is_text_container_of_single_line_input())
287 }
288
289 pub(crate) fn text_node_needs_caret_placeholder(&self) -> bool {
290 self.parent_node_ref()
292 .is_some_and(|parent| parent.is_text_container_of_single_line_input())
293 }
294
295 pub(crate) fn replaced_is_selected(&self) -> bool {
296 let unsafe_self = self.unsafe_get();
297 unsafe_self.get_flag(NodeFlags::OVERLAPS_DOCUMENT_SELECTION) &&
298 !unsafe_self.get_flag(NodeFlags::SELECTION_INHIBITED)
299 }
300
301 pub(crate) fn image_url(self) -> Option<ServoUrl> {
302 self.downcast::<HTMLImageElement>()
303 .expect("not an image!")
304 .image_url()
305 }
306
307 pub(crate) fn image_data(self) -> Option<(Option<Image>, Option<ImageMetadata>)> {
308 self.downcast::<HTMLImageElement>().map(|e| e.image_data())
309 }
310
311 pub(crate) fn load_time(self) -> Option<CrossProcessInstant> {
312 self.downcast::<HTMLImageElement>()
313 .and_then(|image_element| image_element.load_time())
314 }
315
316 pub(crate) fn image_density(self) -> Option<f64> {
317 self.downcast::<HTMLImageElement>()
318 .expect("not an image!")
319 .image_density()
320 }
321
322 pub(crate) fn showing_broken_image_icon(self) -> bool {
323 self.downcast::<HTMLImageElement>()
324 .map(|image_element| image_element.showing_broken_image_icon())
325 .unwrap_or_default()
326 }
327
328 pub(crate) fn canvas_data(self) -> Option<HTMLCanvasData> {
329 self.downcast::<HTMLCanvasElement>()
330 .map(|canvas| canvas.data())
331 }
332
333 pub(crate) fn media_data(self) -> Option<HTMLMediaData> {
334 self.downcast::<HTMLVideoElement>()
335 .map(|media| media.data())
336 }
337
338 pub(crate) fn svg_data(self) -> Option<SVGElementData<'dom>> {
339 self.downcast::<SVGSVGElement>().map(|svg| svg.data())
340 }
341
342 pub(crate) fn iframe_browsing_context_id(self) -> Option<BrowsingContextId> {
343 self.downcast::<HTMLIFrameElement>()
344 .and_then(|iframe_element| iframe_element.browsing_context_id())
345 }
346
347 pub(crate) fn iframe_pipeline_id(self) -> Option<PipelineId> {
348 self.downcast::<HTMLIFrameElement>()
349 .and_then(|iframe_element| iframe_element.pipeline_id())
350 }
351
352 #[expect(unsafe_code)]
353 pub(crate) fn opaque(self) -> OpaqueNode {
354 unsafe { OpaqueNode(self.get_jsobject() as usize) }
355 }
356
357 #[expect(unsafe_code)]
358 pub(crate) fn implemented_pseudo_element(&self) -> Option<PseudoElement> {
359 unsafe {
360 self.unsafe_get()
361 .get_rare_data()
362 .borrow_for_layout()
363 .as_ref()
364 .and_then(|rare_data| rare_data.implemented_pseudo_element)
365 }
366 }
367
368 pub(crate) fn is_in_ua_widget(&self) -> bool {
369 self.unsafe_get().is_in_ua_widget()
370 }
371
372 pub(crate) fn is_root_of_user_agent_widget(&self) -> bool {
373 self.downcast::<Element>().is_some_and(|element| {
374 element
375 .get_shadow_root_for_layout()
376 .is_some_and(|shadow_root| shadow_root.is_user_agent_widget())
377 })
378 }
379
380 pub(crate) fn children_count(&self) -> u32 {
381 self.unsafe_get().children_count()
382 }
383}
384
385pub(crate) struct NodeTypeIdWrapper(pub(crate) NodeTypeId);
386
387impl From<NodeTypeIdWrapper> for LayoutNodeType {
388 #[inline(always)]
389 fn from(node_type: NodeTypeIdWrapper) -> LayoutNodeType {
390 match node_type.0 {
391 NodeTypeId::Element(e) => LayoutNodeType::Element(ElementTypeIdWrapper(e).into()),
392 NodeTypeId::CharacterData(CharacterDataTypeId::Text(_)) => LayoutNodeType::Text,
393 x => unreachable!("Layout should not traverse nodes of type {:?}", x),
394 }
395 }
396}
397
398struct ElementTypeIdWrapper(ElementTypeId);
399
400impl From<ElementTypeIdWrapper> for LayoutElementType {
401 #[inline(always)]
402 fn from(element_type: ElementTypeIdWrapper) -> LayoutElementType {
403 match element_type.0 {
404 ElementTypeId::HTMLElement(id) => match id {
405 HTMLElementTypeId::HTMLBRElement => Self::HTMLBRElement,
406 HTMLElementTypeId::HTMLBodyElement => Self::HTMLBodyElement,
407 HTMLElementTypeId::HTMLButtonElement => Self::HTMLButtonElement,
408 HTMLElementTypeId::HTMLCanvasElement => Self::HTMLCanvasElement,
409 HTMLElementTypeId::HTMLHtmlElement => Self::HTMLHtmlElement,
410 HTMLElementTypeId::HTMLIFrameElement => Self::HTMLIFrameElement,
411 HTMLElementTypeId::HTMLImageElement => Self::HTMLImageElement,
412 HTMLElementTypeId::HTMLInputElement => Self::HTMLInputElement,
413 HTMLElementTypeId::HTMLMediaElement(_) => Self::HTMLMediaElement,
414 HTMLElementTypeId::HTMLMeterElement => Self::HTMLMeterElement,
415 HTMLElementTypeId::HTMLObjectElement => Self::HTMLObjectElement,
416 HTMLElementTypeId::HTMLOptGroupElement => Self::HTMLOptGroupElement,
417 HTMLElementTypeId::HTMLOptionElement => Self::HTMLOptionElement,
418 HTMLElementTypeId::HTMLParagraphElement => Self::HTMLParagraphElement,
419 HTMLElementTypeId::HTMLPreElement => Self::HTMLPreElement,
420 HTMLElementTypeId::HTMLProgressElement => Self::HTMLProgressElement,
421 HTMLElementTypeId::HTMLSelectElement => Self::HTMLSelectElement,
422 HTMLElementTypeId::HTMLTableCellElement => Self::HTMLTableCellElement,
423 HTMLElementTypeId::HTMLTableColElement => Self::HTMLTableColElement,
424 HTMLElementTypeId::HTMLTableElement => Self::HTMLTableElement,
425 HTMLElementTypeId::HTMLTableRowElement => Self::HTMLTableRowElement,
426 HTMLElementTypeId::HTMLTableSectionElement => Self::HTMLTableSectionElement,
427 HTMLElementTypeId::HTMLTextAreaElement => Self::HTMLTextAreaElement,
428 _ => Self::Element,
429 },
430 ElementTypeId::SVGElement(SVGElementTypeId::SVGGraphicsElement(id)) => match id {
431 SVGGraphicsElementTypeId::SVGImageElement => Self::SVGImageElement,
432 SVGGraphicsElementTypeId::SVGSVGElement => Self::SVGSVGElement,
433 _ => Self::Element,
434 },
435 _ => Self::Element,
436 }
437 }
438}