Przeglądaj źródła

TreeView selection and position tests and docs.

Piotr Jasiun 9 lat temu
rodzic
commit
dff9ce29c6

+ 0 - 4
packages/ckeditor5-engine/src/treeview/position.js

@@ -189,8 +189,6 @@ export default class Position {
 	/**
 	 * Creates a new position after given node.
 	 *
-	 * @see {@link engine.treeView.TreeWalkerValue}
-	 *
 	 * @param {engine.treeView.Node} node Node the position should be directly after.
 	 * @returns {engine.treeView.Position}
 	 */
@@ -211,8 +209,6 @@ export default class Position {
 	/**
 	 * Creates a new position before the given node.
 	 *
-	 * @see {@link engine.treeView.TreeWalkerValue}
-	 *
 	 * @param {engine.treeView.node} node Node the position should be directly before.
 	 * @returns {engine.treeView.Position}
 	 */

+ 8 - 3
packages/ckeditor5-engine/src/treeview/selection.js

@@ -207,10 +207,10 @@ export default class Selection {
 	}
 
 	/**
-	 * Two ranges equal if their start and end positions equal.
+	 * Two selections equals if they have the same ranges and directions.
 	 *
-	 * @param {engine.treeView.Range} otherRange Range to compare with.
-	 * @returns {Boolean} True if ranges equal.
+	 * @param {engine.treeView.Selection} otherSelection Selection to compare with.
+	 * @returns {Boolean} True if selections equal.
 	 */
 	isEqual( otherSelection ) {
 		const rangeCount = this.rangeCount;
@@ -262,6 +262,11 @@ export default class Selection {
 		this.fire( 'change' );
 	}
 
+	/**
+	 * Set this selection ranges and direction to the ranges and direction of other selection
+	 *
+	 * @param {engine.treeView.Selection} otherSelection Other selection.
+	 */
 	setTo( otherSelection ) {
 		this.removeAllRanges();
 

+ 36 - 0
packages/ckeditor5-engine/tests/treeview/position.js

@@ -12,6 +12,10 @@ import Node from '/ckeditor5/engine/treeview/node.js';
 import Element from '/ckeditor5/engine/treeview/element.js';
 import Text from '/ckeditor5/engine/treeview/text.js';
 
+import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
+
+import { parse } from '/tests/engine/_utils/view.js';
+
 describe( 'Position', () => {
 	const parentMock = {};
 
@@ -283,4 +287,36 @@ describe( 'Position', () => {
 			expect( position.compareWith( compared ) ).to.equal( 'DIFFERENT' );
 		} );
 	} );
+
+	describe( 'createBefore', () => {
+		it( 'should create positions before nodes', () => {
+			const { selection } = parse( '<p>[]<b></b></p>' );
+			const position = selection.getFirstPosition();
+			const nodeAfter = position.nodeAfter;
+
+			expect( Position.createBefore( nodeAfter ).isEqual( position ) ).to.be.true;
+		} );
+
+		it( 'should throw error if one try to create positions before root', () => {
+			expect( () => {
+				Position.createBefore( parse( '<p></p>' ) );
+			} ).to.throw( CKEditorError, /position-before-root/ );
+		} );
+	} );
+
+	describe( 'createAfter', () => {
+		it( 'should create positions after nodes', () => {
+			const { selection } = parse( '<p><b></b>[]</p>' );
+			const position = selection.getFirstPosition();
+			const nodeBefore = position.nodeBefore;
+
+			expect( Position.createAfter( nodeBefore ).isEqual( position ) ).to.be.true;
+		} );
+
+		it( 'should throw error if one try to create positions after root', () => {
+			expect( () => {
+				Position.createAfter( parse( '<p></p>' ) );
+			} ).to.throw( CKEditorError, /position-after-root/ );
+		} );
+	} );
 } );

+ 70 - 0
packages/ckeditor5-engine/tests/treeview/selection.js

@@ -224,6 +224,56 @@ describe( 'Selection', () => {
 		} );
 	} );
 
+	describe( 'isEqual', () => {
+		it( 'should return true if selections equal', () => {
+			selection.addRange( range1 );
+			selection.addRange( range2 );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range1 );
+			otherSelection.addRange( range2 );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.true;
+		} );
+
+		it( 'should return true if backward selections equal', () => {
+			selection.addRange( range1, true );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range1, true );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.true;
+		} );
+
+		it( 'should return false if ranges count does not equal', () => {
+			selection.addRange( range1 );
+			selection.addRange( range2 );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range1 );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.false;
+		} );
+
+		it( 'should return false if ranges do not equal', () => {
+			selection.addRange( range1 );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range2 );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.false;
+		} );
+
+		it( 'should return false if directions do not equal', () => {
+			selection.addRange( range1 );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range2, true );
+
+			expect( selection.isEqual( otherSelection ) ).to.be.false;
+		} );
+	} );
+
 	describe( 'removeAllRanges', () => {
 		it( 'should remove all ranges and fire change event', ( done ) => {
 			selection.addRange( range1 );
@@ -263,6 +313,26 @@ describe( 'Selection', () => {
 		} );
 	} );
 
+	describe( 'setTo', () => {
+		it( 'should return true if selections equal', () => {
+			selection.addRange( range1 );
+
+			const otherSelection = new Selection();
+			otherSelection.addRange( range2 );
+			otherSelection.addRange( range3, true );
+
+			selection.setTo( otherSelection );
+
+			expect( selection.rangeCount ).to.equal( 2 );
+			expect( selection._ranges[ 0 ].isEqual( range2 ) ).to.be.true;
+			expect( selection._ranges[ 0 ] ).is.not.equal( range2 );
+			expect( selection._ranges[ 1 ].isEqual( range3 ) ).to.be.true;
+			expect( selection._ranges[ 1 ] ).is.not.equal( range3 );
+
+			expect( selection.anchor.isEqual( range3.end ) ).to.be.true;
+		} );
+	} );
+
 	describe( 'collapseToStart', () => {
 		it( 'should collapse to start position and fire change event', ( done ) => {
 			selection.setRanges( [ range1, range2, range3 ] );