2012-04-30 18:26:22 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* DataModel transaction processor.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* This class reads operations from a transaction and applies them one by one. It's not intended
|
|
|
|
* to be used directly; use the static functions ve.dm.TransactionProcessor.commit() and .rollback()
|
|
|
|
* instead.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 02:06:20 +00:00
|
|
|
* NOTE: Instances of this class are not recyclable: you can only call .process() on them once.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor = function( doc, transaction, reversed ) {
|
2012-05-15 00:16:18 +00:00
|
|
|
// Properties
|
2012-04-30 18:26:22 +00:00
|
|
|
this.document = doc;
|
2012-05-01 02:06:20 +00:00
|
|
|
this.operations = transaction.getOperations();
|
2012-05-09 00:29:10 +00:00
|
|
|
this.synchronizer = new ve.dm.DocumentSynchronizer( doc );
|
2012-04-30 18:26:22 +00:00
|
|
|
this.reversed = reversed;
|
2012-05-15 00:16:18 +00:00
|
|
|
/*
|
|
|
|
* Linear model offset that we're currently at. Operations in the transaction are ordered, so
|
|
|
|
* the cursor only ever moves forward.
|
|
|
|
*/
|
2012-04-30 18:26:22 +00:00
|
|
|
this.cursor = 0;
|
2012-06-06 22:34:55 +00:00
|
|
|
/**
|
|
|
|
* Adjustment used to convert between linear model offsets in the original linear model and
|
|
|
|
* in the half-updated linear model.
|
|
|
|
*/
|
|
|
|
this.adjustment = 0;
|
2012-05-15 00:16:18 +00:00
|
|
|
/*
|
|
|
|
* Set and clear are lists of annotations which should be added or removed to content being
|
|
|
|
* inserted or retained. The format of these objects is { hash: annotationObjectReference }
|
|
|
|
* where hash is the result of ve.getHash( annotationObjectReference ).
|
|
|
|
*/
|
|
|
|
this.set = {};
|
|
|
|
this.clear = {};
|
2012-04-30 18:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Static methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commit a transaction to a document.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {ve.dm.Document} doc Document object to apply the transaction to
|
|
|
|
* @param {ve.dm.Transaction} transaction Transaction to apply
|
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.commit = function( doc, transaction ) {
|
|
|
|
new ve.dm.TransactionProcessor( doc, transaction, false ).process();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Roll back a transaction; this applies the transaction to the document in reverse.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {ve.dm.Document} doc Document object to apply the transaction to
|
|
|
|
* @param {ve.dm.Transaction} transaction Transaction to apply
|
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.rollback = function( doc, transaction ) {
|
|
|
|
new ve.dm.TransactionProcessor( doc, transaction, true ).process();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.nextOperation = function() {
|
|
|
|
return this.operations[this.operationIndex++] || false;
|
|
|
|
};
|
|
|
|
|
2012-05-15 00:16:18 +00:00
|
|
|
/**
|
|
|
|
* Executes an operation.
|
|
|
|
*
|
|
|
|
* @param {Object} op Operation object to execute
|
|
|
|
* @throws 'Invalid operation error. Operation type is not supported'
|
|
|
|
*/
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.executeOperation = function( op ) {
|
|
|
|
if ( op.type in this ) {
|
|
|
|
this[op.type]( op );
|
|
|
|
} else {
|
|
|
|
throw 'Invalid operation error. Operation type is not supported: ' + operation.type;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-15 00:16:18 +00:00
|
|
|
/**
|
|
|
|
* Processes all operations.
|
|
|
|
*
|
|
|
|
* When all operations are done being processed, the document will be synchronized.
|
|
|
|
*/
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.process = function() {
|
|
|
|
var op;
|
|
|
|
// This loop is factored this way to allow operations to be skipped over or executed
|
|
|
|
// from within other operations
|
|
|
|
this.operationIndex = 0;
|
|
|
|
while ( ( op = this.nextOperation() ) ) {
|
|
|
|
this.executeOperation( op );
|
|
|
|
}
|
2012-05-09 00:29:10 +00:00
|
|
|
this.synchronizer.synchronize();
|
2012-05-01 02:06:20 +00:00
|
|
|
};
|
|
|
|
|
2012-04-30 18:26:22 +00:00
|
|
|
/**
|
|
|
|
* Apply the current annotation stacks. This will set all annotations in this.set and clear all
|
|
|
|
* annotations in this.clear on the data between the offsets this.cursor and this.cursor + to
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {Number} to Offset to stop annotating at. Annotating starts at this.cursor
|
2012-05-15 00:16:18 +00:00
|
|
|
* @throws 'Invalid transaction, can not annotate a branch element'
|
|
|
|
* @throws 'Invalid transaction, annotation to be set is already set'
|
|
|
|
* @throws 'Invalid transaction, annotation to be cleared is not set'
|
2012-04-30 18:26:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.prototype.applyAnnotations = function( to ) {
|
2012-05-15 00:16:18 +00:00
|
|
|
if ( ve.isEmptyObject( this.set ) && ve.isEmptyObject( this.clear ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var item,
|
|
|
|
element,
|
|
|
|
annotated,
|
|
|
|
annotations,
|
|
|
|
hash,
|
|
|
|
empty;
|
|
|
|
for ( var i = this.cursor; i < to; i++ ) {
|
|
|
|
item = this.document.data[i];
|
|
|
|
element = item.type !== undefined;
|
2012-06-08 19:20:54 +00:00
|
|
|
if ( element ) {
|
|
|
|
if ( item.type.charAt( 0 ) === '/' ) {
|
|
|
|
throw 'Invalid transaction, cannot annotate a branch closing element';
|
|
|
|
} else if ( ve.dm.nodeFactory.canNodeHaveChildren( item.type ) ) {
|
|
|
|
throw 'Invalid transaction, cannot annotate a branch opening element';
|
|
|
|
}
|
2012-05-01 02:06:20 +00:00
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
annotated = element ? 'annotations' in item : ve.isArray( item );
|
|
|
|
annotations = annotated ? ( element ? item.annotations : item[1] ) : {};
|
|
|
|
// Set and clear annotations
|
2012-05-01 02:06:20 +00:00
|
|
|
for ( hash in this.set ) {
|
2012-05-15 00:16:18 +00:00
|
|
|
if ( hash in annotations ) {
|
|
|
|
throw 'Invalid transaction, annotation to be set is already set';
|
2012-04-30 18:26:22 +00:00
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
annotations[hash] = this.set[hash];
|
2012-04-30 18:26:22 +00:00
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
for ( hash in this.clear ) {
|
|
|
|
if ( !( hash in annotations ) ) {
|
|
|
|
throw 'Invalid transaction, annotation to be cleared is not set';
|
2012-05-01 02:06:20 +00:00
|
|
|
}
|
2012-05-15 00:16:18 +00:00
|
|
|
delete annotations[hash];
|
|
|
|
}
|
|
|
|
// Auto initialize/cleanup
|
2012-05-15 00:22:37 +00:00
|
|
|
if ( !ve.isEmptyObject( annotations ) && !annotated ) {
|
2012-05-15 00:16:18 +00:00
|
|
|
if ( element ) {
|
|
|
|
// Initialize new element annotation
|
|
|
|
item.annotations = annotations;
|
|
|
|
} else {
|
|
|
|
// Initialize new character annotation
|
|
|
|
this.document.data[i] = [item, annotations];
|
|
|
|
}
|
|
|
|
} else if ( ve.isEmptyObject( annotations ) && annotated ) {
|
|
|
|
if ( element ) {
|
|
|
|
// Cleanup empty element annotation
|
|
|
|
delete item.annotations;
|
|
|
|
} else {
|
|
|
|
// Cleanup empty character annotation
|
|
|
|
this.document.data[i] = item[0];
|
2012-05-01 02:06:20 +00:00
|
|
|
}
|
2012-04-30 18:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-04 22:17:55 +00:00
|
|
|
this.synchronizer.pushAnnotation( new ve.Range( this.cursor, to ) );
|
2012-04-30 18:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a retain operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* This moves the cursor by op.length and applies annotations to the characters that the cursor
|
|
|
|
* moved over.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {Object} op Operation object:
|
2012-05-15 00:16:18 +00:00
|
|
|
* @param {Integer} op.length Number of elements to retain
|
2012-04-30 18:26:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.prototype.retain = function( op ) {
|
2012-05-01 02:06:20 +00:00
|
|
|
this.applyAnnotations( this.cursor + op.length );
|
2012-04-30 18:26:22 +00:00
|
|
|
this.cursor += op.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an annotate operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* This adds or removes an annotation to this.set or this.clear
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {Object} op Operation object
|
2012-05-15 00:16:18 +00:00
|
|
|
* @param {String} op.method Annotation method, either 'set' to add or 'clear' to remove
|
|
|
|
* @param {String} op.bias Endpoint of marker, either 'start' to begin or 'stop' to end
|
|
|
|
* @param {String} op.annotation Annotation object to set or clear from content
|
|
|
|
* @throws 'Invalid annotation method'
|
2012-04-30 18:26:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.prototype.annotate = function( op ) {
|
|
|
|
var target, hash;
|
|
|
|
if ( op.method === 'set' ) {
|
|
|
|
target = this.reversed ? this.clear : this.set;
|
|
|
|
} else if ( op.method === 'clear' ) {
|
|
|
|
target = this.reversed ? this.set : this.clear;
|
|
|
|
} else {
|
|
|
|
throw 'Invalid annotation method ' + op.method;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = $.toJSON( op.annotation );
|
|
|
|
if ( op.bias === 'start' ) {
|
|
|
|
target[hash] = op.annotation;
|
|
|
|
} else {
|
|
|
|
delete target[hash];
|
|
|
|
}
|
2012-05-04 22:17:55 +00:00
|
|
|
|
|
|
|
// Tree sync is done by applyAnnotations()
|
2012-04-30 18:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute an attribute operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* This sets the attribute named op.key on the element at this.cursor to op.to , or unsets it if
|
|
|
|
* op.to === undefined . op.from is not checked against the old value, but is used instead of op.to
|
|
|
|
* in reverse mode. So if op.from is incorrect, the transaction will commit fine, but won't roll
|
|
|
|
* back correctly.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* @param {Object} op Operation object
|
2012-05-15 00:16:18 +00:00
|
|
|
* @param {String} op.key: Attribute name
|
|
|
|
* @param {Mixed} op.from: Old attribute value, or undefined if not previously set
|
|
|
|
* @param {Mixed} op.to: New attribute value, or undefined to unset
|
2012-04-30 18:26:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.prototype.attribute = function( op ) {
|
2012-05-04 18:07:55 +00:00
|
|
|
var element = this.document.data[this.cursor];
|
2012-04-30 18:26:22 +00:00
|
|
|
if ( element.type === undefined ) {
|
2012-05-15 00:16:18 +00:00
|
|
|
throw 'Invalid element error, can not set attributes on non-element data';
|
2012-04-30 18:26:22 +00:00
|
|
|
}
|
|
|
|
var to = this.reversed ? op.from : op.to;
|
2012-05-04 22:17:55 +00:00
|
|
|
var from = this.reversed ? op.to : op.from;
|
2012-04-30 18:26:22 +00:00
|
|
|
if ( to === undefined ) {
|
|
|
|
// Clear
|
|
|
|
if ( element.attributes ) {
|
|
|
|
delete element.attributes[op.key];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Automatically initialize attributes object
|
|
|
|
if ( !element.attributes ) {
|
|
|
|
element.attributes = {};
|
|
|
|
}
|
|
|
|
// Set
|
|
|
|
element.attributes[op.key] = to;
|
|
|
|
}
|
2012-05-17 18:23:56 +00:00
|
|
|
|
|
|
|
this.synchronizer.pushAttributeChange(
|
|
|
|
this.document.getNodeFromOffset( this.cursor + 1 ),
|
|
|
|
op.key,
|
|
|
|
from, to
|
|
|
|
);
|
2012-04-30 18:26:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a replace operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-17 18:23:56 +00:00
|
|
|
* This replaces a range of linear model data with another at this.cursor, figures out how the model
|
|
|
|
* tree needs to be synchronized, and queues this in the DocumentSynchronizer.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-30 18:26:22 +00:00
|
|
|
* op.remove isn't checked against the actual data (instead op.remove.length things are removed
|
2012-05-11 22:25:01 +00:00
|
|
|
* starting at this.cursor), but it's used instead of op.insert in reverse mode. So if
|
2012-04-30 18:26:22 +00:00
|
|
|
* op.remove is incorrect but of the right length, the transaction will commit fine, but won't roll
|
|
|
|
* back correctly.
|
|
|
|
*
|
|
|
|
* @param {Object} op Operation object
|
2012-05-17 18:23:56 +00:00
|
|
|
* @param {Array} op.remove Linear model data to remove
|
|
|
|
* @param {Array} op.insert Linear model data to insert
|
2012-04-30 18:26:22 +00:00
|
|
|
*/
|
|
|
|
ve.dm.TransactionProcessor.prototype.replace = function( op ) {
|
2012-05-11 22:25:01 +00:00
|
|
|
var remove = this.reversed ? op.insert : op.remove,
|
|
|
|
insert = this.reversed ? op.remove : op.insert,
|
2012-05-31 13:23:44 +00:00
|
|
|
removeIsContent = ve.dm.Document.isContentData( remove ),
|
|
|
|
insertIsContent = ve.dm.Document.isContentData( insert ),
|
2012-05-16 23:38:58 +00:00
|
|
|
node, selection;
|
2012-05-31 13:23:44 +00:00
|
|
|
if ( removeIsContent && insertIsContent ) {
|
2012-05-15 04:42:30 +00:00
|
|
|
// Content replacement
|
2012-05-04 18:07:55 +00:00
|
|
|
// Update the linear model
|
2012-05-11 22:25:01 +00:00
|
|
|
ve.batchSplice( this.document.data, this.cursor, remove.length, insert );
|
|
|
|
this.applyAnnotations( this.cursor + insert.length );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
|
|
|
// Get the node containing the replaced content
|
2012-05-31 13:23:44 +00:00
|
|
|
selection = this.document.selectNodes(
|
2012-06-06 22:34:55 +00:00
|
|
|
new ve.Range( this.cursor - this.adjustment,
|
|
|
|
this.cursor - this.adjustment + remove.length ),
|
2012-05-15 04:42:30 +00:00
|
|
|
'leaves'
|
|
|
|
);
|
2012-05-31 13:23:44 +00:00
|
|
|
|
|
|
|
var removeHasStructure = ve.dm.Document.containsElementData( remove ),
|
|
|
|
insertHasStructure = ve.dm.Document.containsElementData( insert );
|
|
|
|
if ( removeHasStructure || insertHasStructure ) {
|
|
|
|
// Replacement is not exclusively text
|
|
|
|
// Rebuild all covered nodes
|
|
|
|
var range = new ve.Range( selection[0].nodeRange.start,
|
|
|
|
selection[selection.length - 1].nodeRange.end );
|
|
|
|
this.synchronizer.pushRebuild( range,
|
2012-06-06 22:34:55 +00:00
|
|
|
new ve.Range( range.start + this.adjustment,
|
|
|
|
range.end + this.adjustment + insert.length - remove.length )
|
2012-05-31 13:23:44 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Text-only replacement
|
|
|
|
// Queue a resize for this node
|
|
|
|
node = selection[0].node;
|
|
|
|
this.synchronizer.pushResize( node, insert.length - remove.length );
|
|
|
|
}
|
|
|
|
|
2012-05-04 18:07:55 +00:00
|
|
|
// Advance the cursor
|
2012-05-11 22:25:01 +00:00
|
|
|
this.cursor += insert.length;
|
2012-06-06 22:34:55 +00:00
|
|
|
this.adjustment += insert.length - remove.length;
|
2012-05-04 18:07:55 +00:00
|
|
|
} else {
|
2012-05-15 04:46:39 +00:00
|
|
|
// Structural replacement
|
2012-05-11 03:12:07 +00:00
|
|
|
|
|
|
|
// It's possible that multiple replace operations are needed before the
|
|
|
|
// model is back in a consistent state. This loop applies the current
|
|
|
|
// replace operation to the linear model, then keeps applying subsequent
|
|
|
|
// operations until the model is consistent. We keep track of the changes
|
|
|
|
// and queue a single rebuild after the loop finishes.
|
|
|
|
var operation = op,
|
|
|
|
removeLevel = 0,
|
2012-05-16 20:56:55 +00:00
|
|
|
insertLevel = 0,
|
2012-05-11 03:12:07 +00:00
|
|
|
startOffset = this.cursor,
|
|
|
|
i,
|
2012-05-16 23:38:58 +00:00
|
|
|
type,
|
|
|
|
prevCursor,
|
|
|
|
affectedRanges = [],
|
|
|
|
scope,
|
|
|
|
minInsertLevel = 0,
|
2012-05-17 18:23:56 +00:00
|
|
|
coveringRange,
|
|
|
|
scopeStart,
|
|
|
|
scopeEnd;
|
2012-05-11 03:12:07 +00:00
|
|
|
|
|
|
|
while ( true ) {
|
|
|
|
if ( operation.type == 'replace' ) {
|
2012-05-11 22:25:01 +00:00
|
|
|
var opRemove = this.reversed ? operation.insert : operation.remove,
|
|
|
|
opInsert = this.reversed ? operation.remove : operation.insert;
|
|
|
|
// Update the linear model for this insert
|
2012-05-14 22:03:44 +00:00
|
|
|
ve.batchSplice( this.document.data, this.cursor, opRemove.length, opInsert );
|
2012-05-16 23:38:58 +00:00
|
|
|
affectedRanges.push( new ve.Range( this.cursor, this.cursor + opRemove.length ) );
|
|
|
|
prevCursor = this.cursor;
|
2012-05-14 22:03:44 +00:00
|
|
|
this.cursor += opInsert.length;
|
2012-05-16 23:38:58 +00:00
|
|
|
|
|
|
|
// Paint the removed selection, figure out which nodes were
|
|
|
|
// covered, and add their ranges to the affected ranges list
|
|
|
|
if ( opRemove.length > 0 ) {
|
|
|
|
selection = this.document.selectNodes( new ve.Range(
|
2012-06-06 22:34:55 +00:00
|
|
|
prevCursor - this.adjustment,
|
|
|
|
prevCursor + opRemove.length - this.adjustment
|
2012-05-16 23:38:58 +00:00
|
|
|
), 'siblings' );
|
|
|
|
for ( i = 0; i < selection.length; i++ ) {
|
|
|
|
// .nodeRange is the inner range, we need the
|
|
|
|
// outer range (including opening and closing)
|
|
|
|
if ( selection[i].node.isWrapped() ) {
|
|
|
|
affectedRanges.push( new ve.Range(
|
|
|
|
selection[i].nodeRange.start - 1,
|
|
|
|
selection[i].nodeRange.end + 1
|
|
|
|
) );
|
|
|
|
} else {
|
|
|
|
affectedRanges.push( selection[i].nodeRange );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-11 03:12:07 +00:00
|
|
|
|
2012-05-11 22:25:01 +00:00
|
|
|
// Walk through the remove and insert data
|
2012-05-11 03:12:07 +00:00
|
|
|
// and keep track of the element depth change (level)
|
|
|
|
// for each of these two separately. The model is
|
|
|
|
// only consistent if both levels are zero.
|
|
|
|
for ( i = 0; i < opRemove.length; i++ ) {
|
|
|
|
type = opRemove[i].type;
|
|
|
|
if ( type === undefined ) {
|
|
|
|
// This is content, ignore
|
|
|
|
} else if ( type.charAt( 0 ) === '/' ) {
|
|
|
|
// Closing element
|
|
|
|
removeLevel--;
|
|
|
|
} else {
|
|
|
|
// Opening element
|
|
|
|
removeLevel++;
|
|
|
|
}
|
|
|
|
}
|
2012-05-16 23:38:58 +00:00
|
|
|
// Keep track of the scope of the insertion
|
|
|
|
// Normally this is the node we're inserting into, except if the
|
|
|
|
// insertion closes elements it doesn't open (i.e. splits elements),
|
|
|
|
// in which case it's the affected ancestor
|
2012-05-14 22:03:44 +00:00
|
|
|
for ( i = 0; i < opInsert.length; i++ ) {
|
|
|
|
type = opInsert[i].type;
|
2012-05-11 03:12:07 +00:00
|
|
|
if ( type === undefined ) {
|
|
|
|
// This is content, ignore
|
|
|
|
} else if ( type.charAt( 0 ) === '/' ) {
|
|
|
|
// Closing element
|
2012-05-16 20:56:55 +00:00
|
|
|
insertLevel--;
|
2012-05-16 23:38:58 +00:00
|
|
|
if ( insertLevel < minInsertLevel ) {
|
|
|
|
// Closing an unopened element at a higher
|
|
|
|
// (more negative) level than before
|
|
|
|
// Lazy-initialize scope
|
|
|
|
scope = scope || this.document.getNodeFromOffset( prevCursor );
|
|
|
|
// Push the full range of the old scope as an affected range
|
|
|
|
scopeStart = this.document.getDocumentNode().getOffsetFromNode( scope );
|
|
|
|
scopeEnd = scopeStart + scope.getOuterLength();
|
|
|
|
affectedRanges.push( new ve.Range( scopeStart, scopeEnd ) );
|
|
|
|
// Update scope
|
|
|
|
scope = scope.getParent() || scope;
|
|
|
|
}
|
|
|
|
|
2012-05-11 03:12:07 +00:00
|
|
|
} else {
|
|
|
|
// Opening element
|
2012-05-16 20:56:55 +00:00
|
|
|
insertLevel++;
|
2012-05-11 03:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-16 23:38:58 +00:00
|
|
|
|
|
|
|
// Update adjustment
|
2012-06-06 22:34:55 +00:00
|
|
|
this.adjustment += opInsert.length - opRemove.length;
|
2012-05-11 03:12:07 +00:00
|
|
|
} else {
|
|
|
|
// We know that other operations won't cause adjustments, so we
|
|
|
|
// don't have to update adjustment
|
|
|
|
this.executeOperation( operation );
|
|
|
|
}
|
|
|
|
|
2012-05-16 20:56:55 +00:00
|
|
|
if ( removeLevel === 0 && insertLevel === 0 ) {
|
2012-05-11 03:12:07 +00:00
|
|
|
// The model is back in a consistent state, so we're done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next operation
|
|
|
|
operation = this.nextOperation();
|
|
|
|
if ( !operation ) {
|
|
|
|
throw 'Unbalanced set of replace operations found';
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 04:46:39 +00:00
|
|
|
|
2012-05-16 23:38:58 +00:00
|
|
|
// From all the affected ranges we have gathered, compute a range that covers all
|
|
|
|
// of them, and rebuild that
|
|
|
|
coveringRange = ve.Range.newCoveringRange( affectedRanges );
|
|
|
|
this.synchronizer.pushRebuild( coveringRange, new ve.Range( coveringRange.start,
|
2012-06-06 22:34:55 +00:00
|
|
|
coveringRange.end + this.adjustment )
|
2012-05-16 23:38:58 +00:00
|
|
|
);
|
2012-05-04 18:07:55 +00:00
|
|
|
}
|
2012-04-30 18:26:22 +00:00
|
|
|
};
|