Index: /FCKeditor/trunk/_whatsnew.html
===================================================================
--- /FCKeditor/trunk/_whatsnew.html	(revision 1794)
+++ /FCKeditor/trunk/_whatsnew.html	(revision 1795)
@@ -134,4 +134,7 @@
 			the caret will not any more move to the previous line when selecting a Format style
 			inside an empty paragraph.</li>
+		<li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/1990">#1990</a>] In IE,
+			dialogs using API calls which deals with the selection, like InsertHtml now can
+			be sure the selection will be placed in the correct position.</li>
 	</ul>
 	<h3>
Index: /FCKeditor/trunk/editor/_source/commandclasses/fcklistcommands.js
===================================================================
--- /FCKeditor/trunk/editor/_source/commandclasses/fcklistcommands.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/commandclasses/fcklistcommands.js	(revision 1795)
@@ -146,5 +146,5 @@
 				{
 					rangeQueue = [] ;
-					var selectionObject = FCK.EditorWindow.getSelection() ;
+					var selectionObject = FCKSelection.GetSelection() ;
 					if ( selectionObject && listGroups.length == 0 )
 						rangeQueue.push( selectionObject.getRangeAt( 0 ) ) ;
Index: /FCKeditor/trunk/editor/_source/internals/fck.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fck.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fck.js	(revision 1795)
@@ -661,4 +661,6 @@
 		var elementName = element.nodeName.toLowerCase() ;
 
+		FCKSelection.Restore() ;
+
 		// Create a range for the selection. V3 will have a new selection
 		// object that may internally supply this feature.
@@ -864,5 +866,5 @@
 		// Prevent the caret from going between the body and the padding node in Firefox.
 		// i.e. <body>|<p></p></body>
-		var sel = FCK.EditorWindow.getSelection() ;
+		var sel = FCKSelection.GetSelection() ;
 		if ( sel && sel.rangeCount == 1 )
 		{
Index: /FCKeditor/trunk/editor/_source/internals/fck_gecko.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fck_gecko.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fck_gecko.js	(revision 1795)
@@ -89,5 +89,5 @@
 		var moveCursor = function()
 		{
-			var selection = FCK.EditorWindow.getSelection() ;
+			var selection = FCKSelection.GetSelection() ;
 			var range = selection.getRangeAt(0) ;
 			if ( ! range || ! range.collapsed )
@@ -449,5 +449,5 @@
 {
 	FCK._FillEmptyBlock( FCK.EditorDocument.body.firstChild ) ;
-	var sel = FCK.EditorWindow.getSelection() ;
+	var sel = FCKSelection.GetSelection() ;
 	if ( !sel || sel.rangeCount < 1 )
 		return ;
Index: /FCKeditor/trunk/editor/_source/internals/fck_ie.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fck_ie.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fck_ie.js	(revision 1795)
@@ -94,5 +94,5 @@
 {
 	// Don't fire the event if no document is loaded.
-	if ( FCK.EditorDocument )
+	if ( !FCK.IsSelectionChangeLocked && FCK.EditorDocument )
 		FCK.Events.FireEvent( "OnSelectionChange" ) ;
 }
@@ -156,5 +156,5 @@
 
 	// Gets the actual selection.
-	var oSel = FCK.EditorDocument.selection ;
+	var oSel = FCKSelection.GetSelection() ;
 
 	// Deletes the actual selection contents.
Index: /FCKeditor/trunk/editor/_source/internals/fckdialog.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckdialog.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fckdialog.js	(revision 1795)
@@ -86,6 +86,4 @@
 
 	return {
-		SelectionData : null,
-
 		/**
 		 * Opens a dialog window using the standard dialog template.
@@ -103,40 +101,8 @@
 				Editor : window,
 				CustomValue : customValue,		// Optional
-				SelectionData : null,
 				TopWindow : topWindow
 			}
 
-			var currentInstance = FCK.ToolbarSet.CurrentInstance ;
-
-			// IE doens't support multiple selections, even if in different
-			// IFRAMEs, like the dialog, so the current selection must be saved to
-			// be restored in the dialog code.
-			if ( FCKBrowserInfo.IsIE && !topDialog )
-			{
-				// Ensures the editor has the selection focus. (#1801)
-				currentInstance.Focus() ;
-
-				var editorDocument = ( currentInstance.EditMode == FCK_EDITMODE_WYSIWYG ?
-						currentInstance.EditorDocument :
-						FCKTools.GetElementDocument( currentInstance.EditingArea.Textarea ) ) ;
-				var selection = editorDocument.selection ;
-				var range ;
-
-				if ( selection )
-				{
-					range = selection.createRange() ;
-
-					// Ensure that the range comes from the editor document.
-					if ( range )
-					{
-						if ( range.parentElement && FCKTools.GetElementDocument( range.parentElement() ) != editorDocument )
-							range = null ;
-						else if ( range.item && FCKTools.GetElementDocument( range.item(0) )!= editorDocument )
-							range = null ;
-					}
-				}
-
-				this.SelectionData = range ;
-			}
+			FCK.ToolbarSet.CurrentInstance.Selection.Save() ;
 
 			// Calculate the dialog position, centering it on the screen.
@@ -194,5 +160,5 @@
 			{
 				// Set the Focus in the browser, so the "OnBlur" event is not
-				// fired. In IE, there is no need to d othat because the dialog
+				// fired. In IE, there is no need to do that because the dialog
 				// already moved the selection to the editing area before
 				// closing (EnsureSelection). Also, the Focus() call here
@@ -204,5 +170,7 @@
 				// Bug #1918: Assigning topDialog = null directly causes IE6 to crash.
 				setTimeout( function(){ topDialog = null ; }, 0 ) ;
-				this.SelectionData = null ;
+
+				// Release the previously saved selection.
+				FCK.ToolbarSet.CurrentInstance.Selection.Release() ;
 			}
 		},
Index: /FCKeditor/trunk/editor/_source/internals/fckselection_gecko.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckselection_gecko.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fckselection_gecko.js	(revision 1795)
@@ -31,5 +31,5 @@
 
 	var sel ;
-	try { sel = FCK.EditorWindow.getSelection() ; } catch (e) {}
+	try { sel = this.GetSelection() ; } catch (e) {}
 
 	if ( sel && sel.rangeCount == 1 )
@@ -52,5 +52,5 @@
 FCKSelection.GetSelectedElement = function()
 {
-	var selection = !!FCK.EditorWindow && FCK.EditorWindow.getSelection() ;
+	var selection = !!FCK.EditorWindow && this.GetSelection() ;
 	if ( !selection || selection.rangeCount < 1 )
 		return null ;
@@ -73,5 +73,5 @@
 	else
 	{
-		var oSel = FCK.EditorWindow.getSelection() ;
+		var oSel = this.GetSelection() ;
 		if ( oSel )
 		{
@@ -118,5 +118,5 @@
 	else
 	{
-		var oSel = FCK.EditorWindow.getSelection() ;
+		var oSel = this.GetSelection() ;
 		if ( oSel && oSel.rangeCount > 0 )
 		{
@@ -136,5 +136,5 @@
 	oRange.selectNode( element ) ;
 
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 	oSel.removeAllRanges() ;
 	oSel.addRange( oRange ) ;
@@ -143,5 +143,5 @@
 FCKSelection.Collapse = function( toStart )
 {
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 
 	if ( toStart == null || toStart === true )
@@ -157,5 +157,5 @@
 	if ( ! oContainer && FCK.EditorWindow )
 	{
-		try		{ oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; }
+		try		{ oContainer = this.GetSelection().getRangeAt(0).startContainer ; }
 		catch(e){}
 	}
@@ -177,5 +177,5 @@
 	var oContainer = this.GetSelectedElement() ;
 	if ( ! oContainer )
-		oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ;
+		oContainer = this.GetSelection().getRangeAt(0).startContainer ;
 
 	while ( oContainer )
@@ -192,5 +192,5 @@
 {
 	// Gets the actual selection.
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 
 	// Deletes the actual selection contents.
@@ -202,2 +202,19 @@
 	return oSel ;
 }
+
+/**
+ * Returns the native selection object.
+ */
+FCKSelection.GetSelection = function()
+{
+	return FCK.EditorWindow.getSelection() ;
+}
+
+// The following are IE only features (we don't need then in other browsers
+// currently).
+FCKSelection.Save = function()
+{}
+FCKSelection.Restore = function()
+{}
+FCKSelection.Release = function()
+{}
Index: /FCKeditor/trunk/editor/_source/internals/fckselection_ie.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckselection_ie.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fckselection_ie.js	(revision 1795)
@@ -29,9 +29,9 @@
 	try
 	{
-		var ieType = FCK.EditorDocument.selection.type ;
+		var ieType = FCKSelection.GetSelection().type ;
 		if ( ieType == 'Control' || ieType == 'Text' )
 			return ieType ;
 
-		if ( FCK.EditorDocument.selection.createRange().parentElement )
+		if ( this.GetSelection().createRange().parentElement )
 			return 'Text' ;
 	}
@@ -50,8 +50,8 @@
 	if ( this.GetType() == 'Control' )
 	{
-		var oRange = FCK.EditorDocument.selection.createRange() ;
+		var oRange = this.GetSelection().createRange() ;
 
 		if ( oRange && oRange.item )
-			return FCK.EditorDocument.selection.createRange().item(0) ;
+			return this.GetSelection().createRange().item(0) ;
 	}
 	return null ;
@@ -70,5 +70,5 @@
 
 		default :
-			return FCK.EditorDocument.selection.createRange().parentElement() ;
+			return this.GetSelection().createRange().parentElement() ;
 	}
 } ;
@@ -102,5 +102,5 @@
 {
 	FCK.Focus() ;
-	FCK.EditorDocument.selection.empty() ;
+	this.GetSelection().empty() ;
 	var oRange ;
 	try
@@ -125,5 +125,5 @@
 	if ( this.GetType() == 'Text' )
 	{
-		var oRange = FCK.EditorDocument.selection.createRange() ;
+		var oRange = this.GetSelection().createRange() ;
 		oRange.collapse( toStart == null || toStart === true ) ;
 		oRange.select() ;
@@ -136,5 +136,5 @@
 	var oContainer ;
 
-	if ( FCK.EditorDocument.selection.type == "Control" )
+	if ( this.GetSelection().type == "Control" )
 	{
 		oContainer = this.GetSelectedElement() ;
@@ -142,5 +142,5 @@
 	else
 	{
-		var oRange  = FCK.EditorDocument.selection.createRange() ;
+		var oRange  = this.GetSelection().createRange() ;
 		oContainer = oRange.parentElement() ;
 	}
@@ -163,7 +163,7 @@
 		return null ;
 
-	if ( FCK.EditorDocument.selection.type == "Control" )
-	{
-		oRange = FCK.EditorDocument.selection.createRange() ;
+	if ( this.GetSelection().type == "Control" )
+	{
+		oRange = this.GetSelection().createRange() ;
 		for ( i = 0 ; i < oRange.length ; i++ )
 		{
@@ -177,5 +177,5 @@
 	else
 	{
-		oRange  = FCK.EditorDocument.selection.createRange() ;
+		oRange  = this.GetSelection().createRange() ;
 		oNode = oRange.parentElement() ;
 	}
@@ -190,5 +190,5 @@
 {
 	// Gets the actual selection.
-	var oSel = FCK.EditorDocument.selection ;
+	var oSel = this.GetSelection() ;
 
 	// Deletes the actual selection contents.
@@ -200,2 +200,62 @@
 	return oSel ;
 } ;
+
+/**
+ * Returns the native selection object.
+ */
+FCKSelection.GetSelection = function()
+{
+	this.Restore() ;
+	return FCK.EditorDocument.selection ;
+}
+
+FCKSelection.Save = function()
+{
+	// Ensures the editor has the selection focus. (#1801)
+	FCK.Focus() ;
+
+	var editorDocument = FCK.EditorDocument ;
+	
+	if ( !editorDocument )
+		return ;
+
+	var selection = editorDocument.selection ;
+	var range ;
+
+	if ( selection )
+	{
+		range = selection.createRange() ;
+
+		// Ensure that the range comes from the editor document.
+		if ( range )
+		{
+			if ( range.parentElement && FCKTools.GetElementDocument( range.parentElement() ) != editorDocument )
+				range = null ;
+			else if ( range.item && FCKTools.GetElementDocument( range.item(0) )!= editorDocument )
+				range = null ;
+		}
+	}
+
+	this.SelectionData = range ;
+}
+
+FCKSelection.Restore = function()
+{
+	if ( this.SelectionData )
+	{
+		FCK.IsSelectionChangeLocked = true ;
+
+		try 
+		{
+			this.SelectionData.select() ;
+		}
+		catch ( e ) {}
+
+		FCK.IsSelectionChangeLocked = false ;
+	}
+}
+
+FCKSelection.Release = function()
+{
+	delete this.SelectionData ;			
+}
Index: /FCKeditor/trunk/editor/_source/internals/fcktablehandler_gecko.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fcktablehandler_gecko.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fcktablehandler_gecko.js	(revision 1795)
@@ -26,5 +26,5 @@
 	var aCells = new Array() ;
 
-	var oSelection = FCK.EditorWindow.getSelection() ;
+	var oSelection = FCKSelection.GetSelection() ;
 
 	// If the selection is a text.
Index: /FCKeditor/trunk/editor/_source/internals/fcktablehandler_ie.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fcktablehandler_ie.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fcktablehandler_ie.js	(revision 1795)
@@ -32,5 +32,5 @@
 	var aCells = new Array() ;
 
-	var oRange = FCK.EditorDocument.selection.createRange() ;
+	var oRange = FCKSelection.GetSelection().createRange() ;
 //	var oParent = oRange.parentElement() ;
 	var oParent = FCKSelection.GetParentElement() ;
Index: /FCKeditor/trunk/editor/_source/internals/fckundo.js
===================================================================
--- /FCKeditor/trunk/editor/_source/internals/fckundo.js	(revision 1794)
+++ /FCKeditor/trunk/editor/_source/internals/fckundo.js	(revision 1795)
@@ -32,4 +32,6 @@
 FCKUndo._GetBookmark = function()
 {
+	FCKSelection.Restore() ;
+
 	var range = new FCKDomRange( FCK.EditorWindow ) ;
 	try
Index: /FCKeditor/trunk/editor/dialog/common/fck_dialog_common.js
===================================================================
--- /FCKeditor/trunk/editor/dialog/common/fck_dialog_common.js	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/common/fck_dialog_common.js	(revision 1795)
@@ -281,20 +281,21 @@
 				oldNode = null ;
 
-				if ( oEditor.FCKDialog.SelectionData )
+				if ( oEditor.FCK.Selection.SelectionData )
 				{
-					// Trick to refresh the selection object and avoid error in fckdialog.html Selection.EnsureSelection
+					// Trick to refresh the selection object and avoid error in
+					// fckdialog.html Selection.EnsureSelection
 					var oSel = oEditor.FCK.EditorDocument.selection ;
-					oEditor.FCKDialog.SelectionData = oSel.createRange() ; // Now oSel.type will be 'None' reflecting the real situation
+					oEditor.FCK.Selection.SelectionData = oSel.createRange() ; // Now oSel.type will be 'None' reflecting the real situation
 				}
 			}
 			oNewNode = oEditor.FCK.InsertElement( oNewNode ) ;
 
-			// FCKDialog.SelectionData is broken by now since we've deleted the previously selected element.
-			// So we need to reassign it.
-			if ( oEditor.FCKDialog.SelectionData )
+			// FCK.Selection.SelectionData is broken by now since we've
+			// deleted the previously selected element. So we need to reassign it.
+			if ( oEditor.FCK.Selection.SelectionData )
 			{
 				var range = oEditor.FCK.EditorDocument.body.createControlRange() ;
 				range.add( oNewNode ) ;
-				oEditor.FCKDialog.SelectionData = range ;
+				oEditor.FCK.Selection.SelectionData = range ;
 			}
 		}
Index: /FCKeditor/trunk/editor/dialog/fck_image/fck_image.js
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_image/fck_image.js	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/fck_image/fck_image.js	(revision 1795)
@@ -251,6 +251,4 @@
 		else			// Creating a new link.
 		{
-			dialog.Selection.EnsureSelection() ;
-
 			if ( !bHasImage )
 				oEditor.FCKSelection.SelectNode( oImage ) ;
Index: /FCKeditor/trunk/editor/dialog/fck_link/fck_link.js
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_link/fck_link.js	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/fck_link/fck_link.js	(revision 1795)
@@ -639,5 +639,4 @@
 
 	// Select the (first) link.
-	dialog.Selection.EnsureSelection() ;
 	oEditor.FCKSelection.SelectNode( aLinks[0] );
 
Index: /FCKeditor/trunk/editor/dialog/fck_smiley.html
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_smiley.html	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/fck_smiley.html	(revision 1795)
@@ -50,6 +50,4 @@
 function InsertSmiley( url )
 {
-	dialog.Selection.EnsureSelection() ;
-
 	oEditor.FCKUndo.SaveUndoStep() ;
 
Index: /FCKeditor/trunk/editor/dialog/fck_specialchar.html
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_specialchar.html	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/fck_specialchar.html	(revision 1795)
@@ -44,5 +44,4 @@
 {
 	oEditor.FCKUndo.SaveUndoStep() ;
-	parent.Selection.EnsureSelection() ;
 	oEditor.FCK.InsertHtml( charValue || "" ) ;
 	window.parent.Cancel() ;
Index: /FCKeditor/trunk/editor/dialog/fck_tablecell.html
===================================================================
--- /FCKeditor/trunk/editor/dialog/fck_tablecell.html	(revision 1794)
+++ /FCKeditor/trunk/editor/dialog/fck_tablecell.html	(revision 1795)
@@ -37,5 +37,4 @@
 
 // Array of selected Cells
-dialog.Selection.EnsureSelection() ;
 var aCells = oEditor.FCKTableHandler.GetSelectedCells() ;
 
Index: /FCKeditor/trunk/editor/fckdialog.html
===================================================================
--- /FCKeditor/trunk/editor/fckdialog.html	(revision 1794)
+++ /FCKeditor/trunk/editor/fckdialog.html	(revision 1795)
@@ -376,42 +376,33 @@
 
 // Selection related functions.
-var Selection = function()
-{
-	return {
-		/**
-		 * Ensures that the editing area contains an active selection. This is a
-		 * requirement for IE, as it looses the selection when the focus moves to other
-		 * frames.
-		 */
-		EnsureSelection : function()
-		{
-			if ( FCKDialog.SelectionData )
-			{
-				try
-				{
-					FCKDialog.SelectionData.select() ;
-				}
-				catch ( e ) {}
-			}
-		},
-
-		/**
-		 * Get the FCKSelection object for the editor instance.
-		 */
-		GetSelection : function()
-		{
-			this.EnsureSelection() ;
-			return FCK.Selection ;
-		},
-
-		/**
-		 * Get the selected element in the editing area (for object selections).
-		 */
-		GetSelectedElement : function()
-		{
-			return this.GetSelection().GetSelectedElement() ;
-		}
-	} ;
-}() ;
+//(Became simple shortcuts after the fix for #1990)
+var Selection =
+{
+	/**
+	 * Ensures that the editing area contains an active selection. This is a
+	 * requirement for IE, as it looses the selection when the focus moves to other
+	 * frames.
+	 */
+	EnsureSelection : function()
+	{
+		FCK.Selection.Restore() ;
+	},
+
+	/**
+	 * Get the FCKSelection object for the editor instance.
+	 */
+	GetSelection : function()
+	{
+		return FCK.Selection ;
+	},
+
+	/**
+	 * Get the selected element in the editing area (for object selections).
+	 */
+	GetSelectedElement : function()
+	{
+		return FCK.Selection.GetSelectedElement() ;
+	}
+}
 
 // Tab related functions.
