Kaynağa Gözat

Improved docs and tests.

Kamil Piechaczek 8 yıl önce
ebeveyn
işleme
3d69876eba

+ 3 - 2
packages/ckeditor5-engine/src/view/position.js

@@ -176,10 +176,11 @@ export default class Position {
 	}
 
 	/**
-	 * Returns a {@link module:engine/view/node~Node} which is a common ancestor for both positions.
+	 * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
+	 * which is a common ancestor for both positions.
 	 *
 	 * @param {module:engine/view/position~Position} position
-	 * @returns {module:engine/view/node~Node|null}
+	 * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
 	 */
 	getCommonAncestor( position ) {
 		const ancestorsA = this.getAncestors();

+ 3 - 2
packages/ckeditor5-engine/src/view/range.js

@@ -305,9 +305,10 @@ export default class Range {
 	}
 
 	/**
-	 * Returns a {@link module:engine/view/node~Node} which is a common ancestor for both positions.
+	 * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
+	 * which is a common ancestor for both positions.
 	 *
-	 * @returns {module:engine/view/node~Node|null}
+	 * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
 	 */
 	getCommonAncestor() {
 		return this.start.getCommonAncestor( this.end );

+ 27 - 5
packages/ckeditor5-engine/tests/view/position.js

@@ -521,25 +521,47 @@ describe( 'Position', () => {
 	} );
 
 	describe( 'getCommonAncestor()', () => {
-		let ul, li1, li2, foz, bar;
+		let div, p, ul, li1, li2, foz, bar, ipsum;
+
+		// |- div
+		//   |- ul
+		//   |  |- li
+		//   |  |  |- f
+		//   |  |  |- o
+		//   |  |  |- z
+		//   |  |- li
+		//   |     |- b
+		//   |     |- a
+		//   |     |- r
+		//   |- p
+		//     |- i
+		//     |- p
+		//     |- s
+		//     |- u
+		//     |- m
 
 		beforeEach( () => {
 			foz = new Text( 'foz' );
 			bar = new Text( 'bar' );
+			ipsum = new Text( 'ipsum' );
 
 			li1 = new Element( 'li', null, foz );
 			li2 = new Element( 'li', null, bar );
 
+			p = new Element( 'p', null, ipsum );
+
 			ul = new Element( 'ul', null, [ li1, li2 ] );
+			div = new Element( 'div', null, [ ul, p ] );
 		} );
 
 		it( 'for two the same positions returns the parent element', () => {
 			const fPosition = new Position( li1, 0 );
+			const otherPosition = Position.createFromPosition( fPosition );
 
-			test( fPosition, fPosition, li1 );
+			test( fPosition, otherPosition, li1 );
 		} );
 
-		it( 'for two positions in the same parent returns the parent element', () => {
+		it( 'for two positions in the same element returns the element', () => {
 			const fPosition = new Position( li1, 0 );
 			const zPosition = new Position( li1, 2 );
 
@@ -548,9 +570,9 @@ describe( 'Position', () => {
 
 		it( 'for two different positions returns first element which contains both positions', () => {
 			const zPosition = new Position( li1, 2 );
-			const bPosition = new Position( li2, 0 );
+			const iPosition = Position.createAt( ipsum, 'start' );
 
-			test( bPosition, zPosition, ul );
+			test( iPosition, zPosition, div );
 		} );
 
 		function test( positionA, positionB, lca ) {