mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-29 16:44:51 +00:00
d3af8b877b
Code with a similar purpose was added in568e0e5701
but got lost when some things were moved from ve.Surface to ve.ce.Surface in5012ed10
. Initializing the selection at (0,0) was known to cause problems before, and since789d0caf09
breaks editing of empty documents: typing in an empty document begins in an inline slug, but SurfaceObserver doesn't notice typing in an inline slug unless the ce.Surface pawns it, which is OK because insertions in slugs are always pawned, but the pawning logic believes the cursor to be at offset 0 where there is no slug (it's at offset 1) and so it doesn't pawn. Bonus: update tests and add descriptions for dm.Surface.change tests Change-Id: Id72314d0fe650dacc7cdb842f5cea2f3bfba5145
83 lines
2.6 KiB
JavaScript
83 lines
2.6 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel Surface tests.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
QUnit.module( 've.dm.Surface' );
|
|
|
|
ve.dm.SurfaceStub = function VeDmSurfaceStub( data ) {
|
|
if ( data !== undefined ) {
|
|
this.dm = new ve.dm.Document( data );
|
|
} else {
|
|
this.dm = new ve.dm.Document( [{ 'type': 'paragraph' }, 'h', 'i', { 'type': '/paragraph' }] );
|
|
}
|
|
|
|
// Inheritance
|
|
ve.dm.Surface.call( this, this.dm );
|
|
};
|
|
|
|
ve.inheritClass( ve.dm.SurfaceStub, ve.dm.Surface );
|
|
|
|
// Tests
|
|
|
|
QUnit.test( 'getDocument', 1, function ( assert ) {
|
|
var surface = new ve.dm.SurfaceStub();
|
|
assert.strictEqual( surface.getDocument(), surface.documentModel );
|
|
} );
|
|
|
|
QUnit.test( 'getSelection', 1, function ( assert ) {
|
|
var surface = new ve.dm.SurfaceStub();
|
|
assert.strictEqual( surface.getSelection(), surface.selection );
|
|
} );
|
|
|
|
QUnit.test( 'change', 3, function ( assert ) {
|
|
var surface = new ve.dm.SurfaceStub(),
|
|
tx = new ve.dm.Transaction(),
|
|
events = {
|
|
'transact': 0,
|
|
'select': 0,
|
|
'change': 0
|
|
};
|
|
|
|
surface.on( 'transact', function () {
|
|
events.transact++;
|
|
} );
|
|
surface.on( 'select', function () {
|
|
events.select++;
|
|
} );
|
|
surface.on( 'change', function () {
|
|
events.change++;
|
|
} );
|
|
surface.change( tx );
|
|
assert.deepEqual( events, { 'transact': 1, 'select': 0, 'change': 1 }, 'transaction without selection' );
|
|
surface.change( null, new ve.Range( 2, 2 ) );
|
|
assert.deepEqual( events, { 'transact': 1, 'select': 1, 'change': 2 }, 'selection without transaction' );
|
|
surface.change( tx, new ve.Range( 3, 3 ) );
|
|
assert.deepEqual( events, { 'transact': 2, 'select': 2, 'change': 3 }, 'transaction and selection' );
|
|
} );
|
|
|
|
QUnit.test( 'breakpoint', 7, function ( assert ) {
|
|
var surface = new ve.dm.SurfaceStub(),
|
|
tx = new ve.dm.Transaction.newFromInsertion( surface.dm, 1, ['x'] ),
|
|
selection = new ve.Range( 1, 1 );
|
|
|
|
assert.equal( surface.breakpoint(), false, 'Returns false if no transactions applied' );
|
|
|
|
surface.change( tx, selection );
|
|
assert.deepEqual( surface.bigStack, [], 'Big stack data matches before breakpoint' );
|
|
assert.deepEqual( surface.smallStack, [tx], 'Small stack data matches before breakpoint' );
|
|
|
|
assert.equal( surface.breakpoint(), true, 'Returns true after transaction applied' );
|
|
assert.equal( surface.breakpoint(), false, 'Returns false if no transactions applied since last breakpoint' );
|
|
|
|
assert.deepEqual( surface.bigStack, [ {
|
|
'stack': [tx],
|
|
'selection': selection
|
|
} ],
|
|
'Big stack data matches after breakpoint'
|
|
);
|
|
assert.deepEqual( surface.smallStack, [], 'Small stack data matches after breakpoint' );
|
|
} );
|