Skip to content

Commit

Permalink
Merge branch 't/12961' into major
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinmar committed Mar 12, 2015
2 parents c415544 + 3ef3e87 commit 910de7a
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 17 deletions.
41 changes: 37 additions & 4 deletions plugins/clipboard/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2130,7 +2130,7 @@
}

var that = this,
i;
i, file;

function getAndSetData( type ) {
type = that._.normalizeType( type );
Expand All @@ -2154,12 +2154,19 @@
}

// Copy files references.
if ( this.$ && this.$.files ) {
file = this._getImageFromClipboard();
if ( ( this.$ && this.$.files ) || file ) {
this._.files = [];

for ( i = 0; i < this.$.files.length; i++ ) {
this._.files.push( this.$.files[ i ] );
}

// Don't include $.items if both $.files and $.items contains files, because,
// according to spec and browsers behavior, they contain the same files.
if ( this._.files.length === 0 && file ) {
this._.files.push( file );
}
}
},

Expand All @@ -2177,7 +2184,7 @@
return this.$.files.length;
}

return 0;
return this._getImageFromClipboard() ? 1 : 0;
},

/**
Expand All @@ -2195,7 +2202,8 @@
return this.$.files[ i ];
}

return null;
// File or null if file was not founded.
return i === 0 ? this._getImageFromClipboard() : undefined;
},

/**
Expand Down Expand Up @@ -2243,6 +2251,31 @@
}

return true;
},

/**
* When the contents of the clipboard is pasted on Chrome the clipboard date object has empty `files` property,
* but it is possible to get file as items[0].getAsFile(); (#12961).
*
* @private
* @returns {File} File instance or null if not found.
*/
_getImageFromClipboard: function() {
var file;

if ( this.$ && this.$.items && this.$.items[ 0 ] ) {
try {
file = this.$.items[ 0 ].getAsFile();
// Duck typing
if ( file && file.type ) {
return file;
}
} catch ( err ) {
// noop
}
}

return undefined;
}
};
} )();
Expand Down
9 changes: 5 additions & 4 deletions plugins/filetools/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,11 @@

if ( fileName ) {
this.fileName = fileName;
} else {
} 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( '/', '.' );
}

this.uploaded = 0;
Expand Down Expand Up @@ -736,9 +739,7 @@
byteArrays.push( byteArray );
}

var file = new Blob( byteArrays, { type: contentType } );
file.name = contentType.replace( '/', '.' );
return file;
return new Blob( byteArrays, { type: contentType } );
}

//
Expand Down
120 changes: 120 additions & 0 deletions tests/plugins/clipboard/datatransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,70 @@ bender.test( {
assert.areSame( 2, dataTransfer.getFilesCount() );
assert.areSame( 'foo', dataTransfer.getFile( 0 ) );
assert.areSame( 'bar', dataTransfer.getFile( 1 ) );
assert.isUndefined( dataTransfer.getFile( 2 ) );
},

// #12961
'test file in items': function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
assert.ignore();
}

var nativeData = bender.tools.mockNativeDataTransfer(),
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeData ),
file = { type: 'type' };

nativeData.items = [ {
getAsFile: function() {
return file;
}
} ];

assert.areSame( 1, dataTransfer.getFilesCount() );
assert.areSame( file, dataTransfer.getFile( 0 ) );
assert.isUndefined( dataTransfer.getFile( 1 ) );
},

// #12961
'test file in items with error': function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
assert.ignore();
}

var nativeData = bender.tools.mockNativeDataTransfer(),
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeData );

nativeData.items = [ {
getAsFile: function() {
return {}; // no 'type', so not file.
}
} ];

assert.areSame( 0, dataTransfer.getFilesCount() );
assert.isUndefined( dataTransfer.getFile( 0 ) );
},

// #12961
'test file in items and files': function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
assert.ignore();
}

var nativeData = bender.tools.mockNativeDataTransfer(),
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeData ),
file = { type: 'type' };

nativeData.items = [ {
getAsFile: function() {
return file;
}
} ];

nativeData.files.push( 'foo' );

assert.areSame( 1, dataTransfer.getFilesCount() );
assert.areSame( 'foo', dataTransfer.getFile( 0 ) );
assert.isUndefined( dataTransfer.getFile( 1 ) );
},

'test files with cache': function() {
Expand Down Expand Up @@ -411,6 +475,62 @@ bender.test( {
assert.isFalse( !!dataTransfer.getFile( 2 ) );
},

// #12961
'test file in items with cache': function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
assert.ignore();
}

var nativeData = bender.tools.mockNativeDataTransfer(),
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeData ),
file = { type: 'type' };

nativeData.items = [ {
getAsFile: function() {
return file;
}
} ];

dataTransfer.cacheData();

assert.areSame( 1, dataTransfer.getFilesCount() );
assert.areSame( file, dataTransfer.getFile( 0 ) );
assert.isUndefined( dataTransfer.getFile( 1 ) );

nativeData.files = [];
nativeData.items = [];

assert.areSame( 1, dataTransfer.getFilesCount() );
assert.areSame( file, dataTransfer.getFile( 0 ) );
assert.isUndefined( dataTransfer.getFile( 1 ) );
},

// #12961
'test file in items and files with cache': function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
assert.ignore();
}

var nativeData = bender.tools.mockNativeDataTransfer(),
dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer( nativeData ),
file = { type: 'type' };

nativeData.items = [ {
getAsFile: function() {
return file;
}
} ];

nativeData.files.push( 'foo' );

// debugger;
dataTransfer.cacheData();

assert.areSame( 1, dataTransfer.getFilesCount() );
assert.areSame( 'foo', dataTransfer.getFile( 0 ) );
assert.isUndefined( dataTransfer.getFile( 1 ) );
},

'test isEmpty 1': function() {
var dataTransfer = new CKEDITOR.plugins.clipboard.dataTransfer();

Expand Down
38 changes: 29 additions & 9 deletions tests/plugins/filetools/fileloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@
assert.areSame( 'created', loader.status );
},

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

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

assert.areSame( 'image.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 Expand Up @@ -753,15 +768,18 @@
createXMLHttpRequestMock( [ 'progress', update, 'load', update ] );

resumeAfter( loader, 'uploaded', function() {
observer.assert( [
'update[created,name.png,0/0/82,-,-,-]',
'uploading[uploading,name.png,0/0/82,-,-,-]',
'update[uploading,name.png,0/0/82,-,-,-]',
'update[uploading,name.png,41/0/82,-,-,-]',
'update[uploading,name.png,41/0/82,-,-,-]',
'uploaded[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]',
'update[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]',
'update[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]' ] );
// Wait for all update events.
wait( function() {
observer.assert( [
'update[created,name.png,0/0/82,-,-,-]',
'uploading[uploading,name.png,0/0/82,-,-,-]',
'update[uploading,name.png,0/0/82,-,-,-]',
'update[uploading,name.png,41/0/82,-,-,-]',
'update[uploading,name.png,41/0/82,-,-,-]',
'uploaded[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]',
'update[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]',
'update[uploaded,name2.png,82/0/82,-,-,http://url/name2.png]' ] );
}, 5 );
} );

loader.update();
Expand Down Expand Up @@ -827,6 +845,8 @@
evt.cancel();
} );

createXMLHttpRequestMock( [ 'progress', 'load' ] );

loader.upload( 'http:\/\/url\/' );

observer.assert( [] );
Expand Down

0 comments on commit 910de7a

Please sign in to comment.