Skip to content

Commit

Permalink
Merge branch 't/13501'
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Aug 6, 2015
2 parents ea2dd2f + 2333e2e commit 9c406a0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -3,7 +3,9 @@ CKEditor 4 Changelog

## CKEditor 4.5.3

New Features:

* [#13501](http://dev.ckeditor.com/ticket/13501): Added [`config.fileTools_defaultFileName`](http://docs.ckeditor.com/#!/api/CKEDITOR.config-cfg-fileTools_defaultFileName) option to allow setting default filename for paste uploads.

## CKEditor 4.5.2

Expand Down
34 changes: 29 additions & 5 deletions plugins/filetools/plugin.js
Expand Up @@ -239,9 +239,12 @@
* string encoded with Base64.
* @param {String} [fileName] The file name. If not set and the second parameter is a file, then its name will be used.
* If not set and the second parameter is a Base64 data string, then the file name will be created based on
* the MIME type by replacing '/' with '.', for example for `image/png` the file name will be `image.png`.
* the {@link CKEDITOR.config#fileTools_defaultFileName} option.
*/
function FileLoader( editor, fileOrData, fileName ) {
var mimeParts,
defaultFileName = editor.config.fileTools_defaultFileName;

this.editor = editor;
this.lang = editor.lang;

Expand All @@ -263,8 +266,13 @@
} else if ( this.file.name ) {
this.fileName = this.file.name;
} else {
// If file has no name create a name based on the mime type.
this.fileName = this.file.type.replace( '/', '.' );
mimeParts = this.file.type.split( '/' );

if ( defaultFileName ) {
mimeParts[ 0 ] = defaultFileName;
}

this.fileName = mimeParts.join( '.' );
}

this.uploaded = 0;
Expand Down Expand Up @@ -306,8 +314,8 @@
*/

/**
* The name of the file. If there is no file name, it is created from the MIME type.
* For example for the `image/png` MIME type, the file name will be `image.png`.
* The name of the file. If there is no file name, it is created by using the
* {@link CKEDITOR.config#fileTools_defaultFileName} option.
*
* @readonly
* @property {String} fileName
Expand Down Expand Up @@ -761,3 +769,19 @@
* @cfg {String} [uploadUrl='']
* @member CKEDITOR.config
*/

/**
* Default file name (without extension) that will be used for files created from a Base64 data string
* (for example for files pasted into the editor).
* This name will be combined with MIME type to create full file name with the extension.
*
* If `fileTools_defaultFileName` will be set to `default-name` and data's MIME type will be `image/png`
* the resulting file name will be `default-name.png`.
*
* If `fileTools_defaultFileName` is not set then the file name will be created using only its MIME type.
* For example for `image/png` the file name will be `image.png`.
*
* @since 4.5.3
* @cfg {String} [fileTools_defaultFileName='']
* @member CKEDITOR.config
*/
46 changes: 40 additions & 6 deletions tests/plugins/filetools/fileloader.js
Expand Up @@ -10,7 +10,14 @@
pngBase64 = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAAAXNSR0IArs4c6QAAAAxJREFUCNdjYGBgAAAABAABJzQnCgAAAABJRU5ErkJggg==',
testFile, lastFormData,
listeners = [],
editorMock = {};
editorMock = {
config: {}
},
editorMockDefaultFileName = {
config: {
fileTools_defaultFileName: 'default-file-name'
}
};

function createFileReaderMock( scenario ) {
var isAborted = false;
Expand Down Expand Up @@ -200,7 +207,7 @@
},

'test constructor string, no name': function() {
var loader = new FileLoader( {}, pngBase64 );
var loader = new FileLoader( editorMock, pngBase64 );

assert.areSame( 'image.png', loader.fileName );
assert.areSame( pngBase64, loader.data );
Expand All @@ -211,8 +218,20 @@
assert.areSame( 'created', loader.status );
},

'test constructor string, no name, default file name provided': function() {
var loader = new FileLoader( editorMockDefaultFileName, pngBase64 );

assert.areSame( editorMockDefaultFileName.config.fileTools_defaultFileName + '.png', loader.fileName );
assert.areSame( pngBase64, loader.data );
assert.isObject( loader.file );
assert.areSame( 82, loader.total );
assert.areSame( 82, loader.loaded );
assert.areSame( 0, loader.uploaded );
assert.areSame( 'created', loader.status );
},

'test constructor string, filename': function() {
var loader = new FileLoader( {}, pngBase64, 'foo' );
var loader = new FileLoader( editorMock, pngBase64, 'foo' );

assert.areSame( 'foo', loader.fileName );
assert.areSame( pngBase64, loader.data );
Expand All @@ -224,7 +243,7 @@
},

'test constructor file, no name': function() {
var loader = new FileLoader( {}, testFile );
var loader = new FileLoader( editorMock, testFile );

assert.areSame( 'name.png', loader.fileName );
assert.isNull( loader.data );
Expand All @@ -236,7 +255,7 @@
},

'test constructor file, filename': function() {
var loader = new FileLoader( {}, testFile, 'bar' );
var loader = new FileLoader( editorMock, testFile, 'bar' );

assert.areSame( 'bar', loader.fileName );
assert.isNull( loader.data );
Expand All @@ -251,7 +270,7 @@
var testFileWithoutName = bender.tools.getTestPngFile();
testFileWithoutName.name = undefined;

var loader = new FileLoader( {}, testFileWithoutName );
var loader = new FileLoader( editorMock, testFileWithoutName );

assert.areSame( 'image.png', loader.fileName );
assert.isNull( loader.data );
Expand All @@ -262,6 +281,21 @@
assert.areSame( 'created', loader.status );
},

'test constructor file, no filename in file, default file name provided': function() {
var testFileWithoutName = bender.tools.getTestPngFile();
testFileWithoutName.name = undefined;

var loader = new FileLoader( editorMockDefaultFileName, testFileWithoutName );

assert.areSame( editorMockDefaultFileName.config.fileTools_defaultFileName + '.png', loader.fileName );
assert.isNull( loader.data );
assert.isObject( loader.file );
assert.areSame( 82, loader.total );
assert.areSame( 0, loader.loaded );
assert.areSame( 0, loader.uploaded );
assert.areSame( 'created', loader.status );
},

'test load': function() {
var loader = new FileLoader( editorMock, testFile ),
observer = observeEvents( loader );
Expand Down
3 changes: 2 additions & 1 deletion tests/plugins/uploadwidget/uploadwidget.js
Expand Up @@ -63,7 +63,8 @@
widgets: {
add: function() {}
},
lang: {}
lang: {},
config: {}
};

editor.uploadRepository = new CKEDITOR.fileTools.uploadRepository( editor );
Expand Down

0 comments on commit 9c406a0

Please sign in to comment.