Răsfoiți Sursa

Fixed: A collision between existing items with arbitrary ids and a new item with automatically generated id.

Aleksander Nowodzinski 10 ani în urmă
părinte
comite
405c5b4521
1 a modificat fișierele cu 15 adăugiri și 1 ștergeri
  1. 15 1
      packages/ckeditor5-engine/src/collection.js

+ 15 - 1
packages/ckeditor5-engine/src/collection.js

@@ -111,7 +111,7 @@ export default class Collection {
 				throw new CKEditorError( 'collection-add-item-already-exists' );
 			}
 		} else {
-			itemId = String( utils.uid() );
+			itemId = this._getNextId();
 			item[ idProperty ] = itemId;
 		}
 
@@ -258,6 +258,20 @@ export default class Collection {
 		return this._items[ Symbol.iterator ]();
 	}
 
+	/**
+	 * Generates next (not yet used) id for unidentified item being add to the collection.
+	 *
+	 * @returns {String} The next id.
+	 */
+	_getNextId() {
+		let id;
+
+		do {
+			id = String( utils.uid() );
+		} while ( this._itemMap.has( id ) );
+
+		return id;
+	}
 }
 
 utilsObject.extend( Collection.prototype, EmitterMixin );