| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542 |
- /**
- * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- /* bender-tags: view */
- import Selection from '/ckeditor5/engine/view/selection.js';
- import Range from '/ckeditor5/engine/view/range.js';
- import Document from '/ckeditor5/engine/view/document.js';
- import Element from '/ckeditor5/engine/view/element.js';
- import Text from '/ckeditor5/engine/view/text.js';
- import Position from '/ckeditor5/engine/view/position.js';
- import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
- describe( 'Selection', () => {
- let selection;
- let el;
- let range1, range2, range3;
- beforeEach( () => {
- selection = new Selection();
- el = new Element( 'p' );
- range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
- range2 = Range.createFromParentsAndOffsets( el, 1, el, 2 );
- range3 = Range.createFromParentsAndOffsets( el, 12, el, 14 );
- } );
- describe( 'anchor', () => {
- it( 'should return null if no ranges in selection', () => {
- expect( selection.anchor ).to.be.null;
- } );
- it( 'should return start of single range in selection', () => {
- selection.addRange( range1 );
- const anchor = selection.anchor;
- expect( anchor.isEqual( range1.start ) ).to.be.true;
- expect( anchor ).to.not.equal( range1.start );
- } );
- it( 'should return end of single range in selection when added as backward', () => {
- selection.addRange( range1, true );
- const anchor = selection.anchor;
- expect( anchor.isEqual( range1.end ) ).to.be.true;
- expect( anchor ).to.not.equal( range1.end );
- } );
- it( 'should get anchor from last inserted range', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- expect( selection.anchor.isEqual( range2.start ) ).to.be.true;
- } );
- } );
- describe( 'focus', () => {
- it( 'should return null if no ranges in selection', () => {
- expect( selection.focus ).to.be.null;
- } );
- it( 'should return end of single range in selection', () => {
- selection.addRange( range1 );
- const focus = selection.focus;
- expect( focus.isEqual( range1.end ) ).to.be.true;
- } );
- it( 'should return start of single range in selection when added as backward', () => {
- selection.addRange( range1, true );
- const focus = selection.focus;
- expect( focus.isEqual( range1.start ) ).to.be.true;
- expect( focus ).to.not.equal( range1.start );
- } );
- it( 'should get focus from last inserted range', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- expect( selection.focus.isEqual( range2.end ) ).to.be.true;
- } );
- } );
- describe( 'isCollapsed', () => {
- it( 'should return true when there is single collapsed range', () => {
- const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
- selection.addRange( range );
- expect( selection.isCollapsed ).to.be.true;
- } );
- it( 'should return false when there are multiple ranges', () => {
- const range1 = Range.createFromParentsAndOffsets( el, 5, el, 5 );
- const range2 = Range.createFromParentsAndOffsets( el, 15, el, 15 );
- selection.addRange( range1 );
- selection.addRange( range2 );
- expect( selection.isCollapsed ).to.be.false;
- } );
- it( 'should return false when there is not collapsed range', () => {
- const range = Range.createFromParentsAndOffsets( el, 15, el, 16 );
- selection.addRange( range );
- expect( selection.isCollapsed ).to.be.false;
- } );
- } );
- describe( 'rangeCount', () => {
- it( 'should return proper range count', () => {
- expect( selection.rangeCount ).to.equal( 0 );
- selection.addRange( range1 );
- expect( selection.rangeCount ).to.equal( 1 );
- selection.addRange( range2 );
- expect( selection.rangeCount ).to.equal( 2 );
- } );
- } );
- describe( 'isBackward', () => {
- it( 'is defined by the last added range', () => {
- const range1 = Range.createFromParentsAndOffsets( el, 5, el, 10 );
- const range2 = Range.createFromParentsAndOffsets( el, 15, el, 16 );
- selection.addRange( range1, true );
- expect( selection ).to.have.property( 'isBackward', true );
- selection.addRange( range2 );
- expect( selection ).to.have.property( 'isBackward', false );
- } );
- it( 'is false when last range is collapsed', () => {
- const range = Range.createFromParentsAndOffsets( el, 5, el, 5 );
- selection.addRange( range, true );
- expect( selection.isBackward ).to.be.false;
- } );
- } );
- describe( 'addRange', () => {
- it( 'should throw an error when range is invalid', () => {
- expect( () => {
- selection.addRange( { invalid: 'range' } );
- } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
- } );
- it( 'should add range to selection ranges', () => {
- selection.addRange( range1 );
- expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
- } );
- it( 'should fire change event', ( done ) => {
- selection.once( 'change', () => {
- expect( selection._ranges[ 0 ].isEqual( range1 ) ).to.be.true;
- done();
- } );
- selection.addRange( range1 );
- } );
- it( 'should throw when range is intersecting with already added range', () => {
- const range2 = Range.createFromParentsAndOffsets( el, 7, el, 15 );
- selection.addRange( range1 );
- expect( () => {
- selection.addRange( range2 );
- } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
- expect( () => {
- selection.addRange( range1 );
- } ).to.throw( CKEditorError, 'view-selection-range-intersects' );
- } );
- } );
- describe( 'getRanges', () => {
- it( 'should return iterator with copies of all ranges', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- const iterable = selection.getRanges();
- const ranges = Array.from( iterable );
- expect( ranges.length ).to.equal( 2 );
- expect( ranges[ 0 ].isEqual( range1 ) ).to.be.true;
- expect( ranges[ 0 ] ).to.not.equal( range1 );
- expect( ranges[ 1 ].isEqual( range2 ) ).to.be.true;
- expect( ranges[ 1 ] ).to.not.equal( range2 );
- } );
- } );
- describe( 'getFirstRange', () => {
- it( 'should return copy of range with first position', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- selection.addRange( range3 );
- const range = selection.getFirstRange();
- expect( range.isEqual( range2 ) ).to.be.true;
- expect( range ).to.not.equal( range2 );
- } );
- it( 'should return null if no ranges are present', () => {
- expect( selection.getFirstRange() ).to.be.null;
- } );
- } );
- describe( 'getLastRange', () => {
- it( 'should return copy of range with last position', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- selection.addRange( range3 );
- const range = selection.getLastRange();
- expect( range.isEqual( range3 ) ).to.be.true;
- expect( range ).to.not.equal( range3 );
- } );
- it( 'should return null if no ranges are present', () => {
- expect( selection.getLastRange() ).to.be.null;
- } );
- } );
- describe( 'getFirstPosition', () => {
- it( 'should return copy of first position', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- selection.addRange( range3 );
- const position = selection.getFirstPosition();
- expect( position.isEqual( range2.start ) ).to.be.true;
- expect( position ).to.not.equal( range2.start );
- } );
- it( 'should return null if no ranges are present', () => {
- expect( selection.getFirstPosition() ).to.be.null;
- } );
- } );
- describe( 'getLastPosition', () => {
- it( 'should return copy of range with last position', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- selection.addRange( range3 );
- const position = selection.getLastPosition();
- expect( position.isEqual( range3.end ) ).to.be.true;
- expect( position ).to.not.equal( range3.end );
- } );
- it( 'should return null if no ranges are present', () => {
- expect( selection.getLastPosition() ).to.be.null;
- } );
- } );
- describe( 'isEqual', () => {
- it( 'should return true if selections equal', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- const otherSelection = new Selection();
- otherSelection.addRange( range1 );
- otherSelection.addRange( range2 );
- expect( selection.isEqual( otherSelection ) ).to.be.true;
- } );
- it( 'should return true if backward selections equal', () => {
- selection.addRange( range1, true );
- const otherSelection = new Selection();
- otherSelection.addRange( range1, true );
- expect( selection.isEqual( otherSelection ) ).to.be.true;
- } );
- it( 'should return false if ranges count does not equal', () => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- const otherSelection = new Selection();
- otherSelection.addRange( range1 );
- expect( selection.isEqual( otherSelection ) ).to.be.false;
- } );
- it( 'should return false if ranges do not equal', () => {
- selection.addRange( range1 );
- const otherSelection = new Selection();
- otherSelection.addRange( range2 );
- expect( selection.isEqual( otherSelection ) ).to.be.false;
- } );
- it( 'should return false if directions do not equal', () => {
- selection.addRange( range1 );
- const otherSelection = new Selection();
- otherSelection.addRange( range1, true );
- expect( selection.isEqual( otherSelection ) ).to.be.false;
- } );
- } );
- describe( 'removeAllRanges', () => {
- it( 'should remove all ranges and fire change event', ( done ) => {
- selection.addRange( range1 );
- selection.addRange( range2 );
- selection.once( 'change', () => {
- expect( selection.rangeCount ).to.equal( 0 );
- done();
- } );
- selection.removeAllRanges();
- } );
- it( 'should do nothing when no ranges are present', () => {
- const fireSpy = sinon.spy( selection, 'fire' );
- selection.removeAllRanges();
- fireSpy.restore();
- expect( fireSpy.notCalled ).to.be.true;
- } );
- } );
- describe( 'setRanges', () => {
- it( 'should throw an error when range is invalid', () => {
- expect( () => {
- selection.setRanges( [ { invalid: 'range' } ] );
- } ).to.throw( CKEditorError, 'view-selection-invalid-range: Invalid Range.' );
- } );
- it( 'should add ranges and fire change event', ( done ) => {
- selection.addRange( range1 );
- selection.once( 'change', () => {
- expect( selection.rangeCount ).to.equal( 2 );
- expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
- expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
- expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
- expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
- done();
- } );
- selection.setRanges( [ range2, range3 ] );
- } );
- } );
- describe( 'setTo', () => {
- it( 'should return true if selections equal', () => {
- selection.addRange( range1 );
- const otherSelection = new Selection();
- otherSelection.addRange( range2 );
- otherSelection.addRange( range3, true );
- selection.setTo( otherSelection );
- expect( selection.rangeCount ).to.equal( 2 );
- expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
- expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
- expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
- expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
- expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
- } );
- it( 'should fire change event', ( done ) => {
- selection.on( 'change', () => {
- expect( selection.rangeCount ).to.equal( 1 );
- expect( selection.getFirstRange().isEqual( range1 ) ).to.be.true;
- done();
- } );
- const otherSelection = new Selection();
- otherSelection.addRange( range1 );
- selection.setTo( otherSelection );
- } );
- } );
- describe( 'collapse', () => {
- beforeEach( () => {
- selection.setRanges( [ range1, range2 ] );
- } );
- it( 'should collapse selection at position', () => {
- const position = new Position( el, 4 );
- selection.collapse( position );
- const range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( el );
- expect( range.start.offset ).to.equal( 4 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- } );
- it( 'should collapse selection at node and offset', () => {
- const foo = new Text( 'foo' );
- const p = new Element( 'p', null, foo );
- selection.collapse( foo );
- let range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( foo );
- expect( range.start.offset ).to.equal( 0 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- selection.collapse( p, 1 );
- range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( p );
- expect( range.start.offset ).to.equal( 1 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- } );
- it( 'should collapse selection at node and flag', () => {
- const foo = new Text( 'foo' );
- const p = new Element( 'p', null, foo );
- selection.collapse( foo, 'end' );
- let range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( foo );
- expect( range.start.offset ).to.equal( 3 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- selection.collapse( foo, 'before' );
- range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( p );
- expect( range.start.offset ).to.equal( 0 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- selection.collapse( foo, 'after' );
- range = selection.getFirstRange();
- expect( range.start.parent ).to.equal( p );
- expect( range.start.offset ).to.equal( 1 );
- expect( range.start.isEqual( range.end ) ).to.be.true;
- } );
- } );
- describe( 'collapseToStart', () => {
- it( 'should collapse to start position and fire change event', ( done ) => {
- selection.setRanges( [ range1, range2, range3 ] );
- selection.once( 'change', () => {
- expect( selection.rangeCount ).to.equal( 1 );
- expect( selection.isCollapsed ).to.be.true;
- expect( selection._ranges[ 0 ].start.isEqual( range2.start ) ).to.be.true;
- done();
- } );
- selection.collapseToStart();
- } );
- it( 'should do nothing if no ranges present', () => {
- const fireSpy = sinon.spy( selection, 'fire' );
- selection.collapseToStart();
- fireSpy.restore();
- expect( fireSpy.notCalled ).to.be.true;
- } );
- } );
- describe( 'collapseToEnd', () => {
- it( 'should collapse to end position and fire change event', ( done ) => {
- selection.setRanges( [ range1, range2, range3 ] );
- selection.once( 'change', () => {
- expect( selection.rangeCount ).to.equal( 1 );
- expect( selection.isCollapsed ).to.be.true;
- expect( selection._ranges[ 0 ].end.isEqual( range3.end ) ).to.be.true;
- done();
- } );
- selection.collapseToEnd();
- } );
- it( 'should do nothing if no ranges present', () => {
- const fireSpy = sinon.spy( selection, 'fire' );
- selection.collapseToEnd();
- fireSpy.restore();
- expect( fireSpy.notCalled ).to.be.true;
- } );
- } );
- describe( 'getEditableElement', () => {
- it( 'should return null if no ranges in selection', () => {
- expect( selection.editableElement ).to.be.null;
- } );
- it( 'should return null if selection is placed in container that is not EditableElement', () => {
- selection.addRange( range1 );
- expect( selection.editableElement ).to.be.null;
- } );
- it( 'should return EditableElement when selection is placed inside', () => {
- const viewDocument = new Document();
- const selection = viewDocument.selection;
- const root = viewDocument.createRoot( 'div' );
- const element = new Element( 'p' );
- root.appendChildren( element );
- selection.addRange( Range.createFromParentsAndOffsets( element, 0, element, 0 ) );
- expect( selection.editableElement ).to.equal( root );
- } );
- } );
- describe( 'createFromSelection', () => {
- it( 'should return a Selection instance with same ranges and direction as given selection', () => {
- selection.setRanges( [ range1, range2 ], true );
- const snapshot = Selection.createFromSelection( selection );
- expect( snapshot.isBackward ).to.equal( selection.isBackward );
- const selectionRanges = Array.from( selection.getRanges() );
- const snapshotRanges = Array.from( snapshot.getRanges() );
- expect( selectionRanges.length ).to.equal( snapshotRanges.length );
- for ( let i = 0; i < selectionRanges.length; i++ ) {
- expect( selectionRanges[ i ].isEqual( snapshotRanges[ i ] ) ).to.be.true;
- }
- } );
- } );
- } );
|