Index: /CKEditor/branches/prototype/_samples/replacebyclass.html
===================================================================
--- /CKEditor/branches/prototype/_samples/replacebyclass.html	(revision 3005)
+++ /CKEditor/branches/prototype/_samples/replacebyclass.html	(revision 3006)
@@ -11,5 +11,5 @@
 <body>
 	<div id="html">
-		<form action="">
+		<form action="sample_posteddata.php" method="post">
 			<p>
 				<label for="editor1">
Index: /CKEditor/branches/prototype/_samples/replacebycode.html
===================================================================
--- /CKEditor/branches/prototype/_samples/replacebycode.html	(revision 3005)
+++ /CKEditor/branches/prototype/_samples/replacebycode.html	(revision 3006)
@@ -11,5 +11,5 @@
 <body>
 	<div id="html">
-		<form action="">
+		<form action="sample_posteddata.php" method="post">
 			<p>
 				<label for="editor1">
Index: /CKEditor/branches/prototype/_samples/sample.css
===================================================================
--- /CKEditor/branches/prototype/_samples/sample.css	(revision 3005)
+++ /CKEditor/branches/prototype/_samples/sample.css	(revision 3006)
@@ -48,2 +48,34 @@
 	float: right;
 }
+
+#outputSample
+{
+	width: 100%;
+	table-layout: fixed;
+}
+
+#outputSample thead th
+{
+	color: #dddddd;
+	background-color: #999999;
+	padding: 4px;
+	white-space: nowrap;
+}
+
+#outputSample tbody th
+{
+	vertical-align: top;
+	text-align: left;
+}
+
+#outputSample pre
+{
+	margin: 0;
+	padding: 0;
+	white-space: pre; /* CSS2 */
+	white-space: -moz-pre-wrap; /* Mozilla*/
+	white-space: -o-pre-wrap; /* Opera 7 */
+	white-space: pre-wrap; /* CSS 2.1 */
+	white-space: pre-line; /* CSS 3 (and 2.1 as well, actually) */
+	word-wrap: break-word; /* IE */
+}
Index: /CKEditor/branches/prototype/_samples/sample_posteddata.php
===================================================================
--- /CKEditor/branches/prototype/_samples/sample_posteddata.php	(revision 3006)
+++ /CKEditor/branches/prototype/_samples/sample_posteddata.php	(revision 3006)
@@ -0,0 +1,59 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<?php
+/*
+Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.
+For licensing, see LICENSE.html or http://ckeditor.com/license
+*/
+?>
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+	<title>Sample - CKEditor</title>
+	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
+	<link type="text/css" rel="stylesheet" href="sample.css" />
+</head>
+<body>
+	<h1>
+		CKEditor - Posted Data
+	</h1>
+	<table border="1" cellspacing="0" id="outputSample">
+		<colgroup><col width="100" /></colgroup>
+		<thead>
+			<tr>
+				<th>Field&nbsp;Name</th>
+				<th>Value</th>
+			</tr>
+		</thead>
+<?php
+
+if ( isset( $_POST ) )
+	$postArray = &$_POST ;			// 4.1.0 or later, use $_POST
+else
+	$postArray = &$HTTP_POST_VARS ;	// prior to 4.1.0, use HTTP_POST_VARS
+
+foreach ( $postArray as $sForm => $value )
+{
+	if ( get_magic_quotes_gpc() )
+		$postedValue = htmlspecialchars( stripslashes( $value ) ) ;
+	else
+		$postedValue = htmlspecialchars( $value ) ;
+
+?>
+		<tr>
+			<th style="vertical-align: top"><?php echo $sForm?></th>
+			<td><pre><?php echo $postedValue?></pre></td>
+		</tr>
+	<?php
+}
+?>
+	</table>
+	<div id="footer">
+		<hr />
+		<p>
+			CKEditor - The text editor for Internet - <a href="http://ckeditor.com/">http://ckeditor.com</a>
+		</p>
+		<p id="copy">
+			Copyright &copy; 2003-2009, <a href="http://cksource.com/">CKSource</a> - Frederico Knabben. All rights reserved.
+		</p>
+	</div>
+</body>
+</html>
Index: /CKEditor/branches/prototype/_source/core/config.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/config.js	(revision 3005)
+++ /CKEditor/branches/prototype/_source/core/config.js	(revision 3006)
@@ -38,4 +38,6 @@
 	 */
 	customConfig : CKEDITOR.getUrl( 'config.js' ),
+
+	autoUpdateElement : true,
 
 	/**
Index: /CKEditor/branches/prototype/_source/core/editor.js
===================================================================
--- /CKEditor/branches/prototype/_source/core/editor.js	(revision 3005)
+++ /CKEditor/branches/prototype/_source/core/editor.js	(revision 3006)
@@ -213,5 +213,39 @@
 				editorTheme.path = CKEDITOR.themes.getPath( theme );
 				editorTheme.build( editor );
+
+				if ( editor.config.autoUpdateElement )
+					attachToForm( editor );
 			});
+	};
+
+	var attachToForm = function( editor )
+	{
+		var element = editor.element;
+
+		// If are replacing a textarea, we must
+		if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE && element.is( 'textarea' ) )
+		{
+			var form = new CKEDITOR.dom.element( element.$.form );
+			if ( form )
+			{
+				form.on( 'submit', function()
+					{
+						editor.updateElement()
+					});
+
+				// If we have a submit function, override it also, because it doesn't fire the "submit" event.
+				if ( form.submit && form.submit.call )
+				{
+					CKEDITOR.tools.override( form.submit, function( originalSubmit )
+						{
+							return function()
+								{
+									editor.updateElement();
+									originalSubmit.apply( this, arguments );
+								};
+						});
+				}
+			}
+		}
 	};
 
@@ -241,5 +275,5 @@
 			 * alert( <b>editor.element</b>.getName() );  "textarea"
 			 */
-			this.element = element && element;
+			this.element = element;
 
 			/**
@@ -401,4 +435,18 @@
 		},
 
+		getSnapshot : function()
+		{
+			var data = this.fire( 'getSnapshot' );
+
+			if ( typeof data != 'string' )
+			{
+				var element = this.element;
+				if ( element && this.elementMode == CKEDITOR.ELEMENT_MODE_REPLACE )
+					data = element.is( 'textarea' ) ? element.getValue() : element.getHtml();
+			}
+
+			return data;
+		},
+
 		/**
 		 * Sets the editor data. The data must be provided in raw format.
@@ -441,4 +489,15 @@
 		{
 			this.fire( 'insertElement', element );
+		},
+
+		checkDirty : function()
+		{
+			return ( this.mayBeDirty && this._.previousValue !== this.getSnapshot() );
+		},
+
+		resetDirty : function()
+		{
+			if ( this.mayBeDirty )
+				this._.previousValue = this.getSnapshot();
 		},
 
Index: /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js	(revision 3005)
+++ /CKEditor/branches/prototype/_source/plugins/editingblock/plugin.js	(revision 3006)
@@ -65,4 +65,10 @@
 					}
 				});
+
+			editor.on( 'getSnapshot', function( event )
+				{
+					if ( editor.mode )
+						event.data = getMode( editor ).getSnapshotData();
+				});
 		}
 	});
@@ -100,5 +106,6 @@
 	{
 		var data,
-			holderElement = this.getThemeSpace( 'contents' );
+			holderElement = this.getThemeSpace( 'contents' ),
+			isDirty = this.checkDirty();
 
 		// Unload the previous mode.
@@ -121,8 +128,14 @@
 			throw '[CKEDITOR.editor.setMode] Unknown mode "' + mode + '".';
 
+		if ( !isDirty )
+		{
+			this.on( 'mode', function()
+				{
+					this.resetDirty();
+					this.removeListener( 'mode', arguments.callee );
+				});
+		}
+
 		modeEditor.load( holderElement, data || this.getData() );
-
-		this.mode = mode;
-		this.fire( 'mode' );
 	};
 
Index: /CKEditor/branches/prototype/_source/plugins/sourcearea/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/sourcearea/plugin.js	(revision 3005)
+++ /CKEditor/branches/prototype/_source/plugins/sourcearea/plugin.js	(revision 3006)
@@ -64,6 +64,12 @@
 							holderElement.append( textarea );
 
+							// The editor data "may be dirty" after this point.
+							editor.mayBeDirty = true;
+
 							// Set the <textarea> value.
 							this.loadData( data );
+
+							editor.mode = 'source';
+							editor.fire( 'mode' );
 						},
 
Index: /CKEditor/branches/prototype/_source/plugins/wysiwygarea/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/wysiwygarea/plugin.js	(revision 3005)
+++ /CKEditor/branches/prototype/_source/plugins/wysiwygarea/plugin.js	(revision 3006)
@@ -130,5 +130,6 @@
 						iframe,
 						isLoadingData,
-						isPendingFocus;
+						isPendingFocus,
+						fireMode;
 
 					// The following information is needed for IE only.
@@ -242,4 +243,11 @@
 						editor.fire( 'contentDom' );
 
+						if ( fireMode )
+						{
+							editor.mode = 'wysiwyg';
+							editor.fire( 'mode' );
+							fireMode = false;
+						}
+
 						isLoadingData = false;
 
@@ -258,4 +266,10 @@
 								if ( !isCustomDomain || !CKEDITOR.env.gecko )
 									createIFrame();
+
+								// The editor data "may be dirty" after this
+								// point.
+								editor.mayBeDirty = true;
+
+								fireMode = true;
 
 								if ( isSnapshot )
