| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import Document from '../../src/model/document';
- import DocumentFragment from '../../src/model/documentfragment';
- import Element from '../../src/model/element';
- import Text from '../../src/model/text';
- import Position from '../../src/model/position';
- import LivePosition from '../../src/model/liveposition';
- import Range from '../../src/model/range';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- describe( 'LivePosition', () => {
- let doc, root, ul, p, li1, li2;
- before( () => {
- doc = new Document();
- root = doc.createRoot();
- li1 = new Element( 'li', [], new Text( 'abcdef' ) );
- li2 = new Element( 'li', [], new Text( 'foobar' ) );
- ul = new Element( 'ul', [], [ li1, li2 ] );
- p = new Element( 'p', [], new Text( 'qwerty' ) );
- root.insertChildren( 0, [ p, ul ] );
- } );
- it( 'should be an instance of Position', () => {
- const live = new LivePosition( root, [ 0 ] );
- live.detach();
- expect( live ).to.be.instanceof( Position );
- } );
- it( 'should throw if given root is not a RootElement', () => {
- expect( () => {
- new LivePosition( new DocumentFragment(), [ 1 ] ); // eslint-disable-line no-new
- } ).to.throw( CKEditorError, /model-liveposition-root-not-rootelement/ );
- } );
- it( 'should listen to a change event of the document that owns this position root', () => {
- sinon.spy( LivePosition.prototype, 'listenTo' );
- const live = new LivePosition( root, [ 0 ] );
- live.detach();
- expect( live.listenTo.calledWith( doc, 'change' ) ).to.be.true;
- LivePosition.prototype.listenTo.restore();
- } );
- it( 'should stop listening when detached', () => {
- sinon.spy( LivePosition.prototype, 'stopListening' );
- const live = new LivePosition( root, [ 0 ] );
- live.detach();
- expect( live.stopListening.called ).to.be.true;
- LivePosition.prototype.stopListening.restore();
- } );
- it( 'createFromPosition should return LivePosition', () => {
- const position = LivePosition.createFromPosition( new Position( root, [ 0 ] ) );
- expect( position ).to.be.instanceof( LivePosition );
- position.detach();
- } );
- it( 'createFromParentAndOffset should return LivePosition', () => {
- const position = LivePosition.createFromParentAndOffset( ul, 0 );
- expect( position ).to.be.instanceof( LivePosition );
- position.detach();
- } );
- it( 'createBefore should return LivePosition', () => {
- const position = LivePosition.createBefore( ul );
- expect( position ).to.be.instanceof( LivePosition );
- position.detach();
- } );
- it( 'createAfter should return LivePosition', () => {
- const position = LivePosition.createAfter( ul );
- expect( position ).to.be.instanceof( LivePosition );
- position.detach();
- } );
- describe( 'should get transformed if', () => {
- let live, spy;
- beforeEach( () => {
- live = new LivePosition( root, [ 1, 4, 6 ] );
- spy = sinon.spy();
- live.on( 'change', spy );
- } );
- afterEach( () => {
- live.detach();
- } );
- describe( 'insertion', () => {
- it( 'is in the same parent and closer offset', () => {
- const insertRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is at the same position and live position is sticking to right side', () => {
- const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is before a node from the live position path', () => {
- const insertRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- } );
- describe( 'range move', () => {
- it( 'is at the same parent and closer offset', () => {
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 3 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is at the same position and live position is sticking to right side', () => {
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 1, 4, 9 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is at a position before a node from the live position path', () => {
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 2 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 1, 6, 6 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is from the same parent and closer offset', () => {
- const moveSource = new Position( root, [ 1, 4, 0 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 1, 4, 2 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'is from a position before a node from the live position path', () => {
- const moveSource = new Position( root, [ 1, 0 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 1, 0, 6 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'contains live position (same level)', () => {
- const moveSource = new Position( root, [ 1, 4, 4 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 2, 2 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- it( 'contains live position (deep)', () => {
- const moveSource = new Position( root, [ 1, 3 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( [ 2, 1, 6 ] );
- expect( spy.calledOnce ).to.be.true;
- } );
- } );
- } );
- describe( 'should not get transformed if', () => {
- let path, otherRoot, spy, live;
- before( () => {
- path = [ 1, 4, 6 ];
- otherRoot = doc.createRoot( '$root', 'otherRoot' );
- } );
- beforeEach( () => {
- live = new LivePosition( root, path );
- spy = sinon.spy();
- live.on( 'change', spy );
- } );
- afterEach( () => {
- live.detach();
- } );
- describe( 'insertion', () => {
- it( 'is in the same parent and further offset', () => {
- const insertRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is at the same position and live position is sticking to left side', () => {
- const newLive = new LivePosition( root, path, 'sticksToPrevious' );
- spy = sinon.spy();
- newLive.on( 'change', spy );
- const insertRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( newLive.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- newLive.detach();
- } );
- it( 'is after a node from the position path', () => {
- const insertRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is in different root', () => {
- const insertRange = new Range( new Position( otherRoot, [ 1, 4, 0 ] ), new Position( otherRoot, [ 1, 4, 4 ] ) );
- doc.fire( 'change', 'insert', { range: insertRange }, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- } );
- describe( 'range move', () => {
- it( 'is at the same parent and further offset', () => {
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 4, 7 ] ), new Position( root, [ 1, 4, 9 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is at the same position and live position is sticking to left side', () => {
- const newLive = new LivePosition( root, path, 'sticksToPrevious' );
- spy = sinon.spy();
- newLive.on( 'change', spy );
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 4, 6 ] ), new Position( root, [ 1, 4, 9 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( newLive.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- newLive.detach();
- } );
- it( 'is at a position after a node from the live position path', () => {
- const moveSource = new Position( root, [ 2 ] );
- const moveRange = new Range( new Position( root, [ 1, 5 ] ), new Position( root, [ 1, 7 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is from the same parent and further offset', () => {
- const moveSource = new Position( root, [ 1, 4, 7 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is from a position after a node from the live position path', () => {
- const moveSource = new Position( root, [ 1, 5 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is to different root', () => {
- const moveSource = new Position( root, [ 2, 0 ] );
- const moveRange = new Range( new Position( otherRoot, [ 1, 0 ] ), new Position( otherRoot, [ 1, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- it( 'is from different root', () => {
- const moveSource = new Position( otherRoot, [ 1, 0 ] );
- const moveRange = new Range( new Position( root, [ 2, 0 ] ), new Position( root, [ 2, 4 ] ) );
- const changes = {
- range: moveRange,
- sourcePosition: moveSource
- };
- doc.fire( 'change', 'move', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- } );
- it( 'attributes changed', () => {
- const changes = {
- range: new Range( new Position( root, [ 1, 4, 0 ] ), new Position( root, [ 1, 4, 10 ] ) ),
- key: 'foo',
- oldValue: null,
- newValue: 'bar'
- };
- doc.fire( 'change', 'setAttribute', changes, null );
- expect( live.path ).to.deep.equal( path );
- expect( spy.called ).to.be.false;
- } );
- } );
- } );
|