2012-04-19 23:22:08 +00:00
|
|
|
/**
|
2012-04-30 23:58:41 +00:00
|
|
|
* DataModel transaction.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
*/
|
2012-04-19 23:22:41 +00:00
|
|
|
ve.dm.Transaction = function() {
|
|
|
|
this.operations = [];
|
2012-04-19 23:22:08 +00:00
|
|
|
this.lengthDifference = 0;
|
|
|
|
};
|
|
|
|
|
2012-05-17 17:50:52 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
2012-05-31 21:39:34 +00:00
|
|
|
/**
|
|
|
|
* Generates a transaction that inserts data at a given offset.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Document} doc Document to create transaction for
|
|
|
|
* @param {Integer} offset Offset to insert at
|
|
|
|
* @param {Array} data Data to insert
|
|
|
|
* @returns {ve.dm.Transaction} Transcation that inserts data
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.newFromInsertion = function( doc, offset, insertion ) {
|
|
|
|
var tx = new ve.dm.Transaction(),
|
|
|
|
data = doc.getData();
|
|
|
|
// Fix up the insertion
|
|
|
|
insertion = doc.fixupInsertion( insertion, offset );
|
|
|
|
// Retain up to insertion point, if needed
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( offset );
|
2012-05-31 21:39:34 +00:00
|
|
|
// Insert data
|
|
|
|
tx.pushReplace( [], insertion );
|
|
|
|
// Retain to end of document, if needed (for completeness)
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( data.length - offset );
|
2012-05-22 20:17:27 +00:00
|
|
|
return tx;
|
2012-05-17 17:50:52 +00:00
|
|
|
};
|
|
|
|
|
2012-05-31 21:39:34 +00:00
|
|
|
/**
|
|
|
|
* Generates a transaction which removes data from a given range.
|
|
|
|
*
|
|
|
|
* There are three possible results from a removal:
|
|
|
|
* 1. Remove content only
|
|
|
|
* - Occurs when the range starts and ends on elements of different type, depth or ancestry
|
|
|
|
* 2. Remove entire elements and their content
|
2012-06-05 13:54:35 +00:00
|
|
|
* - Occurs when the range spans across an entire element
|
2012-05-31 21:39:34 +00:00
|
|
|
* 3. Merge two elements by removing the end of one and the beginning of another
|
|
|
|
* - Occurs when the range starts and ends on elements of similar type, depth and ancestry
|
|
|
|
*
|
|
|
|
* This function uses the following logic to decide what to actually remove:
|
|
|
|
* 1. Elements are only removed if range being removed covers the entire element
|
2012-06-05 13:54:35 +00:00
|
|
|
* 2. Elements can only be merged if ve.dm.Node.canBeMergedWith() returns true
|
2012-05-31 21:39:34 +00:00
|
|
|
* 3. Merges take place at the highest common ancestor
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Document} doc Document to create transaction for
|
|
|
|
* @param {ve.Range} range Range of data to remove
|
|
|
|
* @returns {ve.dm.Transaction} Transcation that removes data
|
|
|
|
* @throws 'Invalid range, can not remove from {range.start} to {range.end}'
|
|
|
|
*/
|
2012-05-17 17:50:52 +00:00
|
|
|
ve.dm.Transaction.newFromRemoval = function( doc, range ) {
|
2012-05-31 21:39:34 +00:00
|
|
|
var tx = new ve.dm.Transaction(),
|
|
|
|
data = doc.getData();
|
|
|
|
// Normalize and validate range
|
|
|
|
range.normalize();
|
|
|
|
if ( range.start === range.end ) {
|
|
|
|
// Empty range, nothing to remove, retain up to the end of the document (for completeness)
|
|
|
|
tx.pushRetain( data.length );
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
// Select nodes and validate selection
|
2012-06-06 18:30:43 +00:00
|
|
|
var selection = doc.selectNodes( range, 'covered' );
|
2012-05-31 21:39:34 +00:00
|
|
|
if ( selection.length === 0 ) {
|
|
|
|
// Empty selection? Something is wrong!
|
2012-06-05 13:54:35 +00:00
|
|
|
throw 'Invalid range, cannot remove from ' + range.start + ' to ' + range.end;
|
2012-05-31 21:39:34 +00:00
|
|
|
}
|
2012-06-04 23:21:21 +00:00
|
|
|
|
2012-06-06 18:30:43 +00:00
|
|
|
var first, last, offset = 0, removeStart = null, removeEnd = null, nodeStart, nodeEnd;
|
|
|
|
first = selection[0];
|
|
|
|
last = selection[selection.length - 1];
|
|
|
|
// If the first and last node are mergeable, merge them
|
|
|
|
if ( first.node.canBeMergedWith( last.node ) ) {
|
|
|
|
if ( !first.range && !last.range ) {
|
|
|
|
// First and last node are both completely covered, remove them
|
|
|
|
removeStart = first.nodeOuterRange.start;
|
|
|
|
removeEnd = last.nodeOuterRange.end;
|
|
|
|
} else {
|
|
|
|
// Either the first node or the last node is partially covered, so remove
|
|
|
|
// the selected content
|
|
|
|
removeStart = ( first.range || first.nodeRange ).start;
|
|
|
|
removeEnd = ( last.range || last.nodeRange ).end;
|
|
|
|
}
|
|
|
|
tx.pushRetain( removeStart );
|
|
|
|
tx.pushReplace( data.slice( removeStart, removeEnd ), [] );
|
|
|
|
tx.pushRetain( data.length - removeEnd );
|
|
|
|
// All done
|
|
|
|
return tx;
|
|
|
|
}
|
2012-06-04 23:21:21 +00:00
|
|
|
|
2012-06-06 18:30:43 +00:00
|
|
|
// The selection wasn't mergeable, so remove nodes that are completely covered, and strip
|
|
|
|
// nodes that aren't
|
|
|
|
for ( i = 0; i < selection.length; i++ ) {
|
|
|
|
if ( !selection[i].range ) {
|
|
|
|
// Entire node is covered, remove it
|
|
|
|
nodeStart = selection[i].nodeOuterRange.start;
|
|
|
|
nodeEnd = selection[i].nodeOuterRange.end;
|
|
|
|
} else {
|
|
|
|
// Part of the node is covered, remove that range
|
|
|
|
nodeStart = selection[i].range.start;
|
|
|
|
nodeEnd = selection[i].range.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge contiguous removals. Only apply a removal when a gap appears, or at the
|
|
|
|
// end of the loop
|
|
|
|
if ( removeEnd === null ) {
|
|
|
|
// First removal
|
|
|
|
removeStart = nodeStart;
|
|
|
|
removeEnd = nodeEnd;
|
|
|
|
} else if ( removeEnd === nodeStart ) {
|
|
|
|
// Merge this removal into the previous one
|
|
|
|
removeEnd = nodeEnd;
|
|
|
|
} else {
|
|
|
|
// There is a gap between the previous removal and this one
|
|
|
|
|
|
|
|
// Push the previous removal first
|
|
|
|
tx.pushRetain( removeStart - offset );
|
|
|
|
tx.pushReplace( data.slice( removeStart, removeEnd ), [] );
|
|
|
|
offset = removeEnd;
|
|
|
|
|
|
|
|
// Now start this removal
|
|
|
|
removeStart = nodeStart;
|
|
|
|
removeEnd = nodeEnd;
|
2012-05-31 21:39:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-06-06 18:30:43 +00:00
|
|
|
// Apply the last removal, if any
|
|
|
|
if ( removeEnd !== null ) {
|
|
|
|
tx.pushRetain( removeStart - offset );
|
|
|
|
tx.pushReplace( data.slice( removeStart, removeEnd ), [] );
|
|
|
|
offset = removeEnd;
|
|
|
|
}
|
|
|
|
// Retain up to the end of the document
|
|
|
|
tx.pushRetain( data.length - offset );
|
2012-05-31 21:39:34 +00:00
|
|
|
return tx;
|
2012-05-17 17:50:52 +00:00
|
|
|
};
|
|
|
|
|
2012-05-17 22:17:17 +00:00
|
|
|
/**
|
2012-05-31 21:39:34 +00:00
|
|
|
* Generates a transaction that changes an attribute.
|
2012-05-17 22:17:17 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Document} doc Document to create transaction for
|
|
|
|
* @param {Integer} offset Offset of element
|
|
|
|
* @param {String} key Attribute name
|
|
|
|
* @param {Mixed} value New value
|
|
|
|
* @returns {ve.dm.Transaction} Transcation that changes an element
|
|
|
|
* @throws 'Can not set attributes to non-element data'
|
|
|
|
* @throws 'Can not set attributes on closing element'
|
|
|
|
*/
|
2012-05-17 17:50:52 +00:00
|
|
|
ve.dm.Transaction.newFromAttributeChange = function( doc, offset, key, value ) {
|
2012-05-17 22:17:17 +00:00
|
|
|
var tx = new ve.dm.Transaction(),
|
|
|
|
data = doc.getData();
|
|
|
|
// Verify element exists at offset
|
|
|
|
if ( data[offset].type === undefined ) {
|
|
|
|
throw 'Can not set attributes to non-element data';
|
|
|
|
}
|
|
|
|
// Verify element is not a closing
|
|
|
|
if ( data[offset].type.charAt( 0 ) === '/' ) {
|
|
|
|
throw 'Can not set attributes on closing element';
|
|
|
|
}
|
|
|
|
// Retain up to element
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( offset );
|
2012-05-17 22:17:17 +00:00
|
|
|
// Change attribute
|
|
|
|
tx.pushReplaceElementAttribute(
|
|
|
|
key, 'attributes' in data[offset] ? data[offset].attributes[key] : undefined, value
|
|
|
|
);
|
|
|
|
// Retain to end of document
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( data.length - offset );
|
2012-05-17 22:17:17 +00:00
|
|
|
return tx;
|
2012-05-17 17:50:52 +00:00
|
|
|
};
|
|
|
|
|
2012-05-17 22:17:17 +00:00
|
|
|
/**
|
2012-05-31 21:39:34 +00:00
|
|
|
* Generates a transaction that annotates content.
|
2012-05-17 22:17:17 +00:00
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Document} doc Document to create transaction for
|
|
|
|
* @param {ve.Range} range Range to annotate
|
|
|
|
* @param {String} method Annotation mode
|
|
|
|
* 'set': Adds annotation to all content in range
|
|
|
|
* 'clear': Removes instances of annotation from content in range
|
|
|
|
* @param {Object} annotation Annotation to set or clear
|
|
|
|
* @returns {ve.dm.Transaction} Transcation that annotates content
|
|
|
|
*/
|
2012-05-17 17:50:52 +00:00
|
|
|
ve.dm.Transaction.newFromAnnotation = function( doc, range, method, annotation ) {
|
2012-05-17 22:17:17 +00:00
|
|
|
var tx = new ve.dm.Transaction(),
|
|
|
|
data = doc.getData(),
|
|
|
|
hash = ve.getHash( annotation );
|
|
|
|
// Iterate over all data in range, annotating where appropriate
|
|
|
|
range.normalize();
|
|
|
|
var i = range.start,
|
|
|
|
span = i,
|
|
|
|
on = false;
|
|
|
|
while ( i < range.end ) {
|
|
|
|
if ( data[i].type !== undefined ) {
|
|
|
|
// Element
|
|
|
|
if ( on ) {
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( span );
|
2012-05-17 22:17:17 +00:00
|
|
|
tx.pushStopAnnotating( method, annotation );
|
|
|
|
span = 0;
|
|
|
|
on = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Content
|
|
|
|
var covered = doc.offsetContainsAnnotation( i, annotation );
|
|
|
|
if ( ( covered && method === 'set' ) || ( !covered && method === 'clear' ) ) {
|
|
|
|
// Skip annotated content
|
|
|
|
if ( on ) {
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( span );
|
2012-05-17 22:17:17 +00:00
|
|
|
tx.pushStopAnnotating( method, annotation );
|
|
|
|
span = 0;
|
|
|
|
on = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Cover non-annotated content
|
|
|
|
if ( !on ) {
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( span );
|
2012-05-17 22:17:17 +00:00
|
|
|
tx.pushStartAnnotating( method, annotation );
|
|
|
|
span = 0;
|
|
|
|
on = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
span++;
|
|
|
|
i++;
|
|
|
|
}
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( span );
|
2012-05-17 22:17:17 +00:00
|
|
|
if ( on ) {
|
|
|
|
tx.pushStopAnnotating( method, annotation );
|
|
|
|
}
|
2012-06-04 23:21:21 +00:00
|
|
|
tx.pushRetain( data.length - range.end );
|
2012-05-17 22:17:17 +00:00
|
|
|
return tx;
|
2012-05-17 17:50:52 +00:00
|
|
|
};
|
|
|
|
|
2012-06-11 22:58:29 +00:00
|
|
|
/**
|
|
|
|
* Generates a transaction that converts elements that can contain content.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @method
|
|
|
|
* @param {ve.dm.Document} doc Document to create transaction for
|
|
|
|
* @param {ve.Range} range Range to convert
|
|
|
|
* @param {String} type Symbolic name of element type to convert to
|
|
|
|
* @param {Object} attr Attributes to initialize element with
|
|
|
|
* @returns {ve.dm.Transaction} Transcation that annotates content
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.newFromContentBranchConversion = function( doc, range, type, attr ) {
|
|
|
|
var tx = new ve.dm.Transaction(),
|
|
|
|
data = doc.getData(),
|
|
|
|
selection = doc.selectNodes( range, 'leaves' ),
|
|
|
|
opening = { 'type': type },
|
|
|
|
closing = { 'type': '/' + type },
|
|
|
|
previousBranch,
|
|
|
|
previousBranchOuterRange;
|
|
|
|
// Add attributes to opening if needed
|
|
|
|
if ( ve.isPlainObject( attr ) ) {
|
|
|
|
opening.attributes = attr;
|
|
|
|
}
|
|
|
|
// Replace the wrappings of each content branch in the range
|
|
|
|
for ( var i = 0; i < selection.length; i++ ) {
|
|
|
|
var selected = selection[i];
|
|
|
|
if ( selected.node.isContent() ) {
|
|
|
|
var branch = selected.node.getParent(),
|
|
|
|
branchOuterRange = branch.getOuterRange();
|
|
|
|
// Don't convert the same branch twice
|
|
|
|
if ( branch === previousBranch ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Retain up to this branch, considering where the previous one left off
|
|
|
|
tx.pushRetain(
|
|
|
|
branchOuterRange.start - ( previousBranch ? previousBranchOuterRange.end : 0 )
|
|
|
|
);
|
|
|
|
// Replace the opening
|
|
|
|
tx.pushReplace( [data[branchOuterRange.start]], [ve.copyObject( opening )] );
|
|
|
|
// Retain the contents
|
|
|
|
tx.pushRetain( branch.getLength() );
|
|
|
|
// Replace the closing
|
|
|
|
tx.pushReplace( [data[branchOuterRange.end - 1]], [ve.copyObject( closing )] );
|
|
|
|
// Remember this branch and it's range for next time
|
|
|
|
previousBranch = branch;
|
|
|
|
previousBranchOuterRange = branchOuterRange;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Retain until the end
|
|
|
|
tx.pushRetain(
|
|
|
|
data.length - ( previousBranch ? previousBranchOuterRange.end : 0 )
|
|
|
|
);
|
|
|
|
return tx;
|
|
|
|
};
|
|
|
|
|
2012-04-19 23:22:08 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a list of all operations.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @returns {Object[]} List of operations
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.getOperations = function() {
|
|
|
|
return this.operations;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the difference in content length this transaction will cause if applied.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @returns {Integer} Difference in content length
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.getLengthDifference = function() {
|
|
|
|
return this.lengthDifference;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a retain operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @param {Integer} length Length of content data to retain
|
2012-06-04 23:21:21 +00:00
|
|
|
* @throws 'Invalid retain length, can not retain backwards: {length}'
|
2012-04-19 23:22:08 +00:00
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.pushRetain = function( length ) {
|
2012-06-04 23:21:21 +00:00
|
|
|
if ( length < 0 ) {
|
|
|
|
throw 'Invalid retain length, can not retain backwards:' + length;
|
|
|
|
}
|
|
|
|
if ( length ) {
|
|
|
|
var end = this.operations.length - 1;
|
|
|
|
if ( this.operations.length && this.operations[end].type === 'retain' ) {
|
|
|
|
this.operations[end].length += length;
|
|
|
|
} else {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'retain',
|
|
|
|
'length': length
|
|
|
|
} );
|
|
|
|
}
|
2012-04-19 23:22:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a replace operation
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @param {Array} remove Data to remove
|
2012-05-11 22:25:01 +00:00
|
|
|
* @param {Array] insert Data to replace 'remove' with
|
2012-04-19 23:22:08 +00:00
|
|
|
*/
|
2012-05-11 22:25:01 +00:00
|
|
|
ve.dm.Transaction.prototype.pushReplace = function( remove, insert ) {
|
2012-06-06 17:58:08 +00:00
|
|
|
if ( remove.length === 0 && insert.length === 0 ) {
|
|
|
|
// Don't push no-ops
|
|
|
|
return;
|
|
|
|
}
|
2012-04-19 23:22:08 +00:00
|
|
|
this.operations.push( {
|
|
|
|
'type': 'replace',
|
|
|
|
'remove': remove,
|
2012-05-11 22:25:01 +00:00
|
|
|
'insert': insert
|
2012-04-19 23:22:08 +00:00
|
|
|
} );
|
2012-05-11 22:25:01 +00:00
|
|
|
this.lengthDifference += insert.length - remove.length;
|
2012-04-19 23:22:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds an element attribute change operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} key Name of attribute to change
|
|
|
|
* @param {Mixed} from Value change attribute from
|
|
|
|
* @param {Mixed} to Value to change attribute to
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.pushReplaceElementAttribute = function( key, from, to ) {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'attribute',
|
|
|
|
'key': key,
|
|
|
|
'from': from,
|
|
|
|
'to': to
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a start annotating operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} method Method to use, either "set" or "clear"
|
|
|
|
* @param {Object} annotation Annotation object to start setting or clearing from content data
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.pushStartAnnotating = function( method, annotation ) {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'annotate',
|
|
|
|
'method': method,
|
|
|
|
'bias': 'start',
|
|
|
|
'annotation': annotation
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a stop annotating operation.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-04-19 23:22:08 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} method Method to use, either "set" or "clear"
|
|
|
|
* @param {Object} annotation Annotation object to stop setting or clearing from content data
|
|
|
|
*/
|
|
|
|
ve.dm.Transaction.prototype.pushStopAnnotating = function( method, annotation ) {
|
|
|
|
this.operations.push( {
|
|
|
|
'type': 'annotate',
|
|
|
|
'method': method,
|
|
|
|
'bias': 'stop',
|
|
|
|
'annotation': annotation
|
|
|
|
} );
|
|
|
|
};
|