| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: model, delta */
- import { getNodesAndText } from '/tests/engine/model/_utils/utils.js';
- import Document from '/ckeditor5/engine/model/document.js';
- import Position from '/ckeditor5/engine/model/position.js';
- import Range from '/ckeditor5/engine/model/range.js';
- import Element from '/ckeditor5/engine/model/element.js';
- import Text from '/ckeditor5/engine/model/text.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- import MoveDelta from '/ckeditor5/engine/model/delta/movedelta.js';
- import MoveOperation from '/ckeditor5/engine/model/operation/moveoperation.js';
- describe( 'Batch', () => {
- let doc, root, div, p, batch, chain;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- div = new Element( 'div', [], new Text( 'foobar' ) );
- p = new Element( 'p', [], new Text( 'abcxyz' ) );
- div.insertChildren( 0, [ new Element( 'p', [], new Text( 'gggg' ) ) ] );
- div.insertChildren( 2, [ new Element( 'p', [], new Text( 'hhhh' ) ) ] );
- root.insertChildren( 0, [ div, p ] );
- batch = doc.batch();
- } );
- describe( 'move', () => {
- it( 'should move specified node', () => {
- batch.move( div, new Position( root, [ 2 ] ) );
- expect( root.maxOffset ).to.equal( 2 );
- expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'abcxyz' );
- expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'PggggPfoobarPhhhhP' );
- } );
- it( 'should move flat range of nodes', () => {
- let range = new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 7 ] ) );
- batch.move( range, new Position( root, [ 1, 3 ] ) );
- expect( getNodesAndText( Range.createIn( root.getChild( 0 ) ) ) ).to.equal( 'PggggPfoPhhhhP' );
- expect( getNodesAndText( Range.createIn( root.getChild( 1 ) ) ) ).to.equal( 'abcobarxyz' );
- } );
- it( 'should throw if given range is not flat', () => {
- let notFlatRange = new Range( new Position( root, [ 0, 2, 2 ] ), new Position( root, [ 0, 6 ] ) );
- expect( () => {
- doc.batch().move( notFlatRange, new Position( root, [ 1, 3 ] ) );
- } ).to.throw( CKEditorError, /^batch-move-range-not-flat/ );
- } );
- it( 'should be chainable', () => {
- chain = batch.move( div, new Position( root, [ 1, 3 ] ) );
- expect( chain ).to.equal( batch );
- } );
- it( 'should add delta to batch and operation to delta before applying operation', () => {
- sinon.spy( doc, 'applyOperation' );
- batch.move( div, new Position( root, [ 2 ] ) );
- const correctDeltaMatcher = sinon.match( ( operation ) => {
- return operation.delta && operation.delta.batch && operation.delta.batch == batch;
- } );
- expect( doc.applyOperation.calledWith( correctDeltaMatcher ) ).to.be.true;
- } );
- } );
- } );
- describe( 'MoveDelta', () => {
- let moveDelta, doc, root;
- beforeEach( () => {
- doc = new Document();
- root = doc.createRoot();
- moveDelta = new MoveDelta();
- } );
- describe( 'constructor', () => {
- it( 'should create move delta with no operations added', () => {
- expect( moveDelta.operations.length ).to.equal( 0 );
- } );
- } );
- describe( 'sourcePosition', () => {
- it( 'should be null if there are no operations in delta', () => {
- expect( moveDelta.sourcePosition ).to.be.null;
- } );
- it( 'should be equal to the position where node is inserted', () => {
- moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
- expect( moveDelta.sourcePosition.root ).to.equal( root );
- expect( moveDelta.sourcePosition.path ).to.deep.equal( [ 1, 1 ] );
- } );
- } );
- describe( 'howMany', () => {
- it( 'should be null if there are no operations in delta', () => {
- expect( moveDelta.howMany ).to.be.null;
- } );
- it( 'should be equal to the position where node is inserted', () => {
- moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
- expect( moveDelta.howMany ).to.equal( 2 );
- } );
- } );
- describe( 'targetPosition', () => {
- it( 'should be null if there are no operations in delta', () => {
- expect( moveDelta.targetPosition ).to.be.null;
- } );
- it( 'should be equal to the move operation\'s target position', () => {
- moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
- expect( moveDelta.targetPosition.root ).to.equal( root );
- expect( moveDelta.targetPosition.path ).to.deep.equal( [ 2, 2 ] );
- } );
- } );
- describe( 'getReversed', () => {
- it( 'should return empty MoveDelta if there are no operations in delta', () => {
- let reversed = moveDelta.getReversed();
- expect( reversed ).to.be.instanceof( MoveDelta );
- expect( reversed.operations.length ).to.equal( 0 );
- } );
- it( 'should return correct MoveDelta', () => {
- moveDelta.operations.push( new MoveOperation( new Position( root, [ 1, 1 ] ), 2, new Position( root, [ 2, 2 ] ), 0 ) );
- let reversed = moveDelta.getReversed();
- expect( reversed ).to.be.instanceof( MoveDelta );
- expect( reversed.operations.length ).to.equal( 1 );
- expect( reversed.operations[ 0 ] ).to.be.instanceof( MoveOperation );
- expect( reversed.operations[ 0 ].sourcePosition.path ).to.deep.equal( [ 2, 2 ] );
- expect( reversed.operations[ 0 ].howMany ).to.equal( 2 );
- expect( reversed.operations[ 0 ].targetPosition.path ).to.deep.equal( [ 1, 1 ] );
- } );
- } );
- it( 'should provide proper className', () => {
- expect( MoveDelta.className ).to.equal( 'engine.model.delta.MoveDelta' );
- } );
- } );
|