Index: CKEditor/trunk/CHANGES.html
===================================================================
--- CKEditor/trunk/CHANGES.html	(revision 5262)
+++ CKEditor/trunk/CHANGES.html	(revision 5263)
@@ -82,4 +82,5 @@
 		<li><a href="http://dev.fckeditor.net/ticket/5238">#5238</a> : Menu button doesn't display arrow icon in high-contrast mode.</li>
 		<li><a href="http://dev.fckeditor.net/ticket/3576">#3576</a> : Non-attributed element of the same name with the applied style is correctly removed.</li>
+		<li><a href="http://dev.fckeditor.net/ticket/5221">#5221</a> : Insert table into empty document cause JavaScript error thrown.</li>
 	</ul>
 	<h3>
Index: CKEditor/trunk/_source/plugins/forms/dialogs/hiddenfield.js
===================================================================
--- CKEditor/trunk/_source/plugins/forms/dialogs/hiddenfield.js	(revision 5262)
+++ CKEditor/trunk/_source/plugins/forms/dialogs/hiddenfield.js	(revision 5263)
@@ -1,3 +1,3 @@
-﻿/*
+/*
 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
 For licensing, see LICENSE.html or http://ckeditor.com/license
@@ -7,4 +7,5 @@
 	return {
 		title : editor.lang.hidden.title,
+		hiddenField : null,
 		minWidth : 350,
 		minHeight : 110,
@@ -13,27 +14,38 @@
 			delete this.hiddenField;
 
-			var element = this.getParentEditor().getSelection().getSelectedElement();
-			if ( element && element.getName() == "input" && element.getAttribute( 'type' ) == "checkbox" )
-			{
-				this.hiddenField = element;
+			var editor = this.getParentEditor(), 
+			selection = editor.getSelection(), 
+			element = selection.getSelectedElement(); 
+	                         
+	        if ( element && element.getAttribute( '_cke_real_element_type' ) && element.getAttribute( '_cke_real_element_type' ) == 'hiddenfield' ) 
+            { 
+				this.hiddenField = element; 
+				element = editor.restoreRealElement( this.hiddenField ); 
 				this.setupContent( element );
-			}
+				selection.selectElement( this.hiddenField ); 
+	        } 
 		},
 		onOk : function()
 		{
-			var editor,
-				element = this.hiddenField,
-				isInsertMode = !element;
+			var name = this.getValueOf( 'info', '_cke_saved_name' ), 
+				value = this.getValueOf( 'info', 'value' ), 
+				editor = this.getParentEditor(), 
+				element = CKEDITOR.env.ie ?
+					editor.document.createElement( '<input name="' + CKEDITOR.tools.htmlEncode( name ) + '">' ) :
+						editor.document.createElement( 'input' );
 
-			if ( isInsertMode )
-			{
-				editor = this.getParentEditor();
-				element = editor.document.createElement( 'input' );
-				element.setAttribute( 'type', 'hidden' );
-			}
-
-			if ( isInsertMode )
-				editor.insertElement( element );
+			element.setAttribute( 'type', 'hidden' ); 
 			this.commitContent( element );
+			var fakeElement = editor.createFakeElement( element, 'cke_hidden', 'hiddenfield' ); 
+			
+			if ( !this.hiddenField ) 
+				editor.insertElement( fakeElement ); 
+			else 
+			{ 
+				fakeElement.replace( this.hiddenField ); 
+				editor.getSelection().selectElement( fakeElement );
+            }
+			
+			return true; 
 		},
 		contents : [
@@ -51,4 +63,5 @@
 						setup : function( element )
 						{
+							
 							this.setValue(
 									element.getAttribute( '_cke_saved_name' ) ||
@@ -59,8 +72,7 @@
 						{
 							if ( this.getValue() )
-								element.setAttribute( '_cke_saved_name', this.getValue() );
+								element.setAttribute( 'name', this.getValue() );
 							else
 							{
-								element.removeAttribute( '_cke_saved_name' );
 								element.removeAttribute( 'name' );
 							}
Index: CKEditor/trunk/_source/plugins/forms/plugin.js
===================================================================
--- CKEditor/trunk/_source/plugins/forms/plugin.js	(revision 5262)
+++ CKEditor/trunk/_source/plugins/forms/plugin.js	(revision 5263)
@@ -19,5 +19,16 @@
 				'border: 1px dotted #FF0000;' +
 				'padding: 2px;' +
-			'}' );
+			'}\n' );
+			
+		editor.addCss(
+			'img.cke_hidden' +
+			'{' +
+				'background-image: url(' + CKEDITOR.getUrl( this.path + 'images/hiddenfield.gif' ) + ');' +
+				'background-position: center center;' +
+				'background-repeat: no-repeat;' +
+				'border: 1px solid #a9a9a9;' +
+				'width: 16px;' +
+				'height: 16px;' +
+			'}' );	
 
 		// All buttons use the same code to register. So, to avoid
@@ -166,4 +177,8 @@
 	afterInit : function( editor )
 	{
+		var dataProcessor = editor.dataProcessor, 
+				htmlFilter = dataProcessor && dataProcessor.htmlFilter, 
+				dataFilter = dataProcessor && dataProcessor.dataFilter;
+				
 		// Cleanup certain IE form elements default values.
 		if ( CKEDITOR.env.ie )
@@ -186,6 +201,23 @@
 			} );
 		}
+		
+		if ( dataFilter ) 
+		{ 
+			dataFilter.addRules( 
+			{ 
+				elements : 
+				{ 
+					input : function( element ) 
+					{ 
+						if ( element.attributes.type == 'hidden' ) { 
+								return editor.createFakeParserElement( element, 'cke_hidden', 'hiddenfield' ); 
+						} 
+					} 
+				} 
+			} ); 
+		} 
 	},
-	requires : [ 'image' ]
+	
+	requires : [ 'image', 'fakeobjects' ]
 } );
 
Index: CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js
===================================================================
--- CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js	(revision 5262)
+++ CKEditor/trunk/_source/plugins/wysiwygarea/plugin.js	(revision 5263)
@@ -1,3 +1,3 @@
-﻿/*
+/*
 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
 For licensing, see LICENSE.html or http://ckeditor.com/license
@@ -162,4 +162,13 @@
 	}
 
+	function restoreSelection( selection )
+	{
+		if ( selection.isLocked )
+		{
+			selection.unlock();
+			setTimeout( function() { selection.lock(); }, 0 );
+		}
+	}
+
 	/**
 	 *  Auto-fixing block-less content by wrapping paragraph (#3190), prevent
@@ -184,4 +193,6 @@
 		{
 			restoreDirty( editor );
+			CKEDITOR.env.ie && restoreSelection( selection );
+
 			var fixedBlock = range.fixBlock( true,
 					editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'  );
@@ -226,4 +237,6 @@
 		{
 			restoreDirty( editor );
+			CKEDITOR.env.ie && restoreSelection( selection );
+
 			if ( !CKEDITOR.env.ie )
 				body.appendBogus();
