From 6780aa569654bc270e6e5d3f84aff73da0411768 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Fri, 8 Feb 2019 11:17:51 +0100 Subject: [PATCH 1/2] Feature: Implemented Selection#is() and DocumentSelection#is() methods. --- src/model/documentselection.js | 18 ++++++++++++++++++ src/model/selection.js | 17 +++++++++++++++++ tests/model/documentselection.js | 18 ++++++++++++++++++ tests/model/selection.js | 15 +++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/src/model/documentselection.js b/src/model/documentselection.js index 336a8bba9..c3741203a 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -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. diff --git a/src/model/selection.js b/src/model/selection.js index 5471ab0c2..67071f900 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -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. * diff --git a/tests/model/documentselection.js b/tests/model/documentselection.js index 91133c1d9..c43e2f63c 100644 --- a/tests/model/documentselection.js +++ b/tests/model/documentselection.js @@ -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 ] ); diff --git a/tests/model/selection.js b/tests/model/selection.js index e08178511..bd822d1b8 100644 --- a/tests/model/selection.js +++ b/tests/model/selection.js @@ -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 ] ); From b81e92160158452f471bd0f8bf8670a6e4da98b5 Mon Sep 17 00:00:00 2001 From: Aleksander Nowodzinski Date: Fri, 8 Feb 2019 12:36:13 +0100 Subject: [PATCH 2/2] Feature: Implemented Selection#is() and DocumentSelection#is() methods in the view classes. --- src/model/documentselection.js | 2 +- src/model/selection.js | 2 +- src/view/documentselection.js | 18 ++++++++++++++++++ src/view/selection.js | 17 +++++++++++++++++ tests/view/documentselection.js | 18 ++++++++++++++++++ tests/view/selection.js | 15 +++++++++++++++ 6 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/model/documentselection.js b/src/model/documentselection.js index c3741203a..195919b08 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -361,7 +361,7 @@ export default class DocumentSelection { /** * Checks whether object is of given type following the convention set by - * {@link module:engine/model/node~Node#is}. + * {@link module:engine/model/node~Node#is `Node#is()`}. * * const selection = new DocumentSelection( ... ); * diff --git a/src/model/selection.js b/src/model/selection.js index 67071f900..98a1dee1f 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -610,7 +610,7 @@ export default class Selection { /** * Checks whether object is of given type following the convention set by - * {@link module:engine/model/node~Node#is}. + * {@link module:engine/model/node~Node#is `Node#is()`}. * * const selection = new Selection( ... ); * diff --git a/src/view/documentselection.js b/src/view/documentselection.js index e78f96c69..3bbd11c8a 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -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}. diff --git a/src/view/selection.js b/src/view/selection.js index cfd85e9b1..5a1e7fb45 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -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}. diff --git a/tests/view/documentselection.js b/tests/view/documentselection.js index e6dbaa1b9..8eca0fe75 100644 --- a/tests/view/documentselection.js +++ b/tests/view/documentselection.js @@ -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', () => { diff --git a/tests/view/selection.js b/tests/view/selection.js index 491d209f3..6e08762a2 100644 --- a/tests/view/selection.js +++ b/tests/view/selection.js @@ -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', () => {