@@ -7,6 +7,7 @@ use ownable::{IntoOwned, ToBorrowed, ToOwned};
77use serde:: Serialize ;
88use std:: borrow:: Cow ;
99use std:: default:: Default ;
10+ use std:: ops:: ControlFlow ;
1011
1112/// Normal: `<div></div>` or Void: `<meta/>`and `<meta>`
1213#[ derive( Debug , Clone , Serialize , PartialEq ) ]
@@ -68,3 +69,66 @@ impl Default for Element<'static> {
6869 }
6970 }
7071}
72+
73+ pub enum WalkResult {
74+ Continue ,
75+ Skip ,
76+ Break ,
77+ }
78+
79+ impl < ' a > Element < ' a > {
80+ #[ inline]
81+ pub fn for_each_element < F > ( & mut self , mut f : F )
82+ where
83+ F : FnMut ( & mut Element < ' a > ) -> WalkResult ,
84+ {
85+ let _ = for_each_element ( self . children . as_mut_slice ( ) , & mut f) ;
86+ }
87+
88+ #[ inline]
89+ pub async fn for_each_element_async < F > ( & mut self , mut f : F )
90+ where
91+ F : AsyncFnMut ( & mut Element < ' a > ) -> WalkResult ,
92+ {
93+ let _ = for_each_element_async ( self . children . as_mut_slice ( ) , & mut f) . await ;
94+ }
95+ }
96+
97+ pub ( crate ) fn for_each_element < ' a , F > ( tree : & mut [ Node < ' a > ] , f : & mut F ) -> ControlFlow < ( ) , ( ) >
98+ where
99+ F : FnMut ( & mut Element < ' a > ) -> WalkResult ,
100+ {
101+ for n in tree {
102+ if let Node :: Element ( e) = n {
103+ match f ( e) {
104+ WalkResult :: Continue => {
105+ for_each_element ( e. children . as_mut_slice ( ) , f) ?;
106+ }
107+ WalkResult :: Skip => ( ) ,
108+ WalkResult :: Break => return ControlFlow :: Break ( ( ) ) ,
109+ }
110+ }
111+ }
112+ ControlFlow :: Continue ( ( ) )
113+ }
114+
115+ pub ( crate ) async fn for_each_element_async < ' a , F > (
116+ tree : & mut [ Node < ' a > ] ,
117+ f : & mut F ,
118+ ) -> ControlFlow < ( ) , ( ) >
119+ where
120+ F : AsyncFnMut ( & mut Element < ' a > ) -> WalkResult ,
121+ {
122+ for n in tree {
123+ if let Node :: Element ( e) = n {
124+ match f ( e) . await {
125+ WalkResult :: Continue => {
126+ Box :: pin ( for_each_element_async ( e. children . as_mut_slice ( ) , f) ) . await ?;
127+ }
128+ WalkResult :: Skip => ( ) ,
129+ WalkResult :: Break => return ControlFlow :: Break ( ( ) ) ,
130+ }
131+ }
132+ }
133+ ControlFlow :: Continue ( ( ) )
134+ }
0 commit comments