<!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>[27909] trunk/src/wp-admin: Widget Customizer: Improve support for dynamically-created inputs.</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/27909">27909</a></dd>
<dt>Author</dt> <dd>ocean90</dd>
<dt>Date</dt> <dd>2014-04-02 18:20:00 +0000 (Wed, 02 Apr 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Widget Customizer: Improve support for dynamically-created inputs.

* Re-work how and when widget forms get updated.
* Replace ad hoc hooks system with jQuery events,
* Add `widget-updated`/`widget-synced` events for widget soft/hard updates.
* Enter into a non-live form update mode, where the Apply button is restored when a sanitized form does not have the same fields as currently in the form, and so the fields cannot be easily updated to their sanitized values without doing a complete form replacement. Also restores live update mode if sanitized fields are aligned with the existing fields again.

Note: jQuery events are *not* final yet, see <a href="http://core.trac.wordpress.org/ticket/19675">#19675</a>.

props westonruter.
see <a href="http://core.trac.wordpress.org/ticket/27491">#27491</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmincsscustomizewidgetscss">trunk/src/wp-admin/css/customize-widgets.css</a></li>
<li><a href="#trunksrcwpadminjscustomizewidgetsjs">trunk/src/wp-admin/js/customize-widgets.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadmincsscustomizewidgetscss"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/css/customize-widgets.css (27908 => 27909)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/css/customize-widgets.css     2014-04-02 17:13:09 UTC (rev 27908)
+++ trunk/src/wp-admin/css/customize-widgets.css        2014-04-02 18:20:00 UTC (rev 27909)
</span><span class="lines">@@ -37,6 +37,14 @@
</span><span class="cx"> .customize-control-widget_form.previewer-loading .spinner {
</span><span class="cx">  opacity: 1.0;
</span><span class="cx"> }
</span><ins>+.customize-control-widget_form.widget-form-disabled .widget-content {
+       opacity: 0.7;
+       pointer-events: none;
+       -moz-user-select: none;
+       -webkit-user-select: none;
+       -ms-user-select: none;
+       user-select: none;
+}
</ins><span class="cx"> 
</span><span class="cx"> .customize-control-widget_form .widget {
</span><span class="cx">  margin-bottom: 0;
</span></span></pre></div>
<a id="trunksrcwpadminjscustomizewidgetsjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/js/customize-widgets.js (27908 => 27909)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/js/customize-widgets.js       2014-04-02 17:13:09 UTC (rev 27908)
+++ trunk/src/wp-admin/js/customize-widgets.js  2014-04-02 18:20:00 UTC (rev 27909)
</span><span class="lines">@@ -8,6 +8,7 @@
</span><span class="cx">          Sidebar,
</span><span class="cx">          SidebarCollection,
</span><span class="cx">          OldPreviewer,
</span><ins>+               builtin_form_sync_handlers,
</ins><span class="cx">           customize = wp.customize, self = {
</span><span class="cx">          nonce: null,
</span><span class="cx">          i18n: {
</span><span class="lines">@@ -131,6 +132,32 @@
</span><span class="cx">  self.registered_sidebars = new SidebarCollection( self.registered_sidebars );
</span><span class="cx"> 
</span><span class="cx">  /**
</span><ins>+        * Handlers for the widget-synced event, organized by widget ID base.
+        * Other widgets may provide their own update handlers by adding
+        * listeners for the widget-synced event.
+        */
+       builtin_form_sync_handlers = {
+
+               /**
+                * @param {jQuery.Event} e
+                * @param {jQuery} widget_el
+                * @param {String} new_form
+                */
+               rss: function ( e, widget_el, new_form ) {
+                       var old_widget_error = widget_el.find( '.widget-error:first' ),
+                               new_widget_error = $( '<div>' + new_form + '</div>' ).find( '.widget-error:first' );
+
+                       if ( old_widget_error.length && new_widget_error.length ) {
+                               old_widget_error.replaceWith( new_widget_error );
+                       } else if ( old_widget_error.length ) {
+                               old_widget_error.remove();
+                       } else if ( new_widget_error.length ) {
+                               widget_el.find( '.widget-content:first' ).prepend( new_widget_error );
+                       }
+               }
+       };
+
+       /**
</ins><span class="cx">    * On DOM ready, initialize some meta functionality independent of specific
</span><span class="cx">   * customizer controls.
</span><span class="cx">   */
</span><span class="lines">@@ -454,6 +481,7 @@
</span><span class="cx">          addWidget: function ( widget_id ) {
</span><span class="cx">                  var control = this,
</span><span class="cx">                          control_html,
</span><ins>+                               widget_el,
</ins><span class="cx">                           customize_control_type = 'widget_form',
</span><span class="cx">                          customize_control,
</span><span class="cx">                          parsed_widget_id = parse_widget_id( widget_id ),
</span><span class="lines">@@ -488,11 +516,12 @@
</span><span class="cx">                  } else {
</span><span class="cx">                          widget.set( 'is_disabled', true ); // Prevent single widget from being added again now
</span><span class="cx">                  }
</span><ins>+                       widget_el = $( control_html );
</ins><span class="cx"> 
</span><span class="cx">                  customize_control = $( '<li></li>' );
</span><span class="cx">                  customize_control.addClass( 'customize-control' );
</span><span class="cx">                  customize_control.addClass( 'customize-control-' + customize_control_type );
</span><del>-                       customize_control.append( $( control_html ) );
</del><ins>+                        customize_control.append( widget_el );
</ins><span class="cx">                   customize_control.find( '> .widget-icon' ).remove();
</span><span class="cx">                  if ( widget.get( 'is_multi' ) ) {
</span><span class="cx">                          customize_control.find( 'input[name="widget_number"]' ).val( widget_number );
</span><span class="lines">@@ -578,6 +607,8 @@
</span><span class="cx">                          }
</span><span class="cx">                  } );
</span><span class="cx"> 
</span><ins>+                       $( document ).trigger( 'widget-added', [ widget_el ] );
+
</ins><span class="cx">                   return widget_form_control;
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -602,50 +633,9 @@
</span><span class="cx">                  control._setupHighlightEffects();
</span><span class="cx">                  control._setupUpdateUI();
</span><span class="cx">                  control._setupRemoveUI();
</span><del>-                       control.hook( 'init' );
</del><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          /**
</span><del>-                * Hooks for widgets to support living in the customizer control
-                */
-               hooks: {
-                       _default: {},
-                       rss: {
-                               formUpdated: function ( serialized_form ) {
-                                       var control = this,
-                                               old_widget_error = control.container.find( '.widget-error:first' ),
-                                               new_widget_error = serialized_form.find( '.widget-error:first' );
-
-                                       if ( old_widget_error.length && new_widget_error.length ) {
-                                               old_widget_error.replaceWith( new_widget_error );
-                                       } else if ( old_widget_error.length ) {
-                                               old_widget_error.remove();
-                                       } else if ( new_widget_error.length ) {
-                                               control.container.find( '.widget-content' ).prepend( new_widget_error );
-                                       }
-                               }
-                       }
-               },
-
-               /**
-                * Trigger an 'action' which a specific widget type can handle
-                *
-                * @param name
-                */
-               hook: function ( name ) {
-                       var args = Array.prototype.slice.call( arguments, 1 ), handler;
-
-                       if ( this.hooks[this.params.widget_id_base] && this.hooks[this.params.widget_id_base][name] ) {
-                               handler = this.hooks[this.params.widget_id_base][name];
-                       } else if ( this.hooks._default[name] ) {
-                               handler = this.hooks._default[name];
-                       }
-                       if ( handler ) {
-                               handler.apply( this, args );
-                       }
-               },
-
-               /**
</del><span class="cx">            * Handle changes to the setting
</span><span class="cx">           */
</span><span class="cx">          _setupModel: function () {
</span><span class="lines">@@ -660,6 +650,7 @@
</span><span class="cx"> 
</span><span class="cx">                  control._update_count = 0;
</span><span class="cx">                  control.is_widget_updating = false;
</span><ins>+                       control.live_update_mode = true;
</ins><span class="cx"> 
</span><span class="cx">                  // Update widget whenever model changes
</span><span class="cx">                  control.setting.bind( function( to, from ) {
</span><span class="lines">@@ -945,11 +936,14 @@
</span><span class="cx">           */
</span><span class="cx">          _setupUpdateUI: function () {
</span><span class="cx">                  var control = this,
</span><ins>+                               widget_root,
</ins><span class="cx">                           widget_content,
</span><span class="cx">                          save_btn,
</span><del>-                               update_widget_debounced;
</del><ins>+                                update_widget_debounced,
+                               form_update_event_handler;
</ins><span class="cx"> 
</span><del>-                       widget_content = control.container.find( '.widget-content' );
</del><ins>+                        widget_root = control.container.find( '.widget:first' );
+                       widget_content = widget_root.find( '.widget-content:first' );
</ins><span class="cx"> 
</span><span class="cx">                  // Configure update button
</span><span class="cx">                  save_btn = control.container.find( '.widget-control-save' );
</span><span class="lines">@@ -958,7 +952,7 @@
</span><span class="cx">                  save_btn.removeClass( 'button-primary' ).addClass( 'button-secondary' );
</span><span class="cx">                  save_btn.on( 'click', function ( e ) {
</span><span class="cx">                          e.preventDefault();
</span><del>-                               control.updateWidget();
</del><ins>+                                control.updateWidget( { disable_form: true } );
</ins><span class="cx">                   } );
</span><span class="cx"> 
</span><span class="cx">                  update_widget_debounced = _.debounce( function () {
</span><span class="lines">@@ -976,10 +970,12 @@
</span><span class="cx"> 
</span><span class="cx">                  // Handle widgets that support live previews
</span><span class="cx">                  widget_content.on( 'change input propertychange', ':input', function ( e ) {
</span><del>-                               if ( e.type === 'change' ) {
-                                       control.updateWidget();
-                               } else if ( this.checkValidity && this.checkValidity() ) {
-                                       update_widget_debounced();
</del><ins>+                                if ( control.live_update_mode ) {
+                                       if ( e.type === 'change' ) {
+                                               control.updateWidget();
+                                       } else if ( this.checkValidity && this.checkValidity() ) {
+                                               update_widget_debounced();
+                                       }
</ins><span class="cx">                           }
</span><span class="cx">                  } );
</span><span class="cx"> 
</span><span class="lines">@@ -998,6 +994,15 @@
</span><span class="cx">                          var is_rendered = !! rendered_widgets[control.params.widget_id];
</span><span class="cx">                          control.container.toggleClass( 'widget-rendered', is_rendered );
</span><span class="cx">                  } );
</span><ins>+
+                       form_update_event_handler = builtin_form_sync_handlers[ control.params.widget_id_base ];
+                       if ( form_update_event_handler ) {
+                               $( document ).on( 'widget-synced', function ( e, widget_el ) {
+                                       if ( widget_root.is( widget_el ) ) {
+                                               form_update_event_handler.apply( document, arguments );
+                                       }
+                               } );
+                       }
</ins><span class="cx">           },
</span><span class="cx"> 
</span><span class="cx">          /**
</span><span class="lines">@@ -1055,6 +1060,21 @@
</span><span class="cx">          },
</span><span class="cx"> 
</span><span class="cx">          /**
</span><ins>+                * Find all inputs in a widget container that should be considered when
+                * comparing the loaded form with the sanitized form, whose fields will
+                * be aligned to copy the sanitized over. The elements returned by this
+                * are passed into this._getInputsSignature(), and they are iterated
+                * over when copying sanitized values over to the the form loaded.
+                *
+                * @param {jQuery} container element in which to look for inputs
+                * @returns {jQuery} inputs
+                * @private
+                */
+               _getInputs: function ( container ) {
+                       return $( container ).find( ':input[name]' );
+               },
+
+               /**
</ins><span class="cx">            * Iterate over supplied inputs and create a signature string for all of them together.
</span><span class="cx">           * This string can be used to compare whether or not the form has all of the same fields.
</span><span class="cx">           *
</span><span class="lines">@@ -1066,12 +1086,10 @@
</span><span class="cx">                  var inputs_signatures = _( inputs ).map( function ( input ) {
</span><span class="cx">                          input = $( input );
</span><span class="cx">                          var signature_parts;
</span><del>-                               if ( input.is( 'option' ) ) {
-                                       signature_parts = [ input.prop( 'nodeName' ), input.prop( 'value' ) ];
-                               } else if ( input.is( ':checkbox, :radio' ) ) {
-                                       signature_parts = [ input.prop( 'type' ), input.attr( 'id' ), input.attr( 'name' ), input.prop( 'value' ) ];
</del><ins>+                                if ( input.is( ':checkbox, :radio' ) ) {
+                                       signature_parts = [ input.attr( 'id' ), input.attr( 'name' ), input.prop( 'value' ) ];
</ins><span class="cx">                           } else {
</span><del>-                                       signature_parts = [ input.prop( 'nodeName' ), input.attr( 'id' ), input.attr( 'name' ), input.attr( 'type' ) ];
</del><ins>+                                        signature_parts = [ input.attr( 'id' ), input.attr( 'name' ) ];
</ins><span class="cx">                           }
</span><span class="cx">                          return signature_parts.join( ',' );
</span><span class="cx">                  } );
</span><span class="lines">@@ -1089,8 +1107,6 @@
</span><span class="cx">                  input = $( input );
</span><span class="cx">                  if ( input.is( ':radio, :checkbox' ) ) {
</span><span class="cx">                          return 'checked';
</span><del>-                       } else if ( input.is( 'option' ) ) {
-                               return 'selected';
</del><span class="cx">                   } else {
</span><span class="cx">                          return 'value';
</span><span class="cx">                  }
</span><span class="lines">@@ -1127,16 +1143,15 @@
</span><span class="cx">                  var control = this,
</span><span class="cx">                          instance_override,
</span><span class="cx">                          complete_callback,
</span><ins>+                               widget_root,
</ins><span class="cx">                           update_number,
</span><span class="cx">                          widget_content,
</span><del>-                               element_id_to_refocus = null,
-                               active_input_selection_start = null,
-                               active_input_selection_end = null,
</del><span class="cx">                           params,
</span><span class="cx">                          data,
</span><span class="cx">                          inputs,
</span><span class="cx">                          processing,
</span><del>-                               jqxhr;
</del><ins>+                                jqxhr,
+                               is_changed;
</ins><span class="cx"> 
</span><span class="cx">                  args = $.extend( {
</span><span class="cx">                          instance: null,
</span><span class="lines">@@ -1150,34 +1165,28 @@
</span><span class="cx">                  control._update_count += 1;
</span><span class="cx">                  update_number = control._update_count;
</span><span class="cx"> 
</span><del>-                       widget_content = control.container.find( '.widget-content' );
</del><ins>+                        widget_root = control.container.find( '.widget:first' );
+                       widget_content = widget_root.find( '.widget-content:first' );
</ins><span class="cx"> 
</span><span class="cx">                  // Remove a previous error message
</span><span class="cx">                  widget_content.find( '.widget-error' ).remove();
</span><span class="cx"> 
</span><del>-                       // @todo Support more selectors than IDs?
-                       if ( $.contains( control.container[0], document.activeElement ) && $( document.activeElement ).is( '[id]' ) ) {
-                               element_id_to_refocus = $( document.activeElement ).prop( 'id' );
-                               // @todo IE8 support: http://stackoverflow.com/a/4207763/93579
-                               try {
-                                       active_input_selection_start = document.activeElement.selectionStart;
-                                       active_input_selection_end = document.activeElement.selectionEnd;
-                               }
-                               catch( e ) {} // catch InvalidStateError in case of checkboxes
-                       }
-
</del><span class="cx">                   control.container.addClass( 'widget-form-loading' );
</span><span class="cx">                  control.container.addClass( 'previewer-loading' );
</span><span class="cx">                  processing = wp.customize.state( 'processing' );
</span><span class="cx">                  processing( processing() + 1 );
</span><span class="cx"> 
</span><ins>+                       if ( ! control.live_update_mode ) {
+                               control.container.addClass( 'widget-form-disabled' );
+                       }
+
</ins><span class="cx">                   params = {};
</span><span class="cx">                  params.action = 'update-widget';
</span><span class="cx">                  params.wp_customize = 'on';
</span><span class="cx">                  params.nonce = self.nonce;
</span><span class="cx"> 
</span><span class="cx">                  data = $.param( params );
</span><del>-                       inputs = widget_content.find( ':input, option' );
</del><ins>+                        inputs = control._getInputs( widget_content );
</ins><span class="cx"> 
</span><span class="cx">                  // Store the value we're submitting in data so that when the response comes back,
</span><span class="cx">                  // we know if it got sanitized; if there is no difference in the sanitized value,
</span><span class="lines">@@ -1200,7 +1209,7 @@
</span><span class="cx">                                  sanitized_form,
</span><span class="cx">                                  sanitized_inputs,
</span><span class="cx">                                  has_same_inputs_in_response,
</span><del>-                                       is_instance_identical;
</del><ins>+                                        is_live_update_aborted = false;
</ins><span class="cx"> 
</span><span class="cx">                          // Check if the user is logged out.
</span><span class="cx">                          if ( '0' === r ) {
</span><span class="lines">@@ -1220,51 +1229,50 @@
</span><span class="cx"> 
</span><span class="cx">                          if ( r.success ) {
</span><span class="cx">                                  sanitized_form = $( '<div>' + r.data.form + '</div>' );
</span><ins>+                                       sanitized_inputs = control._getInputs( sanitized_form );
+                                       has_same_inputs_in_response = control._getInputsSignature( inputs ) === control._getInputsSignature( sanitized_inputs );
</ins><span class="cx"> 
</span><del>-                                       control.hook( 'formUpdate', sanitized_form );
</del><ins>+                                        // Restore live update mode if sanitized fields are now aligned with the existing fields
+                                       if ( has_same_inputs_in_response && ! control.live_update_mode ) {
+                                               control.live_update_mode = true;
+                                               control.container.removeClass( 'widget-form-disabled' );
+                                               control.container.find( 'input[name="savewidget"]' ).hide();
+                                       }
</ins><span class="cx"> 
</span><del>-                                       sanitized_inputs = sanitized_form.find( ':input, option' );
-                                       has_same_inputs_in_response = control._getInputsSignature( inputs ) === control._getInputsSignature( sanitized_inputs );
-
-                                       if ( has_same_inputs_in_response ) {
</del><ins>+                                        // Sync sanitized field states to existing fields if they are aligned
+                                       if ( has_same_inputs_in_response && control.live_update_mode ) {
</ins><span class="cx">                                           inputs.each( function ( i ) {
</span><span class="cx">                                                  var input = $( this ),
</span><span class="cx">                                                          sanitized_input = $( sanitized_inputs[i] ),
</span><span class="cx">                                                          property = control._getInputStatePropertyName( this ),
</span><del>-                                                               state,
-                                                               sanitized_state;
</del><ins>+                                                                submitted_state,
+                                                               sanitized_state,
+                                                               can_update_state;
</ins><span class="cx"> 
</span><del>-                                                       state = input.data( 'state' + update_number );
</del><ins>+                                                        submitted_state = input.data( 'state' + update_number );
</ins><span class="cx">                                                   sanitized_state = sanitized_input.prop( property );
</span><span class="cx">                                                  input.data( 'sanitized', sanitized_state );
</span><span class="cx"> 
</span><del>-                                                       if ( state !== sanitized_state ) {
-
-                                                               // Only update now if not currently focused on it,
-                                                               // so that we don't cause the cursor
-                                                               // it will be updated upon the change event
-                                                               if ( args.ignore_active_element || ! input.is( document.activeElement ) ) {
-                                                                       input.prop( property, sanitized_state );
-                                                               }
-                                                               control.hook( 'unsanitaryField', input, sanitized_state, state );
-
-                                                       } else {
-                                                               control.hook( 'sanitaryField', input, state );
</del><ins>+                                                        can_update_state = (
+                                                               submitted_state !== sanitized_state &&
+                                                               ( args.ignore_active_element || ! input.is( document.activeElement ) )
+                                                       );
+                                                       if ( can_update_state ) {
+                                                               input.prop( property, sanitized_state );
</ins><span class="cx">                                                   }
</span><span class="cx">                                          } );
</span><del>-                                               control.hook( 'formUpdated', sanitized_form );
</del><ins>+                                                $( document ).trigger( 'widget-synced', [ widget_root, r.data.form ] );
+
+                                       // Otherwise, if sanitized fields are not aligned with existing fields, disable live update mode if enabled
+                                       } else if ( control.live_update_mode ) {
+                                               control.live_update_mode = false;
+                                               control.container.find( 'input[name="savewidget"]' ).show();
+                                               is_live_update_aborted = true;
+                                       // Otherwise, replace existing form with the sanitized form
</ins><span class="cx">                                   } else {
</span><del>-                                               widget_content.html( sanitized_form.html() );
-                                               if ( element_id_to_refocus ) {
-                                                       // not using jQuery selector so we don't have to worry about escaping IDs with brackets and other characters
-                                                       $( document.getElementById( element_id_to_refocus ) )
-                                                               .prop( {
-                                                                       selectionStart: active_input_selection_start,
-                                                                       selectionEnd: active_input_selection_end
-                                                               } )
-                                                               .focus();
-                                               }
-                                               control.hook( 'formRefreshed' );
</del><ins>+                                                widget_content.html( r.data.form );
+                                               control.container.removeClass( 'widget-form-disabled' );
+                                               $( document ).trigger( 'widget-updated', [ widget_root ] );
</ins><span class="cx">                                   }
</span><span class="cx"> 
</span><span class="cx">                                  /**
</span><span class="lines">@@ -1272,15 +1280,15 @@
</span><span class="cx">                                   * needing to be rendered, and so we can preempt the event for the
</span><span class="cx">                                   * preview finishing loading.
</span><span class="cx">                                   */
</span><del>-                                       is_instance_identical = _( control.setting() ).isEqual( r.data.instance );
-                                       if ( ! is_instance_identical ) {
</del><ins>+                                        is_changed = ! is_live_update_aborted && ! _( control.setting() ).isEqual( r.data.instance );
+                                       if ( is_changed ) {
</ins><span class="cx">                                           control.is_widget_updating = true; // suppress triggering another updateWidget
</span><span class="cx">                                          control.setting( r.data.instance );
</span><span class="cx">                                          control.is_widget_updating = false;
</span><span class="cx">                                  }
</span><span class="cx"> 
</span><span class="cx">                                  if ( complete_callback ) {
</span><del>-                                               complete_callback.call( control, null, { no_change: is_instance_identical, ajax_finished: true } );
</del><ins>+                                                complete_callback.call( control, null, { no_change: ! is_changed, ajax_finished: true } );
</ins><span class="cx">                                   }
</span><span class="cx">                          } else {
</span><span class="cx">                                  message = self.i18n.error;
</span></span></pre>
</div>
</div>

</body>
</html>