Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Feature: Implemented Selection#is() and DocumentSelection#is() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
oleq committed Feb 8, 2019
1 parent c9932b8 commit 6780aa5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,24 @@ export default class DocumentSelection {
return this._selection.hasAttribute( key );
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/model/node~Node#is}.
*
* const selection = new DocumentSelection( ... );
*
* selection.is( 'selection' ); // true
* selection.is( 'documentSelection' ); // true
* selection.is( 'node' ); // false
* selection.is( 'element' ); // false
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'selection' || type == 'documentSelection';
}

/**
* Moves {@link module:engine/model/documentselection~DocumentSelection#focus} to the specified location.
* Should be used only within the {@link module:engine/model/writer~Writer#setSelectionFocus} method.
Expand Down
17 changes: 17 additions & 0 deletions src/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,23 @@ export default class Selection {
return ( nodeAfterStart instanceof Element && nodeAfterStart == nodeBeforeEnd ) ? nodeAfterStart : null;
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/model/node~Node#is}.
*
* const selection = new Selection( ... );
*
* selection.is( 'selection' ); // true
* selection.is( 'node' ); // false
* selection.is( 'element' ); // false
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'selection';
}

/**
* Gets elements of type "block" touched by the selection.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,24 @@ describe( 'DocumentSelection', () => {
} );
} );

describe( 'is', () => {
it( 'should return true for selection', () => {
expect( selection.is( 'selection' ) ).to.be.true;
} );

it( 'should return true for documentSelection', () => {
expect( selection.is( 'documentSelection' ) ).to.be.true;
} );

it( 'should return false for other values', () => {
expect( selection.is( 'node' ) ).to.be.false;
expect( selection.is( 'text' ) ).to.be.false;
expect( selection.is( 'textProxy' ) ).to.be.false;
expect( selection.is( 'element' ) ).to.be.false;
expect( selection.is( 'rootElement' ) ).to.be.false;
} );
} );

describe( '_setTo() - set collapsed at', () => {
it( 'detaches all existing ranges', () => {
selection._setTo( [ range, liveRange ] );
Expand Down
15 changes: 15 additions & 0 deletions tests/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,21 @@ describe( 'Selection', () => {
} );
} );

describe( 'is', () => {
it( 'should return true for selection', () => {
expect( selection.is( 'selection' ) ).to.be.true;
} );

it( 'should return false for other values', () => {
expect( selection.is( 'documentSelection' ) ).to.be.false;
expect( selection.is( 'node' ) ).to.be.false;
expect( selection.is( 'text' ) ).to.be.false;
expect( selection.is( 'textProxy' ) ).to.be.false;
expect( selection.is( 'element' ) ).to.be.false;
expect( selection.is( 'rootElement' ) ).to.be.false;
} );
} );

describe( 'setTo - used to collapse at start', () => {
it( 'should collapse to start position and fire change event', () => {
selection.setTo( [ range2, range1, range3 ] );
Expand Down

0 comments on commit 6780aa5

Please sign in to comment.