From 979177744f806899d187ea15513d6b02d5193291 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 12 Dec 2017 14:49:14 +0100 Subject: [PATCH 01/27] Some working solution for using XHR with filebrowser. --- plugins/dialogui/plugin.js | 1 + plugins/filebrowser/plugin.js | 42 ++++++++++++++++++++++++++--------- plugins/filetools/plugin.js | 9 +++++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/plugins/dialogui/plugin.js b/plugins/dialogui/plugin.js index 04a9ee3431e..1e4e0114844 100644 --- a/plugins/dialogui/plugin.js +++ b/plugins/dialogui/plugin.js @@ -743,6 +743,7 @@ CKEDITOR.plugins.add( 'dialogui', { myDefinition.className = ( myDefinition.className ? myDefinition.className + ' ' : '' ) + 'cke_dialog_ui_button'; myDefinition.onClick = function( evt ) { var target = elementDefinition[ 'for' ]; // [ pageId, elementId ] + // onClick.call() will create XHR request (if possible) and prevent of using submit method. if ( !onClick || onClick.call( this, evt ) !== false ) { dialog.getContentElement( target[ 0 ], target[ 1 ] ).submit(); this.disable(); diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index 5b9c3b67e1a..bc205d4cf91 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -114,6 +114,7 @@ */ ( function() { + 'use strict'; // Default input element name for CSRF protection token. var TOKEN_INPUT_NAME = 'ckCsrfToken'; @@ -200,7 +201,7 @@ } } - // The onlick function assigned to the 'Upload' button. Makes the final + // The onclick function assigned to the 'Upload' button. Makes the final // decision whether form is really submitted and updates target field when // file is uploaded. // @@ -297,22 +298,41 @@ if ( url ) { var onClick = element.onClick; element.onClick = function( evt ) { - // "element" here means the definition object, so we need to find the correct - // button to scope the event call var sender = evt.sender; - if ( onClick && onClick.call( sender, evt ) === false ) + var fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement(); + // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). + if ( editor.config.filebrowser_forceSubmit || !( CKEDITOR.plugins.clipboard && CKEDITOR.plugins.clipboard.isFileApiSupported ) ) { + // "element" here means the definition object, so we need to find the correct + // button to scope the event call + if ( onClick && onClick.call( sender, evt ) === false ) { + return false; + } + + if ( uploadFile.call( sender, evt ) ) { + // Append token preventing CSRF attacks. + appendToken( fileInput ); + return true; + } + return false; + } else { + if ( uploadFile.call( sender, evt ) ) { + var loader = editor.uploadRepository.create( fileInput.$.files[ 0 ] ); + // loader.loadAndUpload( sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).action ); - if ( uploadFile.call( sender, evt ) ) { - var fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement(); + loader.on( 'uploaded', function( evt ) { + var response = evt.sender.responseData; - // Append token preventing CSRF attacks. - appendToken( fileInput ); - return true; - } + setUrl.call( evt.sender.editor, response.url, response.message ); + + } ); + loader.loadAndUpload( CKEDITOR.fileTools.getUploadUrl( editor.config, 'image' ) ); + // Return false to not trigger submit option in dialogui. - return false; + } + return false; + } }; element.filebrowser.url = url; diff --git a/plugins/filetools/plugin.js b/plugins/filetools/plugin.js index f1ca0f2f9fd..953262da248 100644 --- a/plugins/filetools/plugin.js +++ b/plugins/filetools/plugin.js @@ -48,7 +48,8 @@ editor.on( 'fileUploadRequest', function( evt ) { var fileLoader = evt.data.fileLoader, $formData = new FormData(), - requestData = evt.data.requestData; + requestData = evt.data.requestData, + header; for ( var name in requestData ) { var value = requestData[ name ]; @@ -64,6 +65,12 @@ // Append token preventing CSRF attacks. $formData.append( 'ckCsrfToken', CKEDITOR.tools.getCsrfToken() ); + if ( editor.config.xmlHttpRequestHeaders ) { + for ( header in editor.config.xmlHttpRequestHeaders ) { + fileLoader.xhr.setRequestHeader( header, editor.config.xmlHttpRequestHeaders[ header ] ); + } + } + fileLoader.xhr.send( $formData ); }, null, null, 999 ); From 7bf2375f860bd638064983ede4e432d8dcd79c2c Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 13 Dec 2017 13:24:42 +0100 Subject: [PATCH 02/27] Few improvements to using XHR with uploading images. --- plugins/dialogui/plugin.js | 12 +++++++++--- plugins/filebrowser/plugin.js | 33 +++++++++++++++------------------ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/plugins/dialogui/plugin.js b/plugins/dialogui/plugin.js index 1e4e0114844..fdf08c7cff8 100644 --- a/plugins/dialogui/plugin.js +++ b/plugins/dialogui/plugin.js @@ -743,9 +743,15 @@ CKEDITOR.plugins.add( 'dialogui', { myDefinition.className = ( myDefinition.className ? myDefinition.className + ' ' : '' ) + 'cke_dialog_ui_button'; myDefinition.onClick = function( evt ) { var target = elementDefinition[ 'for' ]; // [ pageId, elementId ] - // onClick.call() will create XHR request (if possible) and prevent of using submit method. - if ( !onClick || onClick.call( this, evt ) !== false ) { - dialog.getContentElement( target[ 0 ], target[ 1 ] ).submit(); + + // If exists onClick in elementDefinition, then it is called and checked response type. + // If it's posiblle, then XHR is used, what prevents of using submit. + var responseType = onClick && onClick.call( this, evt ); + + if ( !onClick || responseType !== false ) { + if ( responseType === 'form' ) { + dialog.getContentElement( target[ 0 ], target[ 1 ] ).submit(); + } this.disable(); } }; diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index bc205d4cf91..bd14c0872cb 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -297,42 +297,39 @@ if ( url ) { var onClick = element.onClick; + + // "element" here means the definition object, so we need to find the correct + // button to scope the event call element.onClick = function( evt ) { var sender = evt.sender; var fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement(); - // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). - if ( editor.config.filebrowser_forceSubmit || !( CKEDITOR.plugins.clipboard && CKEDITOR.plugins.clipboard.isFileApiSupported ) ) { - // "element" here means the definition object, so we need to find the correct - // button to scope the event call - if ( onClick && onClick.call( sender, evt ) === false ) { - return false; - } - if ( uploadFile.call( sender, evt ) ) { + if ( onClick && onClick.call( sender, evt ) === false ) { + return false; + } + + if ( uploadFile.call( sender, evt ) ) { + + // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). + if ( editor.config.filebrowser_forceSubmit || !( CKEDITOR.plugins.clipboard && CKEDITOR.plugins.clipboard.isFileApiSupported ) ) { // Append token preventing CSRF attacks. appendToken( fileInput ); - return true; - } + return 'form'; - return false; - } else { - if ( uploadFile.call( sender, evt ) ) { + } else { var loader = editor.uploadRepository.create( fileInput.$.files[ 0 ] ); - // loader.loadAndUpload( sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).action ); loader.on( 'uploaded', function( evt ) { var response = evt.sender.responseData; - setUrl.call( evt.sender.editor, response.url, response.message ); - } ); loader.loadAndUpload( CKEDITOR.fileTools.getUploadUrl( editor.config, 'image' ) ); - // Return false to not trigger submit option in dialogui. + return 'xhr'; } - return false; } + return false; }; element.filebrowser.url = url; From f3449bd661211e60449658bc535c19778ecd73b4 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 13 Dec 2017 16:22:26 +0100 Subject: [PATCH 03/27] Unit test for adding XHR headers. --- tests/plugins/filetools/fileloader.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/plugins/filetools/fileloader.js b/tests/plugins/filetools/fileloader.js index 7a5d8ad0a5c..5a2ff27cf2e 100644 --- a/tests/plugins/filetools/fileloader.js +++ b/tests/plugins/filetools/fileloader.js @@ -14,7 +14,12 @@ testFile, lastFormData, listeners = [], editorMock = { - config: {} + config: { + xmlHttpRequestHeaders: { + foo: 'bar', + hello: 'world' + } + } }, editorMockDefaultFileName = { config: { @@ -160,6 +165,11 @@ setTimeout( function() { xhr.onabort(); }, 0 ); + }, + + setRequestHeader: function( key, value ) { + this.requestHeaders = this.requestHeaders || {}; + this.requestHeaders[ key ] = value; } }; @@ -1215,6 +1225,18 @@ loader.status = 'abort'; assert.isTrue( loader.isFinished() ); + }, + + 'text custom XHR headers': function() { + var loader = new FileLoader( editorMock, pngBase64 ); + + createXMLHttpRequestMock( [ 'load' ] ); + loader.loadAndUpload( 'http://example.com' ); + + resumeAfter( loader, 'uploaded', function( evt ) { + objectAssert.areEqual( { 'foo': 'bar', 'hello': 'world' }, evt.sender.xhr.requestHeaders, 'XHR headers are not equal.' ); + } ); + wait(); } } ); } )(); From 56259abb46d449b4353649252d0d5c3120c1b7f3 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 15 Dec 2017 11:45:05 +0100 Subject: [PATCH 04/27] Change condition to be independent from clipboard plugin. --- plugins/filebrowser/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index bd14c0872cb..e1ceea51e72 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -311,7 +311,7 @@ if ( uploadFile.call( sender, evt ) ) { // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). - if ( editor.config.filebrowser_forceSubmit || !( CKEDITOR.plugins.clipboard && CKEDITOR.plugins.clipboard.isFileApiSupported ) ) { + if ( editor.config.filebrowser_forceSubmit || CKEDITOR.env.ie && CKEDITOR.env.version <= 9 ) { // Append token preventing CSRF attacks. appendToken( fileInput ); return 'form'; From b491787f546d78aa1c9820ad14de74271be5ec37 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 15 Dec 2017 11:49:56 +0100 Subject: [PATCH 05/27] Unit test for uploading images. --- tests/plugins/filebrowser/uploadbutton.js | 138 ++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tests/plugins/filebrowser/uploadbutton.js diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js new file mode 100644 index 00000000000..23a96a70bb5 --- /dev/null +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -0,0 +1,138 @@ +/* bender-tags: editor */ +/* bender-ckeditor-plugins: dialog,filebrowser,filetools,clipboard */ + +( function() { + 'use strict'; + + function mockInput( dialog, submit ) { + return sinon.stub( dialog, 'getContentElement', function() { + return { + getInputElement: function() { + return { + $: { + files: [ + new File( [ '' ], 'sample.png' ) + ], + value: 'C:\\fakepath\\sample.gif', + form: ( new CKEDITOR.dom.element( 'form' ) ).$ + } + }; + }, + getAction: function() { + return 'http://url-to-php-form'; + }, + submit: submit + }; + } ); + } + + bender.editors = { + xhr: { + config: { + xmlHttpRequestHeaders: { + foo: 'bar', + hello: 'world' + }, + filebrowserUploadUrl: 'foo' + } + }, + submit: { + config: { + filebrowserUploadUrl: 'foo', + filebrowser_forceSubmit: true + } + } + }; + + CKEDITOR.on( 'instanceLoaded', function() { + CKEDITOR.dialog.add( 'testDialog', function() { + return { + title: 'Test Dialog', + contents: [ + { + id: 'Upload', + hidden: true, + filebrowser: 'uploadButton', + label: 'Upload input', + elements: [ { + type: 'file', + id: 'upload', + label: 'Load file', + style: 'height:40px', + size: 38 + }, + { + type: 'fileButton', + id: 'uploadButton', + filebrowser: 'Upload:upload', + label: 'Send', + 'for': [ 'Upload', 'upload' ] + } ] + } + ] + }; + } ); + } ); + + bender.test( { + _should: { + ignore: { + 'test for XHR request': CKEDITOR.env.ie && CKEDITOR.env.version < 9 + } + }, + + setUp: function() { + this.xhr = sinon.useFakeXMLHttpRequest(); + var requests = this.requests = []; + + this.xhr.onCreate = function( xhr ) { + requests.push( xhr ); + }; + }, + + tearDown: function() { + this.xhr.restore(); + }, + + 'test for XHR request': function() { + var editor = this.editors.xhr, + bot = this.editorBots.xhr; + + editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); + bot.dialog( 'testDialog', function( dialog ) { + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ); + var inputStub = mockInput( dialog ); + + // Execute just after XHR request is generated; + editor.on( 'fileUploadRequest', function() { + resume( function() { + arrayAssert.isNotEmpty( this.requests ); + objectAssert.areEqual( { 'foo': 'bar', 'hello': 'world' }, this.requests[ 0 ].requestHeaders ); + dialog.hide(); + inputStub.restore(); + } ); + }, null, null, 1000 ); + + sendButton.click(); + wait(); + } ); + + }, + + 'test for submit form': function() { + var editor = this.editors.submit, + bot = this.editorBots.submit; + + editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); + bot.dialog( 'testDialog', function( dialog ) { + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ); + var mockSubmit = sinon.spy(); + var inputStub = mockInput( dialog, mockSubmit ); + sendButton.click(); + assert.isTrue( mockSubmit.called, 'Submit method should be used.' ); + inputStub.restore(); + dialog.hide(); + } ); + } + } ); +} )(); From 156fb1087c20508bae161a3488b7a7236486db03 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 15 Dec 2017 12:52:24 +0100 Subject: [PATCH 06/27] Rename config flag, add docs. --- plugins/filebrowser/plugin.js | 11 ++++++++++- tests/plugins/filebrowser/uploadbutton.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index e1ceea51e72..a3c44e7f0c4 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -311,7 +311,7 @@ if ( uploadFile.call( sender, evt ) ) { // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). - if ( editor.config.filebrowser_forceSubmit || CKEDITOR.env.ie && CKEDITOR.env.version <= 9 ) { + if ( editor.config.filebrowser_forceFormSubmit || CKEDITOR.env.ie && CKEDITOR.env.version <= 9 ) { // Append token preventing CSRF attacks. appendToken( fileInput ); return 'form'; @@ -588,3 +588,12 @@ * @cfg {Number/String} [filebrowserWindowHeight='70%'] * @member CKEDITOR.config */ + +/** + * Force filebrowser plugin to use form submit action instead of XHR request when file is uploaded in dialogs. + * + * config.filebrowser_forceFormSubmit = true + * + * @cfg {Boolean} [filebrowser_forceFormSubmit=false] + * @member CKEDITOR.config + */ diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index 23a96a70bb5..1b0ddcd0f1a 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -39,7 +39,7 @@ submit: { config: { filebrowserUploadUrl: 'foo', - filebrowser_forceSubmit: true + filebrowser_forceFormSubmit: true } } }; From ef08686166acb2f47bcadd479b27110bc269d9bb Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 15 Dec 2017 16:34:15 +0100 Subject: [PATCH 07/27] Code optimization, adding feature detection for file uploading. --- plugins/dialogui/plugin.js | 6 +++--- plugins/filebrowser/plugin.js | 3 +-- plugins/filetools/plugin.js | 26 ++++++++++++++++++++++---- tests/plugins/filetools/fileloader.js | 12 +++++++----- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/plugins/dialogui/plugin.js b/plugins/dialogui/plugin.js index fdf08c7cff8..474b97133fe 100644 --- a/plugins/dialogui/plugin.js +++ b/plugins/dialogui/plugin.js @@ -745,10 +745,10 @@ CKEDITOR.plugins.add( 'dialogui', { var target = elementDefinition[ 'for' ]; // [ pageId, elementId ] // If exists onClick in elementDefinition, then it is called and checked response type. - // If it's posiblle, then XHR is used, what prevents of using submit. - var responseType = onClick && onClick.call( this, evt ); + // If it's possible, then XHR is used, what prevents of using submit. + var responseType = onClick ? onClick.call( this, evt ) : false; - if ( !onClick || responseType !== false ) { + if ( responseType !== false ) { if ( responseType === 'form' ) { dialog.getContentElement( target[ 0 ], target[ 1 ] ).submit(); } diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index a3c44e7f0c4..305bc95b9e9 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -309,9 +309,8 @@ } if ( uploadFile.call( sender, evt ) ) { - // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). - if ( editor.config.filebrowser_forceFormSubmit || CKEDITOR.env.ie && CKEDITOR.env.version <= 9 ) { + if ( editor.config.filebrowser_forceFormSubmit || !CKEDITOR.fileTools.isFileUploadSupported ) { // Append token preventing CSRF attacks. appendToken( fileInput ); return 'form'; diff --git a/plugins/filetools/plugin.js b/plugins/filetools/plugin.js index 953262da248..f5201fefae0 100644 --- a/plugins/filetools/plugin.js +++ b/plugins/filetools/plugin.js @@ -49,6 +49,7 @@ var fileLoader = evt.data.fileLoader, $formData = new FormData(), requestData = evt.data.requestData, + configXHRHeaders = editor.config.xmlHttpRequestHeaders, header; for ( var name in requestData ) { @@ -65,9 +66,9 @@ // Append token preventing CSRF attacks. $formData.append( 'ckCsrfToken', CKEDITOR.tools.getCsrfToken() ); - if ( editor.config.xmlHttpRequestHeaders ) { - for ( header in editor.config.xmlHttpRequestHeaders ) { - fileLoader.xhr.setRequestHeader( header, editor.config.xmlHttpRequestHeaders[ header ] ); + if ( configXHRHeaders ) { + for ( header in configXHRHeaders ) { + fileLoader.xhr.setRequestHeader( header, configXHRHeaders[ header ] ); } } @@ -863,7 +864,24 @@ */ isTypeSupported: function( file, supportedTypes ) { return !!file.type.match( supportedTypes ); - } + }, + + /** + * Property inform if current browser poses native methods used by {@link CKEDITOR.fileTools} to upload files. + * + * @property {Boolean} isFileUploadSupported + */ + isFileUploadSupported: ( function() { + if ( typeof FileReader === 'function' && + typeof ( new FileReader() ).readAsDataURL === 'function' && + typeof FormData === 'function' && + typeof ( new FormData() ).append === 'function' && + typeof XMLHttpRequest === 'function' && typeof Blob === 'function' ) { + + return true; + } + return false; + } )() } ); } )(); diff --git a/tests/plugins/filetools/fileloader.js b/tests/plugins/filetools/fileloader.js index 5a2ff27cf2e..27468cdb653 100644 --- a/tests/plugins/filetools/fileloader.js +++ b/tests/plugins/filetools/fileloader.js @@ -167,10 +167,7 @@ }, 0 ); }, - setRequestHeader: function( key, value ) { - this.requestHeaders = this.requestHeaders || {}; - this.requestHeaders[ key ] = value; - } + setRequestHeader: sinon.stub() }; return xhr; @@ -1234,7 +1231,12 @@ loader.loadAndUpload( 'http://example.com' ); resumeAfter( loader, 'uploaded', function( evt ) { - objectAssert.areEqual( { 'foo': 'bar', 'hello': 'world' }, evt.sender.xhr.requestHeaders, 'XHR headers are not equal.' ); + var setRequestHeaderStub = evt.sender.xhr.setRequestHeader; + + sinon.assert.calledWithExactly( setRequestHeaderStub, 'foo', 'bar' ); + sinon.assert.calledWithExactly( setRequestHeaderStub, 'hello', 'world' ); + + assert.areSame( 2, setRequestHeaderStub.callCount, 'setRequestHeader call count' ); } ); wait(); } From ccfffa1dec5040419fc2fbff4c451b449899123c Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 15 Dec 2017 16:57:37 +0100 Subject: [PATCH 08/27] Preserve backward compatybility with returned data formats. --- plugins/dialogui/plugin.js | 2 +- plugins/filebrowser/plugin.js | 4 ++-- plugins/filetools/plugin.js | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/plugins/dialogui/plugin.js b/plugins/dialogui/plugin.js index 474b97133fe..75cfb12fddd 100644 --- a/plugins/dialogui/plugin.js +++ b/plugins/dialogui/plugin.js @@ -749,7 +749,7 @@ CKEDITOR.plugins.add( 'dialogui', { var responseType = onClick ? onClick.call( this, evt ) : false; if ( responseType !== false ) { - if ( responseType === 'form' ) { + if ( responseType !== 'xhr' ) { dialog.getContentElement( target[ 0 ], target[ 1 ] ).submit(); } this.disable(); diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index 305bc95b9e9..a9d682308dc 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -313,7 +313,7 @@ if ( editor.config.filebrowser_forceFormSubmit || !CKEDITOR.fileTools.isFileUploadSupported ) { // Append token preventing CSRF attacks. appendToken( fileInput ); - return 'form'; + return true; } else { var loader = editor.uploadRepository.create( fileInput.$.files[ 0 ] ); @@ -323,7 +323,7 @@ setUrl.call( evt.sender.editor, response.url, response.message ); } ); - loader.loadAndUpload( CKEDITOR.fileTools.getUploadUrl( editor.config, 'image' ) ); + loader.loadAndUpload( url ); return 'xhr'; } diff --git a/plugins/filetools/plugin.js b/plugins/filetools/plugin.js index f5201fefae0..b1f37ef711f 100644 --- a/plugins/filetools/plugin.js +++ b/plugins/filetools/plugin.js @@ -876,7 +876,8 @@ typeof ( new FileReader() ).readAsDataURL === 'function' && typeof FormData === 'function' && typeof ( new FormData() ).append === 'function' && - typeof XMLHttpRequest === 'function' && typeof Blob === 'function' ) { + typeof XMLHttpRequest === 'function' && + typeof Blob === 'function' ) { return true; } From 9642a81e7ba444cc639fc16747658ba3e4a83d90 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 18 Dec 2017 09:16:54 +0100 Subject: [PATCH 09/27] Little changes in unit tests. --- tests/plugins/filebrowser/uploadbutton.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index 1b0ddcd0f1a..e8edb59f85f 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -100,8 +100,8 @@ editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); bot.dialog( 'testDialog', function( dialog ) { - var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ); - var inputStub = mockInput( dialog ); + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ), + inputStub = mockInput( dialog ); // Execute just after XHR request is generated; editor.on( 'fileUploadRequest', function() { @@ -116,7 +116,6 @@ sendButton.click(); wait(); } ); - }, 'test for submit form': function() { @@ -125,9 +124,10 @@ editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); bot.dialog( 'testDialog', function( dialog ) { - var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ); - var mockSubmit = sinon.spy(); - var inputStub = mockInput( dialog, mockSubmit ); + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ), + mockSubmit = sinon.spy(), + inputStub = mockInput( dialog, mockSubmit ); + sendButton.click(); assert.isTrue( mockSubmit.called, 'Submit method should be used.' ); inputStub.restore(); From 5fcd547e96b9c203f10ad680c3337bd001f14e68 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 18 Dec 2017 11:06:10 +0100 Subject: [PATCH 10/27] Manual tests for XHR upload. --- .../plugins/filebrowser/manual/uploadxhr.html | 50 +++++++++++++++++++ tests/plugins/filebrowser/manual/uploadxhr.md | 18 +++++++ 2 files changed, 68 insertions(+) create mode 100644 tests/plugins/filebrowser/manual/uploadxhr.html create mode 100644 tests/plugins/filebrowser/manual/uploadxhr.md diff --git a/tests/plugins/filebrowser/manual/uploadxhr.html b/tests/plugins/filebrowser/manual/uploadxhr.html new file mode 100644 index 00000000000..aa0d9f1a3e5 --- /dev/null +++ b/tests/plugins/filebrowser/manual/uploadxhr.html @@ -0,0 +1,50 @@ + + +
+ + diff --git a/tests/plugins/filebrowser/manual/uploadxhr.md b/tests/plugins/filebrowser/manual/uploadxhr.md new file mode 100644 index 00000000000..5bed918ec64 --- /dev/null +++ b/tests/plugins/filebrowser/manual/uploadxhr.md @@ -0,0 +1,18 @@ +@bender-tags: 4.8.1, feature, tp3117 +@bender-ui: collapsed +@bender-ckeditor-plugins: wysiwygarea, toolbar, filebrowser, filetools, image, link, flash + +---- +1. Open image dialog +2. Go to upload tab +3. Select some image and send it to server +4. Close dialog (There might appear warning that image url is not set up) + +Repeat those steps for Link and Flash plugin. + +_Note:_ When new upload request is made, it should be visible as separate line below. + +**Expected:** Below editor will appear green div with listed headers attempted to send. There are 2 headers in single line: +`hello: world, foo: bar`. + +**Unexpected:** Headers are not listed below. From 998568048a42c48f7c1c37b9b49c0e3c491597fa Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 19 Dec 2017 18:49:53 +0100 Subject: [PATCH 11/27] Review fixes: chnage in behaviour to have 'form' as default action, big changes to docs related to the feature. --- plugins/filebrowser/plugin.js | 23 ++++++++++--- plugins/filetools/plugin.js | 13 ++++++++ .../plugins/filebrowser/manual/uploadxhr.html | 33 +++++++++---------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index a9d682308dc..35c2b7c5508 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -310,7 +310,8 @@ if ( uploadFile.call( sender, evt ) ) { // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). - if ( editor.config.filebrowser_forceFormSubmit || !CKEDITOR.fileTools.isFileUploadSupported ) { + // With version 4.9.0 change: `!== 'xhr'` to `=== 'form'`. + if ( editor.config.filebrowserUploadMethod !== 'xhr' || !( CKEDITOR.fileTools && CKEDITOR.fileTools.isFileUploadSupported ) ) { // Append token preventing CSRF attacks. appendToken( fileInput ); return true; @@ -589,10 +590,24 @@ */ /** - * Force filebrowser plugin to use form submit action instead of XHR request when file is uploaded in dialogs. + * Define preferred option to submit file upload with filebrowser plugin. If value is not specified, + * `'form'` option is used. Additional XHR headers might be set up with {@link CKEDITOR.config#xmlHttpRequestHeaders}. * - * config.filebrowser_forceFormSubmit = true + * Available values: * - * @cfg {Boolean} [filebrowser_forceFormSubmit=false] + * * `'xhr'` - XMLHttpRequest is used to upload file + * * `'form'` - Form submit is used to upload file + * * `undefiend` - when configuration option is not specified `'form'` configuration is used + * + * Note: please be aware that `'xhr'` requires {@link CKEDITOR.fileTools} for proper work. Missing this plugins + * or using browsers which isn't supported {@link CKEDITOR.fileTools#isFileUploadSupported}, + * will force using `'form'` behaviour despite configuration option. + * + * // Modern browsers will use XMLHttpRequest to upload files. + * // IE8 and IE9 will use form submit even tough config option is set to 'xhr'. + * config.filebrowserUploadMethod = 'xhr'; + * + * @since 4.8.1 + * @cfg {Boolean} [filebrowserUploadMethod=undefined] * @member CKEDITOR.config */ diff --git a/plugins/filetools/plugin.js b/plugins/filetools/plugin.js index b1f37ef711f..ba4e0254dcc 100644 --- a/plugins/filetools/plugin.js +++ b/plugins/filetools/plugin.js @@ -911,3 +911,16 @@ * @cfg {String} [fileTools_defaultFileName=''] * @member CKEDITOR.config */ + +/** + * Additional headers of XMLHttpRequest used during file upload with plugins: {@link CKEDITOR.fileTools} and filebrowser. + * + * config.xmlHttpRequestHeaders = { + * 'Cache-Control': 'no-cache', + * 'X-CUSTOM': 'HEADER' + * }; + * + * @since 4.8.1 + * @cfg {Object} [xmlHttpRequestHeaders=null] + * @member CKEDITOR.config + */ diff --git a/tests/plugins/filebrowser/manual/uploadxhr.html b/tests/plugins/filebrowser/manual/uploadxhr.html index aa0d9f1a3e5..ca69964eb97 100644 --- a/tests/plugins/filebrowser/manual/uploadxhr.html +++ b/tests/plugins/filebrowser/manual/uploadxhr.html @@ -7,29 +7,28 @@ diff --git a/tests/plugins/filebrowser/manual/uploadxhrwitherror.md b/tests/plugins/filebrowser/manual/uploadxhrwitherror.md new file mode 100644 index 00000000000..25a26d63591 --- /dev/null +++ b/tests/plugins/filebrowser/manual/uploadxhrwitherror.md @@ -0,0 +1,16 @@ +@bender-tags: 4.8.1, feature, tp3117 +@bender-ui: collapsed +@bender-ckeditor-plugins: wysiwygarea, toolbar, filebrowser, filetools, image, link, flash + +---- +1. Open image dialog +2. Go to upload tab +3. Select some image and send it to server +4. **Alert** should be displayed +5. Try to upload file again + +Repeat those steps for Link and Flash plugin. + +**Expected:** After first upload attempt, send button is enabled. Which means that every next click trigger new uploading process and generate new alert message. + +**Unexpected:** After first upload send button is disabled. Second and further upload attempts don't trigger upload process, and alert is not shown. diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index e8edb59f85f..c818a2a80ab 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -33,13 +33,15 @@ foo: 'bar', hello: 'world' }, - filebrowserUploadUrl: 'foo' + filebrowserUploadUrl: 'foo', + filebrowserUploadMethod: 'xhr', + lang: 'en' } }, submit: { config: { filebrowserUploadUrl: 'foo', - filebrowser_forceFormSubmit: true + filebrowserUploadMethod: 'form' } } }; @@ -94,7 +96,7 @@ this.xhr.restore(); }, - 'test for XHR request': function() { + 'test for xhr request': function() { var editor = this.editors.xhr, bot = this.editorBots.xhr; @@ -104,7 +106,7 @@ inputStub = mockInput( dialog ); // Execute just after XHR request is generated; - editor.on( 'fileUploadRequest', function() { + editor.once( 'fileUploadRequest', function() { resume( function() { arrayAssert.isNotEmpty( this.requests ); objectAssert.areEqual( { 'foo': 'bar', 'hello': 'world' }, this.requests[ 0 ].requestHeaders ); @@ -133,6 +135,76 @@ inputStub.restore(); dialog.hide(); } ); + }, + + 'test for xhr loader error': function() { + var editor = this.editors.xhr, + bot = this.editorBots.xhr; + + editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); + bot.dialog( 'testDialog', function( dialog ) { + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ), + inputStub = mockInput( dialog ); + + var alertStub = sinon.stub( window, 'alert' ); + + editor.once( 'fileUploadRequest', function( evt ) { + var loader = evt.data.fileLoader; + loader.once( 'error', function() { + resume( function() { + sinon.assert.calledWithExactly( alertStub, 'HTTP error occurred during file upload (403: Forbidden).' ); + assert.isTrue( sendButton.isEnabled(), 'Button should be enabled after error' ); + + dialog.hide(); + inputStub.restore(); + alertStub.restore(); + } ); + } ); + + assert.isFalse( sendButton.isEnabled(), 'Button should be disabled during upload' ); + + // Simulate loading error + loader.xhr.status = 403; + loader.xhr.onload(); + } ); + + sendButton.click(); + wait(); + } ); + }, + + 'test for xhr loader abort': function() { + var editor = this.editors.xhr, + bot = this.editorBots.xhr; + + editor.addCommand( 'testDialog', new CKEDITOR.dialogCommand( 'testDialog' ) ); + bot.dialog( 'testDialog', function( dialog ) { + var sendButton = dialog.getContentElement( 'Upload', 'uploadButton' ), + inputStub = mockInput( dialog ); + + var alertStub = sinon.stub( window, 'alert' ); + + editor.once( 'fileUploadRequest', function( evt ) { + var loader = evt.data.fileLoader; + loader.once( 'abort', function() { + resume( function() { + assert.isTrue( alertStub.called, 'Abort message should appear' ); + assert.isTrue( sendButton.isEnabled(), 'Button should be enabled after abort' ); + + dialog.hide(); + inputStub.restore(); + alertStub.restore(); + } ); + } ); + + assert.isFalse( sendButton.isEnabled(), 'Button should be disabled during upload' ); + + loader.changeStatus( 'abort' ); + } ); + + sendButton.click(); + wait(); + } ); } } ); } )(); From f9f54df477f58fc6e3d5bdab25907fb96a06c95b Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 20 Dec 2017 15:40:19 +0100 Subject: [PATCH 13/27] Simplified file upload api detection. --- plugins/filebrowser/plugin.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugins/filebrowser/plugin.js b/plugins/filebrowser/plugin.js index d362b09c5da..40c68bd44af 100644 --- a/plugins/filebrowser/plugin.js +++ b/plugins/filebrowser/plugin.js @@ -301,8 +301,9 @@ // "element" here means the definition object, so we need to find the correct // button to scope the event call element.onClick = function( evt ) { - var sender = evt.sender; - var fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement(); + var sender = evt.sender, + fileInput = sender.getDialog().getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement(), + isFileUploadApiSupported = CKEDITOR.fileTools && CKEDITOR.fileTools.isFileUploadSupported; if ( onClick && onClick.call( sender, evt ) === false ) { return false; @@ -311,7 +312,7 @@ if ( uploadFile.call( sender, evt ) ) { // Backward compatibility for IE8 and IE9 (https://cksource.tpondemand.com/entity/3117). // With version 4.9.0 change: `!== 'xhr'` to `=== 'form'`. - if ( editor.config.filebrowserUploadMethod !== 'xhr' || !( CKEDITOR.fileTools && CKEDITOR.fileTools.isFileUploadSupported ) ) { + if ( editor.config.filebrowserUploadMethod !== 'xhr' || !isFileUploadApiSupported ) { // Append token preventing CSRF attacks. appendToken( fileInput ); return true; From 40a1d06b0afbaa53a67a4f54bbc8cec90dab72cf Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Wed, 20 Dec 2017 15:59:44 +0100 Subject: [PATCH 14/27] Test: simplified manual test for xhr upload in filebrowser plugin. --- .../plugins/filebrowser/manual/uploadxhr.html | 25 +++++++------------ tests/plugins/filebrowser/manual/uploadxhr.md | 10 ++++---- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/tests/plugins/filebrowser/manual/uploadxhr.html b/tests/plugins/filebrowser/manual/uploadxhr.html index 3612e0241ad..e548952fbc5 100644 --- a/tests/plugins/filebrowser/manual/uploadxhr.html +++ b/tests/plugins/filebrowser/manual/uploadxhr.html @@ -1,11 +1,10 @@ - - - -
+

 
 
diff --git a/tests/plugins/filebrowser/manual/uploadxhr.md b/tests/plugins/filebrowser/manual/uploadxhr.md
index 5bed918ec64..17ba12f107a 100644
--- a/tests/plugins/filebrowser/manual/uploadxhr.md
+++ b/tests/plugins/filebrowser/manual/uploadxhr.md
@@ -1,4 +1,4 @@
-@bender-tags: 4.8.1, feature, tp3117
+@bender-tags: 4.8.1, feature, 643, tp3117
 @bender-ui: collapsed
 @bender-ckeditor-plugins: wysiwygarea, toolbar, filebrowser, filetools, image, link, flash
 
@@ -8,11 +8,11 @@
 3. Select some image and send it to server
 4. Close dialog (There might appear warning that image url is not set up)
 
-Repeat those steps for Link and Flash plugin.
-
-_Note:_ When new upload request is made, it should be visible as separate line below.
-
 **Expected:** Below editor will appear green div with listed headers attempted to send. There are 2 headers in single line:
 `hello: world, foo: bar`.
 
 **Unexpected:** Headers are not listed below.
+
+Repeat those steps for Link and Flash plugin.
+
+_Note:_ When a new upload request is made, it should be visible as a separate line below.
\ No newline at end of file

From d2da1809fef5135121eb9bc4f787c32c8912e3a3 Mon Sep 17 00:00:00 2001
From: Marek Lewandowski 
Date: Wed, 20 Dec 2017 16:07:12 +0100
Subject: [PATCH 15/27] Tests: adjusted manual tests for 404 errors using XHR
 in filebrowser plugin.

---
 .../filebrowser/manual/uploadxhrwitherror.html   |  5 +----
 .../filebrowser/manual/uploadxhrwitherror.md     | 16 +++++++++-------
 2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/tests/plugins/filebrowser/manual/uploadxhrwitherror.html b/tests/plugins/filebrowser/manual/uploadxhrwitherror.html
index 98263162616..944862873b7 100644
--- a/tests/plugins/filebrowser/manual/uploadxhrwitherror.html
+++ b/tests/plugins/filebrowser/manual/uploadxhrwitherror.html
@@ -1,17 +1,14 @@
 
 
 
From 8276465ac06f48063dbd09dfb3a4caa640af1de0 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Thu, 21 Dec 2017 18:13:50 +0100 Subject: [PATCH 23/27] Fixed unit tests for Edge, IEs. --- tests/plugins/filebrowser/uploadbutton.js | 73 +++++++++++++++-------- tests/plugins/filetools/fileloader.js | 2 +- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index 176e2f19aca..ab40b55ffd9 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -4,25 +4,38 @@ ( function() { 'use strict'; + function createFileMock() { + window.File = function( data, name ) { + var file = new Blob( data , {} ); + file.name = name; + + return file; + }; + } + function mockInput( dialog, submit ) { - return sinon.stub( dialog, 'getContentElement', function() { - return { - getInputElement: function() { - return { - $: { - files: [ - new File( [ '' ], 'sample.png' ) - ], - value: 'C:\\fakepath\\sample.gif', - form: ( new CKEDITOR.dom.element( 'form' ) ).$ - } - }; - }, - getAction: function() { - return 'http://url-to-php-form'; - }, - submit: submit - }; + return sinon.stub( dialog, 'getContentElement' ).returns( { + getInputElement: function() { + var ret = { + $: { + value: 'C:\\fakepath\\sample.gif', + form: ( new CKEDITOR.dom.element( 'form' ) ).$ + } + }; + + if ( CKEDITOR.fileTools.isFileUploadSupported ) { + // Only modern browsers will contain files property. + ret.$.files = [ + new File( [ '' ], 'sample.png' ) + ]; + } + + return ret; + }, + getAction: function() { + return 'http://url-to-php-form'; + }, + submit: submit } ); } @@ -77,13 +90,10 @@ } ); bender.test( { - _should: { - ignore: { - 'test for XHR request': CKEDITOR.env.ie && CKEDITOR.env.version < 9 - } - }, - setUp: function() { + if ( typeof MSBlobBuilder === 'function' || !window.File ) + createFileMock(); + this.xhr = sinon.useFakeXMLHttpRequest(); var requests = this.requests = []; @@ -96,7 +106,11 @@ this.xhr.restore(); }, - 'test for xhr request': function() { + 'test for XHR request': function() { + if ( !CKEDITOR.fileTools.isFileUploadSupported ) { + assert.ignore(); + } + var editor = this.editors.xhr, bot = this.editorBots.xhr; @@ -134,10 +148,15 @@ assert.isTrue( mockSubmit.called, 'Submit method should be used.' ); inputStub.restore(); dialog.hide(); + assert.isTrue( true ); } ); }, 'test for xhr loader error': function() { + if ( !CKEDITOR.fileTools.isFileUploadSupported ) { + assert.ignore(); + } + var editor = this.editors.xhr, bot = this.editorBots.xhr; @@ -174,6 +193,10 @@ }, 'test for xhr loader abort': function() { + if ( !CKEDITOR.fileTools.isFileUploadSupported ) { + assert.ignore(); + } + var editor = this.editors.xhr, bot = this.editorBots.xhr; diff --git a/tests/plugins/filetools/fileloader.js b/tests/plugins/filetools/fileloader.js index 06072b7d7a7..2a3be1362d1 100644 --- a/tests/plugins/filetools/fileloader.js +++ b/tests/plugins/filetools/fileloader.js @@ -1224,7 +1224,7 @@ assert.isTrue( loader.isFinished() ); }, - 'text custom XHR headers': function() { + 'test custom XHR headers': function() { var loader = new FileLoader( editorMock, pngBase64 ); createXMLHttpRequestMock( [ 'load' ] ); From 76cacbef1e1ea15a8d87b51fe1322bf37ca57599 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Thu, 21 Dec 2017 18:25:53 +0100 Subject: [PATCH 24/27] Extracted common workarounds. --- tests/plugins/filebrowser/uploadbutton.js | 14 +++----------- tests/plugins/filetools/_helpers/tools.js | 21 +++++++++++++++++++++ tests/plugins/filetools/fileloader.js | 18 +++++------------- 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 tests/plugins/filetools/_helpers/tools.js diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index ab40b55ffd9..bc42d155573 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -1,18 +1,11 @@ /* bender-tags: editor */ /* bender-ckeditor-plugins: dialog,filebrowser,filetools,clipboard */ +/* bender-include: ../filetools/_helpers/tools.js */ +/* global fileTools */ ( function() { 'use strict'; - function createFileMock() { - window.File = function( data, name ) { - var file = new Blob( data , {} ); - file.name = name; - - return file; - }; - } - function mockInput( dialog, submit ) { return sinon.stub( dialog, 'getContentElement' ).returns( { getInputElement: function() { @@ -91,8 +84,7 @@ bender.test( { setUp: function() { - if ( typeof MSBlobBuilder === 'function' || !window.File ) - createFileMock(); + fileTools.mockFileType(); this.xhr = sinon.useFakeXMLHttpRequest(); var requests = this.requests = []; diff --git a/tests/plugins/filetools/_helpers/tools.js b/tests/plugins/filetools/_helpers/tools.js new file mode 100644 index 00000000000..3a5b5457316 --- /dev/null +++ b/tests/plugins/filetools/_helpers/tools.js @@ -0,0 +1,21 @@ +'use strict'; + +/* exported fileTools */ + +var fileTools = { + // A mockup type that should replace File constructor. + fileMock: function( data, name ) { + var file = new Blob( data , {} ); + file.name = name; + + return file; + }, + + // Replaces window.File constructor with a callable constructor replacement, that + // returns file instances mock. + mockFileType: function() { + if ( typeof MSBlobBuilder === 'function' || !window.File ) { + window.File = fileTools.fileMock; + } + } +}; \ No newline at end of file diff --git a/tests/plugins/filetools/fileloader.js b/tests/plugins/filetools/fileloader.js index 2a3be1362d1..8340eee86ac 100644 --- a/tests/plugins/filetools/fileloader.js +++ b/tests/plugins/filetools/fileloader.js @@ -1,9 +1,14 @@ /* bender-tags: editor,clipboard,filetools */ /* bender-ckeditor-plugins: filetools */ +/* bender-include: _helpers/tools.js */ +/* global fileTools */ 'use strict'; ( function() { + // IE/Edge doesn't support File constructor, so there is a need to mimic it. + fileTools.mockFileType(); + var File = window.File, Blob = window.Blob, FormData = window.FormData, @@ -27,15 +32,6 @@ } }; - function createFileMock() { - window.File = File = function( data, name ) { - var file = new Blob( data , {} ); - file.name = name; - - return file; - }; - } - function createFormDataMock() { window.FormData = function() { var entries = {}, @@ -243,10 +239,6 @@ assert.ignore(); } - // IE doesn't support File constructor, so there is a need to mimic it. - if ( typeof MSBlobBuilder === 'function' ) - createFileMock(); - // FormData in IE & Chrome 47- supports only adding data, not getting it, so mocking (polyfilling?) is required. // Note that mocking is needed only for tests, as CKEditor.fileTools uses only append method if ( !FormData.prototype.get || !FormData.prototype.has ) From 1eeab6fbc161f615f8467be8072f7aea9e703522 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Fri, 22 Dec 2017 12:45:42 +0100 Subject: [PATCH 25/27] Tests: removed dummy assertion. --- tests/plugins/filebrowser/uploadbutton.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index bc42d155573..d05f12b8078 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -140,7 +140,6 @@ assert.isTrue( mockSubmit.called, 'Submit method should be used.' ); inputStub.restore(); dialog.hide(); - assert.isTrue( true ); } ); }, From 5a577feb3317283a0366753c521aa697d11d0435 Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Fri, 22 Dec 2017 14:36:57 +0100 Subject: [PATCH 26/27] Tests: corrected language config property. --- tests/plugins/filebrowser/uploadbutton.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins/filebrowser/uploadbutton.js b/tests/plugins/filebrowser/uploadbutton.js index d05f12b8078..de6fbe00262 100644 --- a/tests/plugins/filebrowser/uploadbutton.js +++ b/tests/plugins/filebrowser/uploadbutton.js @@ -41,7 +41,7 @@ }, filebrowserUploadUrl: 'foo', filebrowserUploadMethod: 'xhr', - lang: 'en' + language: 'en' } }, submit: { From cd36fe81794bd499dba534b75e675077e1af43ce Mon Sep 17 00:00:00 2001 From: Marek Lewandowski Date: Fri, 22 Dec 2017 15:01:20 +0100 Subject: [PATCH 27/27] Tests: corrected manual test expected result. --- tests/plugins/filebrowser/manual/uploadxhr.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/plugins/filebrowser/manual/uploadxhr.md b/tests/plugins/filebrowser/manual/uploadxhr.md index 17ba12f107a..54a4b8410cf 100644 --- a/tests/plugins/filebrowser/manual/uploadxhr.md +++ b/tests/plugins/filebrowser/manual/uploadxhr.md @@ -8,8 +8,14 @@ 3. Select some image and send it to server 4. Close dialog (There might appear warning that image url is not set up) -**Expected:** Below editor will appear green div with listed headers attempted to send. There are 2 headers in single line: -`hello: world, foo: bar`. +**Expected:** Below editor will appear green div with listed headers attempted to send. You should see following headers for each case: + +``` +{ + "hello": "world", + "foo": "bar" +} +``` **Unexpected:** Headers are not listed below.