瀏覽代碼

"model.DocumentFragment#getNodeByPath()" works with offsets instead of indexes.

Kamil Piechaczek 8 年之前
父節點
當前提交
568bb0b515

+ 1 - 1
packages/ckeditor5-engine/src/model/documentfragment.js

@@ -186,7 +186,7 @@ export default class DocumentFragment {
 		let node = this; // eslint-disable-line consistent-this
 
 		for ( const index of relativePath ) {
-			node = node.getChild( index );
+			node = node.getChild( node.offsetToIndex( index ) );
 		}
 
 		return node;

+ 36 - 2
packages/ckeditor5-engine/tests/model/documentfragment.js

@@ -309,16 +309,50 @@ describe( 'DocumentFragment', () => {
 		} );
 
 		it( 'should return a descendant of this node', () => {
+			const foo = new Text( 'foo' );
 			const image = new Element( 'image' );
 			const element = new Element( 'elem', [], [
 				new Element( 'elem', [], [
-					new Text( 'foo' ),
+					foo,
 					image
 				] )
 			] );
 			const frag = new DocumentFragment( element );
 
-			expect( frag.getNodeByPath( [ 0, 0, 1 ] ) ).to.equal( image );
+			expect( frag.getNodeByPath( [ 0, 0, 0 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 0, 1 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 0, 2 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 0, 3 ] ) ).to.equal( image );
+		} );
+
+		it( 'works fine with offsets', () => {
+			const bar = new Text( 'bar' );
+			const foo = new Text( 'foo' );
+			const bom = new Text( 'bom' );
+			const bold = new Element( 'b', [], [
+				bar
+			] );
+			const paragraph = new Element( 'paragraph', [], [
+				foo,
+				bold,
+				bom
+			] );
+			const frag = new DocumentFragment( paragraph );
+
+			// <paragraph>foo<bold>bar</bold>bom</paragraph>
+
+			expect( frag.getNodeByPath( [ 0, 0 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 1 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 2 ] ) ).to.equal( foo );
+			expect( frag.getNodeByPath( [ 0, 3 ] ) ).to.equal( bold );
+			expect( frag.getNodeByPath( [ 0, 3, 0 ] ) ).to.equal( bar );
+			expect( frag.getNodeByPath( [ 0, 3, 1 ] ) ).to.equal( bar );
+			expect( frag.getNodeByPath( [ 0, 3, 2 ] ) ).to.equal( bar );
+			expect( frag.getNodeByPath( [ 0, 3, 3 ] ) ).to.equal( null );
+			expect( frag.getNodeByPath( [ 0, 4 ] ) ).to.equal( bom );
+			expect( frag.getNodeByPath( [ 0, 5 ] ) ).to.equal( bom );
+			expect( frag.getNodeByPath( [ 0, 6 ] ) ).to.equal( bom );
+			expect( frag.getNodeByPath( [ 0, 7 ] ) ).to.equal( null );
 		} );
 	} );
 } );