Index: _tests/dt/core/dom/range.html
===================================================================
--- _tests/dt/core/dom/range.html	(revision 4118)
+++ _tests/dt/core/dom/range.html	Thu Sep 03 13:04:44 CST 2009
@@ -1062,7 +1062,7 @@
 		 */
 		test_enlarge_block6 : function()
 		{
-			var range = getRange( 'S11', null );
+			var range = getRange( 'S11' );
 			range.enlarge( CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS );
 
 			assert.areSame( document.getElementById( '_EnlargeP17' ),
@@ -1070,6 +1070,19 @@
 			assert.areSame( 0, range.startOffset, 'range.startOffset' );
 		},
 
+		/**
+		 * Test enlarge block when there's no nodes to enlarge.
+		 */
+		test_enlarge_block7 : function()
+		{
+			var range = getRange( 'S12' );
+			range.enlarge( CKEDITOR.ENLARGE_BLOCK_CONTENTS );
+			var frag = range.extractContents(),
+				tmp = new CKEDITOR.dom.element( 'div' );
+			tmp.append( frag );
+			assert.areSame( '<span>test block</span>', CKEDITOR.test.getInnerHtml( tmp ) );
+		},
+
 		test_deleteContents_W3C_1 : function()
 		{
 			// W3C DOM Range Specs - Section 2.6 - Example 1
@@ -2385,7 +2398,7 @@
 		<p id="_EnlargeP4">Test <i id="_EnlargeI4"> Enlarge</i></p>
 		<p id="_EnlargeP5">Test <i id="_EnlargeI5">Enlarge</i></p>
 		<p id="_EnlargeP6">Test <i id="_EnlargeI6"><b></b>Enlarge</i></p>
-		<p id="_EnlargeP7">Test <span id="S1"></span>List<br/ >Item Enlarge</p>
+		<p id="_EnlargeP7">Test <span id="S1"></span>List<br />Item Enlarge</p>
 		<p id="_EnlargeP8">Test <span id="S2"></span>List<span id="E2"></span> <br /><br />Item Enlarge</p>
 		<p id="_EnlargeP9">Test List <br /><span id="S3"></span><br />Item Enlarge</p>
 		<p id="_EnlargeP10">Test List <br /><br />Item<span id="S4"></span> Enlarge</p>
@@ -2393,9 +2406,10 @@
 		<div id="_EnlargeP12">Test<span id="S6"></span> Block <div>Enlarge</div></div>
 		<div>Test <div id="_EnlargeP13">Blo<span id="S7"></span>ck</div> Enlarge</div>
 		<p id="_EnlargeP14"><span id="S8"></span></p>
-		<p id="_EnlargeP15">Test <span id="S9"></span>List<br/ >Item Enlarge</p>
+		<p id="_EnlargeP15">Test <span id="S9"></span>List<br />Item Enlarge</p>
 		<p id="_EnlargeP16">Test <strong>Block<span id="S10"></span></strong><br /><br />Enlarge</p>
-		<p id="_EnlargeP17"><span><span id="S11"></span>Test Block Enlarge<span id="E11"></span></span></p>
+		<p id="_EnlargeP17"><span><span id="S11"></span>Test Block Enlarge</span></p>
+		<div id="_EnlargeP18"><span>Test Block</span><span id="S12"></span><p> Enlarge</p></div>
 		<p id="_trim_ct">Test trim</p>
 	</div>
 	<script type="text/javascript">
Index: _source/plugins/templates/dialogs/templates.js
===================================================================
--- _source/plugins/templates/dialogs/templates.js	(revision 3741)
+++ _source/plugins/templates/dialogs/templates.js	Thu Sep 03 14:28:40 CST 2009
@@ -81,15 +81,25 @@
 
 		if( isInsert )
 		{
+			// Everything should happen after the document is loaded (#4073).
+			editor.on( 'contentDom', function( evt )
+			{
+				evt.removeListener();
+				dialog.hide();
+
+				// Place the cursor at the first editable place.
+				var range = new CKEDITOR.dom.range( editor.document );
+				range.moveToElementEditStart( editor.document.getBody() );
+				range.select( true );
+			} );
 			editor.setData( html );
 		}
 		else
 		{
 			editor.insertHtml( html );
-		}
-
-		dialog.hide();
-	}
+			dialog.hide();
+		}
+	}
 
 	CKEDITOR.dialog.add( 'templates', function( editor )
 		{
Index: _source/plugins/dialog/plugin.js
===================================================================
--- _source/plugins/dialog/plugin.js	(revision 4165)
+++ _source/plugins/dialog/plugin.js	Thu Sep 03 14:26:02 CST 2009
@@ -576,8 +576,12 @@
 		 */
 		show : function()
 		{
-			if ( this._.editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
-				this._.editor.getSelection().lock();
+			var editor = this._.editor;
+			if ( editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
+			{
+				var selection = editor.getSelection();
+				selection && selection.lock();
+			}
 
 			// Insert the dialog's element to the root document.
 			var element = this._.element;
@@ -763,8 +767,11 @@
 				editor.focus();
 
 				if ( editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
-					editor.getSelection().unlock( true );
+				{
+					var selection = editor.getSelection();
+					selection && selection.unlock( true );
-			}
+				}
+			}
 			else
 				CKEDITOR.dialog._.currentZIndex -= 10;
 
Index: _source/core/dom/range.js
===================================================================
--- _source/core/dom/range.js	(revision 4053)
+++ _source/core/dom/range.js	Thu Sep 03 13:29:28 CST 2009
@@ -1173,17 +1173,19 @@
 
 					walker.guard = boundaryGuard;
 
-					enlargeable = walker.lastBackward();
+					// Take the range end container if there's no
+					// nodes to enlarge( #4073 ).
+					enlargeable = walker.lastBackward() || walkerRange.endContainer;
 
 					// It's the body which stop the enlarging if no block boundary found.
 					blockBoundary = blockBoundary || body;
 
 					// Start the range at different position by comparing
 					// the document position of it with 'enlargeable' node.
-					this.setStartAt(
-							blockBoundary,
+					this.setStartAt( blockBoundary,
 							!blockBoundary.is( 'br' ) &&
-							( !enlargeable || blockBoundary.contains( enlargeable ) ) ?
+							( blockBoundary.contains( enlargeable )
+							  || blockBoundary.equals( enlargeable ) ) ?
 								CKEDITOR.POSITION_AFTER_START :
 								CKEDITOR.POSITION_AFTER_END );
 
@@ -1199,17 +1201,19 @@
 					blockBoundary = null;
 					// End the range right before the block boundary node.
 
-					enlargeable = walker.lastForward();
+					// Take the range start container if there's no
+					// nodes to enlarge( #4073 ).
+					enlargeable = walker.lastForward() || walkerRange.startContainer;
 
 					// It's the body which stop the enlarging if no block boundary found.
 					blockBoundary = blockBoundary || body;
 
 					// Start the range at different position by comparing
 					// the document position of it with 'enlargeable' node.
-					this.setEndAt(
-							blockBoundary,
+					this.setEndAt( blockBoundary,
 							!blockBoundary.is( 'br' ) &&
-							( !enlargeable || blockBoundary.contains( enlargeable ) ) ?
+							( blockBoundary.contains( enlargeable )
+							  || blockBoundary.equals( enlargeable ) ) ?
 								CKEDITOR.POSITION_BEFORE_END :
 								CKEDITOR.POSITION_BEFORE_START );
 					// We must include the <br> at the end of range if there's
