mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
857535b63f
* ve.dm.Converter still generates metaInline/metaBlock elements as before, it's not affected by this change * ve.dm.Document constructor splits its input into "real" data and metadata ** Metadata is stored in this.metadata (the meta-linmod) as a sparse array of arrays, with an element for each offset in this.data ** this.data itself does not contain the metadata ** This means the node tree also doesn't contain the metadata ** Which means CE doesn't know about it at all * All splice operations on the linear model are sent through ve.dm.Document.spliceData(), which performs the splice and syncs the meta-linmod ** Metadata in the removed range is reaped and added to the metadata for the offset immediately following the removal * ve.dm.Document.getFullData() splices the linmod and meta-linmod back into each other; this "full data" is then fed back to ve.dm.Converter Change-Id: Ief6dfd5b59cc13a8457993ed85c725413029c4fb
69 lines
3 KiB
JavaScript
69 lines
3 KiB
JavaScript
/**
|
|
* VisualEditor data model DocumentSynchronizer tests.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
QUnit.module( 've.dm.DocumentSynchronizer' );
|
|
|
|
/* Tests */
|
|
|
|
QUnit.test( 'getDocument', 1, function ( assert ) {
|
|
var doc = new ve.dm.Document( ve.copyArray( ve.dm.example.data ) ),
|
|
ds = new ve.dm.DocumentSynchronizer( doc );
|
|
assert.strictEqual( ds.getDocument(), doc );
|
|
} );
|
|
|
|
QUnit.test( 'synchronize', 6, function ( assert ) {
|
|
var doc = new ve.dm.Document( ve.copyArray( ve.dm.example.data ) ),
|
|
ds = new ve.dm.DocumentSynchronizer( doc ),
|
|
firstTextNodeUpdates = 0,
|
|
firstTextNodeAnnotations = 0,
|
|
firstTextNodeLengthChanges = [],
|
|
secondTextNodeUpdates = 0,
|
|
secondTextNodeAnnotations = 0,
|
|
secondTextNodeLengthChanges = [];
|
|
|
|
// Annotate "a" with bold formatting
|
|
doc.data[1] = ['a', new ve.AnnotationSet( [ new ve.dm.TextStyleBoldAnnotation() ] )];
|
|
ds.pushAnnotation( new ve.Range( 1, 2 ) );
|
|
// Insert "xyz" between "a" and "b"
|
|
doc.spliceData( 2, 0, [ 'x', 'y', 'z' ] );
|
|
ds.pushResize( doc.getDocumentNode().getNodeFromOffset( 2 ), 3 );
|
|
// Annotate "d" with italic formatting (was at 10, now at 13)
|
|
doc.data[13] = ['d', new ve.AnnotationSet( [ new ve.dm.TextStyleItalicAnnotation() ] )];
|
|
ds.pushAnnotation( new ve.Range( 10, 11 ) );
|
|
|
|
doc.getDocumentNode().getChildren()[0].getChildren()[0].on( 'update', function () {
|
|
firstTextNodeUpdates++;
|
|
} );
|
|
doc.getDocumentNode().getChildren()[0].getChildren()[0].on( 'annotation', function () {
|
|
firstTextNodeAnnotations++;
|
|
} );
|
|
doc.getDocumentNode().getChildren()[0].getChildren()[0].on( 'lengthChange', function ( diff ) {
|
|
firstTextNodeLengthChanges.push( diff );
|
|
} );
|
|
doc.getDocumentNode().getChildren()[1].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0]
|
|
.on( 'update', function () {
|
|
secondTextNodeUpdates++;
|
|
} );
|
|
doc.getDocumentNode().getChildren()[1].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0]
|
|
.on( 'annotation', function () {
|
|
secondTextNodeAnnotations++;
|
|
} );
|
|
doc.getDocumentNode().getChildren()[1].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0].getChildren()[0]
|
|
.on( 'lengthChange', function ( diff ) {
|
|
secondTextNodeLengthChanges.push( diff );
|
|
} );
|
|
ds.synchronize();
|
|
|
|
// TODO technically this should be 1, not 2, see DocumentSynchronizer.synchronizers.resize
|
|
assert.deepEqual( firstTextNodeUpdates, 2, 'annotation and insertion each trigger update event (1st paragraph)' );
|
|
assert.deepEqual( firstTextNodeAnnotations, 1, 'annotation triggers annotation event (1st paragraph)' );
|
|
assert.deepEqual( firstTextNodeLengthChanges, [ 3 ], 'insertion triggers lengthChange event (1st paragraph)' );
|
|
assert.deepEqual( secondTextNodeUpdates, 1, 'annotation triggers update event (2nd paragraph)' );
|
|
assert.deepEqual( secondTextNodeAnnotations, 1, 'annotation triggers annotation event (2nd paragraph)' );
|
|
assert.deepEqual( secondTextNodeLengthChanges, [], 'lengthChange not triggered for 2nd paragraph' );
|
|
} );
|