Przeglądaj źródła

Enhancement: Make ComponentFactory case-insensitive.

Maciej Gołaszewski 8 lat temu
rodzic
commit
c774b728be

+ 15 - 3
packages/ckeditor5-ui/src/componentfactory.js

@@ -83,7 +83,7 @@ export default class ComponentFactory {
 			);
 		}
 
-		this._components.set( name, callback );
+		this._components.set( _componentsMapKey( name ), callback );
 	}
 
 	/**
@@ -111,7 +111,7 @@ export default class ComponentFactory {
 			);
 		}
 
-		return this._components.get( name )( this.editor.locale );
+		return this._components.get( _componentsMapKey( name ) )( this.editor.locale );
 	}
 
 	/**
@@ -121,6 +121,18 @@ export default class ComponentFactory {
 	 * @returns {Boolean}
 	 */
 	has( name ) {
-		return this._components.has( name );
+		return this._components.has( _componentsMapKey( name ) );
 	}
 }
+
+/**
+ * Ensures that component name used as key in internal map is in lower case.
+ *
+ * @ignore
+ * @private
+ * @param {String} name
+ * @returns {String}
+ */
+function _componentsMapKey( name ) {
+	return String( name ).toLowerCase();
+}

+ 29 - 1
packages/ckeditor5-ui/tests/componentfactory.js

@@ -31,8 +31,9 @@ describe( 'ComponentFactory', () => {
 		it( 'returns iterator of command names', () => {
 			factory.add( 'foo', () => {} );
 			factory.add( 'bar', () => {} );
+			factory.add( 'Baz', () => {} );
 
-			expect( Array.from( factory.names() ) ).to.have.members( [ 'foo', 'bar' ] );
+			expect( Array.from( factory.names() ) ).to.have.members( [ 'foo', 'bar', 'baz' ] );
 		} );
 	} );
 
@@ -44,6 +45,14 @@ describe( 'ComponentFactory', () => {
 				factory.add( 'foo', () => {} );
 			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
 		} );
+
+		it( 'throws when trying to override already registered component added with different case', () => {
+			factory.add( 'Foo', () => {} );
+
+			expect( () => {
+				factory.add( 'foo', () => {} );
+			} ).to.throw( CKEditorError, /^componentfactory-item-exists/ );
+		} );
 	} );
 
 	describe( 'create()', () => {
@@ -69,6 +78,23 @@ describe( 'ComponentFactory', () => {
 			expect( instance ).to.be.instanceof( View );
 			expect( instance.locale ).to.equal( locale );
 		} );
+
+		it( 'creates an instance even with different case', () => {
+			class View {
+				constructor( locale ) {
+					this.locale = locale;
+				}
+			}
+
+			const locale = editor.locale = {};
+
+			factory.add( 'Foo', locale => new View( locale ) );
+
+			const instance = factory.create( 'foo' );
+
+			expect( instance ).to.be.instanceof( View );
+			expect( instance.locale ).to.equal( locale );
+		} );
 	} );
 
 	describe( 'has()', () => {
@@ -79,6 +105,8 @@ describe( 'ComponentFactory', () => {
 			expect( factory.has( 'foo' ) ).to.be.true;
 			expect( factory.has( 'bar' ) ).to.be.true;
 			expect( factory.has( 'baz' ) ).to.be.false;
+			expect( factory.has( 'Foo' ) ).to.be.true;
+			expect( factory.has( 'fOO' ) ).to.be.true;
 		} );
 	} );
 } );