Przeglądaj źródła

Added CKEDITOR.getModulePath.

Piotrek Koszuliński 10 lat temu
rodzic
commit
b5255cc28e

+ 35 - 0
ckeditor.js

@@ -53,6 +53,41 @@ const CKEDITOR = {
 	isDev: true,
 
 	/**
+	 * Resolves a simplified module name convention to a real path. The returned
+	 * paths are relative to the main `ckeditor.js` file, but they do not start with `./`.
+	 *
+	 * For instance:
+	 *
+	 * * `foo` will be transformed to `ckeditor5-foo/foo.js`,
+	 * * `ckeditor` to `ckeditor.js`,
+	 * * `core/editor` to `ckeditor5-core/editor.js` and
+	 * * `foo/bar/bom` to `ckeditor5-foo/bar/bom.js`.
+	 *
+	 * @param {String} name
+	 * @returns {String} Path to the module.
+	 */
+	getModulePath( name ) {
+		//
+		// Note: This piece of code is duplicated in bender.amd.getModulePath().
+		//
+
+		if ( name != 'ckeditor' ) {
+			// Resolve shortened feature names to `featureName/featureName`.
+			if ( name.indexOf( '/' ) < 0 ) {
+				name = name + '/' + name;
+			}
+
+			// Add the prefix to shortened paths like `core/editor` (will be `ckeditor5-core/editor`).
+			// Don't add the prefix to the main file and files frok ckeditor5/ module.
+			if ( !( /^ckeditor5\//.test( name ) ) ) {
+				name = 'ckeditor5-' + name;
+			}
+		}
+
+		return name + '.js';
+	},
+
+	/**
 	 * Computes the value of the `basePath` property.
 	 *
 	 * @private

+ 5 - 6
dev/bender/plugins/ckeditor5/static/amd.js

@@ -18,12 +18,8 @@
 		 * generating a base path for that file, taking into account whether a Bender job is being run
 		 * or a simple test.
 		 *
-		 * The name should be given in a simplified features naming convention. For instance:
-		 *
-		 * * `foo` will be transformed to `<app base path>/ckeditor5-foo/foo.js`,
-		 * * `ckeditor` to `<app base path>/ckeditor.js`,
-		 * * `core/editor` to `<app base path>/ckeditor5-core/editor.js` and
-		 * * `foo/bar/bom` to `<app base path>/ckeditor5-foo/bar/bom.js`.
+		 * The name should be given in a simplified features naming convention. See {@link CKEDITOR#getModulePath}
+		 * for more details.
 		 *
 		 * @param {String} name The name of the module.
 		 * @returns {String} The absolute path to the module.
@@ -48,6 +44,9 @@
 					)
 					.join( '/' );
 
+			// NOTE: This code is duplicated in CKEDITOR.getModulePath() because we're not able to use here
+			// that function. It may be possible to resolve this once we start using ES6 modules and transpilation
+			// also for tests.
 			if ( name != 'ckeditor' ) {
 				// Resolve shortened feature names to `featureName/featureName`.
 				if ( name.indexOf( '/' ) < 0 ) {

+ 0 - 2
tests/.jshintrc

@@ -15,7 +15,6 @@
 	"globalstrict": true,
 
 	"globals": {
-		"CKEDITOR": false,
 		"bender": false,
 		"describe": false,
 		"it": false,
@@ -24,7 +23,6 @@
 		"after": false,
 		"afterEach": false,
 		"expect": false,
-		"bender": false,
 		"sinon": false
 	}
 }

+ 37 - 2
tests/ckeditor/ckeditor.js

@@ -6,11 +6,46 @@
 'use strict';
 
 const modules = bender.amd.require( 'ckeditor' );
+let CKEDITOR;
+
+before( () => {
+	CKEDITOR = modules.ckeditor;
+} );
 
 describe( 'isDebug', () => {
 	it( 'is a boolean', () => {
-		const CKEDITOR = modules.ckeditor;
-
 		expect( CKEDITOR.isDebug ).to.be.a( 'boolean' );
 	} );
 } );
+
+describe( 'getModulePath()', () => {
+	it( 'generates path for the main file', () => {
+		const path = CKEDITOR.getModulePath( 'ckeditor' );
+
+		expect( path ).to.equal( 'ckeditor.js' );
+	} );
+
+	it( 'generates path for modules within ckeditor5 package', () => {
+		const path = CKEDITOR.getModulePath( 'ckeditor5/foo' );
+
+		expect( path ).to.equal( 'ckeditor5/foo.js' );
+	} );
+
+	it( 'generates path for modules within the core package', () => {
+		const path = CKEDITOR.getModulePath( 'core/ui/controller' );
+
+		expect( path ).to.equal( 'ckeditor5-core/ui/controller.js' );
+	} );
+
+	it( 'generates path for modules within some package', () => {
+		const path = CKEDITOR.getModulePath( 'some/ba' );
+
+		expect( path ).to.equal( 'ckeditor5-some/ba.js' );
+	} );
+
+	it( 'generates path from simplified feature name', () => {
+		const path = CKEDITOR.getModulePath( 'foo' );
+
+		expect( path ).to.equal( 'ckeditor5-foo/foo.js' );
+	} );
+} );