Index: _whatsnew.html
===================================================================
--- _whatsnew.html	(revision 1772)
+++ _whatsnew.html	(working copy)
@@ -109,6 +109,9 @@
 			as expected.</li>
 		<li>[<a target="_blank" href="http://dev.fckeditor.net/ticket/1998">#1998</a>] The native
 			XMLHttpRequest class is now used in IE, whenever it is available.</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>
 		Version 2.6 Beta 1</h3>
Index: editor/_source/commandclasses/fcklistcommands.js
===================================================================
--- editor/_source/commandclasses/fcklistcommands.js	(revision 1772)
+++ editor/_source/commandclasses/fcklistcommands.js	(working copy)
@@ -145,7 +145,7 @@
 				if ( rangeQueue == null )
 				{
 					rangeQueue = [] ;
-					var selectionObject = FCK.EditorWindow.getSelection() ;
+					var selectionObject = FCKSelection.GetSelection() ;
 					if ( selectionObject && listGroups.length == 0 )
 						rangeQueue.push( selectionObject.getRangeAt( 0 ) ) ;
 					for ( var i = 1 ; selectionObject && i < selectionObject.rangeCount ; i++ )
Index: editor/_source/internals/fck.js
===================================================================
--- editor/_source/internals/fck.js	(revision 1772)
+++ editor/_source/internals/fck.js	(working copy)
@@ -660,6 +660,8 @@
 
 		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.
 		var range = new FCKDomRange( this.EditorWindow ) ;
@@ -863,7 +865,7 @@
 	{
 		// 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 )
 		{
 			var range = sel.getRangeAt( 0 ) ;
Index: editor/_source/internals/fck_gecko.js
===================================================================
--- editor/_source/internals/fck_gecko.js	(revision 1772)
+++ editor/_source/internals/fck_gecko.js	(working copy)
@@ -88,7 +88,7 @@
 
 		var moveCursor = function()
 		{
-			var selection = FCK.EditorWindow.getSelection() ;
+			var selection = FCKSelection.GetSelection() ;
 			var range = selection.getRangeAt(0) ;
 			if ( ! range || ! range.collapsed )
 				return ;
@@ -448,7 +448,7 @@
 FCK._ExecCheckEmptyBlock = function()
 {
 	FCK._FillEmptyBlock( FCK.EditorDocument.body.firstChild ) ;
-	var sel = FCK.EditorWindow.getSelection() ;
+	var sel = FCKSelection.GetSelection() ;
 	if ( !sel || sel.rangeCount < 1 )
 		return ;
 	var range = sel.getRangeAt( 0 );
Index: editor/_source/internals/fck_ie.js
===================================================================
--- editor/_source/internals/fck_ie.js	(revision 1772)
+++ editor/_source/internals/fck_ie.js	(working copy)
@@ -93,7 +93,7 @@
 function Doc_OnSelectionChange()
 {
 	// Don't fire the event if no document is loaded.
-	if ( FCK.EditorDocument )
+	if ( !FCK.IsSelectionChangeLocked && FCK.EditorDocument )
 		FCK.Events.FireEvent( "OnSelectionChange" ) ;
 }
 
@@ -155,7 +155,7 @@
 	FCKUndo.SaveUndoStep() ;
 
 	// Gets the actual selection.
-	var oSel = FCK.EditorDocument.selection ;
+	var oSel = FCKSelection.GetSelection() ;
 
 	// Deletes the actual selection contents.
 	if ( oSel.type.toLowerCase() == 'control' )
Index: editor/_source/internals/fckdialog.js
===================================================================
--- editor/_source/internals/fckdialog.js	(revision 1772)
+++ editor/_source/internals/fckdialog.js	(working copy)
@@ -76,8 +76,6 @@
 	}
 
 	return {
-		SelectionData : null,
-
 		/**
 		 * Opens a dialog window using the standard dialog template.
 		 */
@@ -93,43 +91,11 @@
 				Page : dialogPage,
 				Editor : window,
 				CustomValue : customValue,		// Optional
-				SelectionData : null,
 				TopWindow : topWindow
 			}
 
-			var currentInstance = FCK.ToolbarSet.CurrentInstance ;
+			FCK.ToolbarSet.CurrentInstance.Selection.Save() ;
 
-			// 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 ;
-			}
-
 			// Calculate the dialog position, centering it on the screen.
 			var viewSize = FCKTools.GetViewPaneSize( topWindow ) ;
 			var scrollPosition = FCKTools.GetScrollPosition( topWindow ) ;
@@ -183,7 +149,7 @@
 			else							// First Dialog.
 			{
 				// 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
 				// causes memory leak on IE7 (weird).
@@ -193,7 +159,9 @@
 				this.HideMainCover() ;
 				// Bug #1918: Assigning topDialog = null directly causes IE6 to crash.
 				setTimeout( function(){ topDialog = null ; }, 0 ) ;
-				this.SelectionData = null ;
+
+				// Release the previously saved selection.
+				FCKSelection.Release() ;
 			}
 		},
 
Index: editor/_source/internals/fckselection_gecko.js
===================================================================
--- editor/_source/internals/fckselection_gecko.js	(revision 1772)
+++ editor/_source/internals/fckselection_gecko.js	(working copy)
@@ -30,7 +30,7 @@
 	// Check if the actual selection is a Control (IMG, TABLE, HR, etc...).
 
 	var sel ;
-	try { sel = FCK.EditorWindow.getSelection() ; } catch (e) {}
+	try { sel = this.GetSelection() ; } catch (e) {}
 
 	if ( sel && sel.rangeCount == 1 )
 	{
@@ -51,7 +51,7 @@
 // element (object like and image or a table) is selected.
 FCKSelection.GetSelectedElement = function()
 {
-	var selection = !!FCK.EditorWindow && FCK.EditorWindow.getSelection() ;
+	var selection = !!FCK.EditorWindow && this.GetSelection() ;
 	if ( !selection || selection.rangeCount < 1 )
 		return null ;
 
@@ -72,7 +72,7 @@
 		return FCKSelection.GetSelectedElement().parentNode ;
 	else
 	{
-		var oSel = FCK.EditorWindow.getSelection() ;
+		var oSel = this.GetSelection() ;
 		if ( oSel )
 		{
 			// make the common case fast - for collapsed/nearly collapsed selections just return anchor.parent.
@@ -117,7 +117,7 @@
 		return FCKSelection.GetSelectedElement().parentNode ;
 	else
 	{
-		var oSel = FCK.EditorWindow.getSelection() ;
+		var oSel = this.GetSelection() ;
 		if ( oSel && oSel.rangeCount > 0 )
 		{
 			var range = oSel.getRangeAt( startBoundary ? 0 : ( oSel.rangeCount - 1 ) ) ;
@@ -135,14 +135,14 @@
 	var oRange = FCK.EditorDocument.createRange() ;
 	oRange.selectNode( element ) ;
 
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 	oSel.removeAllRanges() ;
 	oSel.addRange( oRange ) ;
 }
 
 FCKSelection.Collapse = function( toStart )
 {
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 
 	if ( toStart == null || toStart === true )
 		oSel.collapseToStart() ;
@@ -156,7 +156,7 @@
 	var oContainer = this.GetSelectedElement() ;
 	if ( ! oContainer && FCK.EditorWindow )
 	{
-		try		{ oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ; }
+		try		{ oContainer = this.GetSelection().getRangeAt(0).startContainer ; }
 		catch(e){}
 	}
 
@@ -176,7 +176,7 @@
 
 	var oContainer = this.GetSelectedElement() ;
 	if ( ! oContainer )
-		oContainer = FCK.EditorWindow.getSelection().getRangeAt(0).startContainer ;
+		oContainer = this.GetSelection().getRangeAt(0).startContainer ;
 
 	while ( oContainer )
 	{
@@ -191,7 +191,7 @@
 FCKSelection.Delete = function()
 {
 	// Gets the actual selection.
-	var oSel = FCK.EditorWindow.getSelection() ;
+	var oSel = this.GetSelection() ;
 
 	// Deletes the actual selection contents.
 	for ( var i = 0 ; i < oSel.rangeCount ; i++ )
@@ -201,3 +201,20 @@
 
 	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: editor/_source/internals/fckselection_ie.js
===================================================================
--- editor/_source/internals/fckselection_ie.js	(revision 1772)
+++ editor/_source/internals/fckselection_ie.js	(working copy)
@@ -28,11 +28,11 @@
 	// So we'd better check the object returned by createRange() rather than by looking at the type.
 	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' ;
 	}
 	catch(e)
@@ -49,10 +49,10 @@
 {
 	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 ;
 } ;
@@ -69,7 +69,7 @@
 			return null ;
 
 		default :
-			return FCK.EditorDocument.selection.createRange().parentElement() ;
+			return this.GetSelection().createRange().parentElement() ;
 	}
 } ;
 
@@ -101,7 +101,7 @@
 FCKSelection.SelectNode = function( node )
 {
 	FCK.Focus() ;
-	FCK.EditorDocument.selection.empty() ;
+	this.GetSelection().empty() ;
 	var oRange ;
 	try
 	{
@@ -124,7 +124,7 @@
 	FCK.Focus() ;
 	if ( this.GetType() == 'Text' )
 	{
-		var oRange = FCK.EditorDocument.selection.createRange() ;
+		var oRange = this.GetSelection().createRange() ;
 		oRange.collapse( toStart == null || toStart === true ) ;
 		oRange.select() ;
 	}
@@ -135,13 +135,13 @@
 {
 	var oContainer ;
 
-	if ( FCK.EditorDocument.selection.type == "Control" )
+	if ( this.GetSelection().type == "Control" )
 	{
 		oContainer = this.GetSelectedElement() ;
 	}
 	else
 	{
-		var oRange  = FCK.EditorDocument.selection.createRange() ;
+		var oRange  = this.GetSelection().createRange() ;
 		oContainer = oRange.parentElement() ;
 	}
 
@@ -162,9 +162,9 @@
 	if ( ! FCK.EditorDocument )
 		return null ;
 
-	if ( FCK.EditorDocument.selection.type == "Control" )
+	if ( this.GetSelection().type == "Control" )
 	{
-		oRange = FCK.EditorDocument.selection.createRange() ;
+		oRange = this.GetSelection().createRange() ;
 		for ( i = 0 ; i < oRange.length ; i++ )
 		{
 			if (oRange(i).parentNode)
@@ -176,7 +176,7 @@
 	}
 	else
 	{
-		oRange  = FCK.EditorDocument.selection.createRange() ;
+		oRange  = this.GetSelection().createRange() ;
 		oNode = oRange.parentElement() ;
 	}
 
@@ -189,7 +189,7 @@
 FCKSelection.Delete = function()
 {
 	// Gets the actual selection.
-	var oSel = FCK.EditorDocument.selection ;
+	var oSel = this.GetSelection() ;
 
 	// Deletes the actual selection contents.
 	if ( oSel.type.toLowerCase() != "none" )
@@ -199,3 +199,59 @@
 
 	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 ;
+	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: editor/_source/internals/fcktablehandler_gecko.js
===================================================================
--- editor/_source/internals/fcktablehandler_gecko.js	(revision 1772)
+++ editor/_source/internals/fcktablehandler_gecko.js	(working copy)
@@ -25,7 +25,7 @@
 {
 	var aCells = new Array() ;
 
-	var oSelection = FCK.EditorWindow.getSelection() ;
+	var oSelection = FCKSelection.GetSelection() ;
 
 	// If the selection is a text.
 	if ( oSelection.rangeCount == 1 && oSelection.anchorNode.nodeType == 3 )
Index: editor/_source/internals/fcktablehandler_ie.js
===================================================================
--- editor/_source/internals/fcktablehandler_ie.js	(revision 1772)
+++ editor/_source/internals/fcktablehandler_ie.js	(working copy)
@@ -31,7 +31,7 @@
 
 	var aCells = new Array() ;
 
-	var oRange = FCK.EditorDocument.selection.createRange() ;
+	var oRange = FCKSelection.GetSelection().createRange() ;
 //	var oParent = oRange.parentElement() ;
 	var oParent = FCKSelection.GetParentElement() ;
 
Index: editor/_source/internals/fckundo.js
===================================================================
--- editor/_source/internals/fckundo.js	(revision 1772)
+++ editor/_source/internals/fckundo.js	(working copy)
@@ -31,6 +31,8 @@
 
 FCKUndo._GetBookmark = function()
 {
+	FCKSelection.Restore() ;
+
 	var range = new FCKDomRange( FCK.EditorWindow ) ;
 	try
 	{
Index: editor/dialog/common/fck_dialog_common.js
===================================================================
--- editor/dialog/common/fck_dialog_common.js	(revision 1772)
+++ editor/dialog/common/fck_dialog_common.js	(working copy)
@@ -280,22 +280,23 @@
 				oldNode.parentNode.removeChild( oldNode ) ;
 				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 ;
 			}
 		}
 		else
Index: editor/dialog/fck_image/fck_image.js
===================================================================
--- editor/dialog/fck_image/fck_image.js	(revision 1772)
+++ editor/dialog/fck_image/fck_image.js	(working copy)
@@ -250,8 +250,6 @@
 			oLink.href = sLnkUrl ;
 		else			// Creating a new link.
 		{
-			dialog.Selection.EnsureSelection() ;
-
 			if ( !bHasImage )
 				oEditor.FCKSelection.SelectNode( oImage ) ;
 
Index: editor/dialog/fck_link/fck_link.js
===================================================================
--- editor/dialog/fck_link/fck_link.js	(revision 1772)
+++ editor/dialog/fck_link/fck_link.js	(working copy)
@@ -638,7 +638,6 @@
 	}
 
 	// Select the (first) link.
-	dialog.Selection.EnsureSelection() ;
 	oEditor.FCKSelection.SelectNode( aLinks[0] );
 
 	return true ;
Index: editor/dialog/fck_smiley.html
===================================================================
--- editor/dialog/fck_smiley.html	(revision 1772)
+++ editor/dialog/fck_smiley.html	(working copy)
@@ -49,8 +49,6 @@
 
 function InsertSmiley( url )
 {
-	dialog.Selection.EnsureSelection() ;
-
 	oEditor.FCKUndo.SaveUndoStep() ;
 
 	var oImg = oEditor.FCK.InsertElement( 'img' ) ;
Index: editor/dialog/fck_specialchar.html
===================================================================
--- editor/dialog/fck_specialchar.html	(revision 1772)
+++ editor/dialog/fck_specialchar.html	(working copy)
@@ -43,7 +43,6 @@
 function insertChar(charValue)
 {
 	oEditor.FCKUndo.SaveUndoStep() ;
-	parent.Selection.EnsureSelection() ;
 	oEditor.FCK.InsertHtml( charValue || "" ) ;
 	window.parent.Cancel() ;
 }
Index: editor/dialog/fck_tablecell.html
===================================================================
--- editor/dialog/fck_tablecell.html	(revision 1772)
+++ editor/dialog/fck_tablecell.html	(working copy)
@@ -36,7 +36,6 @@
 var oDOM = oEditor.FCK.EditorDocument ;
 
 // Array of selected Cells
-dialog.Selection.EnsureSelection() ;
 var aCells = oEditor.FCKTableHandler.GetSelectedCells() ;
 
 window.onload = function()
Index: editor/fckdialog.html
===================================================================
--- editor/fckdialog.html	(revision 1772)
+++ editor/fckdialog.html	(working copy)
@@ -375,44 +375,35 @@
 }() ;
 
 // Selection related functions.
-var Selection = function()
+//(Became simple shortcuts after the fix for #1990)
+var Selection =
 {
-	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 ) {}
-			}
-		},
+	/**
+	 * 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()
-		{
-			this.EnsureSelection() ;
-			return FCK.Selection ;
-		},
+	/**
+	 * 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 this.GetSelection().GetSelectedElement() ;
-		}
-	} ;
-}() ;
+	/**
+	 * Get the selected element in the editing area (for object selections).
+	 */
+	GetSelectedElement : function()
+	{
+		return FCK.Selection.GetSelectedElement() ;
+	}
+}
 
 // Tab related functions.
 var Tabs = function()
