@@ -78,98 +78,116 @@ pub enum Patch {
7878}
7979
8080impl Patch {
81- pub fn is_mutation ( & self ) -> bool {
82- match self {
83- Self :: Move ( _ ) => false ,
84- _ => true ,
85- }
86- }
87-
88- pub fn apply < B > ( self , doc : & mut B , stack : & mut Vec < NodeRef > )
81+ /// Applies this patch to `doc` using `stack`.
82+ ///
83+ /// If this patch will result in a change to the underlying document, the affected [NodeRef] is returned, else `None`.
84+ ///
85+ /// For modifications, the affected node is the node to which the change applies, but for additions/removals, the affected
86+ /// node is the parent to which the child was added/removed. This can be used to associate callbacks to nodes in the document
87+ /// and be able to properly handle changes to them.
88+ pub fn apply < B > ( self , doc : & mut B , stack : & mut Vec < NodeRef > ) -> Option < NodeRef >
8989 where
9090 B : DocumentBuilder ,
9191 {
9292 match self {
93- Self :: InsertBefore { before, node } => {
94- doc. insert_before ( node, before) ;
95- }
96- Self :: InsertAfter { after, node } => {
97- doc. insert_after ( node, after) ;
98- }
93+ Self :: InsertBefore { before, node } => Some ( doc. insert_before ( node, before) ) ,
94+ Self :: InsertAfter { after, node } => Some ( doc. insert_after ( node, after) ) ,
9995 Self :: Create { node } => {
100- stack. push ( doc. push_node ( node) ) ;
96+ let node = doc. push_node ( node) ;
97+ stack. push ( node) ;
98+ Some ( node)
10199 }
102100 Self :: CreateAndMoveTo { node } => {
103101 let node = doc. push_node ( node) ;
104102 stack. push ( node) ;
105103 doc. set_insertion_point ( node) ;
104+ Some ( node)
106105 }
107106 Self :: PushCurrent => {
108107 stack. push ( doc. insertion_point ( ) ) ;
108+ None
109109 }
110110 Self :: Push ( node) => {
111111 stack. push ( node) ;
112+ None
112113 }
113114 Self :: Pop => {
114115 stack. pop ( ) . unwrap ( ) ;
116+ None
115117 }
116118 Self :: Attach => {
117119 let child = stack. pop ( ) . unwrap ( ) ;
118120 let parent = stack. pop ( ) . unwrap ( ) ;
119121 doc. set_insertion_point ( parent) ;
120122 doc. attach_node ( child) ;
121123 stack. push ( parent) ;
124+ Some ( parent)
122125 }
123- Self :: Append { node } => {
124- doc. append ( node) ;
125- }
126+ Self :: Append { node } => Some ( doc. append ( node) ) ,
126127 Self :: AppendTo { parent, node } => {
127128 doc. append_child ( parent, node) ;
129+ Some ( parent)
128130 }
129131 Self :: AppendAfter { after } => {
130132 let node = stack. pop ( ) . unwrap ( ) ;
131133 let d = doc. document_mut ( ) ;
132134 d. insert_after ( node, after) ;
135+ d. parent ( after)
133136 }
134137 Self :: Remove { node } => {
138+ let parent = doc. document_mut ( ) . parent ( node) ;
135139 doc. remove ( node) ;
140+ parent
141+ }
142+ Self :: Replace { node, replacement } => {
143+ doc. replace ( node, replacement) ;
144+ Some ( node)
136145 }
137- Self :: Replace { node, replacement } => doc. replace ( node, replacement) ,
138146 Self :: AddAttribute { name, value } => {
139147 doc. set_attribute ( name, value) ;
148+ Some ( doc. insertion_point ( ) )
140149 }
141150 Self :: AddAttributeTo { node, name, value } => {
142151 let mut guard = doc. insert_guard ( ) ;
143152 guard. set_insertion_point ( node) ;
144153 guard. set_attribute ( name, value) ;
154+ Some ( node)
145155 }
146156 Self :: UpdateAttribute { node, name, value } => {
147157 let mut guard = doc. insert_guard ( ) ;
148158 guard. set_insertion_point ( node) ;
149159 guard. set_attribute ( name, value) ;
160+ Some ( node)
150161 }
151162 Self :: RemoveAttributeByName { node, name } => {
152163 let mut guard = doc. insert_guard ( ) ;
153164 guard. set_insertion_point ( node) ;
154165 guard. remove_attribute ( name) ;
166+ Some ( node)
155167 }
156168 Self :: Move ( MoveTo :: Node ( node) ) => {
157169 doc. set_insertion_point ( node) ;
170+ None
158171 }
159172 Self :: Move ( MoveTo :: Parent ) => {
160173 doc. set_insertion_point_to_parent ( ) ;
174+ None
161175 }
162176 Self :: Move ( MoveTo :: Child ( n) ) => {
163177 doc. set_insertion_point_to_child ( n as usize ) ;
178+ None
164179 }
165180 Self :: Move ( MoveTo :: ReverseChild ( n) ) => {
166181 doc. set_insertion_point_to_child_reverse ( n as usize ) ;
182+ None
167183 }
168184 Self :: Move ( MoveTo :: Sibling ( n) ) => {
169185 doc. set_insertion_point_to_sibling ( n as usize ) ;
186+ None
170187 }
171188 Self :: Move ( MoveTo :: ReverseSibling ( n) ) => {
172189 doc. set_insertion_point_to_sibling_reverse ( n as usize ) ;
190+ None
173191 }
174192 }
175193 }
0 commit comments