浏览代码

Added setFake method, isFake and fakeSelectionLabel getters to view selection.

Szymon Kupś 9 年之前
父节点
当前提交
ba2fbcdf4d
共有 2 个文件被更改,包括 133 次插入0 次删除
  1. 60 0
      packages/ckeditor5-engine/src/view/selection.js
  2. 73 0
      packages/ckeditor5-engine/tests/view/selection.js

+ 60 - 0
packages/ckeditor5-engine/src/view/selection.js

@@ -45,6 +45,55 @@ export default class Selection {
 		 * @member {Boolean} engine.view.Selection#_lastRangeBackward
 		 */
 		this._lastRangeBackward = false;
+
+		/**
+		 * Specifies whether selection instance is fake.
+		 *
+		 * @private
+		 * @member {Boolean} engine.view.Selection#_isFake
+		 */
+		this._isFake = false;
+
+		/**
+		 * Fake selection's label.
+		 *
+		 * @private
+		 * @member {String} engine.view.Selection#_fakeSelectionLabel
+		 */
+		this._fakeSelectionLabel = '';
+	}
+
+	/**
+	 * Sets this selection instance to be marked as `fake`. This means only that selection will not be rendered
+	 * as real selection.
+	 * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
+	 * properly handled by screen readers).
+	 *
+	 * @param {Boolean} [value=true] If set to true selection will be marked as `fake`.
+	 * @param {Object} [options] Additional options.
+	 * @param {String} [options.label=''] Fake selection label.
+	 */
+	setFake( value = true, options = {} ) {
+		this._isFake = value;
+		this._fakeSelectionLabel = value ? options.label || '' : '';
+	}
+
+	/**
+	 * Returns true if selection instance is marked as `fake`.
+	 *
+	 * @returns {Boolean}
+	 */
+	get isFake() {
+		return this._isFake;
+	}
+
+	/**
+	 * Returns fake selection label.
+	 *
+	 * @returns {String}
+	 */
+	get fakeSelectionLabel() {
+		return this._fakeSelectionLabel;
 	}
 
 	/**
@@ -239,6 +288,14 @@ export default class Selection {
 			return false;
 		}
 
+		if ( this.isFake != otherSelection.isFake ) {
+			return false;
+		}
+
+		if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) {
+			return false;
+		}
+
 		for ( let i = 0; i < this.rangeCount; i++ ) {
 			if ( !this._ranges[ i ].isEqual( otherSelection._ranges[ i ] ) ) {
 				return false;
@@ -292,6 +349,9 @@ export default class Selection {
 	 * @param {engine.view.Selection} otherSelection
 	 */
 	setTo( otherSelection ) {
+		this._isFake = otherSelection._isFake;
+		this._fakeSelectionLabel = otherSelection._fakeSelectionLabel;
+
 		this.setRanges( otherSelection.getRanges(), otherSelection.isBackward );
 	}
 

+ 73 - 0
packages/ckeditor5-engine/tests/view/selection.js

@@ -460,6 +460,33 @@ describe( 'Selection', () => {
 
 			expect( selection.isEqual( otherSelection ) ).to.be.false;
 		} );
+
+		it( 'should return false if one selection is fake', () => {
+			const otherSelection = new Selection();
+			otherSelection.setFake( true );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.false;
+		} );
+
+		it( 'should return true if both selection are fake', () => {
+			const otherSelection = new Selection();
+			otherSelection.addRange( range1 );
+			otherSelection.setFake( true );
+			selection.setFake( true );
+			selection.addRange( range1 );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.true;
+		} );
+
+		it( 'should return false if both selection are fake but have different label', () => {
+			const otherSelection = new Selection();
+			otherSelection.addRange( range1 );
+			otherSelection.setFake( true , { label: 'foo bar baz' } );
+			selection.setFake( true );
+			selection.addRange( range1 );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.false;
+		} );
 	} );
 
 	describe( 'removeAllRanges', () => {
@@ -538,6 +565,16 @@ describe( 'Selection', () => {
 
 			selection.setTo( otherSelection );
 		} );
+
+		it( 'should set fake state and label', () => {
+			const otherSelection = new Selection();
+			const label = 'foo bar baz';
+			otherSelection.setFake( true, { label } );
+			selection.setTo( otherSelection );
+
+			expect( selection.isFake ).to.be.true;
+			expect( selection.fakeSelectionLabel ).to.equal( label );
+		} );
 	} );
 
 	describe( 'collapse', () => {
@@ -690,4 +727,40 @@ describe( 'Selection', () => {
 			}
 		} );
 	} );
+
+	describe( 'isFake', () => {
+		it( 'should be false for newly created instance', () => {
+			expect( selection.isFake ).to.be.false;
+		} );
+	} );
+
+	describe( 'setFake', () => {
+		it( 'should allow to set selection to fake', () => {
+			selection.setFake( true );
+
+			expect( selection.isFake ).to.be.true;
+		} );
+
+		it( 'should allow to set fake selection label', () => {
+			const label = 'foo bar baz';
+			selection.setFake( true, { label } );
+
+			expect( selection.fakeSelectionLabel ).to.equal( label );
+		} );
+
+		it( 'should not set label when set to false', () => {
+			const label = 'foo bar baz';
+			selection.setFake( false, { label } );
+
+			expect( selection.fakeSelectionLabel ).to.equal( '' );
+		} );
+
+		it( 'should reset label when set to false', () => {
+			const label = 'foo bar baz';
+			selection.setFake( true, { label } );
+			selection.setFake( false );
+
+			expect( selection.fakeSelectionLabel ).to.equal( '' );
+		} );
+	} );
 } );