1use std::cmp::Ordering;
2
3use style::{dom::TNode as _, values::specified::box_::DisplayInside};
4
5use crate::{BaseDocument, Node};
6
7macro_rules! iter_children {
8 ($node_expr:expr, $cb:expr) => {{
9 let node = &mut $node_expr;
10 let children = core::mem::take(&mut node.children);
11 for child_id in children.iter().copied() {
12 $cb(child_id)
13 }
14 $node_expr.children = children;
15 }};
16}
17pub(crate) use iter_children;
18
19macro_rules! iter_children_and_pseudos {
20 ($node_expr:expr, $cb:expr) => {{
21 let node = &mut $node_expr;
23
24 let before = node.before;
26 let after = node.after;
27 let children = core::mem::take(&mut node.children);
28
29 if let Some(before) = before {
30 $cb(before)
31 }
32 for child_id in children.iter().copied() {
33 $cb(child_id)
34 }
35 if let Some(after) = after {
36 $cb(after)
37 }
38
39 $node_expr.children = children;
41 }};
42}
43pub(crate) use iter_children_and_pseudos;
44
45#[derive(Clone)]
46pub struct TreeTraverser<'a> {
48 doc: &'a BaseDocument,
49 stack: Vec<usize>,
50}
51
52impl<'a> TreeTraverser<'a> {
53 pub fn new(doc: &'a BaseDocument) -> Self {
55 Self::new_with_root(doc, 0)
56 }
57
58 pub fn new_with_root(doc: &'a BaseDocument, root: usize) -> Self {
60 let mut stack = Vec::with_capacity(32);
61 stack.push(root);
62 TreeTraverser { doc, stack }
63 }
64}
65impl Iterator for TreeTraverser<'_> {
66 type Item = usize;
67
68 fn next(&mut self) -> Option<Self::Item> {
69 let id = self.stack.pop()?;
70 let node = self.doc.get_node(id)?;
71 self.stack.extend(node.children.iter().rev());
72 Some(id)
73 }
74}
75
76#[derive(Clone)]
77pub struct AncestorTraverser<'a> {
79 doc: &'a BaseDocument,
80 current: usize,
81}
82impl<'a> AncestorTraverser<'a> {
83 pub fn new(doc: &'a BaseDocument, node_id: usize) -> Self {
85 AncestorTraverser {
86 doc,
87 current: node_id,
88 }
89 }
90}
91impl Iterator for AncestorTraverser<'_> {
92 type Item = usize;
93
94 fn next(&mut self) -> Option<Self::Item> {
95 let current_node = self.doc.get_node(self.current)?;
96 self.current = current_node.parent?;
97 Some(self.current)
98 }
99}
100
101impl Node {
102 #[allow(dead_code)]
103 pub(crate) fn should_traverse_layout_children(&mut self) -> bool {
104 let prefer_layout_children = match self.display_constructed_as.inside() {
105 DisplayInside::None => return false,
106 DisplayInside::Contents => false,
107 DisplayInside::Flow | DisplayInside::FlowRoot | DisplayInside::TableCell => {
108 self.element_data()
110 .is_none_or(|el| el.inline_layout_data.is_none())
111 }
112 DisplayInside::Flex | DisplayInside::Grid => true,
113 DisplayInside::Table => false,
114 DisplayInside::TableRowGroup => false,
115 DisplayInside::TableColumn => false,
116 DisplayInside::TableColumnGroup => false,
117 DisplayInside::TableHeaderGroup => false,
118 DisplayInside::TableFooterGroup => false,
119 DisplayInside::TableRow => false,
120 };
121 let has_layout_children = self.layout_children.get_mut().is_some();
122 prefer_layout_children & has_layout_children
123 }
124}
125
126impl BaseDocument {
127 pub fn node_chain(&self, node_id: usize) -> Vec<usize> {
129 let mut chain = Vec::with_capacity(16);
130 chain.push(node_id);
131 chain.extend(
132 AncestorTraverser::new(self, node_id).filter(|id| self.nodes[*id].is_element()),
133 );
134 chain
135 }
136
137 pub fn visit<F>(&self, mut visit: F)
138 where
139 F: FnMut(usize, &Node),
140 {
141 TreeTraverser::new(self).for_each(|node_id| visit(node_id, &self.nodes[node_id]));
142 }
143
144 pub fn non_anon_ancestor_if_anon(&self, mut node_id: usize) -> usize {
147 loop {
148 let node = &self.nodes[node_id];
149
150 if !node.is_anonymous() {
151 return node.id;
152 }
153
154 let Some(parent_id) = node.layout_parent.get() else {
155 panic!("Node does not exist or does not have a non-anonymous parent");
158 };
159
160 node_id = parent_id;
161 }
162 }
163
164 pub fn iter_children_mut(
165 &mut self,
166 node_id: usize,
167 mut cb: impl FnMut(usize, &mut BaseDocument),
168 ) {
169 let children = std::mem::take(&mut self.nodes[node_id].children);
170 for child_id in children.iter().cloned() {
171 cb(child_id, self);
172 }
173 self.nodes[node_id].children = children;
174 }
175
176 pub fn iter_subtree_mut(
177 &mut self,
178 node_id: usize,
179 mut cb: impl FnMut(usize, &mut BaseDocument),
180 ) {
181 cb(node_id, self);
182 iter_subtree_mut_inner(self, node_id, &mut cb);
183 fn iter_subtree_mut_inner(
184 doc: &mut BaseDocument,
185 node_id: usize,
186 cb: &mut impl FnMut(usize, &mut BaseDocument),
187 ) {
188 let children = std::mem::take(&mut doc.nodes[node_id].children);
189 for child_id in children.iter().cloned() {
190 cb(child_id, doc);
191 iter_subtree_mut_inner(doc, child_id, cb);
192 }
193 doc.nodes[node_id].children = children;
194 }
195 }
196
197 pub fn iter_children_and_pseudos_mut(
198 &mut self,
199 node_id: usize,
200 mut cb: impl FnMut(usize, &mut BaseDocument),
201 ) {
202 let before = self.nodes[node_id].before.take();
203 if let Some(before_node_id) = before {
204 cb(before_node_id, self)
205 }
206 self.nodes[node_id].before = before;
207
208 self.iter_children_mut(node_id, &mut cb);
209
210 let after = self.nodes[node_id].after.take();
211 if let Some(after_node_id) = after {
212 cb(after_node_id, self)
213 }
214 self.nodes[node_id].after = after;
215 }
216
217 pub fn next_node(&self, start: &Node, mut filter: impl FnMut(&Node) -> bool) -> Option<usize> {
218 let start_id = start.id;
219 let mut node = start;
220 let mut look_in_children = true;
221 loop {
222 let next = if look_in_children && !node.children.is_empty() {
224 let node_id = node.children[0];
225 &self.nodes[node_id]
226 }
227 else if let Some(parent) = node.parent_node() {
229 let self_idx = parent
230 .children
231 .iter()
232 .position(|id| *id == node.id)
233 .unwrap();
234 if let Some(sibling_id) = parent.children.get(self_idx + 1) {
236 look_in_children = true;
237 &self.nodes[*sibling_id]
238 }
239 else {
241 look_in_children = false;
242 node = parent;
243 continue;
244 }
245 }
246 else {
248 look_in_children = true;
249 self.root_node()
250 };
251
252 if filter(next) {
253 return Some(next.id);
254 } else if next.id == start_id {
255 return None;
256 }
257
258 node = next;
259 }
260 }
261
262 pub fn node_layout_ancestors(&self, node_id: usize) -> Vec<usize> {
263 let mut ancestors = Vec::with_capacity(12);
264 let mut maybe_id = Some(node_id);
265 while let Some(id) = maybe_id {
266 ancestors.push(id);
267 maybe_id = self.nodes[id].layout_parent.get();
268 }
269 ancestors.reverse();
270 ancestors
271 }
272
273 pub fn maybe_node_layout_ancestors(&self, node_id: Option<usize>) -> Vec<usize> {
274 node_id
275 .map(|id| self.node_layout_ancestors(id))
276 .unwrap_or_default()
277 }
278
279 pub fn compare_document_order(&self, node_a: usize, node_b: usize) -> Ordering {
284 if node_a == node_b {
285 return Ordering::Equal;
286 }
287
288 let chain_a = self.ancestor_chain_from_root(node_a);
290 let chain_b = self.ancestor_chain_from_root(node_b);
291
292 let mut common_depth = 0;
294 for (a, b) in chain_a.iter().zip(chain_b.iter()) {
295 if a != b {
296 break;
297 }
298 common_depth += 1;
299 }
300
301 if common_depth == chain_a.len() {
303 return Ordering::Less; }
305 if common_depth == chain_b.len() {
306 return Ordering::Greater; }
308
309 debug_assert!(
313 common_depth > 0,
314 "nodes must share a common ancestor (the root)"
315 );
316
317 let divergent_a = chain_a[common_depth];
319 let divergent_b = chain_b[common_depth];
320 let parent_id = chain_a[common_depth - 1];
321 let parent = &self.nodes[parent_id];
322
323 for &child_id in &parent.children {
324 if child_id == divergent_a {
325 return Ordering::Less;
326 }
327 if child_id == divergent_b {
328 return Ordering::Greater;
329 }
330 }
331
332 Ordering::Equal
334 }
335
336 fn ancestor_chain_from_root(&self, node_id: usize) -> Vec<usize> {
338 let mut ancestors = Vec::with_capacity(16);
339 let mut current = Some(node_id);
340 while let Some(id) = current {
341 ancestors.push(id);
342 current = self.nodes[id].parent;
343 }
344 ancestors.reverse();
345 ancestors
346 }
347
348 pub fn collect_inline_roots_in_range(&self, start_node: usize, end_node: usize) -> Vec<usize> {
352 let (start_anchor, start_anon) = self.resolve_for_traversal(start_node);
354 let (end_anchor, end_anon) = self.resolve_for_traversal(end_node);
355
356 if start_anon.is_some() && end_anon.is_some() && start_anchor == end_anchor {
358 return self.collect_anonymous_siblings(start_anchor, start_node, end_node);
359 }
360
361 let (first_anchor, first_anon, last_anchor, last_anon) = match self
363 .compare_document_order(start_anchor, end_anchor)
364 {
365 Ordering::Less | Ordering::Equal => (start_anchor, start_anon, end_anchor, end_anon),
366 Ordering::Greater => (end_anchor, end_anon, start_anchor, start_anon),
367 };
368
369 let mut result = Vec::new();
370 let mut found_first = false;
371
372 for node_id in TreeTraverser::new(self) {
374 if !found_first {
375 if node_id == first_anchor {
376 found_first = true;
377 if let Some(anon_id) = first_anon {
378 let stop_at = if first_anchor == last_anchor {
381 last_anon
383 } else {
384 Some(last_anchor)
386 };
387 self.collect_layout_children_inline_roots(
388 node_id,
389 Some(anon_id),
390 stop_at,
391 &mut result,
392 );
393 if result.last() == Some(&last_anchor)
395 || last_anon.is_some_and(|la| result.last() == Some(&la))
396 {
397 break;
398 }
399 continue;
400 }
401 }
402 }
403
404 if found_first {
405 if node_id == last_anchor {
406 if let Some(anon_id) = last_anon {
407 self.collect_layout_children_inline_roots(
409 node_id,
410 None,
411 Some(anon_id),
412 &mut result,
413 );
414 if !result.contains(&anon_id) {
416 result.push(anon_id);
417 }
418 } else {
419 let node = &self.nodes[node_id];
421 if node.flags.is_inline_root() && !result.contains(&node_id) {
422 result.push(node_id);
423 }
424 }
425 break;
426 }
427
428 let node = &self.nodes[node_id];
429 if node.flags.is_inline_root() && !result.contains(&node_id) {
430 result.push(node_id);
431 } else {
432 self.collect_layout_children_inline_roots(
435 node_id,
436 None,
437 Some(last_anchor),
438 &mut result,
439 );
440 }
441 }
442 }
443
444 result
445 }
446
447 fn resolve_for_traversal(&self, node_id: usize) -> (usize, Option<usize>) {
451 let node = &self.nodes[node_id];
452 if node.is_anonymous() {
453 (node.parent.unwrap_or(node_id), Some(node_id))
454 } else {
455 (node_id, None)
456 }
457 }
458
459 fn collect_anonymous_siblings(&self, parent_id: usize, start: usize, end: usize) -> Vec<usize> {
462 let parent = &self.nodes[parent_id];
463 let layout_children = parent.layout_children.borrow();
464 let Some(children) = layout_children.as_ref() else {
465 return Vec::new();
466 };
467
468 let start_idx = children.iter().position(|&id| id == start);
469 let end_idx = children.iter().position(|&id| id == end);
470
471 let (first_idx, last_idx) = match (start_idx, end_idx) {
472 (Some(s), Some(e)) if s <= e => (s, e),
473 (Some(s), Some(e)) => (e, s),
474 _ => return Vec::new(),
475 };
476
477 let mut result = Vec::new();
478 for &child_id in &children[first_idx..=last_idx] {
479 let child = &self.nodes[child_id];
480 if child.flags.is_inline_root() {
481 result.push(child_id);
482 } else {
483 self.collect_all_inline_roots_in_subtree(child_id, &mut result);
485 }
486 }
487 result
488 }
489
490 fn collect_all_inline_roots_in_subtree(&self, node_id: usize, result: &mut Vec<usize>) {
492 let node = &self.nodes[node_id];
493 let layout_children = node.layout_children.borrow();
494 let Some(children) = layout_children.as_ref() else {
495 return;
496 };
497
498 for &child_id in children.iter() {
499 let child = &self.nodes[child_id];
500 if child.flags.is_inline_root() {
501 result.push(child_id);
502 } else {
503 self.collect_all_inline_roots_in_subtree(child_id, result);
505 }
506 }
507 }
508
509 fn collect_layout_children_inline_roots(
513 &self,
514 parent_id: usize,
515 from: Option<usize>,
516 until: Option<usize>,
517 result: &mut Vec<usize>,
518 ) {
519 let parent = &self.nodes[parent_id];
520 let layout_children = parent.layout_children.borrow();
521 let Some(children) = layout_children.as_ref() else {
522 return;
523 };
524
525 let mut collecting = from.is_none(); for &child_id in children.iter() {
527 if from == Some(child_id) {
528 collecting = true;
529 }
530 if collecting {
531 if let Some(until_id) = until {
533 if self.is_ancestor_of(child_id, until_id) {
534 break;
535 }
536 }
537 if until == Some(child_id) {
539 break;
540 }
541 let child = &self.nodes[child_id];
542 if child.flags.is_inline_root() {
543 result.push(child_id);
544 } else {
545 self.collect_all_inline_roots_in_subtree(child_id, result);
547 }
548 }
549 }
550 }
551
552 fn is_ancestor_of(&self, ancestor_id: usize, descendant_id: usize) -> bool {
554 let mut current = descendant_id;
555 while let Some(parent) = self.nodes[current].parent {
556 if parent == ancestor_id {
557 return true;
558 }
559 current = parent;
560 }
561 false
562 }
563}