2011-11-14 23:04:36 +00:00
|
|
|
/**
|
2012-02-06 23:50:56 +00:00
|
|
|
* Creates an ve.dm.TransactionProcessor object.
|
2011-11-14 23:04:36 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor = function( model, transaction ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
this.model = model;
|
|
|
|
this.transaction = transaction;
|
|
|
|
this.cursor = 0;
|
|
|
|
this.set = [];
|
|
|
|
this.clear = [];
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Members */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.operationMap = {
|
2011-11-14 23:04:36 +00:00
|
|
|
// Retain
|
|
|
|
'retain': {
|
|
|
|
'commit': function( op ) {
|
|
|
|
this.retain( op );
|
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
|
|
|
this.retain( op );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Insert
|
|
|
|
'insert': {
|
|
|
|
'commit': function( op ) {
|
|
|
|
this.insert( op );
|
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
|
|
|
this.remove( op );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Remove
|
|
|
|
'remove': {
|
|
|
|
'commit': function( op ) {
|
|
|
|
this.remove( op );
|
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
|
|
|
this.insert( op );
|
|
|
|
}
|
|
|
|
},
|
2012-03-08 23:21:17 +00:00
|
|
|
'replace': {
|
|
|
|
'commit': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.replace( op );
|
2012-03-08 23:21:17 +00:00
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.replace( op );
|
2012-03-08 23:21:17 +00:00
|
|
|
}
|
|
|
|
},
|
2011-11-14 23:04:36 +00:00
|
|
|
// Change element attributes
|
|
|
|
'attribute': {
|
|
|
|
'commit': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.attribute( op );
|
2011-11-14 23:04:36 +00:00
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.attribute( op );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Change content annotations
|
|
|
|
'annotate': {
|
|
|
|
'commit': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.mark( op );
|
2011-11-14 23:04:36 +00:00
|
|
|
},
|
|
|
|
'rollback': function( op ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.mark( op );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.commit = function( doc, transaction ) {
|
|
|
|
var tp = new ve.dm.TransactionProcessor( doc, transaction );
|
2011-11-14 23:04:36 +00:00
|
|
|
tp.process( 'commit' );
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.rollback = function( doc, transaction ) {
|
|
|
|
var tp = new ve.dm.TransactionProcessor( doc, transaction );
|
2011-11-14 23:04:36 +00:00
|
|
|
tp.process( 'rollback' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2012-03-14 21:02:34 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.nextOperation = function() {
|
|
|
|
return this.operations[this.operationIndex++] || false;
|
|
|
|
};
|
|
|
|
|
2012-03-14 21:02:36 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.executeOperation = function( op ) {
|
2012-03-14 21:02:34 +00:00
|
|
|
if ( op.type in ve.dm.TransactionProcessor.operationMap ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
ve.dm.TransactionProcessor.operationMap[op.type][this.method].call( this, op );
|
2012-03-14 21:02:34 +00:00
|
|
|
} else {
|
|
|
|
throw 'Invalid operation error. Operation type is not supported: ' + operation.type;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.process = function( method ) {
|
2012-03-14 21:02:34 +00:00
|
|
|
var op;
|
2012-03-12 03:55:32 +00:00
|
|
|
this.synchronizer = new ve.dm.DocumentSynchronizer( this.model );
|
2012-03-14 21:02:36 +00:00
|
|
|
// Store the method (commit or rollback) and the operations array so executeOperation()
|
|
|
|
// can access them easily
|
|
|
|
this.method = method;
|
2012-03-14 21:02:34 +00:00
|
|
|
this.operations = this.transaction.getOperations();
|
2012-03-14 21:02:36 +00:00
|
|
|
|
|
|
|
// This loop is factored this way to allow operations to be skipped over or executed
|
|
|
|
// from within other operations
|
2012-03-14 21:02:34 +00:00
|
|
|
this.operationIndex = 0;
|
|
|
|
while ( ( op = this.nextOperation() ) ) {
|
2012-03-14 21:02:36 +00:00
|
|
|
this.executeOperation( op );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2012-03-12 03:55:32 +00:00
|
|
|
|
|
|
|
this.synchronizer.synchronize();
|
2011-11-14 23:04:36 +00:00
|
|
|
};
|
|
|
|
|
2011-11-17 19:19:02 +00:00
|
|
|
/**
|
|
|
|
* Get the parent node that would be affected by inserting given data into it's child.
|
|
|
|
*
|
|
|
|
* This is used when inserting data that closes and reopens one or more parent nodes into a child
|
|
|
|
* node, which requires rebuilding at a higher level.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Node} node Child node to start from
|
2011-11-17 19:19:02 +00:00
|
|
|
* @param {Array} data Data to inspect for closings
|
2012-02-06 23:50:56 +00:00
|
|
|
* @returns {ve.Node} Lowest level parent node being affected
|
2011-11-17 19:19:02 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.getScope = function( node, data ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
var i,
|
|
|
|
length,
|
|
|
|
level = 0,
|
2011-11-17 19:19:02 +00:00
|
|
|
max = 0;
|
2011-11-14 23:04:36 +00:00
|
|
|
for ( i = 0, length = data.length; i < length; i++ ) {
|
|
|
|
if ( typeof data[i].type === 'string' ) {
|
2011-11-17 19:19:02 +00:00
|
|
|
level += data[i].type.charAt( 0 ) === '/' ? 1 : -1;
|
|
|
|
max = Math.max( max, level );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-17 19:19:02 +00:00
|
|
|
if ( max > 0 ) {
|
2012-01-31 16:43:21 +00:00
|
|
|
for ( i = 0; i < max; i++ ) {
|
|
|
|
node = node.getParent() || node;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.applyAnnotations = function( to, update ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
var i,
|
|
|
|
j,
|
2011-11-30 19:21:33 +00:00
|
|
|
k,
|
2011-11-14 23:04:36 +00:00
|
|
|
length,
|
2011-11-23 23:37:27 +00:00
|
|
|
annotation,
|
2011-11-30 19:21:33 +00:00
|
|
|
changes = 0,
|
|
|
|
index;
|
2011-11-14 23:04:36 +00:00
|
|
|
// Handle annotations
|
|
|
|
if ( this.set.length ) {
|
|
|
|
for ( i = 0, length = this.set.length; i < length; i++ ) {
|
|
|
|
annotation = this.set[i];
|
|
|
|
// Auto-build annotation hash
|
|
|
|
if ( annotation.hash === undefined ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
annotation.hash = ve.dm.DocumentNode.getHash( annotation );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
for ( j = this.cursor; j < to; j++ ) {
|
|
|
|
// Auto-convert to array
|
2012-02-06 23:50:56 +00:00
|
|
|
if ( ve.isArray( this.model.data[j] ) ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
this.model.data[j].push( annotation );
|
|
|
|
} else {
|
|
|
|
this.model.data[j] = [this.model.data[j], annotation];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 23:37:27 +00:00
|
|
|
changes++;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
if ( this.clear.length ) {
|
|
|
|
for ( i = 0, length = this.clear.length; i < length; i++ ) {
|
|
|
|
annotation = this.clear[i];
|
2011-11-30 19:21:33 +00:00
|
|
|
if ( annotation instanceof RegExp ) {
|
|
|
|
for ( j = this.cursor; j < to; j++ ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
var matches = ve.dm.DocumentNode.getMatchingAnnotations(
|
2011-11-30 19:21:33 +00:00
|
|
|
this.model.data[j], annotation
|
|
|
|
);
|
|
|
|
for ( k = 0; k < matches.length; k++ ) {
|
|
|
|
index = this.model.data[j].indexOf( matches[k] );
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.model.data[j].splice( index, 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Auto-convert to string
|
|
|
|
if ( this.model.data[j].length === 1 ) {
|
|
|
|
this.model.data[j] = this.model.data[j][0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Auto-build annotation hash
|
|
|
|
if ( annotation.hash === undefined ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
annotation.hash = ve.dm.DocumentNode.getHash( annotation );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2011-11-30 19:21:33 +00:00
|
|
|
for ( j = this.cursor; j < to; j++ ) {
|
2012-02-06 23:50:56 +00:00
|
|
|
index = ve.dm.DocumentNode.getIndexOfAnnotation(
|
2011-11-30 19:21:33 +00:00
|
|
|
this.model.data[j], annotation
|
|
|
|
);
|
|
|
|
if ( index !== -1 ) {
|
|
|
|
this.model.data[j].splice( index, 1 );
|
|
|
|
}
|
|
|
|
// Auto-convert to string
|
|
|
|
if ( this.model.data[j].length === 1 ) {
|
|
|
|
this.model.data[j] = this.model.data[j][0];
|
|
|
|
}
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 23:37:27 +00:00
|
|
|
changes++;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2011-11-23 23:37:27 +00:00
|
|
|
if ( update && changes ) {
|
2011-11-23 23:40:38 +00:00
|
|
|
var fromNode = this.model.getNodeFromOffset( this.cursor ),
|
|
|
|
toNode = this.model.getNodeFromOffset( to );
|
2011-11-23 23:37:27 +00:00
|
|
|
this.model.traverseLeafNodes( function( node ) {
|
|
|
|
node.emit( 'update' );
|
2011-11-23 23:40:38 +00:00
|
|
|
if ( node === toNode ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, fromNode );
|
2011-11-23 22:21:46 +00:00
|
|
|
}
|
2011-11-14 23:04:36 +00:00
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.retain = function( op ) {
|
2011-11-23 22:21:46 +00:00
|
|
|
this.applyAnnotations( this.cursor + op.length, true );
|
2011-11-14 23:04:36 +00:00
|
|
|
this.cursor += op.length;
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.insert = function( op ) {
|
2011-12-07 02:22:06 +00:00
|
|
|
var node,
|
|
|
|
index,
|
2012-01-31 16:43:21 +00:00
|
|
|
offset,
|
|
|
|
scope;
|
2011-12-10 02:19:21 +00:00
|
|
|
|
2012-01-31 16:43:21 +00:00
|
|
|
node = this.model.getNodeFromOffset( this.cursor );
|
2012-01-31 17:14:16 +00:00
|
|
|
|
|
|
|
// Shortcut 1: we're inserting content. We don't need to bother with any structural stuff
|
2012-02-06 23:50:56 +00:00
|
|
|
if ( !ve.dm.DocumentNode.containsElementData( op.data ) ) {
|
2012-01-31 17:14:16 +00:00
|
|
|
// TODO should we check whether we're at a structural offset, and throw an exception
|
|
|
|
// if that's the case? Or can we assume that the transaction is valid at this point?
|
|
|
|
|
|
|
|
// Insert data into linear model
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.insertIntoArray( this.model.data, this.cursor, op.data );
|
2012-01-31 17:14:16 +00:00
|
|
|
this.applyAnnotations( this.cursor + op.data.length );
|
|
|
|
// Update the length of the containing node
|
2012-03-12 00:07:33 +00:00
|
|
|
this.synchronizer.pushResize( node, op.data.length );
|
2012-01-31 17:14:16 +00:00
|
|
|
// Move the cursor
|
|
|
|
this.cursor += op.data.length;
|
|
|
|
// All done
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the scope of the inserted data. If the data is an enclosed piece of structure,
|
|
|
|
// this will return node. Otherwise, the data closes one or more nodes, and this will return
|
|
|
|
// the first ancestor of node that isn't closed, which is the node that will contain the
|
|
|
|
// inserted data entirely.
|
2012-01-31 16:43:21 +00:00
|
|
|
scope = this.getScope( node, op.data );
|
2012-01-31 17:14:16 +00:00
|
|
|
|
|
|
|
// Shortcut 2: we're inserting an enclosed piece of structural data at a structural offset
|
|
|
|
// that isn't the end of the document.
|
2012-03-12 00:07:33 +00:00
|
|
|
// TODO why can't it be at the end of the document?
|
2012-02-07 00:42:16 +00:00
|
|
|
if (
|
|
|
|
ve.dm.DocumentNode.isStructuralOffset( this.model.data, this.cursor ) &&
|
|
|
|
this.cursor != this.model.data.length &&
|
|
|
|
scope == node
|
2012-01-31 16:43:21 +00:00
|
|
|
) {
|
|
|
|
// We're inserting an enclosed element into something else, so we don't have to rebuild
|
|
|
|
// the parent node. Just build a node from the inserted data and stick it in
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.insertIntoArray( this.model.data, this.cursor, op.data );
|
2011-12-07 02:22:06 +00:00
|
|
|
this.applyAnnotations( this.cursor + op.data.length );
|
|
|
|
offset = this.model.getOffsetFromNode( node );
|
|
|
|
index = node.getIndexFromOffset( this.cursor - offset );
|
2012-03-12 00:07:33 +00:00
|
|
|
this.synchronizer.pushRebuild( new ve.Range( this.cursor, this.cursor ),
|
|
|
|
new ve.Range( this.cursor, this.cursor + op.data.length ) );
|
2011-12-06 22:04:18 +00:00
|
|
|
} else {
|
2012-01-31 17:14:16 +00:00
|
|
|
// This is the non-shortcut case
|
|
|
|
|
2012-03-12 00:07:33 +00:00
|
|
|
// Rebuild all children of scope, which is the node that encloses everything we might have to rebuild
|
|
|
|
node = scope.getChildren()[0];
|
2012-01-31 16:43:21 +00:00
|
|
|
offset = this.model.getOffsetFromNode( node );
|
2012-01-31 17:14:16 +00:00
|
|
|
if ( offset === -1 ) {
|
|
|
|
throw 'Invalid offset error. Node is not in model tree';
|
2011-12-07 02:13:43 +00:00
|
|
|
}
|
2012-01-31 17:14:16 +00:00
|
|
|
// Perform insert on linear data model
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.insertIntoArray( this.model.data, this.cursor, op.data );
|
2012-01-31 17:14:16 +00:00
|
|
|
this.applyAnnotations( this.cursor + op.data.length );
|
|
|
|
// Synchronize model tree
|
2012-03-12 00:07:33 +00:00
|
|
|
this.synchronizer.pushRebuild( new ve.Range( offset, offset + scope.getContentLength() ),
|
|
|
|
new ve.Range( offset, offset + scope.getContentLength() + op.data.length ) );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
this.cursor += op.data.length;
|
|
|
|
};
|
|
|
|
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.remove = function( op ) {
|
|
|
|
if ( ve.dm.DocumentNode.containsElementData( op.data ) ) {
|
2012-03-12 00:07:33 +00:00
|
|
|
// TODO rewrite all this
|
2011-11-14 23:04:36 +00:00
|
|
|
// Figure out which nodes are covered by the removal
|
2012-02-07 00:42:16 +00:00
|
|
|
var ranges = this.model.selectNodes(
|
|
|
|
new ve.Range( this.cursor, this.cursor + op.data.length )
|
|
|
|
);
|
2011-11-18 10:17:35 +00:00
|
|
|
|
|
|
|
// Build the list of nodes to rebuild and the data to keep
|
2011-12-07 18:29:02 +00:00
|
|
|
var oldNodes = [],
|
|
|
|
newData = [],
|
|
|
|
parent = null,
|
|
|
|
index = null,
|
|
|
|
firstKeptNode,
|
|
|
|
lastKeptNode,
|
|
|
|
i;
|
|
|
|
for ( i = 0; i < ranges.length; i++ ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
oldNodes.push( ranges[i].node );
|
2011-11-16 20:01:12 +00:00
|
|
|
if ( ranges[i].range !== undefined ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
// We have to keep part of this node
|
2011-11-18 10:17:35 +00:00
|
|
|
if ( firstKeptNode === undefined ) {
|
2011-11-14 23:04:36 +00:00
|
|
|
// This is the first node we're keeping
|
2011-11-18 10:17:35 +00:00
|
|
|
firstKeptNode = ranges[i].node;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2011-11-14 23:47:07 +00:00
|
|
|
// Compute the start and end offset of this node
|
|
|
|
// We could do that with getOffsetFromNode() but
|
|
|
|
// we already have all the numbers we need so why would we
|
2011-11-14 23:04:36 +00:00
|
|
|
var startOffset = ranges[i].globalRange.start - ranges[i].range.start,
|
|
|
|
endOffset = startOffset + ranges[i].node.getContentLength(),
|
|
|
|
// Get this node's data
|
|
|
|
nodeData = this.model.data.slice( startOffset, endOffset );
|
|
|
|
// Remove data covered by the range from nodeData
|
2012-02-07 00:42:16 +00:00
|
|
|
nodeData.splice(
|
|
|
|
ranges[i].range.start, ranges[i].range.end - ranges[i].range.start
|
|
|
|
);
|
2011-11-14 23:04:36 +00:00
|
|
|
// What remains in nodeData is the data we need to keep
|
|
|
|
// Append it to newData
|
|
|
|
newData = newData.concat( nodeData );
|
|
|
|
|
2011-11-18 10:17:35 +00:00
|
|
|
lastKeptNode = ranges[i].node;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-18 10:17:35 +00:00
|
|
|
|
|
|
|
// Surround newData with the right openings and closings if needed
|
|
|
|
if ( firstKeptNode !== undefined ) {
|
|
|
|
// There are a number of conceptually different cases here,
|
|
|
|
// but the algorithm for dealing with them is the same.
|
|
|
|
// 1. Removal within one node: firstKeptNode === lastKeptNode
|
|
|
|
// 2. Merge of siblings: firstKeptNode.getParent() === lastKeptNode.getParent()
|
|
|
|
// 3. Merge of arbitrary depth: firstKeptNode and lastKeptNode have a common ancestor
|
2012-02-07 00:42:16 +00:00
|
|
|
// Because #1 and #2 are special cases of #3 (merges with depth=0 and depth=1,
|
|
|
|
// respectively), the code below that deals with the general case (#3) and automatically
|
|
|
|
// covers #1 and #2 that way as well.
|
2011-11-18 10:17:35 +00:00
|
|
|
|
|
|
|
// Simultaneously traverse upwards from firstKeptNode and lastKeptNode
|
|
|
|
// to find the common ancestor. On our way up, keep the element of each
|
|
|
|
// node we visit and verify that the transaction is a valid merge (i.e. it satisfies
|
|
|
|
// the merge criteria in prepareRemoval()'s canMerge()).
|
|
|
|
// FIXME: The code is essentially the same as canMerge(), merge these algorithms
|
2011-12-07 18:29:02 +00:00
|
|
|
var openings = [],
|
|
|
|
closings = [],
|
2012-02-06 23:50:56 +00:00
|
|
|
paths = ve.Node.getCommonAncestorPaths( firstKeptNode, lastKeptNode ),
|
2011-12-07 18:29:02 +00:00
|
|
|
prevN1,
|
|
|
|
prevN2;
|
2011-11-24 15:55:54 +00:00
|
|
|
|
|
|
|
if ( !paths ) {
|
2012-02-07 00:42:16 +00:00
|
|
|
throw 'Removal is not a valid merge: ' +
|
|
|
|
'nodes do not have a common ancestor or are not at the same depth';
|
2011-11-24 15:55:54 +00:00
|
|
|
}
|
|
|
|
for ( i = 0; i < paths.node1Path.length; i++ ) {
|
2011-11-18 10:17:35 +00:00
|
|
|
// Verify the element types are equal
|
2011-11-24 15:55:54 +00:00
|
|
|
if ( paths.node1Path[i].getElementType() !== paths.node2Path[i].getElementType() ) {
|
2012-02-07 00:42:16 +00:00
|
|
|
throw 'Removal is not a valid merge: ' +
|
|
|
|
'corresponding parents have different types ( ' +
|
|
|
|
paths.node1Path[i].getElementType() + ' vs ' +
|
|
|
|
paths.node2Path[i].getElementType() + ' )';
|
2011-11-18 10:17:35 +00:00
|
|
|
}
|
|
|
|
// Record the opening of n1 and the closing of n2
|
2011-11-24 15:55:54 +00:00
|
|
|
openings.push( paths.node1Path[i].getElement() );
|
|
|
|
closings.push( { 'type': '/' + paths.node2Path[i].getElementType() } );
|
2011-11-18 10:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Surround newData with the openings and closings
|
|
|
|
newData = openings.reverse().concat( newData, closings );
|
|
|
|
|
2012-03-12 00:07:33 +00:00
|
|
|
// Rebuild oldNodes' ancestors if needed
|
2011-11-18 10:17:35 +00:00
|
|
|
// This only happens for merges with depth > 1
|
2011-11-24 15:55:54 +00:00
|
|
|
prevN1 = paths.node1Path.length ? paths.node1Path[paths.node1Path.length - 1] : null;
|
|
|
|
prevN2 = paths.node2Path.length ? paths.node2Path[paths.node2Path.length - 1] : null;
|
|
|
|
if ( prevN1 && prevN1 !== oldNodes[0] ) {
|
2011-11-18 10:17:35 +00:00
|
|
|
oldNodes = [ prevN1 ];
|
2011-11-24 15:55:54 +00:00
|
|
|
parent = paths.commonAncestor;
|
|
|
|
index = parent.indexOf( prevN1 ); // Pass to rebuildNodes() so it's not recomputed
|
2011-11-18 10:17:35 +00:00
|
|
|
if ( index === -1 ) {
|
|
|
|
throw "Tree corruption detected: node isn't in its parent's children array";
|
|
|
|
}
|
|
|
|
var foundPrevN2 = false;
|
2011-11-24 15:55:54 +00:00
|
|
|
for ( var j = index + 1; !foundPrevN2 && j < parent.getChildren().length; j++ ) {
|
|
|
|
oldNodes.push( parent.getChildren()[j] );
|
|
|
|
foundPrevN2 = parent.getChildren()[j] === prevN2;
|
2011-11-18 10:17:35 +00:00
|
|
|
}
|
|
|
|
if ( !foundPrevN2 ) {
|
|
|
|
throw "Tree corruption detected: node isn't in its parent's children array";
|
|
|
|
}
|
|
|
|
}
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2011-11-18 10:17:35 +00:00
|
|
|
|
2011-11-14 23:47:07 +00:00
|
|
|
// Update the linear model
|
|
|
|
this.model.data.splice( this.cursor, op.data.length );
|
2011-11-14 23:04:36 +00:00
|
|
|
// Perform the rebuild. This updates the model tree
|
2012-03-12 00:07:33 +00:00
|
|
|
// TODO index of oldNodes[0] in its parent should be computed
|
|
|
|
// on the go by selectNodes() above
|
|
|
|
if ( parent == null ) {
|
|
|
|
parent = oldNodes[0].getParent();
|
|
|
|
}
|
|
|
|
if ( index == null ) {
|
|
|
|
index = parent.indexOf( oldNodes[0] );
|
|
|
|
}
|
|
|
|
// TODO better offset computation
|
|
|
|
// TODO allow direct parameter passing in pushRebuild()
|
|
|
|
var startOffset = this.model.getOffsetFromNode( oldNodes[0] );
|
|
|
|
var endOffset = this.model.getOffsetFromNode( oldNodes[oldNodes.length-1] ) +
|
|
|
|
oldNodes[oldNodes.length-1].getElementLength();
|
|
|
|
this.synchronizer.pushRebuild( new ve.Range( startOffset, endOffset ),
|
|
|
|
new ve.Range( startOffset, startOffset + newData.length ) );
|
2011-11-14 23:04:36 +00:00
|
|
|
} else {
|
|
|
|
// We're removing content only. Take a shortcut
|
|
|
|
// Get the node we are removing content from
|
|
|
|
var node = this.model.getNodeFromOffset( this.cursor );
|
2011-11-14 23:47:07 +00:00
|
|
|
// Update the linear model
|
|
|
|
this.model.data.splice( this.cursor, op.data.length );
|
2012-03-12 00:07:33 +00:00
|
|
|
// Queue a resize
|
|
|
|
this.synchronizer.pushResize( node, -op.data.length );
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-03-14 21:02:36 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.replace = function( op ) {
|
|
|
|
var invert = this.method == 'rollback',
|
|
|
|
remove = invert ? op.replacement : op.remove,
|
2012-03-12 03:55:32 +00:00
|
|
|
replacement = invert ? op.remove : op.replacement,
|
|
|
|
removeHasStructure = ve.dm.DocumentNode.containsElementData( remove ),
|
|
|
|
replacementHasStructure = ve.dm.DocumentNode.containsElementData( replacement ),
|
|
|
|
node;
|
2012-03-08 23:21:20 +00:00
|
|
|
// remove is provided only for OT / conflict resolution and for
|
|
|
|
// reversibility, we don't actually verify it here
|
|
|
|
|
2012-03-12 03:55:32 +00:00
|
|
|
// Tree synchronization
|
|
|
|
// Figure out if this is a structural replacement or a content replacement
|
|
|
|
if ( !ve.dm.DocumentNode.containsElementData( remove ) && !ve.dm.DocumentNode.containsElementData( replacement ) ) {
|
|
|
|
// Content replacement
|
|
|
|
// Update the linear model
|
|
|
|
ve.batchedSplice( this.model.data, this.cursor, remove.length, replacement );
|
|
|
|
this.applyAnnotations( this.cursor + replacement.length );
|
|
|
|
|
|
|
|
// Get the node containing the replaced content
|
|
|
|
node = this.model.getNodeFromOffset( this.cursor );
|
|
|
|
// Queue a resize for this node
|
|
|
|
this.synchronizer.pushResize( node, replacement.length - remove.length );
|
|
|
|
// Advance the cursor
|
|
|
|
this.cursor += replacement.length;
|
|
|
|
} else {
|
|
|
|
// Structural replacement
|
|
|
|
// TODO generalize for insert/remove
|
|
|
|
|
|
|
|
// 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.
|
2012-04-02 22:28:26 +00:00
|
|
|
var operation = op,
|
|
|
|
removeLevel = 0,
|
|
|
|
replaceLevel = 0,
|
|
|
|
startOffset = this.cursor,
|
|
|
|
adjustment = 0,
|
|
|
|
i,
|
|
|
|
type;
|
2012-03-12 03:55:32 +00:00
|
|
|
|
|
|
|
while ( true ) {
|
|
|
|
if ( operation.type == 'replace' ) {
|
|
|
|
var opRemove = invert ? operation.replacement : operation.remove,
|
|
|
|
opReplacement = invert ? operation.remove : operation.replacement;
|
|
|
|
// Update the linear model for this replacement
|
|
|
|
ve.batchedSplice( this.model.data, this.cursor, opRemove.length, opReplacement );
|
|
|
|
this.cursor += opReplacement.length;
|
|
|
|
adjustment += opReplacement.length - opRemove.length;
|
|
|
|
|
|
|
|
// Walk through the remove and replacement data
|
|
|
|
// 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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for ( i = 0; i < opReplacement.length; i++ ) {
|
|
|
|
type = opReplacement[i].type;
|
|
|
|
if ( type === undefined ) {
|
|
|
|
// This is content, ignore
|
|
|
|
} else if ( type.charAt( 0 ) === '/' ) {
|
|
|
|
// Closing element
|
|
|
|
replaceLevel--;
|
|
|
|
} else {
|
|
|
|
// Opening element
|
|
|
|
replaceLevel++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We're assuming that other operations will not cause
|
|
|
|
// adjustments.
|
|
|
|
// TODO actually make this the case by folding insert
|
|
|
|
// and delete into replace
|
|
|
|
this.executeOperation( operation );
|
|
|
|
}
|
|
|
|
|
2012-04-02 22:28:26 +00:00
|
|
|
if ( removeLevel === 0 && replaceLevel === 0 ) {
|
2012-03-12 03:55:32 +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';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Queue a rebuild for the replaced node
|
|
|
|
this.synchronizer.pushRebuild( new ve.Range( startOffset, this.cursor - adjustment ),
|
|
|
|
new ve.Range( startOffset, this.cursor ) );
|
|
|
|
}
|
2012-03-08 23:21:17 +00:00
|
|
|
};
|
|
|
|
|
2012-03-14 21:02:36 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.attribute = function( op ) {
|
|
|
|
var invert = this.method == 'rollback',
|
|
|
|
element = this.model.data[this.cursor];
|
2011-11-14 23:04:36 +00:00
|
|
|
if ( element.type === undefined ) {
|
|
|
|
throw 'Invalid element error. Can not set attributes on non-element data.';
|
|
|
|
}
|
2012-02-22 21:23:28 +00:00
|
|
|
var to = invert ? op.from : op.to;
|
|
|
|
if ( to === undefined ) {
|
|
|
|
// Clear
|
2011-11-14 23:04:36 +00:00
|
|
|
if ( element.attributes ) {
|
|
|
|
delete element.attributes[op.key];
|
|
|
|
}
|
|
|
|
// Automatically clean up attributes object
|
|
|
|
var empty = true;
|
|
|
|
for ( var key in element.attributes ) {
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( empty ) {
|
|
|
|
delete element.attributes;
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-22 21:23:28 +00:00
|
|
|
// Automatically initialize attributes object
|
|
|
|
if ( !element.attributes ) {
|
|
|
|
element.attributes = {};
|
|
|
|
}
|
|
|
|
// Set
|
|
|
|
element.attributes[op.key] = to;
|
2011-11-14 23:04:36 +00:00
|
|
|
}
|
2011-12-07 00:06:30 +00:00
|
|
|
var node = this.model.getNodeFromOffset( this.cursor + 1 );
|
2012-01-23 18:46:31 +00:00
|
|
|
if ( node.hasChildren() ) {
|
|
|
|
node.traverseLeafNodes( function( leafNode ) {
|
|
|
|
leafNode.emit( 'update' );
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
node.emit( 'update' );
|
|
|
|
}
|
2011-11-14 23:04:36 +00:00
|
|
|
};
|
|
|
|
|
2012-03-14 21:02:36 +00:00
|
|
|
ve.dm.TransactionProcessor.prototype.mark = function( op ) {
|
|
|
|
var invert = this.method == 'rollback',
|
|
|
|
target;
|
2011-11-14 23:04:36 +00:00
|
|
|
if ( ( op.method === 'set' && !invert ) || ( op.method === 'clear' && invert ) ) {
|
|
|
|
target = this.set;
|
|
|
|
} else if ( ( op.method === 'clear' && !invert ) || ( op.method === 'set' && invert ) ) {
|
|
|
|
target = this.clear;
|
|
|
|
} else {
|
|
|
|
throw 'Invalid method error. Can not operate attributes this way: ' + method;
|
|
|
|
}
|
|
|
|
if ( op.bias === 'start' ) {
|
|
|
|
target.push( op.annotation );
|
|
|
|
} else if ( op.bias === 'stop' ) {
|
2011-11-30 19:21:33 +00:00
|
|
|
var index;
|
|
|
|
if ( op.annotation instanceof RegExp ) {
|
|
|
|
index = target.indexOf( op.annotation );
|
|
|
|
} else {
|
2012-02-06 23:50:56 +00:00
|
|
|
index = ve.dm.DocumentNode.getIndexOfAnnotation( target, op.annotation );
|
2011-11-30 19:21:33 +00:00
|
|
|
}
|
2011-11-14 23:04:36 +00:00
|
|
|
if ( index === -1 ) {
|
|
|
|
throw 'Annotation stack error. Annotation is missing.';
|
|
|
|
}
|
|
|
|
target.splice( index, 1 );
|
|
|
|
}
|
|
|
|
};
|