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

Commit

Permalink
Merge pull request #1670 from ckeditor/t/1663
Browse files Browse the repository at this point in the history
Feature: Implemented `Selection#is()` and `DocumentSelection#is()` methods in both the model and the view. Closes #1663.
  • Loading branch information
Piotr Jasiun authored Feb 8, 2019
2 parents c9932b8 + b81e921 commit aac4948
Show file tree
Hide file tree
Showing 8 changed files with 136 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 `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 `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 src/view/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,24 @@ export default class DocumentSelection {
return this._selection.isSimilar( otherSelection );
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/view/node~Node#is `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';
}

/**
* Sets this selection's ranges and direction to the specified location based on the given
* {@link module:engine/view/selection~Selectable selectable}.
Expand Down
17 changes: 17 additions & 0 deletions src/view/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,23 @@ export default class Selection {
this.fire( 'change' );
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/view/node~Node#is `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';
}

/**
* Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
* is treated like the last added range and is used to set {@link #anchor anchor} and {@link #focus focus}.
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
18 changes: 18 additions & 0 deletions tests/view/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,24 @@ describe( 'DocumentSelection', () => {
} );
} );

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

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

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

describe( '_setTo()', () => {
describe( 'simple scenarios', () => {
it( 'should set selection ranges from the given selection', () => {
Expand Down
15 changes: 15 additions & 0 deletions tests/view/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,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()', () => {
describe( 'simple scenarios', () => {
it( 'should set selection ranges from the given selection', () => {
Expand Down

0 comments on commit aac4948

Please sign in to comment.