Przeglądaj źródła

Removed: view.DocumentFragment#getAncestors().

Szymon Cofalik 8 lat temu
rodzic
commit
e3c6d85793

+ 0 - 9
packages/ckeditor5-engine/src/view/documentfragment.js

@@ -84,15 +84,6 @@ export default class DocumentFragment {
 	}
 
 	/**
-	 * Returns ancestor elements of `DocumentFragment`, which is an empty array. Added for compatibility reasons.
-	 *
-	 * @returns {Array}
-	 */
-	getAncestors() {
-		return [];
-	}
-
-	/**
 	 * {@link module:engine/view/documentfragment~DocumentFragment#insertChildren Insert} a child node or a list of child nodes at the end
 	 * and sets the parent of these nodes to this fragment.
 	 *

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

@@ -9,6 +9,7 @@
 
 import Text from './text';
 import TextProxy from './textproxy';
+import DocumentFragment from './documentfragment';
 
 import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
@@ -143,7 +144,11 @@ export default class Position {
 	 * @returns {Array} Array with ancestors.
 	 */
 	getAncestors() {
-		return this.parent.getAncestors( { includeNode: true, parentFirst: true } );
+		if ( this.parent instanceof DocumentFragment ) {
+			return [ this.parent ];
+		} else {
+			return this.parent.getAncestors( { includeNode: true, parentFirst: true } );
+		}
 	}
 
 	/**
@@ -202,8 +207,8 @@ export default class Position {
 		}
 
 		// Get path from root to position's parent element.
-		const path = this.parent.getAncestors( { includeNode: true } );
-		const otherPath = otherPosition.parent.getAncestors( { includeNode: true } );
+		const path = this.getAncestors().reverse();
+		const otherPath = otherPosition.getAncestors().reverse();
 
 		// Compare both path arrays to find common ancestor.
 		const result = compareArrays( path, otherPath );

+ 0 - 8
packages/ckeditor5-engine/tests/view/documentfragment.js

@@ -55,14 +55,6 @@ describe( 'DocumentFragment', () => {
 		} );
 	} );
 
-	describe( 'getAncestors', () => {
-		it( 'should return empty array', () => {
-			const fragment = new DocumentFragment();
-
-			expect( fragment.getAncestors() ).to.deep.equal( [] );
-		} );
-	} );
-
 	describe( 'isEmpty', () => {
 		it( 'should return true if there are no children in document fragment', () => {
 			const fragment = new DocumentFragment();

+ 33 - 0
packages/ckeditor5-engine/tests/view/position.js

@@ -123,6 +123,12 @@ describe( 'Position', () => {
 
 			expect( new Position( foo, 1 ).getAncestors() ).to.deep.equal( [ foo, p, div, docFrag ] );
 		} );
+
+		it( 'should return DocumentFragment if position is directly in document fragment', () => {
+			const docFrag = new DocumentFragment();
+
+			expect( new Position( docFrag, 0 ).getAncestors() ).to.deep.equal( [ docFrag ] );
+		} );
 	} );
 
 	describe( 'createAt', () => {
@@ -174,6 +180,19 @@ describe( 'Position', () => {
 			expect( pEnd.parent ).to.equal( p );
 			expect( pEnd.offset ).to.equal( 1 );
 		} );
+
+		it( 'should create positions in document fragment', () => {
+			const foo = new Text( 'foo' );
+			const docFrag = new DocumentFragment( [ foo ] );
+
+			const pStart = Position.createAt( docFrag, 0 );
+			const pEnd = Position.createAt( docFrag, 'end' );
+
+			expect( pStart.parent ).to.equal( docFrag );
+			expect( pStart.offset ).to.equal( 0 );
+			expect( pEnd.parent ).to.equal( docFrag );
+			expect( pEnd.offset ).to.equal( 1 );
+		} );
 	} );
 
 	describe( 'createFromPosition', () => {
@@ -395,6 +414,20 @@ describe( 'Position', () => {
 
 			expect( position.compareWith( compared ) ).to.equal( 'different' );
 		} );
+
+		it( 'should return correct results if position is in document fragment', () => {
+			const node = new Node( 'name' );
+			const docFrag = new DocumentFragment( [ node ] );
+			const position = new Position( docFrag, 0 );
+			const compared = new Position( docFrag, 1 );
+			const posInNode = new Position( node, 0 );
+
+			expect( position.compareWith( compared ) ).to.equal( 'before' );
+			expect( compared.compareWith( position ) ).to.equal( 'after' );
+			expect( compared.compareWith( compared ) ).to.equal( 'same' );
+			expect( position.compareWith( posInNode ) ).to.equal( 'before' );
+			expect( compared.compareWith( posInNode ) ).to.equal( 'after' );
+		} );
 	} );
 
 	describe( 'createBefore', () => {