2012-05-01 02:06:20 +00:00
|
|
|
module( 've.dm.TransactionProcessor' );
|
|
|
|
|
|
|
|
/* Tests */
|
|
|
|
|
|
|
|
test( 'retain', 2, function() {
|
|
|
|
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Transaction for retaining part of the document (lettting the processor retain the rest)
|
2012-05-01 02:06:20 +00:00
|
|
|
var tx = new ve.dm.Transaction();
|
|
|
|
tx.pushRetain( 38 );
|
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Commit and test
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( doc, tx );
|
|
|
|
deepEqual( doc.getData(), ve.dm.example.data,
|
2012-05-04 22:50:50 +00:00
|
|
|
'commits retain transaction without changing data' );
|
|
|
|
|
|
|
|
// Roll back and test
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.rollback( doc, tx );
|
|
|
|
deepEqual( doc.getData(), ve.dm.example.data,
|
2012-05-04 22:50:50 +00:00
|
|
|
'rolls back retain transaction without changing data' );
|
2012-05-01 02:06:20 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'annotate', function() {
|
|
|
|
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Transaction for setting and clearing various annotations
|
2012-05-01 02:06:20 +00:00
|
|
|
var tx = new ve.dm.Transaction();
|
|
|
|
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/bold' } );
|
|
|
|
tx.pushRetain( 2 );
|
|
|
|
tx.pushStartAnnotating( 'clear', { 'type': 'textStyle/italic' } );
|
|
|
|
tx.pushRetain( 2 );
|
|
|
|
tx.pushStopAnnotating( 'clear', { 'type': 'textStyle/italic' } );
|
|
|
|
tx.pushRetain( 8 );
|
|
|
|
tx.pushStopAnnotating( 'set', { 'type': 'textStyle/bold' } );
|
|
|
|
tx.pushRetain( 4 );
|
|
|
|
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/underline' } );
|
|
|
|
tx.pushRetain( 4 );
|
|
|
|
tx.pushStartAnnotating( 'set', { 'type': 'textStyle/superscript' } );
|
|
|
|
tx.pushRetain( 10 );
|
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Expected document data after transaction
|
|
|
|
var data = ve.dm.example.data.slice( 0 );
|
|
|
|
data[1] = [ 'a', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
|
|
|
data[2] = [ 'b', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
|
|
|
data[3] = [ 'c', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
|
|
|
data[9] = [ 'd', { '{"type":"textStyle/bold"}': { 'type': 'textStyle/bold' } } ];
|
|
|
|
data[19] = [ 'f', { '{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' } } ];
|
|
|
|
data[28] = [ 'g', {
|
|
|
|
'{"type":"textStyle/underline"}': { 'type': 'textStyle/underline' },
|
|
|
|
'{"type":"textStyle/superscript"}': { 'type': 'textStyle/superscript' }
|
|
|
|
} ];
|
2012-05-01 02:06:20 +00:00
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Commit and test
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( doc, tx );
|
2012-05-04 22:50:50 +00:00
|
|
|
deepEqual( doc.getData(), data, 'commits complex annotation transaction' );
|
|
|
|
|
|
|
|
// Roll back and test
|
2012-05-01 02:06:20 +00:00
|
|
|
ve.dm.TransactionProcessor.rollback( doc, tx );
|
2012-05-04 22:50:50 +00:00
|
|
|
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back complex annotation' );
|
2012-05-01 02:06:20 +00:00
|
|
|
} );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
|
|
|
test( 'attribute', function() {
|
|
|
|
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Expected document data after transaction
|
|
|
|
var data = doc.getData();
|
|
|
|
data[0].attributes.level = 2;
|
|
|
|
data[11].attributes.style = 'number';
|
|
|
|
data[11].attributes.test = 'abcd';
|
|
|
|
delete data[37].attributes['html/src'];
|
|
|
|
|
|
|
|
// Transaction for adding, changing and removing attributes
|
2012-05-04 18:07:55 +00:00
|
|
|
var tx = new ve.dm.Transaction();
|
|
|
|
tx.pushReplaceElementAttribute( 'level', 1, 2 );
|
|
|
|
tx.pushRetain( 11 );
|
2012-05-04 22:50:50 +00:00
|
|
|
tx.pushReplaceElementAttribute( 'style', 'bullet', 'number' );
|
|
|
|
tx.pushReplaceElementAttribute( 'test', undefined, 'abcd' );
|
|
|
|
tx.pushRetain( 26 );
|
|
|
|
tx.pushReplaceElementAttribute( 'html/src', 'image.png', undefined );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Commit and test
|
2012-05-04 18:07:55 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( doc, tx );
|
2012-05-04 22:50:50 +00:00
|
|
|
deepEqual( doc.getData(), data, 'commits attribute changes' );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Roll back and test
|
|
|
|
ve.dm.TransactionProcessor.rollback( doc, tx );
|
|
|
|
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back attribute changes' );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Transaction for setting attributes on non-element data
|
2012-05-04 18:07:55 +00:00
|
|
|
tx = new ve.dm.Transaction();
|
|
|
|
tx.pushRetain( 1 );
|
|
|
|
tx.pushReplaceElementAttribute( 'foo', 23, 42 );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Test exception when committing
|
2012-05-04 18:07:55 +00:00
|
|
|
raises(
|
|
|
|
function() { ve.dm.TransactionProcessor.commit( doc, tx ); },
|
|
|
|
/^Invalid element error. Can not set attributes on non-element data.$/,
|
2012-05-04 22:50:50 +00:00
|
|
|
'throws exception when trying to replace attributes on content'
|
2012-05-04 18:07:55 +00:00
|
|
|
);
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'replace', function() {
|
|
|
|
var doc = new ve.dm.Document( ve.dm.example.data.slice( 0 ) );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Transaction that replaces "a" with "FOO"
|
2012-05-04 18:07:55 +00:00
|
|
|
var tx = new ve.dm.Transaction();
|
|
|
|
tx.pushRetain( 1 );
|
|
|
|
tx.pushReplace( [ 'a' ], [ 'F', 'O', 'O' ] );
|
2012-05-04 22:50:50 +00:00
|
|
|
|
|
|
|
// Expected document data after transaction
|
|
|
|
var data = ve.dm.example.data.slice( 0 );
|
|
|
|
data.splice( 1, 1, 'F', 'O', 'O' );
|
2012-05-04 18:07:55 +00:00
|
|
|
|
2012-05-04 22:50:50 +00:00
|
|
|
// Commit and test
|
2012-05-04 18:07:55 +00:00
|
|
|
ve.dm.TransactionProcessor.commit( doc, tx );
|
2012-05-04 22:50:50 +00:00
|
|
|
deepEqual( doc.getData(), data, 'commits content replace transaction' );
|
|
|
|
|
|
|
|
// Roll back and test
|
2012-05-04 18:07:55 +00:00
|
|
|
ve.dm.TransactionProcessor.rollback( doc, tx );
|
2012-05-04 22:50:50 +00:00
|
|
|
deepEqual( doc.getData(), ve.dm.example.data, 'rolls back content replace transaction' );
|
|
|
|
} );
|