Index: /CKEditor/branches/prototype/_source/core/dom/range.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/dom/range.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/core/dom/range.js	(revision 2634)
@@ -386,8 +386,14 @@
 		{
 			this.setStartBefore( bookmark.startNode );
+
+			if ( bookmark.endNode )
+			{
+				this.setEndBefore( bookmark.endNode );
+				bookmark.endNode.remove();
+			}
+			else
+				this.setEndBefore( bookmark.startNode );
+			
 			bookmark.startNode.remove();
-
-			this.setEndBefore( bookmark.endNode );
-			bookmark.endNode.remove();
 		},
 
Index: /CKEditor/branches/prototype/_source/core/editor.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/editor.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/core/editor.js	(revision 2634)
@@ -422,5 +422,5 @@
 		 * @param {String} data HTML code to be inserted into the editor.
 		 * @example
-		 * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>
+		 * CKEDITOR.instances.editor1.<b>insertHtml( '&lt;p&gt;This is a new paragraph.&lt;/p&gt;' )</b>;
 		 */
 		insertHtml : function( data )
@@ -429,6 +429,20 @@
 			this.fire( 'insertHtml', eventData );
 			this.fire( 'afterInsertHtml', eventData );
-			if ( eventData.html && eventData.html != this._.data )
-				this._.data = eventData.html;
+			this._.data = this.document.getBody().getHtml();
+		},
+
+		/**
+		 * Inserts an element into the currently selected position in the editor.
+		 * @param {CKEDITOR.dom.element} element Element to be inserted into the editor.
+		 * @example
+		 * var element = CKEDITOR.dom.element.createFromHtml( '<img src="hello.png" border="0" title="Hello" />' );
+		 * CKEDITOR.instances.editor1.<b>insertElement( element )</b>;
+		 */
+		insertElement : function( element )
+		{
+			var eventData = { insert : element };
+			this.fire( 'insertElement', eventData );
+			this.fire( 'afterInsertElement', eventData );
+			this._.data = this.document.getBody().getHtml();
 		},
 
Index: /CKEditor/branches/prototype/_source/plugins/dialog/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/dialog/plugin.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/plugins/dialog/plugin.js	(revision 2634)
@@ -275,4 +275,8 @@
 		this._.buttons[ buttons[i].id ] = buttons[i];
 
+	// Insert dummy text box for grabbing focus away from the editing area.
+	this._.dummyText = CKEDITOR.dom.element.createFromHtml( '<input type="text" style="position: absolute; left: -100000px; top: -100000px" />' );
+	this._.dummyText.appendTo( element );
+
 	CKEDITOR.skins.load( editor.config.skin, 'dialog' );
 };
@@ -419,4 +423,9 @@
 			CKEDITOR.dialog._.currentTop = this;
 		}
+
+		// Save editor selection and grab the focus.
+		this.saveSelection();
+		this._.dummyText.focus();
+		this._.dummyText.$.select();
 		
 		// Execute onLoad for the first show.
@@ -668,4 +677,21 @@
 	{
 		return this._.pageCount;
+	},
+
+	saveSelection : function()
+	{
+		if ( this._.editor.mode )
+		{
+			var selection = new CKEDITOR.dom.selection( this._.editor.document );
+			this._.selectedRanges = selection.getRanges();
+		}
+	},
+
+	restoreSelection : function()
+	{
+		if ( this._.editor.mode && this._.selectedRanges )
+		{
+			( new CKEDITOR.dom.selection( this._.editor.document ) ).selectRanges( this._.selectedRanges );
+		}
 	}
 };
Index: /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js	(revision 2634)
@@ -74,15 +74,39 @@
 			editor.on( 'afterInsertHtml', function( evt )
 				{
-					if ( !isHandlingData && editor.mode )
-					{
-						isHandlingData = true;
+					if ( editor.mode )
+					{
 						if ( CKEDITOR.env.ie )
 							editor.document.$.selection.createRange().pasteHtml( evt.data.insert );
 						else
 							editor.document.$.execCommand( 'inserthtml', false, evt.data.insert );
-						evt.data.html = editor.document.getBody().getHtml();
-						isHandlingData = false;
-					}
-				});
+					}
+				});
+
+			editor.on( 'afterInsertElement', function( evt )
+				{
+					if ( editor.mode )
+					{
+						var selection = new CKEDITOR.dom.selection( editor.document ),
+							ranges = selection.getRanges(),
+							clone, lastRange, bookmark;
+						for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
+						{
+							ranges[i].deleteContents();
+							clone = evt.data.insert.clone( true );
+							ranges[i].insertNode( clone );
+
+							if ( !lastRange )
+							{
+								lastRange = ranges[i];
+								lastRange.setStartAfter( clone );
+								lastRange.collapse( true );
+								bookmark = lastRange.createBookmark();
+							}
+						}
+
+						lastRange.moveToBookmark( bookmark );
+						selection.selectRanges( [lastRange] );
+					}
+				} );
 
 			editor.on( 'beforeGetData', function()
Index: /CKEditor/branches/prototype/_source/plugins/selection/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/selection/plugin.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/plugins/selection/plugin.js	(revision 2634)
@@ -629,5 +629,6 @@
 			else
 			{
-				isStartMakerAlone = ( !startNode.hasPrevious() || startNode.getPrevious().is( 'br' ) ) && !startNode.hasNext();
+				isStartMakerAlone = ( !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) )
+					&& !startNode.hasNext();
 
 				// Append a temporary <span>&#65279;</span> before the selection.
Index: /CKEditor/branches/prototype/_source/plugins/smiley/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/smiley/plugin.js	(revision 2633)
+++ /CKEditor/branches/prototype/_source/plugins/smiley/plugin.js	(revision 2634)
@@ -68,6 +68,9 @@
 					target = target.$;
 
-				editor.insertHtml( '<img src="' + CKEDITOR.tools.htmlEncode( target.src ) + '" />' );
+				this.getDialog().restoreSelection();
+				editor.insertElement( CKEDITOR.dom.element.createFromHtml( '<img src="' + CKEDITOR.tools.htmlEncode( target.src ) + '" />',
+					editor.document ) );
 				this.getDialog().hide();
+				editor.focus();
 			},
 			style : 'width: 100%; height: 100%; border-collapse: separate;'
