Index: trunk/extensions/VisualEditor/modules/es/es.TransactionProcessor.js |
— | — | @@ -262,10 +262,32 @@ |
263 | 263 | scope; |
264 | 264 | |
265 | 265 | node = this.model.getNodeFromOffset( this.cursor ); |
| 266 | + |
| 267 | + // Shortcut 1: we're inserting content. We don't need to bother with any structural stuff |
| 268 | + if ( !es.DocumentModel.containsElementData( op.data ) ) { |
| 269 | + // TODO should we check whether we're at a structural offset, and throw an exception |
| 270 | + // if that's the case? Or can we assume that the transaction is valid at this point? |
| 271 | + |
| 272 | + // Insert data into linear model |
| 273 | + es.insertIntoArray( this.model.data, this.cursor, op.data ); |
| 274 | + this.applyAnnotations( this.cursor + op.data.length ); |
| 275 | + // Update the length of the containing node |
| 276 | + node.adjustContentLength( op.data.length, true ); |
| 277 | + node.emit( 'update', this.cursor - offset ); |
| 278 | + // Move the cursor |
| 279 | + this.cursor += op.data.length; |
| 280 | + // All done |
| 281 | + return; |
| 282 | + } |
| 283 | + |
| 284 | + // Determine the scope of the inserted data. If the data is an enclosed piece of structure, |
| 285 | + // this will return node. Otherwise, the data closes one or more nodes, and this will return |
| 286 | + // the first ancestor of node that isn't closed, which is the node that will contain the |
| 287 | + // inserted data entirely. |
266 | 288 | scope = this.getScope( node, op.data ); |
267 | | - // We can take a shortcut if we're inserting an enclosed piece of structural data at a structural offset |
268 | | - // that isn't at the end of the document. Check for scope == node to ensure the inserted data doesn't try |
269 | | - // to close its containing element |
| 289 | + |
| 290 | + // Shortcut 2: we're inserting an enclosed piece of structural data at a structural offset |
| 291 | + // that isn't the end of the document. |
270 | 292 | if ( es.DocumentModel.isStructuralOffset( this.model.data, this.cursor ) && this.cursor != this.model.data.length |
271 | 293 | && scope == node |
272 | 294 | ) { |
— | — | @@ -277,30 +299,22 @@ |
278 | 300 | index = node.getIndexFromOffset( this.cursor - offset ); |
279 | 301 | this.rebuildNodes( op.data, null, node, index ); |
280 | 302 | } else { |
| 303 | + // This is the non-shortcut case |
| 304 | + |
281 | 305 | // Rebuild scope, which is the node that encloses everything we might have to rebuild |
282 | 306 | node = scope; |
283 | 307 | offset = this.model.getOffsetFromNode( node ); |
284 | | - if ( es.DocumentModel.containsElementData( op.data ) ) { |
285 | | - // Perform insert on linear data model |
286 | | - es.insertIntoArray( this.model.data, this.cursor, op.data ); |
287 | | - this.applyAnnotations( this.cursor + op.data.length ); |
288 | | - // Synchronize model tree |
289 | | - if ( offset === -1 ) { |
290 | | - throw 'Invalid offset error. Node is not in model tree'; |
291 | | - } |
292 | | - this.rebuildNodes( |
293 | | - this.model.data.slice( offset, offset + node.getElementLength() + op.data.length ), |
294 | | - [node] |
295 | | - ); |
296 | | - } else { |
297 | | - // Perform insert on linear data model |
298 | | - // TODO this is duplicated from above |
299 | | - es.insertIntoArray( this.model.data, this.cursor, op.data ); |
300 | | - this.applyAnnotations( this.cursor + op.data.length ); |
301 | | - // Update model tree |
302 | | - node.adjustContentLength( op.data.length, true ); |
303 | | - node.emit( 'update', this.cursor - offset ); |
| 308 | + if ( offset === -1 ) { |
| 309 | + throw 'Invalid offset error. Node is not in model tree'; |
304 | 310 | } |
| 311 | + // Perform insert on linear data model |
| 312 | + es.insertIntoArray( this.model.data, this.cursor, op.data ); |
| 313 | + this.applyAnnotations( this.cursor + op.data.length ); |
| 314 | + // Synchronize model tree |
| 315 | + this.rebuildNodes( |
| 316 | + this.model.data.slice( offset, offset + node.getElementLength() + op.data.length ), |
| 317 | + [node] |
| 318 | + ); |
305 | 319 | } |
306 | 320 | this.cursor += op.data.length; |
307 | 321 | }; |