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

Commit

Permalink
Added more restricted types in setTo().
Browse files Browse the repository at this point in the history
  • Loading branch information
ma2ciek committed Jan 26, 2018
1 parent cf28a1f commit 8cd280c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,11 @@ export default class Selection {
* @param {Boolean|Number|'before'|'end'|'after'} [backwardSelectionOrOffset]
*/
setTo( selectable, backwardSelectionOrOffset ) {
if ( !selectable ) {
if ( selectable === null ) {
this._setRanges( [] );
} else if ( selectable instanceof Selection ) {
this._setRanges( selectable.getRanges(), selectable.isBackward );
} else if ( selectable._selection && selectable._selection instanceof Selection ) {
} else if ( selectable && selectable._selection instanceof Selection ) {
// We assume that the selectable is a DocumentSelection.
// It can't be imported here, because it would lead to circular imports.
this._setRanges( selectable.getRanges(), selectable.isBackward );
Expand Down
2 changes: 1 addition & 1 deletion src/view/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export default class Selection {
* @param {Boolean|Number|'before'|'end'|'after'} [backwardSelectionOrOffset]
*/
setTo( selectable, backwardSelectionOrOffset ) {
if ( selectable == null ) {
if ( selectable === null ) {
this._removeAllRanges();
} else if ( selectable instanceof Selection ) {
this._isFake = selectable.isFake;
Expand Down
6 changes: 6 additions & 0 deletions tests/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ describe( 'Selection', () => {
} ).to.throw( /model-selection-setTo-not-selectable/ );
} );

it( 'should throw an error when trying to set selection to not selectable #2', () => {
expect( () => {
selection.setTo();
} ).to.throw( /model-selection-setTo-not-selectable/ );
} );

it( 'should allow setting selection inside an element', () => {
const element = new Element( 'p', null, [ new Text( 'foo' ), new Text( 'bar' ) ] );

Expand Down
8 changes: 8 additions & 0 deletions tests/view/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,14 @@ describe( 'Selection', () => {
otherSelection.setTo( {} );
} ).to.throw( /view-selection-setTo-not-selectable/ );
} );

it( 'should throw an error when trying to set to not selectable #2', () => {
const otherSelection = new Selection();

expect( () => {
otherSelection.setTo();
} ).to.throw( /view-selection-setTo-not-selectable/ );
} );
} );

describe( 'setting collapsed selection', () => {
Expand Down

0 comments on commit 8cd280c

Please sign in to comment.