Browse Source

Ensured that plugins loaded into PluginCollection are instances of Plugin.

Aleksander Nowodzinski 10 năm trước cách đây
mục cha
commit
a90e12656a

+ 18 - 1
packages/ckeditor5-engine/src/plugincollection.js

@@ -12,7 +12,13 @@
  * @extends Collection
  */
 
-CKEDITOR.define( [ 'collection', 'promise', 'log' ], function( Collection, Promise, log ) {
+CKEDITOR.define( [
+	'collection',
+	'promise',
+	'plugin',
+	'ckeditorerror',
+	'log'
+], function( Collection, Promise, Plugin, CKEditorError, log ) {
 	class PluginCollection extends Collection {
 		/**
 		 * Creates an instance of the PluginCollection class, initializing it with a set of plugins.
@@ -63,6 +69,17 @@ CKEDITOR.define( [ 'collection', 'promise', 'log' ], function( Collection, Promi
 
 							if ( !isPluginDep ) {
 								var loadedPlugin = new LoadedPlugin( that._editor );
+
+								if ( !( loadedPlugin instanceof Plugin ) ) {
+									/**
+									 * The plugin is not an instance of Plugin.
+									 *
+									 * @error plugincollection-instance
+									 * @param {String} plugin The name of the plugin that is not an instance of Plugin.
+									 */
+									reject( new CKEditorError( 'plugincollection-instance: The plugin is not an instance of Plugin.', { plugin: plugin } ) );
+								}
+
 								loadedPlugin.name = plugin;
 								loadedPlugin.path = CKEDITOR.getPluginPath( plugin );
 								loadedPlugin.deps = deps;

+ 22 - 0
packages/ckeditor5-engine/tests/plugincollection/plugincollection.js

@@ -75,6 +75,15 @@ CKEDITOR.define( 'c', function() {
 	return ( spies.c = sinon.spy() );
 } );
 
+CKEDITOR.define( 'plugin!I', [ 'plugin', 'plugin!J' ], function( Plugin ) {
+	return class extends Plugin {};
+} );
+
+// Note: This is NOT a plugin.
+CKEDITOR.define( 'plugin!J', function() {
+	return class {};
+} );
+
 /////////////
 
 describe( 'load', function() {
@@ -263,6 +272,19 @@ describe( 'load', function() {
 				expect( plugins.length ).to.be.equal( 1 );
 			} );
 	} );
+
+	it( 'should load instances of Plugin only', function() {
+		var PluginCollection = modules.plugincollection;
+		var plugins = new PluginCollection( editor );
+
+		return plugins.load( 'I' )
+			.then( () => {
+				throw new Error( 'Test error: this promise should not be resolved successfully' );
+			} ).catch( err => {
+				expect( err.name ).to.be.equal( 'CKEditorError' );
+				expect( err.message ).to.match( /^plugincollection-instance:/ );
+			} );
+	} );
 } );
 
 describe( 'add', function() {