Parcourir la source

Exposed these utils as public helpers and added minimal tests.

Piotrek Koszuliński il y a 5 ans
Parent
commit
8298b37461

+ 0 - 3
packages/ckeditor5-engine/src/model/position.js

@@ -1079,7 +1079,6 @@ export default class Position {
  * * {@link module:engine/model/position~getNodeAfterPosition}
  * * {@link module:engine/model/position~getNodeBeforePosition}
  *
- * @protected
  * @param {module:engine/model/position~Position} position
  * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} positionParent The parent of the
  * given position.
@@ -1114,7 +1113,6 @@ export function getTextNodeAtPosition( position, positionParent ) {
  * * {@link module:engine/model/position~getTextNodeAtPosition}
  * * {@link module:engine/model/position~getNodeBeforePosition}
  *
- * @protected
  * @param {module:engine/model/position~Position} position
  * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} positionParent The parent of the
  * given position.
@@ -1139,7 +1137,6 @@ export function getNodeAfterPosition( position, positionParent, textNode ) {
  * * {@link module:engine/model/position~getTextNodeAtPosition}
  * * {@link module:engine/model/position~getNodeAfterPosition}
  *
- * @protected
  * @param {module:engine/model/position~Position} position
  * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} positionParent The parent of the
  * given position.

+ 46 - 4
packages/ckeditor5-engine/tests/model/position.js

@@ -8,7 +8,12 @@ import DocumentFragment from '../../src/model/documentfragment';
 import Element from '../../src/model/element';
 import Text from '../../src/model/text';
 import TextProxy from '../../src/model/textproxy';
-import Position from '../../src/model/position';
+import {
+	default as Position,
+	getTextNodeAtPosition,
+	getNodeAfterPosition,
+	getNodeBeforePosition
+} from '../../src/model/position';
 import Range from '../../src/model/range';
 import MarkerOperation from '../../src/model/operation/markeroperation';
 import AttributeOperation from '../../src/model/operation/attributeoperation';
@@ -485,10 +490,12 @@ describe( 'Position', () => {
 		} );
 	} );
 
-	it( 'should have proper parent path', () => {
-		const position = new Position( root, [ 1, 2, 3 ] );
+	describe( 'getParentPath()', () => {
+		it( 'should have proper parent path', () => {
+			const position = new Position( root, [ 1, 2, 3 ] );
 
-		expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
+			expect( position.getParentPath() ).to.deep.equal( [ 1, 2 ] );
+		} );
 	} );
 
 	describe( 'isBefore()', () => {
@@ -1275,4 +1282,39 @@ describe( 'Position', () => {
 			expect( positionB.getCommonAncestor( positionA ) ).to.equal( lca );
 		}
 	} );
+
+	describe( 'getTextNodeAtPosition() util', () => {
+		it( 'returns a text node at the given position', () => {
+			const position = new Position( root, [ 1, 0, 1 ] );
+			const positionParent = position.parent;
+
+			expect( getTextNodeAtPosition( position, positionParent ) ).to.equal( foz );
+		} );
+
+		// This util is covered with tests by Position#textNode tests.
+	} );
+
+	describe( 'getNodeAfterPosition() util', () => {
+		it( 'returns a node after the position', () => {
+			const position = new Position( root, [ 1, 0 ] );
+			const positionParent = position.parent;
+			const textNode = getTextNodeAtPosition( position, positionParent );
+
+			expect( getNodeAfterPosition( position, positionParent, textNode ) ).to.equal( li1 );
+		} );
+
+		// This util is covered with tests by Position#nodeAfter tests.
+	} );
+
+	describe( 'getNodeBeforePosition() util', () => {
+		it( 'returns a node before the position', () => {
+			const position = new Position( root, [ 1, 1 ] );
+			const positionParent = position.parent;
+			const textNode = getTextNodeAtPosition( position, positionParent );
+
+			expect( getNodeBeforePosition( position, positionParent, textNode ) ).to.equal( li1 );
+		} );
+
+		// This util is covered with tests by Position#nodeBefore tests.
+	} );
 } );