Index: _source/plugins/link/dialogs/link.js
===================================================================
--- _source/plugins/link/dialogs/link.js	(revision 5237)
+++ _source/plugins/link/dialogs/link.js	Fri Mar 26 11:28:02 CST 2010
@@ -1,10 +1,11 @@
-/*
+/*
 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
 For licensing, see LICENSE.html or http://ckeditor.com/license
 */
 
 CKEDITOR.dialog.add( 'link', function( editor )
 {
+	var plugin = CKEDITOR.plugins.link;
 	// Handles the event when the "Target" selection box is changed.
 	var targetChanged = function()
 	{
@@ -93,7 +94,7 @@
 
 	var parseLink = function( editor, element )
 	{
-		var href = element ? ( element.getAttribute( '_cke_saved_href' ) || element.getAttribute( 'href' ) ) : '',
+		var href = ( element  && ( element.getAttribute( '_cke_saved_href' ) || element.getAttribute( 'href' ) ) ) || '',
 		 	javascriptMatch,
 			emailMatch,
 			anchorMatch,
@@ -1136,30 +1137,21 @@
 
 			var editor = this.getParentEditor(),
 				selection = editor.getSelection(),
-				ranges = selection.getRanges(),
-				element = null,
-				me = this;
-			// Fill in all the relevant fields if there's already one link selected.
-			if ( ranges.length == 1 )
-			{
+				element = null;
 
-				var rangeRoot = ranges[0].getCommonAncestor( true );
-				element = rangeRoot.getAscendant( 'a', true );
-				if ( element && element.getAttribute( 'href' ) )
-				{
+			// Fill in all the relevant fields if there's already one link selected.
+			if ( ( element = plugin.getSelectedLink( editor ) ) && element.hasAttribute( 'href' ) )
-					selection.selectElement( element );
+				selection.selectElement( element );
-				}
-				else if ( ( element = rangeRoot.getAscendant( 'img', true ) ) &&
-						 element.getAttribute( '_cke_real_element_type' ) &&
-						 element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
+			else if ( ( element = selection.getSelectedElement() ) && element.is( 'img' )
+					&& element.getAttribute( '_cke_real_element_type' )
+					&& element.getAttribute( '_cke_real_element_type' ) == 'anchor' )
-				{
-					this.fakeObj = element;
-					element = editor.restoreRealElement( this.fakeObj );
-					selection.selectElement( this.fakeObj );
-				}
-				else
-					element = null;
+			{
+				this.fakeObj = element;
+				element = editor.restoreRealElement( this.fakeObj );
+				selection.selectElement( this.fakeObj );
+			}
+			else
+				element = null;
-			}
 
 			this.setupContent( parseLink.apply( this, [ editor, element ] ) );
 		},
Index: _source/plugins/link/plugin.js
===================================================================
--- _source/plugins/link/plugin.js	(revision 5244)
+++ _source/plugins/link/plugin.js	Fri Mar 26 11:35:22 CST 2010
@@ -107,7 +107,7 @@
 
 					if ( !isAnchor )
 					{
-						if ( !( element = element.getAscendant( 'a', true ) ) )
+						if ( !( element = CKEDITOR.plugins.link.getSelectedLink( editor ) ) )
 							return null;
 
 						isAnchor = ( element.getAttribute( 'name' ) && !element.getAttribute( 'href' ) );
@@ -147,6 +147,35 @@
 	requires : [ 'fakeobjects' ]
 } );
 
+CKEDITOR.plugins.link =
+{
+	/**
+	 *  Get the surrounding link element of current selection.
+	 * @param editor
+	 * @example CKEDITOR.plugins.link.getSelectedLink( editor );
+	 * @since 3.2.1
+	 * The following selection will all return the link element.   
+	 *	 <pre>
+	 *  <a href="#">li^nk</a>
+	 *  <a href="#">[link]</a>
+	 *  text[<a href="#">link]</a>
+	 *  <a href="#">li[nk</a>]
+	 *  [<b><a href="#">li]nk</a></b>]
+	 *  [<a href="#"><b>li]nk</b></a>
+	 * </pre>
+	 */
+	getSelectedLink : function( editor )
+	{
+		var range;
+		try { range  = editor.getSelection().getRanges()[ 0 ]; }
+		catch( e ) { return null; }
+
+		range.shrink( CKEDITOR.SHRINK_TEXT );
+		var root = range.getCommonAncestor();
+		return root.getAscendant( 'a', true );
+	}
+};
+
 CKEDITOR.unlinkCommand = function(){};
 CKEDITOR.unlinkCommand.prototype =
 {
