Explorar el Código

Added missing index for initial items in collecton binging.

Oskar Wróbel hace 8 años
padre
commit
b0607e9371

+ 1 - 1
packages/ckeditor5-utils/src/collection.js

@@ -572,7 +572,7 @@ export default class Collection {
 
 
 		// Load the initial content of the collection.
 		// Load the initial content of the collection.
 		for ( const externalItem of externalCollection ) {
 		for ( const externalItem of externalCollection ) {
-			addItem( null, externalItem );
+			addItem( null, externalItem, externalCollection.getIndex( externalItem ) );
 		}
 		}
 
 
 		// Synchronize the with collection as new items are added.
 		// Synchronize the with collection as new items are added.

+ 21 - 12
packages/ckeditor5-utils/tests/collection.js

@@ -769,6 +769,11 @@ describe( 'Collection', () => {
 				} );
 				} );
 
 
 				it( 'skips when there is no item', () => {
 				it( 'skips when there is no item', () => {
+					// Add before collection is bound.
+					items.add( { value: 1, skip: true } );
+
+					expect( collection ).to.have.length( 0 );
+
 					collection.bindTo( items ).using( item => {
 					collection.bindTo( items ).using( item => {
 						if ( item.skip ) {
 						if ( item.skip ) {
 							return null;
 							return null;
@@ -777,17 +782,18 @@ describe( 'Collection', () => {
 						return item;
 						return item;
 					} );
 					} );
 
 
+					// Still 0 because initial item was skipped.
 					expect( collection ).to.have.length( 0 );
 					expect( collection ).to.have.length( 0 );
 
 
-					items.add( { value: 1, skip: false } );
-					items.add( { value: 2, skip: true } );
-					items.add( { value: 3, skip: false } );
+					items.add( { value: 2, skip: false } );
+					items.add( { value: 3, skip: true } );
+					items.add( { value: 4, skip: false } );
 
 
-					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 1, 3 ] );
+					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
 
 
-					items.add( { value: 4, skip: false }, 1 );
+					items.add( { value: 5, skip: false }, 2 );
 
 
-					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 1, 4, 3 ] );
+					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
 				} );
 				} );
 			} );
 			} );
 
 
@@ -826,19 +832,22 @@ describe( 'Collection', () => {
 				} );
 				} );
 
 
 				it( 'skips when there is no item', () => {
 				it( 'skips when there is no item', () => {
+					items.add( { prop: null } );
+
 					collection.bindTo( items ).using( 'prop' );
 					collection.bindTo( items ).using( 'prop' );
 
 
+					// Still 0 because initial item was skipped.
 					expect( collection ).to.have.length( 0 );
 					expect( collection ).to.have.length( 0 );
 
 
-					items.add( { prop: { value: 'foo' } } );
-					items.add( { value: null } );
-					items.add( { prop: { value: 'biz' } } );
+					items.add( { prop: { value: 2, skip: false } } );
+					items.add( { prop: null } );
+					items.add( { prop: { value: 4, skip: false } } );
 
 
-					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 'foo', 'biz' ] );
+					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 4 ] );
 
 
-					items.add( { prop: { value: 'bar' } }, 1 );
+					items.add( { prop: { value: 5 } }, 2 );
 
 
-					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 'foo', 'bar', 'biz' ] );
+					expect( Array.from( collection, item => item.value ) ).to.deep.equal( [ 2, 5, 4 ] );
 				} );
 				} );
 			} );
 			} );
 		} );
 		} );