Browse Source

Position constructor improved error throwing.

Szymon Cofalik 10 years ago
parent
commit
16e47b3d69

+ 11 - 1
packages/ckeditor5-utils/src/document/position.js

@@ -21,11 +21,21 @@ CKEDITOR.define( [ 'document/rootelement', 'utils', 'ckeditorerror' ], ( RootEle
 		/**
 		 * Creates a position.
 		 *
-		 * @param {Array} path Position path. See {@link #path} property for more information.
+		 * @param {Array.<Number>} path Position path. Must contain at least one item. See {@link #path} property for more information.
 		 * @param {document.RootElement} root Root element for the path. Note that this element can not have a parent.
 		 * @constructor
 		 */
 		constructor( path, root ) {
+			if ( !( path instanceof Array ) || path.length === 0 ) {
+				/**
+				 * Position path must be an Array with at least one item.
+				 *
+				 * @error position-path-incorrect
+				 * @param path
+				 */
+				throw new CKEditorError( 'position-path-incorrect: Position path must be an Array with at least one item.', { path: path } );
+			}
+
 			/**
 			 * Position of the node it the tree. For example:
 			 *

+ 10 - 0
packages/ckeditor5-utils/tests/document/position.js

@@ -71,6 +71,16 @@ describe( 'position', () => {
 		expect( position ).to.have.property( 'root' ).that.equals( root );
 	} );
 
+	it( 'should throw error if given path is incorrect', () => {
+		expect( () => {
+			new Position( {}, root );
+		} ).to.throw( CKEditorError, /position-path-incorrect/ );
+
+		expect( () => {
+			new Position( [], root );
+		} ).to.throw( CKEditorError, /position-path-incorrect/ );
+	} );
+
 	it( 'should throw error if given root is not a RootElement instance', () => {
 		expect( () => {
 			new Position( [ 0 ] );