<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[24774] trunk/wp-includes: Autosave:</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/24774">24774</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-07-23 03:05:21 +0000 (Tue, 23 Jul 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Autosave:
 * Remove editor.js as a dependency for autosave.js, as it was in 3.5.
 * Remove heartbeat.js as an implicit dependency.
 * Abstract out the serialization of title/content/excerpt for comparisons.

props azaozz.
fixes <a href="http://core.trac.wordpress.org/ticket/24756">#24756</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjsautosavejs">trunk/wp-includes/js/autosave.js</a></li>
<li><a href="#trunkwpincludesscriptloaderphp">trunk/wp-includes/script-loader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjsautosavejs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/autosave.js (24773 => 24774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/autosave.js 2013-07-22 22:58:51 UTC (rev 24773)
+++ trunk/wp-includes/js/autosave.js    2013-07-23 03:05:21 UTC (rev 24774)
</span><span class="lines">@@ -7,12 +7,12 @@
</span><span class="cx">                  if ( 'content' == editor.id ) {
</span><span class="cx">                          editor.onLoad.add( function() {
</span><span class="cx">                                  editor.save();
</span><del>-                                       autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '' );
</del><ins>+                                        autosaveLast = wp.autosave.getCompareString();
</ins><span class="cx">                           });
</span><span class="cx">                  }
</span><span class="cx">          });
</span><span class="cx">  } else {
</span><del>-               autosaveLast = ( $('#title').val() || '' ) + ( $('#content').val() || '' );
</del><ins>+                autosaveLast = wp.autosave.getCompareString();
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
</span><span class="lines">@@ -40,21 +40,23 @@
</span><span class="cx">  });
</span><span class="cx"> 
</span><span class="cx">  window.onbeforeunload = function(){
</span><del>-               var mce = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, title, content;
</del><ins>+                var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString;
</ins><span class="cx"> 
</span><del>-               if ( mce && !mce.isHidden() ) {
-                       if ( mce.isDirty() )
</del><ins>+                if ( editor && ! editor.isHidden() ) {
+                       if ( editor.isDirty() )
</ins><span class="cx">                           return autosaveL10n.saveAlert;
</span><span class="cx">          } else {
</span><span class="cx">                  if ( fullscreen && fullscreen.settings.visible ) {
</span><del>-                               title = $('#wp-fullscreen-title').val() || '';
-                               content = $("#wp_mce_fullscreen").val() || '';
</del><ins>+                                compareString = wp.autosave.getCompareString({
+                                       post_title: $('#wp-fullscreen-title').val() || '',
+                                       content: $('#wp_mce_fullscreen').val() || '',
+                                       excerpt: $('#excerpt').val() || ''
+                               });
</ins><span class="cx">                   } else {
</span><del>-                               title = $('#post #title').val() || '';
-                               content = $('#post #content').val() || '';
</del><ins>+                                compareString = wp.autosave.getCompareString();
</ins><span class="cx">                   }
</span><span class="cx"> 
</span><del>-                       if ( ( title || content ) && title + content != autosaveLast )
</del><ins>+                        if ( compareString != autosaveLast )
</ins><span class="cx">                           return autosaveL10n.saveAlert;
</span><span class="cx">          }
</span><span class="cx">  };
</span><span class="lines">@@ -246,7 +248,7 @@
</span><span class="cx"> 
</span><span class="cx"> function autosave_enable_buttons() {
</span><span class="cx">  jQuery(document).trigger('autosave-enable-buttons');
</span><del>-       if ( ! wp.heartbeat.hasConnectionError() ) {
</del><ins>+        if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
</ins><span class="cx">           // delay that a bit to avoid some rare collisions while the DOM is being updated.
</span><span class="cx">          setTimeout(function(){
</span><span class="cx">                  var parent = jQuery('#submitpost');
</span><span class="lines">@@ -273,27 +275,28 @@
</span><span class="cx"> 
</span><span class="cx"> autosave = function() {
</span><span class="cx">  var post_data = wp.autosave.getPostData(),
</span><del>-               doAutoSave = post_data.autosave,
</del><ins>+                compareString,
</ins><span class="cx">           successCallback;
</span><span class="cx"> 
</span><span class="cx">  blockSave = true;
</span><span class="cx"> 
</span><ins>+       // post_data.content cannot be retrieved at the moment
+       if ( ! post_data.autosave )
+               return false;
+
</ins><span class="cx">   // No autosave while thickbox is open (media buttons)
</span><span class="cx">  if ( jQuery("#TB_window").css('display') == 'block' )
</span><del>-               doAutoSave = false;
</del><ins>+                return false;
</ins><span class="cx"> 
</span><ins>+       compareString = wp.autosave.getCompareString( post_data );
+
</ins><span class="cx">   // Nothing to save or no change.
</span><del>-       if ( ( post_data["post_title"].length == 0 && post_data["content"].length == 0 ) || post_data["post_title"] + post_data["content"] == autosaveLast ) {
-               doAutoSave = false;
-       }
-
-       if ( doAutoSave ) {
-               autosaveLast = post_data["post_title"] + post_data["content"];
-               jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
-       } else {
</del><ins>+        if ( compareString == autosaveLast )
</ins><span class="cx">           return false;
</span><del>-       }
</del><span class="cx"> 
</span><ins>+       autosaveLast = compareString;
+       jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
+
</ins><span class="cx">   // Disable buttons until we know the save completed.
</span><span class="cx">  autosave_disable_buttons();
</span><span class="cx"> 
</span><span class="lines">@@ -305,7 +308,7 @@
</span><span class="cx"> 
</span><span class="cx">  jQuery.ajax({
</span><span class="cx">          data: post_data,
</span><del>-               beforeSend: doAutoSave ? autosave_loading : null,
</del><ins>+                beforeSend: autosave_loading,
</ins><span class="cx">           type: "POST",
</span><span class="cx">          url: ajaxurl,
</span><span class="cx">          success: successCallback
</span><span class="lines">@@ -382,13 +385,21 @@
</span><span class="cx">          data['auto_draft'] = '1';
</span><span class="cx"> 
</span><span class="cx">  return data;
</span><del>-}
</del><ins>+};
</ins><span class="cx"> 
</span><ins>+// Concatenate title, content and excerpt. Used to track changes when auto-saving.
+wp.autosave.getCompareString = function( post_data ) {
+       if ( typeof post_data === 'object' ) {
+               return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' );
+       }
+
+       return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
+};
+
</ins><span class="cx"> wp.autosave.local = {
</span><span class="cx"> 
</span><del>-       lastsaveddata: '',
</del><ins>+        lastSavedData: '',
</ins><span class="cx">   blog_id: 0,
</span><del>-       ajaxurl: window.ajaxurl || 'wp-admin/admin-ajax.php',
</del><span class="cx">   hasStorage: false,
</span><span class="cx"> 
</span><span class="cx">  // Check if the browser supports sessionStorage and it's not disabled
</span><span class="lines">@@ -492,7 +503,7 @@
</span><span class="cx">   * @return bool
</span><span class="cx">   */
</span><span class="cx">  save: function( data ) {
</span><del>-               var result = false;
</del><ins>+                var result = false, post_data, compareString;
</ins><span class="cx"> 
</span><span class="cx">          if ( ! data ) {
</span><span class="cx">                  post_data = wp.autosave.getPostData();
</span><span class="lines">@@ -502,12 +513,14 @@
</span><span class="cx">                  post_data.autosave = true;
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               // If the content and title did not change since the last save, don't save again
-               if ( post_data.post_title + ': ' + post_data.content == this.lastsaveddata )
</del><ins>+                // Cannot get the post data at the moment
+               if ( ! post_data.autosave )
</ins><span class="cx">                   return false;
</span><span class="cx"> 
</span><del>-               // Cannot get the post data at the moment
-               if ( !post_data.autosave )
</del><ins>+                compareString = wp.autosave.getCompareString( post_data );
+
+               // If the content, title and excerpt did not change since the last save, don't save again
+               if ( compareString == this.lastSavedData )
</ins><span class="cx">                   return false;
</span><span class="cx"> 
</span><span class="cx">          post_data['save_time'] = (new Date()).getTime();
</span><span class="lines">@@ -515,7 +528,7 @@
</span><span class="cx">          result = this.setData( post_data );
</span><span class="cx"> 
</span><span class="cx">          if ( result )
</span><del>-                       this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
</del><ins>+                        this.lastSavedData = compareString;
</ins><span class="cx"> 
</span><span class="cx">          return result;
</span><span class="cx">  },
</span><span class="lines">@@ -524,8 +537,8 @@
</span><span class="cx">  init: function( settings ) {
</span><span class="cx">          var self = this;
</span><span class="cx"> 
</span><del>-               // Check if the browser supports sessionStorage and editor.js is loaded
-               if ( ! this.checkStorage() || typeof switchEditors == 'undefined' )
</del><ins>+                // Check if the browser supports sessionStorage and it's not disabled
+               if ( ! this.checkStorage() )
</ins><span class="cx">                   return;
</span><span class="cx"> 
</span><span class="cx">          // Don't run if the post type supports neither 'editor' (textarea#content) nor 'excerpt'.
</span><span class="lines">@@ -538,24 +551,16 @@
</span><span class="cx">          if ( !this.blog_id )
</span><span class="cx">                  this.blog_id = typeof window.autosaveL10n != 'undefined' ? window.autosaveL10n.blog_id : 0;
</span><span class="cx"> 
</span><del>-               this.checkPost();
</del><span class="cx">           $(document).ready( function(){ self.run(); } );
</span><span class="cx">  },
</span><span class="cx"> 
</span><span class="cx">  // Run on DOM ready
</span><span class="cx">  run: function() {
</span><del>-               var self = this, post_data;
</del><ins>+                var self = this;
</ins><span class="cx"> 
</span><del>-               // Set the comparison string
-               if ( !this.lastsaveddata ) {
-                       post_data = wp.autosave.getPostData();
</del><ins>+                // Check if the local post data is different than the loaded post data.
+               this.checkPost();
</ins><span class="cx"> 
</span><del>-                       if ( post_data.content && $('#wp-content-wrap').hasClass('tmce-active') )
-                               this.lastsaveddata = post_data.post_title + ': ' + switchEditors.pre_wpautop( post_data.content );
-                       else
-                               this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
-               }
-
</del><span class="cx">           // Set the schedule
</span><span class="cx">          this.schedule = $.schedule({
</span><span class="cx">                  time: 15 * 1000,
</span><span class="lines">@@ -605,7 +610,7 @@
</span><span class="cx">   * @return void
</span><span class="cx">   */
</span><span class="cx">  checkPost: function() {
</span><del>-               var self = this, post_data = this.getData(), content, check_data, strip_tags = false, notice,
</del><ins>+                var self = this, post_data = this.getData(), content, post_title, excerpt, notice,
</ins><span class="cx">                   post_id = $('#post_ID').val() || 0, cookie = wpCookies.get( 'wp-saving-post-' + post_id );
</span><span class="cx"> 
</span><span class="cx">          if ( ! post_data )
</span><span class="lines">@@ -625,20 +630,24 @@
</span><span class="cx">          if ( $('#has-newer-autosave').length )
</span><span class="cx">                  return;
</span><span class="cx"> 
</span><del>-               // cookie == 'check' means the post was not saved properly, always show #local-storage-notice
-               if ( cookie != 'check' ) {
-                       content = $('#content').val();
-                       check_data = $.extend( {}, post_data );
</del><ins>+                content = $('#content').val() || '';
+               post_title = $('#title').val() || '';
+               excerpt = $('#excerpt').val() || '';
</ins><span class="cx"> 
</span><del>-                       if ( $('#wp-content-wrap').hasClass('tmce-active') )
-                               content = switchEditors.pre_wpautop( content );
</del><ins>+                if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' )
+                       content = switchEditors.pre_wpautop( content );
</ins><span class="cx"> 
</span><del>-                       if ( this.compare( content, check_data.content ) && this.compare( $('#title').val(), check_data.post_title ) && this.compare( $('#excerpt').val(), check_data.excerpt ) )
-                               return;
</del><ins>+                // cookie == 'check' means the post was not saved properly, always show #local-storage-notice
+               if ( cookie != 'check' && this.compare( content, post_data.content ) && this.compare( post_title, post_data.post_title ) && this.compare( excerpt, post_data.excerpt ) ) {
+                       return;
</ins><span class="cx">           }
</span><span class="cx"> 
</span><span class="cx">          this.restore_post_data = post_data;
</span><del>-               this.undo_post_data = wp.autosave.getPostData();
</del><ins>+                this.undo_post_data = {
+                       content: content,
+                       post_title: post_title,
+                       excerpt: excerpt
+               };
</ins><span class="cx"> 
</span><span class="cx">          notice = $('#local-storage-notice');
</span><span class="cx">          $('.wrap h2').first().after( notice.addClass('updated').show() );
</span><span class="lines">@@ -666,7 +675,7 @@
</span><span class="cx"> 
</span><span class="cx">          if ( post_data ) {
</span><span class="cx">                  // Set the last saved data
</span><del>-                       this.lastsaveddata = post_data.post_title + ': ' + post_data.content;
</del><ins>+                        this.lastSavedData = wp.autosave.getCompareString( post_data );
</ins><span class="cx"> 
</span><span class="cx">                  if ( $('#title').val() != post_data.post_title )
</span><span class="cx">                          $('#title').focus().val( post_data.post_title || '' );
</span><span class="lines">@@ -674,7 +683,7 @@
</span><span class="cx">                  $('#excerpt').val( post_data.excerpt || '' );
</span><span class="cx">                  editor = typeof tinymce != 'undefined' && tinymce.get('content');
</span><span class="cx"> 
</span><del>-                       if ( editor && ! editor.isHidden() ) {
</del><ins>+                        if ( editor && ! editor.isHidden() && typeof switchEditors != 'undefined' ) {
</ins><span class="cx">                           // Make sure there's an undo level in the editor
</span><span class="cx">                          editor.undoManager.add();
</span><span class="cx">                          editor.setContent( post_data.content ? switchEditors.wpautop( post_data.content ) : '' );
</span><span class="lines">@@ -689,7 +698,7 @@
</span><span class="cx"> 
</span><span class="cx">          return false;
</span><span class="cx">  }
</span><del>-}
</del><ins>+};
</ins><span class="cx"> 
</span><span class="cx"> wp.autosave.local.init();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkwpincludesscriptloaderphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/script-loader.php (24773 => 24774)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/script-loader.php      2013-07-22 22:58:51 UTC (rev 24773)
+++ trunk/wp-includes/script-loader.php 2013-07-23 03:05:21 UTC (rev 24774)
</span><span class="lines">@@ -103,7 +103,7 @@
</span><span class="cx">          'dismiss' => __('Dismiss'),
</span><span class="cx">  ) );
</span><span class="cx"> 
</span><del>-       $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response', 'editor'), false, 1 );
</del><ins>+        $scripts->add( 'autosave', "/wp-includes/js/autosave$suffix.js", array('schedule', 'wp-ajax-response'), false, 1 );
</ins><span class="cx"> 
</span><span class="cx">  $scripts->add( 'heartbeat', "/wp-includes/js/heartbeat$suffix.js", array('jquery'), false, 1 );
</span><span class="cx">  did_action( 'init' ) && $scripts->localize( 'heartbeat', 'heartbeatSettings',
</span></span></pre>
</div>
</div>

</body>
</html>