浏览代码

Added: model.Position#getAncestors.

Szymon Cofalik 9 年之前
父节点
当前提交
a7058d785a
共有 2 个文件被更改,包括 15 次插入0 次删除
  1. 9 0
      packages/ckeditor5-engine/src/model/position.js
  2. 6 0
      packages/ckeditor5-engine/tests/model/position.js

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

@@ -251,6 +251,15 @@ export default class Position {
 	}
 
 	/**
+	 * Returns ancestors array of this position, that is this position's parent and it's ancestors.
+	 *
+	 * @returns {Array.<engine.model.Item>} Array with ancestors.
+	 */
+	getAncestors() {
+		return this.parent.getAncestors( { includeNode: true, parentFirst: true } );
+	}
+
+	/**
 	 * Returns a new instance of `Position`, that has same {@link engine.model.Position#parent parent} but it's offset
 	 * is shifted by `shift` value (can be a negative value).
 	 *

+ 6 - 0
packages/ckeditor5-engine/tests/model/position.js

@@ -503,6 +503,12 @@ describe( 'position', () => {
 		} );
 	} );
 
+	describe( 'getAncestors', () => {
+		it( 'should return position parent element and it\'s ancestors', () => {
+			expect( new Position( root, [ 1, 1, 1 ] ).getAncestors() ).to.deep.equal( [ li2, ul, root ] );
+		} );
+	} );
+
 	describe( 'compareWith', () => {
 		it( 'should return same if positions are same', () => {
 			const position = new Position( root, [ 1, 2, 3 ] );