From ce9e15bf9a91a90bf88b9e35ff0e95d7eada482e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 4 Dec 2014 12:26:00 +0100 Subject: [PATCH 1/2] Tests: New tests for HTML and text insertion into not focused editor. --- tests/core/editable/insertion.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/core/editable/insertion.js b/tests/core/editable/insertion.js index 79f6cb5ce96..f51699360e2 100644 --- a/tests/core/editable/insertion.js +++ b/tests/core/editable/insertion.js @@ -108,6 +108,32 @@ // Just to be sure that test is correct. assert.areSame( 2, toHtml, 'toHtml was fired twice' ); } ); + }, + + 'test insertHtml without focus': function() { + bender.editorBot.create( { + name: 'test_inserthtml_no_focus', + config: { + allowedContent: true + } + }, function( bot ) { + bot.editor.insertHtml( '

foo

' ); + + assert.areSame( '

foo

', bot.editor.getData(), 'HTML was inserted' ); + } ); + }, + + 'test insertText without focus': function() { + bender.editorBot.create( { + name: 'test_inserttext_no_focus', + config: { + allowedContent: true + } + }, function( bot ) { + bot.editor.insertText( 'bar' ); + + assert.areSame( '

bar

', bot.editor.getData(), 'text was inserted' ); + } ); } } ); } )(); \ No newline at end of file From 61e79440287b32b3b5355355e10c3a75c3ffb7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 4 Dec 2014 12:26:37 +0100 Subject: [PATCH 2/2] Editor must be focused before retrieving range and before transforming text to HTML what requires context. --- core/editable.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/core/editable.js b/core/editable.js index 5776b883859..fc2ef312ae2 100644 --- a/core/editable.js +++ b/core/editable.js @@ -238,6 +238,8 @@ * @param {String} text */ insertText: function( text ) { + // Focus the editor before calling transformPlainTextToHtml. (#12726) + this.editor.focus(); this.insertHtml( this.transformPlainTextToHtml( text ), 'text' ); }, @@ -283,12 +285,15 @@ * @param {String} [mode='html'] See {@link CKEDITOR.editor#method-insertHtml}'s param. */ insertHtmlIntoSelection: function( data, mode ) { + var editor = this.editor; + + editor.focus(); + editor.fire( 'saveSnapshot' ); + // HTML insertion only considers the first range. // Note: getRanges will be overwritten for tests since we want to test - // custom ranges and bypass native selections. - var range = this.editor.getSelection().getRanges()[ 0 ]; - - beforeInsert( this ); + // custom ranges and bypass native selections. + var range = editor.getSelection().getRanges()[ 0 ]; // Default mode is 'html'. insert( this, mode || 'html', data, range ); @@ -402,11 +407,13 @@ * @param {CKEDITOR.dom.element} element The element to be inserted. */ insertElementIntoSelection: function( element ) { + var editor = this.editor; + // Prepare for the insertion. For example - focus editor (#11848). - beforeInsert( this ); + editor.focus(); + editor.fire( 'saveSnapshot' ); - var editor = this.editor, - enterMode = editor.activeEnterMode, + var enterMode = editor.activeEnterMode, selection = editor.getSelection(), range = selection.getRanges()[ 0 ], elementName = element.getName(), @@ -2012,13 +2019,6 @@ return insert; } )(); - function beforeInsert( editable ) { - // TODO: For unknown reason we must call directly on the editable to put the focus immediately. - editable.editor.focus(); - - editable.editor.fire( 'saveSnapshot' ); - } - function afterInsert( editable ) { var editor = editable.editor;