Index: _source/plugins/selection/plugin.js
===================================================================
--- _source/plugins/selection/plugin.js	(revision 3848)
+++ _source/plugins/selection/plugin.js	Fri Jul 10 11:18:42 CST 2009
@@ -421,22 +421,41 @@
 
 					if ( !sel )
 						type = CKEDITOR.SELECTION_NONE;
-					else if ( sel.rangeCount == 1 )
+					else if ( sel.rangeCount == 1 && !sel.getRangeAt(0).collapsed )
 					{
 						// Check if the actual selection is a control (IMG,
 						// TABLE, HR, etc...).
-
-						var range = sel.getRangeAt(0),
+						var range = this.getRanges()[ 0 ],
 							startContainer = range.startContainer;
 
-						if ( startContainer == range.endContainer
-							&& startContainer.nodeType == 1
+						if ( startContainer.equals( range.endContainer )
+							&& startContainer.is
 							&& ( range.endOffset - range.startOffset ) == 1
-							&& styleObjectElements[ startContainer.childNodes[ range.startOffset ].nodeName.toLowerCase() ] )
+							&& startContainer.getChild(
+							 range.startOffset ).getName() in styleObjectElements )
+							type = CKEDITOR.SELECTION_ELEMENT;
+						// Webkit and Opera's element selection may leak into siblings
+						// nodes.( #3846)
+						else if ( CKEDITOR.env.webkit || CKEDITOR.env.opera )
 						{
+
+							var walkerRange = range.clone(),
+								walker = new CKEDITOR.dom.walker( walkerRange );
+							walker.guard = CKEDITOR.dom.walker.text( true, true );
+							walker.evaluator = function( node )
+							{
+								return node.is && node.getName() in styleObjectElements;
+							};
+							var selectedElement = walker.next();
+							walker.reset();
+							if ( selectedElement && selectedElement.equals( walker.previous() ) )
+							{
-							type = CKEDITOR.SELECTION_ELEMENT;
+								type = CKEDITOR.SELECTION_ELEMENT;
+								// Determinate the selected element now.
+								cache.selectedElement = selectedElement;
-						}
-					}
+							}
+						}
+					}
 
 					return ( cache.type = type );
 				},
@@ -694,13 +713,13 @@
 		 */
 		getSelectedElement : function()
 		{
+			var selectionType = this.getType();
 			var cache = this._.cache;
 			if ( cache.selectedElement !== undefined )
 				return cache.selectedElement;
 
 			var node;
-
-			if ( this.getType() == CKEDITOR.SELECTION_ELEMENT )
+			if ( selectionType == CKEDITOR.SELECTION_ELEMENT )
 			{
 				var sel = this.getNative();
 
Index: _source/core/dom/walker.js
===================================================================
--- _source/core/dom/walker.js	(revision 3607)
+++ _source/core/dom/walker.js	Fri Jul 10 11:13:52 CST 2009
@@ -304,9 +304,17 @@
 			lastBackward : function()
 			{
 				return iterateToLast.call( this, true );
-			}
+			},
 
+			/**
+			 * Reset walker to initial state.
+			 */
+			reset : function()
+			{
+				delete this._.end;
+				delete this.current;
-		}
+			}
+		}
 	});
 
 	/*
@@ -389,4 +397,18 @@
 		};
 	};
 
+	/**
+	 * Whether the to-be-evaluated node is a text node.
+	 * @param {Boolean} ignoreEmpty Whether count empty text node.
+	 * @param {Boolean} isReject
+	 */
+	CKEDITOR.dom.walker.text = function( ignoreEmpty, isReject )
+	{
+		return function( node )
+		{
+			var retval = node && !node.is && ( !ignoreEmpty || node.getText().length )
+			return isReject ? !retval : !!retval;
+		};
+	};
+
 })();
Index: _source/tests/plugins/selection/selection.html
===================================================================
--- _source/tests/plugins/selection/selection.html	(revision 3477)
+++ _source/tests/plugins/selection/selection.html	Fri Jul 10 11:13:52 CST 2009
@@ -2,7 +2,7 @@
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 	<title>Plugin: selection</title>
-	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 	<link rel="stylesheet" type="text/css" href="../../test.css" />
 	<script type="text/javascript" src="../../../../ckeditor_source.js"></script>
 	<script type="text/javascript" src="../../test.js"></script>
@@ -36,6 +36,33 @@
 			assert.areSame( 0, $range.compareEndPoints( 'EndToEnd', $range2 ), 'EndToEnd' );
 		},
 
+		// Test select a image surrounded by texts.
+		test_selection_3846 : function()
+		{
+			var element = doc.getById( 'testZone' );
+			element.setHtml( '<p>A normal image <img src="../../_assets/regular_smile.gif" id="testImage"> inside the text<\/p>' );
+			var image = doc.getById('testImage');
+
+			doc.getSelection().selectElement( image );
+			var selectedElement = doc.getSelection().getSelectedElement() ;
+			assert.areSame( CKEDITOR.SELECTION_ELEMENT, doc.getSelection().getType() );
+			assert.isNotNull( selectedElement, 'The floated image hasn\'t been selected' );
+			assert.areSame( image.$, selectedElement.$, 'The floated image hasn\'t been selected correctly' );
+		},
+
+		// Test select a float image surrounded by inline style elements.
+		test_selection_3846_2 : function()
+		{
+			var element = doc.getById( 'testZone' );
+			element.setHtml( '<p><b>some</b><span><img src="../../_assets/regular_smile.gif" align="left" id="testImage"></span><b>text</b><\/p>' );
+			var image = doc.getById('testImage');
+
+			doc.getSelection().selectElement( image );
+			var selectedElement = doc.getSelection().getSelectedElement() ;
+			assert.areSame( CKEDITOR.SELECTION_ELEMENT, doc.getSelection().getType(), 'The selection type is incorrect.' );
+			assert.isNotNull( selectedElement, 'The floated image hasn\'t been selected' );
+			assert.areSame( image.$, selectedElement.$, 'The floated image hasn\'t been selected correctly' );
+		},
 		name : document.title
 	};
 })());
@@ -45,5 +72,8 @@
 <body>
 	<p>
 		This is some <strong>sample text</strong>. You are using <a href="http://www.fckeditor.net/">CKEditor</a>.</p>
+		<p>&nbsp;</p>
+		<p>&nbsp;</p>
+	<div id="testZone"></div>
 </body>
 </html>
Index: CHANGES.html
===================================================================
--- CHANGES.html	(revision 3853)
+++ CHANGES.html	Fri Jul 10 11:13:52 CST 2009
@@ -117,6 +117,7 @@
  		<li><a href="http://dev.fckeditor.net/ticket/3887">#3887</a> : Fixed an issue in which the create
  			list command may leak outside of a selected table cell and into the rest of document.</li>
  		<li><a href="http://dev.fckeditor.net/ticket/3916">#3916</a> : Fixed maximize does not enlarge editor width when width is set.</li>
+		<li><a href="http://dev.fckeditor.net/ticket/3846">#3846</a> : [Safari/Chrome]Image selection broken when surrounded with texts.</li>
 	</ul>
 	<h3>
 		CKEditor 3.0 RC</h3>
