tiptap_rusty_parser/
query.rs1use crate::node::Node;
8
9impl Node {
10 pub fn walk(&self, f: &mut impl FnMut(&Node)) {
20 f(self);
21 if let Some(children) = &self.content {
22 for child in children {
23 child.walk(f);
24 }
25 }
26 }
27
28 pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Node)) {
30 f(self);
31 if let Some(children) = &mut self.content {
32 for child in children {
33 child.walk_mut(f);
34 }
35 }
36 }
37
38 pub fn find(&self, mut pred: impl FnMut(&Node) -> bool) -> Option<&Node> {
40 self.descendants().find(|n| pred(n))
41 }
42
43 pub fn find_mut(&mut self, mut pred: impl FnMut(&Node) -> bool) -> Option<&mut Node> {
45 if pred(self) {
46 return Some(self);
47 }
48 let children = self.content.as_mut()?;
49 for child in children {
50 if let Some(found) = child.find_mut(&mut pred) {
51 return Some(found);
52 }
53 }
54 None
55 }
56
57 pub fn find_all(&self, mut pred: impl FnMut(&Node) -> bool) -> Vec<&Node> {
59 self.descendants().filter(|n| pred(n)).collect()
60 }
61
62 pub fn find_all_mut(&mut self, pred: &mut impl FnMut(&Node) -> bool) -> Vec<&mut Node> {
64 let mut out = Vec::new();
65 self.collect_mut(pred, &mut out);
66 out
67 }
68
69 fn collect_mut<'a>(
70 &'a mut self,
71 pred: &mut impl FnMut(&Node) -> bool,
72 out: &mut Vec<&'a mut Node>,
73 ) {
74 let matched = pred(self);
75 let children_ptr = self.content.as_mut().map(|c| c as *mut Vec<Node>);
77 if matched {
78 out.push(self);
79 }
80 if let Some(ptr) = children_ptr {
83 let children = unsafe { &mut *ptr };
84 for child in children {
85 child.collect_mut(pred, out);
86 }
87 }
88 }
89
90 #[inline]
92 pub fn descendants(&self) -> Descendants<'_> {
93 Descendants { stack: vec![self] }
94 }
95}
96
97pub struct Descendants<'a> {
99 stack: Vec<&'a Node>,
100}
101
102impl<'a> Iterator for Descendants<'a> {
103 type Item = &'a Node;
104
105 fn next(&mut self) -> Option<&'a Node> {
106 let node = self.stack.pop()?;
107 if let Some(children) = &node.content {
108 self.stack.extend(children.iter().rev());
110 }
111 Some(node)
112 }
113}