Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Enhancement: Make ComponentFactory case-insensitive. #325

Merged
merged 2 commits into from
Oct 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/componentfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class ComponentFactory {
);
}

this._components.set( name, callback );
this._components.set( _componentsMapKey( name ), callback );
}

/**
Expand Down Expand Up @@ -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 );
}

/**
Expand All @@ -121,6 +121,18 @@ export default class ComponentFactory {
* @returns {Boolean}
*/
has( name ) {
return this._components.has( name );
return this._components.has( _componentsMapKey( name ) );
}
}

/**
Copy link
Member

@oleq oleq Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw two patterns for that. I've used the one looked nicer for me ;) But I guess the other pattern is a standard now.

* Ensures that component name used as key in internal map is in lower case.
*
* @ignore
* @private
* @param {String} name
* @returns {String}
*/
function _componentsMapKey( name ) {
Copy link
Member

@oleq oleq Oct 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return String( name ).toLowerCase();
}
30 changes: 29 additions & 1 deletion tests/componentfactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
} );
} );

Expand All @@ -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()', () => {
Expand All @@ -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()', () => {
Expand All @@ -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;
} );
} );
} );