Added some basic tests for resize and insert. Fixed some bugs in both of those code paths along the way.

This commit is contained in:
Trevor Parscal 2012-03-08 00:52:30 +00:00
parent de0f83643b
commit 459c4fa271
Notes: Trevor Parscal 2012-03-08 00:52:30 +00:00
3 changed files with 73 additions and 6 deletions

View file

@ -19,12 +19,12 @@ ve.dm.DocumentSynchronizer = function( model ) {
* Adds an action to the synchronizer. * Adds an action to the synchronizer.
* *
* @method * @method
* @param {String} type Type of action, can be: "insert", "delete", "rebuild", "resize" or "update"
* @param {ve.dm.Node} node Node this action is related to * @param {ve.dm.Node} node Node this action is related to
* @param {Integer} offset Offset of node, improves performance if this has already been calculated * @param {Integer} offset Offset of node, improves performance if this has already been calculated
* @param {String} type Type of action, can be: "insert", "delete", "rebuild", "resize" or "update"
* @param {Integer} adjustment Node length adjustment, if any * @param {Integer} adjustment Node length adjustment, if any
*/ */
ve.dm.DocumentSynchronizer.prototype.pushAction = function( node, offset, type, adjustment ) { ve.dm.DocumentSynchronizer.prototype.pushAction = function( type, node, offset, adjustment ) {
if ( offset === undefined ) { if ( offset === undefined ) {
offset = this.model.getOffsetFromNode( node ); offset = this.model.getOffsetFromNode( node );
} }
@ -54,10 +54,13 @@ ve.dm.DocumentSynchronizer.prototype.synchronize = function() {
switch ( action.type ) { switch ( action.type ) {
case 'insert': case 'insert':
// Insert the new node at the given offset // Insert the new node at the given offset
var target = this.model.getNodeFromOffset( offset ); var target = this.model.getNodeFromOffset( offset + 1 );
if ( target === this.model ) { if ( target === this.model ) {
// Insert at the beginning of the document // Insert at the beginning of the document
target.splice( 0, 0, action.node ); this.model.splice( 0, 0, action.node );
} else if ( target === null ) {
// Insert at the end of the document
this.model.splice( this.model.getElementLength(), 0, action.node );
} else { } else {
// Insert before the element currently at the offset // Insert before the element currently at the offset
parent = target.getParent(); parent = target.getParent();
@ -85,13 +88,13 @@ ve.dm.DocumentSynchronizer.prototype.synchronize = function() {
break; break;
case 'resize': case 'resize':
// Adjust node length - causes update events to be emitted // Adjust node length - causes update events to be emitted
node.adjustContentLength( adjustment ); action.node.adjustContentLength( action.adjustment );
// Adjust proceeding offsets by the amount the node is being lengthened or shortened // Adjust proceeding offsets by the amount the node is being lengthened or shortened
adjustment += action.adjustment; adjustment += action.adjustment;
break; break;
case 'update': case 'update':
// Emit update events // Emit update events
node.emit( 'update' ); action.node.emit( 'update' );
break; break;
} }
} }

View file

@ -33,6 +33,7 @@
<script src="../../modules/ve/dm/ve.dm.TransactionProcessor.js"></script> <script src="../../modules/ve/dm/ve.dm.TransactionProcessor.js"></script>
<script src="../../modules/ve/dm/ve.dm.Transaction.js"></script> <script src="../../modules/ve/dm/ve.dm.Transaction.js"></script>
<script src="../../modules/ve/dm/ve.dm.Surface.js"></script> <script src="../../modules/ve/dm/ve.dm.Surface.js"></script>
<script src="../../modules/ve/dm/ve.dm.DocumentSynchronizer.js"></script>
<script src="../../modules/ve/dm/nodes/ve.dm.DocumentNode.js"></script> <script src="../../modules/ve/dm/nodes/ve.dm.DocumentNode.js"></script>
<script src="../../modules/ve/dm/nodes/ve.dm.HeadingNode.js"></script> <script src="../../modules/ve/dm/nodes/ve.dm.HeadingNode.js"></script>
@ -55,6 +56,7 @@
<script src="ve.Node.test.js"></script> <script src="ve.Node.test.js"></script>
<script src="ve.BranchNode.test.js"></script> <script src="ve.BranchNode.test.js"></script>
<script src="ve.dm.TransactionProcessor.test.js"></script> <script src="ve.dm.TransactionProcessor.test.js"></script>
<script src="ve.dm.DocumentSynchronizer.test.js"></script>
<script src="ve.dm.BranchNode.test.js"></script> <script src="ve.dm.BranchNode.test.js"></script>
<script src="ve.dm.DocumentNode.test.js"></script> <script src="ve.dm.DocumentNode.test.js"></script>
</body> </body>

View file

@ -0,0 +1,62 @@
module( 've/dm' );
test( 've.dm.TransactionSynchronizer', function() {
var model,
sync,
node,
data;
// Test 1 - node resizing
model = ve.dm.DocumentNode.newFromPlainObject( veTest.obj );
sync = new ve.dm.DocumentSynchronizer( model );
// Delete bold "b" from first paragraph
model.data.splice( 2, 1 );
// Push resize action
sync.pushAction( 'resize', model.getChildren()[0], 0, -1 );
// Sync
sync.synchronize();
equal( model.getChildren()[0].getContentLength(), 2, 'resize actions adjust node lengths' );
// Test 2 - node insertion (in the middle)
model = ve.dm.DocumentNode.newFromPlainObject( veTest.obj );
sync = new ve.dm.DocumentSynchronizer( model );
// Insert element after first paragraph
data = [{ 'type': 'paragraph' }, 'x', { 'type': '/paragraph' }];
node = ve.dm.DocumentNode.createNodesFromData( data )[0];
ve.insertIntoArray( model.data, 5, data );
// Push insertion action
sync.pushAction( 'insert', node, 5 );
// Sync
sync.synchronize();
deepEqual( model.getChildren()[1].getContentData(), ['x'], 'insert actions add new nodes' );
// Test 3 - node insertion (at the start)
model = ve.dm.DocumentNode.newFromPlainObject( veTest.obj );
sync = new ve.dm.DocumentSynchronizer( model );
// Insert element after first paragraph
data = [{ 'type': 'paragraph' }, 'x', { 'type': '/paragraph' }];
node = ve.dm.DocumentNode.createNodesFromData( data )[0];
ve.insertIntoArray( model.data, 0, data );
// Push insertion action
sync.pushAction( 'insert', node, 0 );
// Sync
sync.synchronize();
deepEqual( model.getChildren()[0].getContentData(), ['x'], 'insert actions add new nodes' );
// Test 4 - node insertion (at the end)
model = ve.dm.DocumentNode.newFromPlainObject( veTest.obj );
sync = new ve.dm.DocumentSynchronizer( model );
// Insert element after first paragraph
data = [{ 'type': 'paragraph' }, 'x', { 'type': '/paragraph' }];
node = ve.dm.DocumentNode.createNodesFromData( data )[0];
ve.insertIntoArray( model.data, 34, data );
// Push insertion action
sync.pushAction( 'insert', node, 34 );
// Sync
sync.synchronize();
deepEqual( model.getChildren()[3].getContentData(), ['x'], 'insert actions add new nodes' );
} );