8
0
Просмотр исходного кода

Fixed position creating after and before TextProxy.

Oskar Wrobel 9 лет назад
Родитель
Сommit
aaf6d295b6

+ 10 - 10
packages/ckeditor5-engine/src/view/position.js

@@ -194,6 +194,11 @@ export default class Position {
 	 * @returns {engine.view.Position}
 	 */
 	static createAfter( node ) {
+		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
+		if ( node instanceof TextProxy ) {
+			return new Position( node._textNode, node._index + 1 );
+		}
+
 		if ( !node.parent ) {
 			/**
 			 * You can not make a position after a root.
@@ -204,11 +209,6 @@ export default class Position {
 			throw new CKEditorError( 'position-after-root: You can not make position after root.', { root: node } );
 		}
 
-		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
-		if ( node instanceof TextProxy ) {
-			return new Position( node._textNode, node._index + 1 );
-		}
-
 		return new Position( node.parent, node.getIndex() + 1 );
 	}
 
@@ -219,6 +219,11 @@ export default class Position {
 	 * @returns {engine.view.Position}
 	 */
 	static createBefore( node ) {
+		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
+		if ( node instanceof TextProxy ) {
+			return new Position( node._textNode, node._index );
+		}
+
 		if ( !node.parent ) {
 			/**
 			 * You cannot make a position before a root.
@@ -229,11 +234,6 @@ export default class Position {
 			throw new CKEditorError( 'position-before-root: You can not make position before root.', { root: node } );
 		}
 
-		// {@link engine.view.TextProxy} is not a instance of {@link engine.view.Node} so we need do handle it in specific way.
-		if ( node instanceof TextProxy ) {
-			return new Position( node._textNode, node._index );
-		}
-
 		return new Position( node.parent, node.getIndex() );
 	}
 

+ 4 - 6
packages/ckeditor5-engine/tests/view/position.js

@@ -306,12 +306,11 @@ describe( 'Position', () => {
 
 		it( 'should create positions before `TextProxy`', () => {
 			const text = new Text( 'abc' );
-			const paragraph = new Element( 'p', [], [ text ] );
 
-			const textProxy = new TextProxy( 'b', paragraph, text, 1 );
+			const textProxy = new TextProxy( text, 1, 1 );
 			const position = new Position( text, 1 );
 
-			expect( Position.createBefore( textProxy ).isEqual( position ) ).to.be.true;
+			expect( Position.createBefore( textProxy ) ).deep.equal( position );
 		} );
 	} );
 
@@ -332,12 +331,11 @@ describe( 'Position', () => {
 
 		it( 'should create positions after `TextProxy`', () => {
 			const text = new Text( 'abc' );
-			const paragraph = new Element( 'p', [], [ text ] );
 
-			const textProxy = new TextProxy( 'b', paragraph, text, 1 );
+			const textProxy = new TextProxy( text, 1, 1 );
 			const position = new Position( text, 2 );
 
-			expect( Position.createAfter( textProxy ).isEqual( position ) ).to.be.true;
+			expect( Position.createAfter( textProxy ) ).deep.equal( position );
 		} );
 	} );
 } );