Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.Transaction.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Transaction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Transaction on ve.dm.ElementLinearData, preserving ve.dm.Document tree validity
9 *
10 * A transaction represents a mapping on ve.dm.ElementLinearData, from one state (the start
11 * state) to another (the end state). The transaction is guaranteed not to break tree validity:
12 * if the start state represents a syntactically valid ve.dm.Document tree (without unbalanced
13 * tags, bare listItems, bare table cells etc), then the end state tree must be syntactically
14 * valid too.
15 *
16 * A transaction is comprised of a list of operations, which must preserve tree validity as a
17 * whole, though each individual operation may not. For example, a DivNode wrapping can be
18 * removed by one operation removing the 'div' and another removing the '/div'. The
19 * ve.dm.TransactionBuilder.static.newFrom* methods help build transactions that preserve tree validity.
20 *
21 * @class
22 * @constructor
23 * @param {Object[]} [operations] Operations preserving tree validity as a whole; default []
24 * @param {number|null} [authorId] Positive integer author ID; default null
25 */
26 ve.dm.Transaction = function VeDmTransaction( operations, authorId ) {
27 this.operations = operations || [];
28 // TODO: remove this backwards-incompatibility check
29 this.operations.forEach( ( op ) => {
30 if ( op.type && /meta/i.test( op.type ) ) {
31 throw new Error( 'Metadata ops are no longer supported' );
32 }
33 } );
34 this.applied = false;
35 this.authorId = authorId || null;
36 this.isReversed = false;
37 };
38
39 /* Inheritance */
40
41 OO.initClass( ve.dm.Transaction );
42
43 /* Static Properties */
44
45 /**
46 * Specification for how each type of operation should be reversed.
47 *
48 * This object maps operation types to objects, which map property names to reversal instructions.
49 * A reversal instruction is either a string (which means the value of that property should be used)
50 * or an object (which maps old values to new values). For instance, { from: 'to' }
51 * means that the .from property of the reversed operation should be set to the .to property of the
52 * original operation, and { method: { set: 'clear' } } means that if the .method property of
53 * the original operation was 'set', the reversed operation's .method property should be 'clear'.
54 *
55 * If a property's treatment isn't specified, its value is simply copied without modification.
56 * If an operation type's treatment isn't specified, all properties are copied without modification.
57 *
58 * @type {Object.<string,Object>}
59 */
60 ve.dm.Transaction.static.reversers = {
61 attribute: { from: 'to', to: 'from' }, // Swap .from with .to
62 replace: { // Swap .insert with .remove
63 insert: 'remove',
64 remove: 'insert'
65 }
66 };
67
68 /* Static Methods */
69
70 /**
71 * Deserialize a transaction from a JSONable object
72 *
73 * Values are either new or deep copied, so there is no reference into the serialized structure
74 *
75 * @param {Object|Array} data Transaction serialized as a JSONable object
76 * @return {ve.dm.Transaction} Deserialized transaction
77 */
78 ve.dm.Transaction.static.deserialize = function ( data ) {
79 function deminifyLinearData( element ) {
80 if ( typeof element === 'string' ) {
81 return element.split( '' );
82 }
83 // Else deep copy. For this plain, serializable array, stringify+parse profiles
84 // faster than ve.copy
85 return JSON.parse( JSON.stringify( element ) );
86 }
87
88 function deminify( op ) {
89 if ( typeof op === 'number' ) {
90 return { type: 'retain', length: op };
91 }
92 if ( Array.isArray( op ) ) {
93 return {
94 type: 'replace',
95 remove: deminifyLinearData( op[ 0 ] ),
96 insert: deminifyLinearData( op[ 1 ] )
97 };
98 }
99 // Else deep copy. For this plain, serializable array, stringify+parse profiles
100 // faster than ve.copy
101 return JSON.parse( JSON.stringify( op ) );
102 }
103
104 if ( Array.isArray( data ) ) {
105 return new ve.dm.Transaction(
106 data.map( deminify )
107 );
108 } else {
109 return new ve.dm.Transaction(
110 // operations
111 data.o.map( deminify ),
112 // authorId
113 data.a
114 );
115 }
116 };
117
118 /**
119 * Simpified comparison of linear data elements
120 *
121 * Identical to ve.dm.ElementLinearData.static.compareElementsUnannotated, but without
122 * the complex comparison of node elements that requires the model registry.
123 *
124 * For the purposes of translateOffset it is just sufficient that we catch obvious
125 * cases of annotations being set/clear.
126 *
127 * @param {Object|Array|string} a First element
128 * @param {Object|Array|string} b Second element
129 * @return {boolean} Elements are comparable
130 */
131 ve.dm.Transaction.static.compareElementsForTranslate = function ( a, b ) {
132 let aPlain = a,
133 bPlain = b;
134
135 if ( a === b ) {
136 return true;
137 }
138
139 if ( Array.isArray( a ) ) {
140 aPlain = a[ 0 ];
141 }
142 if ( Array.isArray( b ) ) {
143 bPlain = b[ 0 ];
144 }
145 if ( typeof aPlain === 'string' && typeof bPlain === 'string' ) {
146 return aPlain === bPlain;
147 }
148
149 if ( typeof a !== typeof b ) {
150 // Different types
151 return false;
152 }
153
154 // By this point, both must be objects, so must have equal types
155 if ( a.type !== b.type ) {
156 return false;
157 }
158
159 // Elements of the same type, consider them equal for the purpose of offset translation
160 return true;
161 };
162
163 /**
164 * Check if an operation only changes annotations
165 *
166 * @param {Object} op Operation object
167 * @return {boolean} Operation is annotation-only
168 */
169 ve.dm.Transaction.static.isAnnotationOnlyOperation = function ( op ) {
170 return op.type === 'replace' &&
171 op.insert.length === op.remove.length &&
172 op.insert.every( ( insert, j ) => ve.dm.Transaction.static.compareElementsForTranslate( insert, op.remove[ j ] ) );
173 };
174
175 /* Methods */
176
177 /**
178 * Serialize the transaction into a JSONable object
179 *
180 * Values are not necessarily deep copied
181 *
182 * @param {string} [key] Key in parent object
183 * @return {Object|Array} JSONable object
184 */
185 ve.dm.Transaction.prototype.toJSON = function () {
186 function isSingleCodePoint( x ) {
187 return typeof x === 'string' && x.length === 1;
188 }
189 function minifyLinearData( data ) {
190 if ( data.every( isSingleCodePoint ) ) {
191 return data.join( '' );
192 }
193 return data;
194 }
195
196 function minify( op ) {
197 if ( op.type === 'retain' ) {
198 return op.length;
199 }
200 if (
201 op.type === 'replace' &&
202 !op.insertedDataOffset &&
203 (
204 op.insertedDataLength === undefined ||
205 op.insertedDataLength === op.insert.length
206 )
207 ) {
208 return [ minifyLinearData( op.remove ), minifyLinearData( op.insert ) ];
209 }
210 return op;
211 }
212
213 const operations = this.operations.map( minify );
214
215 if ( this.authorId !== null ) {
216 return {
217 o: operations,
218 a: this.authorId
219 };
220 } else {
221 return operations;
222 }
223 };
224
225 // Deprecated alias
226 ve.dm.Transaction.prototype.serialize = ve.dm.Transaction.prototype.toJSON;
227
228 /**
229 * Push a retain operation
230 *
231 * @param {number} length Length > 0 of content data to retain
232 */
233 ve.dm.Transaction.prototype.pushRetainOp = function ( length ) {
234 this.operations.push( { type: 'retain', length: length } );
235 };
236
237 /**
238 * Build a replace operation
239 *
240 * The `insertedDataOffset` and `insertedDataLength` parameters indicate the intended insertion
241 * is wrapped with fixup data to preserve HTML validity. For instance, an intended table cell
242 * insertion may have been fixed up by wrapping inside a table row, table section and table.
243 *
244 * @param {Array} remove Data to remove
245 * @param {Array} insert Data to insert, possibly fixed up
246 * @param {number} [insertedDataOffset] Offset of intended insertion within fixed up data
247 * @param {number} [insertedDataLength] Length of intended insertion within fixed up data
248 */
249 ve.dm.Transaction.prototype.pushReplaceOp = function ( remove, insert, insertedDataOffset, insertedDataLength ) {
250 const op = { type: 'replace', remove: remove, insert: insert };
251 if ( insertedDataOffset !== undefined && insertedDataLength !== undefined ) {
252 op.insertedDataOffset = insertedDataOffset;
253 op.insertedDataLength = insertedDataLength;
254 }
255 this.operations.push( op );
256 };
257
258 /**
259 * Build an attribute operation
260 *
261 * @param {string} key Name of attribute to change
262 * @param {any} from Value to change attribute from, or undefined if not previously set
263 * @param {any} to Value to change attribute to, or undefined to remove
264 */
265 ve.dm.Transaction.prototype.pushAttributeOp = function ( key, from, to ) {
266 this.operations.push( { type: 'attribute', key: key, from: from, to: to } );
267 };
268
269 /**
270 * Create a clone of this transaction.
271 *
272 * The returned transaction will be exactly the same as this one, except that its 'applied' flag
273 * will be cleared. This means that if a transaction has already been committed, it will still
274 * be possible to commit the clone. This is used for redoing transactions that were undone.
275 *
276 * @return {ve.dm.Transaction} Clone of this transaction
277 */
278 ve.dm.Transaction.prototype.clone = function () {
279 return new this.constructor(
280 // For this plain, serializable array, stringify+parse profiles faster than ve.copy
281 JSON.parse( JSON.stringify( this.operations ) ),
282 this.authorId
283 );
284 };
285
286 /**
287 * Create a reversed version of this transaction.
288 *
289 * The returned transaction will be the same as this one but with all operations reversed. This
290 * means that applying the original transaction and then applying the reversed transaction will
291 * result in no net changes. This is used to undo transactions.
292 *
293 * @return {ve.dm.Transaction} Reverse of this transaction
294 */
295 ve.dm.Transaction.prototype.reversed = function () {
296 const tx = new this.constructor();
297
298 tx.isReversed = !this.isReversed;
299 for ( let i = 0, len = this.operations.length; i < len; i++ ) {
300 const op = this.operations[ i ];
301 const newOp = ve.copy( op );
302 const reverse = this.constructor.static.reversers[ op.type ] || {};
303 for ( const prop in reverse ) {
304 if ( typeof reverse[ prop ] === 'string' ) {
305 newOp[ prop ] = op[ reverse[ prop ] ];
306 } else {
307 newOp[ prop ] = reverse[ prop ][ op[ prop ] ];
308 }
309 }
310 tx.operations.push( newOp );
311 }
312 tx.authorId = this.authorId;
313 return tx;
314 };
315
316 /**
317 * Check if the transaction would make any actual changes if processed.
318 *
319 * There may be more sophisticated checks that can be done, like looking for things being replaced
320 * with identical content, but such transactions probably should not be created in the first place.
321 *
322 * @return {boolean} Transaction is no-op
323 */
324 ve.dm.Transaction.prototype.isNoOp = function () {
325 if ( this.operations.length === 0 ) {
326 return true;
327 }
328 if ( this.operations.length === 1 ) {
329 return this.operations[ 0 ].type === 'retain';
330 }
331 return false;
332 };
333
334 /**
335 * Get all operations.
336 *
337 * @return {Object[]} List of operations
338 */
339 ve.dm.Transaction.prototype.getOperations = function () {
340 return this.operations;
341 };
342
343 /**
344 * Check if the transaction has any operations with a certain type.
345 *
346 * @param {string} type Operation type
347 * @return {boolean} Has operations of a given type
348 */
349 ve.dm.Transaction.prototype.hasOperationWithType = function ( type ) {
350 for ( let i = 0, len = this.operations.length; i < len; i++ ) {
351 if ( this.operations[ i ].type === type ) {
352 return true;
353 }
354 }
355 return false;
356 };
357
358 /**
359 * Check if the transaction has any content data operations, such as insertion or deletion.
360 *
361 * @return {boolean} Has content data operations
362 */
363 ve.dm.Transaction.prototype.hasContentDataOperations = function () {
364 return this.hasOperationWithType( 'replace' );
365 };
366
367 /**
368 * Check if the transaction has any element attribute operations.
369 *
370 * @return {boolean} Has element attribute operations
371 */
372 ve.dm.Transaction.prototype.hasElementAttributeOperations = function () {
373 return this.hasOperationWithType( 'attribute' );
374 };
375
376 /**
377 * Check whether the transaction has already been applied.
378 *
379 * @return {boolean}
380 */
381 ve.dm.Transaction.prototype.hasBeenApplied = function () {
382 return this.applied;
383 };
384
385 /**
386 * Mark the transaction as having been applied.
387 *
388 * Should only be called after committing the transaction.
389 *
390 * @see ve.dm.Transaction#hasBeenApplied
391 */
392 ve.dm.Transaction.prototype.markAsApplied = function () {
393 this.applied = true;
394 };
395
396 /**
397 * Translate an offset based on a transaction.
398 *
399 * This is useful when you want to anticipate what an offset will be after a transaction is
400 * processed.
401 *
402 * @param {number} offset Offset in the linear model before the transaction has been processed
403 * @param {boolean} [excludeInsertion] Map the offset immediately before an insertion to
404 * right before the insertion rather than right after
405 * @return {number} Translated offset, as it will be after processing transaction
406 */
407 ve.dm.Transaction.prototype.translateOffset = function ( offset, excludeInsertion ) {
408 let cursor = 0,
409 adjustment = 0;
410
411 for ( let i = 0; i < this.operations.length; i++ ) {
412 const op = this.operations[ i ];
413 // If a 'replace' only changes annotations, treat it like a 'retain'
414 // This imitates the behaviour of the old 'annotate' operation type.
415 if ( op.type === 'retain' || ve.dm.Transaction.static.isAnnotationOnlyOperation( op ) ) {
416 const retainLength = op.type === 'retain' ? op.length : op.remove.length;
417 if ( offset >= cursor && offset < cursor + retainLength ) {
418 return offset + adjustment;
419 }
420 cursor += retainLength;
421 continue;
422 } else if ( op.type === 'replace' ) {
423 const insertLength = op.insert.length;
424 const removeLength = op.remove.length;
425 const prevAdjustment = adjustment;
426 adjustment += insertLength - removeLength;
427 if ( offset === cursor + removeLength ) {
428 // Offset points to right after the removal or right before the insertion
429 if ( excludeInsertion && insertLength > removeLength ) {
430 // Translate it to before the insertion
431 return offset + adjustment - insertLength + removeLength;
432 } else {
433 // Translate it to after the removal/insertion
434 return offset + adjustment;
435 }
436 } else if ( offset === cursor ) {
437 // The offset points to right before the removal or replacement
438 if ( insertLength === 0 ) {
439 // Translate it to after the removal
440 return cursor + removeLength + adjustment;
441 } else {
442 // Translate it to before the replacement
443 // To translate this correctly, we have to use adjustment as it was before
444 // we adjusted it for this replacement
445 return cursor + prevAdjustment;
446 }
447 } else if ( offset > cursor && offset < cursor + removeLength ) {
448 // The offset points inside of the removal
449 // Translate it to after the removal
450 return cursor + removeLength + adjustment;
451 }
452 cursor += removeLength;
453 }
454 }
455 return offset + adjustment;
456 };
457
458 /**
459 * Translate a range based on the transaction, with grow/shrink preference at changes
460 *
461 * This is useful when you want to anticipate what a selection will be after a transaction is
462 * processed.
463 *
464 * @see #translateOffset
465 * @param {ve.Range} range Range in the linear model before the transaction has been processed
466 * @param {boolean} [excludeInsertion] Do not grow the range to cover insertions
467 * on the boundaries of the range.
468 * @return {ve.Range} Translated range, as it will be after processing transaction
469 */
470 ve.dm.Transaction.prototype.translateRange = function ( range, excludeInsertion ) {
471 const start = this.translateOffset( range.start, !excludeInsertion ),
472 end = this.translateOffset( range.end, excludeInsertion );
473 return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end );
474 };
475
476 /**
477 * Translate a range based on the transaction, with bias depending on author ID comparison
478 *
479 * Biases backward if !authorId || !this.authorId || authorId <= this.authorId
480 *
481 * @see #translateOffset
482 * @param {ve.Range} range Range in the linear model before the transaction has been processed
483 * @param {number} [authorId] Author ID of the range
484 * @return {ve.Range} Translated range, as it will be after processing transaction
485 */
486 ve.dm.Transaction.prototype.translateRangeWithAuthor = function ( range, authorId ) {
487 const backward = !this.authorId || !authorId || authorId < this.authorId,
488 start = this.translateOffset( range.start, backward ),
489 end = this.translateOffset( range.end, backward );
490 return range.isBackwards() ? new ve.Range( end, start ) : new ve.Range( start, end );
491 };
492
493 /**
494 * Get the range that covers modifications made by this transaction.
495 *
496 * In the case of insertions, the range covers content the user intended to insert.
497 * It ignores wrappers added by ve.dm.Document#fixUpInsertion.
498 *
499 * The returned range is relative to the new state, after the transaction is applied. So for a
500 * simple insertion transaction, the range will cover the newly inserted data, and for a simple
501 * removal transaction it will be a zero-length range.
502 *
503 * @param {ve.dm.Document} doc The document in the state to which the transaction applies
504 * @param {Object} [options] Options
505 * @param {boolean} [options.includeInternalList] Include changes within the internal list
506 * @param {boolean} [options.excludeAnnotations] Exclude annotation-only changes
507 * @param {boolean} [options.excludeAttributes] Exclude attribute changes
508 * @return {ve.Range|null} Range covering modifications, or null for a no-op transaction
509 */
510 ve.dm.Transaction.prototype.getModifiedRange = function ( doc, options ) {
511 let docEndOffset = doc.data.getLength(),
512 oldOffset = 0,
513 offset = 0;
514
515 if ( typeof options === 'boolean' ) {
516 // Backwards compatibility
517 options = { includeInternalList: options };
518 } else {
519 options = options || {};
520 }
521
522 if ( !options.includeInternalList ) {
523 const internalListNode = doc.getInternalList().getListNode();
524 if ( internalListNode ) {
525 docEndOffset = internalListNode.getOuterRange().start;
526 }
527 }
528
529 let start, end;
530 opLoop:
531 for ( let i = 0, len = this.operations.length; i < len; i++ ) {
532 const op = this.operations[ i ];
533 switch ( op.type ) {
534 case 'retain':
535 if ( oldOffset + op.length > docEndOffset ) {
536 break opLoop;
537 }
538 offset += op.length;
539 oldOffset += op.length;
540 break;
541
542 case 'attribute':
543 if ( options.excludeAttributes ) {
544 break;
545 }
546 if ( start === undefined ) {
547 start = offset;
548 }
549 // Attribute changes modify the element to their right but don't move the cursor
550 end = offset + 1;
551 break;
552
553 default:
554 if ( options.excludeAnnotations && this.constructor.static.isAnnotationOnlyOperation( op ) ) {
555 // Treat as 'retain'
556 if ( oldOffset + op.length > docEndOffset ) {
557 break opLoop;
558 }
559 offset += op.length;
560 oldOffset += op.length;
561 break;
562 }
563 if ( start === undefined ) {
564 // This is the first non-retain operation, set start to right before it
565 start = offset + ( op.insertedDataOffset || 0 );
566 }
567 if ( op.type === 'replace' ) {
568 offset += op.insert.length;
569 oldOffset += op.remove.length;
570 }
571
572 // Set end, so it'll end up being right after the last non-retain operation
573 if ( op.insertedDataLength ) {
574 end = start + op.insertedDataLength;
575 } else {
576 end = offset;
577 }
578 break;
579 }
580 }
581 if ( start === undefined || end === undefined ) {
582 // No-op transaction
583 return null;
584 }
585 return new ve.Range( start, end );
586 };
587
588 /**
589 * @typedef {Object} RangeAndLengthDiff
590 * @memberof ve.dm.Transaction
591 * @property {number} [start] Start offset of the active range
592 * @property {number} [end] End offset of the active range
593 * @property {number} [startOpIndex] Start operation index of the active range
594 * @property {number} [endOpIndex] End operation index of the active range
595 * @property {number} diff Length change the transaction causes
596 */
597
598 /**
599 * Calculate active range and length change
600 *
601 * @return {ve.dm.Transaction.RangeAndLengthDiff} Active range and length change
602 */
603 ve.dm.Transaction.prototype.getActiveRangeAndLengthDiff = function () {
604 let offset = 0,
605 diff = 0;
606
607 let start, end, startOpIndex, endOpIndex;
608 for ( let i = 0, len = this.operations.length; i < len; i++ ) {
609 const op = this.operations[ i ];
610 const active = op.type !== 'retain';
611 // Place start marker
612 if ( active && start === undefined ) {
613 start = offset;
614 startOpIndex = i;
615 }
616 // Adjust offset and diff
617 if ( op.type === 'retain' ) {
618 offset += op.length;
619 } else if ( op.type === 'replace' ) {
620 offset += op.remove.length;
621 diff += op.insert.length - op.remove.length;
622 }
623 // Place/move end marker
624 if ( op.type === 'attribute' || op.type === 'replaceMetadata' ) {
625 // Op with length 0 but that effectively modifies 1 position
626 end = offset + 1;
627 endOpIndex = i + 1;
628 } else if ( active ) {
629 end = offset;
630 endOpIndex = i + 1;
631 }
632 }
633 return {
634 start: start,
635 end: end,
636 startOpIndex: startOpIndex,
637 endOpIndex: endOpIndex,
638 diff: diff
639 };
640 };
641
642 // TODO: Use adjustRetain to replace ve.dm.TransactionBuilder#pushRetain
643
644 /**
645 * Adjust (in place) the retain length at the start/end of an operations list
646 *
647 * @param {string} place Where to adjust, start|end
648 * @param {number} diff Adjustment; must not cause negative retain length
649 */
650 ve.dm.Transaction.prototype.adjustRetain = function ( place, diff ) {
651 if ( diff === 0 ) {
652 return;
653 }
654
655 const start = place === 'start',
656 ops = this.operations;
657 let i = start ? 0 : ops.length - 1;
658
659 if ( !start && ops[ i ] && ops[ i ].type === 'retainMetadata' ) {
660 i = ops.length - 2;
661 }
662 if ( ops[ i ] && ops[ i ].type === 'retain' ) {
663 ops[ i ].length += diff;
664 if ( ops[ i ].length < 0 ) {
665 throw new Error( 'Negative retain length' );
666 } else if ( ops[ i ].length === 0 ) {
667 ops.splice( i, 1 );
668 }
669 return;
670 }
671 if ( diff < 0 ) {
672 throw new Error( 'Negative retain length' );
673 }
674 ops.splice( start ? 0 : ops.length, 0, { type: 'retain', length: diff } );
675 };
676
677 /**
678 * Split (in place) the retain at the given offset, if any
679 *
680 * Offset cannot be in the interior of a replace operation (i.e. the interior of its removed content).
681 *
682 * @param {number} offset The offset at which to split
683 * @return {number} Index in operations starting at offset
684 * @throws {Error} Offset is in the interior of a replace operation
685 */
686 ve.dm.Transaction.prototype.trySplit = function ( offset ) {
687 let n = 0;
688 let i, iLen;
689 for ( i = 0, iLen = this.operations.length; i < iLen; i++ ) {
690 const op = this.operations[ i ];
691 const opLen = ( op.type === 'retain' ? op.length : op.type === 'replace' ? op.remove.length : 0 );
692 if ( n + opLen <= offset ) {
693 n += opLen;
694 continue;
695 }
696 if ( n === offset ) {
697 // At start edge; no need to split
698 return i;
699 }
700 // Else n < offset < n + opLen
701 if ( op.type !== 'retain' ) {
702 throw new Error( 'Cannot split operation of type ' + op.type );
703 }
704 // Split the retain operation
705 op.length -= n + opLen - offset;
706 this.operations.splice( i + 1, 0, { type: 'retain', length: n + opLen - offset } );
707 return i + 1;
708 }
709 if ( n === offset ) {
710 return iLen + 1;
711 }
712 throw new Error( 'Offset beyond end of transaction' );
713 };
714
715 /**
716 * Unsplit (in place) the two operations around the given index, if possible
717 *
718 * @param {number} index The index at which to unsplit
719 */
720 ve.dm.Transaction.prototype.tryUnsplit = function ( index ) {
721 const op1 = this.operations[ index - 1 ],
722 op2 = this.operations[ index ];
723 if ( !op1 || !op2 || op1.type !== op2.type ) {
724 return;
725 }
726 if ( op1.type === 'retain' ) {
727 op1.length += op2.length;
728 this.operations.splice( index, 1 );
729 } else if ( op1.type === 'replace' ) {
730 ve.batchSplice( op1.remove, op1.remove.length, 0, op2.remove );
731 ve.batchSplice( op1.insert, op1.insert.length, 0, op2.insert );
732 this.operations.splice( index, 1 );
733 }
734 };
735
736 /**
737 * Insert (in place) operations at the given offset
738 *
739 * Merges into existing operations where possible. Offset cannot be in the interior of a replace
740 * operation (i.e. the interior of its removed content).
741 *
742 * @param {number} offset The offset at which to insert
743 * @param {Object[]} operations The operations to insert
744 * @throws {Error} Offset is in the interior of a replace operation
745 */
746 ve.dm.Transaction.prototype.insertOperations = function ( offset, operations ) {
747 if ( operations.length === 0 ) {
748 return;
749 }
750 const opIndex = this.trySplit( offset );
751 ve.batchSplice( this.operations, opIndex, 0, ve.copy( operations ) );
752 this.tryUnsplit( opIndex + operations.length );
753 this.tryUnsplit( opIndex );
754 };