<!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>[29198] trunk/src/wp-includes/js/mce-view.js: Make sure that the `ready` event only fires once for relevant MCE views that are sandbox'd in iframes.</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/29198">29198</a></dd>
<dt>Author</dt> <dd>wonderboymusic</dd>
<dt>Date</dt> <dd>2014-07-16 22:09:13 +0000 (Wed, 16 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make sure that the `ready` event only fires once for relevant MCE views that are sandbox'd in iframes. Move some editor callbacks to `initialize`.

Props avryl.
See <a href="http://core.trac.wordpress.org/ticket/28905">#28905</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesjsmceviewjs">trunk/src/wp-includes/js/mce-view.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesjsmceviewjs"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/js/mce-view.js (29197 => 29198)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/js/mce-view.js     2014-07-16 22:04:08 UTC (rev 29197)
+++ trunk/src/wp-includes/js/mce-view.js        2014-07-16 22:09:13 UTC (rev 29198)
</span><span class="lines">@@ -47,8 +47,6 @@
</span><span class="cx">                          '</div>';
</span><span class="cx">          },
</span><span class="cx">          render: function() {
</span><del>-                       var html = this.getHtml() || this.loadingPlaceholder();
-
</del><span class="cx">                   this.setContent(
</span><span class="cx">                          '<p class="wpview-selection-before">\u00a0</p>' +
</span><span class="cx">                          '<div class="wpview-body" contenteditable="false">' +
</span><span class="lines">@@ -57,84 +55,79 @@
</span><span class="cx">                                          '<div class="dashicons dashicons-no-alt remove"></div>' +
</span><span class="cx">                                  '</div>' +
</span><span class="cx">                                  '<div class="wpview-content wpview-type-' + this.type + '">' +
</span><del>-                                               html +
</del><ins>+                                                ( this.getHtml() || this.loadingPlaceholder() ) +
</ins><span class="cx">                                   '</div>' +
</span><span class="cx">                                  ( this.overlay ? '<div class="wpview-overlay"></div>' : '' ) +
</span><span class="cx">                          '</div>' +
</span><span class="cx">                          '<p class="wpview-selection-after">\u00a0</p>',
</span><del>-                               function( self, editor, node ) {
-                                       $( self ).trigger( 'ready', [ editor, node ] );
-                               },
</del><span class="cx">                           'wrap'
</span><span class="cx">                  );
</span><ins>+
+                       $( this ).trigger( 'ready' );
</ins><span class="cx">           },
</span><span class="cx">          unbind: function() {},
</span><del>-               getNodes: function( callback ) {
-                       var nodes = [];
</del><ins>+                getEditors: function( callback ) {
+                       var editors = [];
</ins><span class="cx"> 
</span><span class="cx">                  _.each( tinymce.editors, function( editor ) {
</span><span class="cx">                          if ( editor.plugins.wpview ) {
</span><del>-                                       $( editor.getBody() )
-                                       .find( '[data-wpview-text="' + this.encodedText + '"]' )
-                                       .each( function ( i, node ) {
-                                               if ( callback ) {
-                                                       callback( editor, node );
-                                               }
</del><ins>+                                        if ( callback ) {
+                                               callback( editor );
+                                       }
</ins><span class="cx"> 
</span><del>-                                               nodes.push( node );
-                                       } );
</del><ins>+                                        editors.push( editor );
</ins><span class="cx">                           }
</span><span class="cx">                  }, this );
</span><span class="cx"> 
</span><ins>+                       return editors;
+               },
+               getNodes: function( callback ) {
+                       var nodes = [],
+                               self = this;
+
+                       this.getEditors( function( editor ) {
+                               $( editor.getBody() )
+                               .find( '[data-wpview-text="' + self.encodedText + '"]' )
+                               .each( function ( i, node ) {
+                                       if ( callback ) {
+                                               callback( editor, node, $( node ).find( '.wpview-content' ).get( 0 ) );
+                                       }
+
+                                       nodes.push( node );
+                               } );
+                       } );
+
</ins><span class="cx">                   return nodes;
</span><span class="cx">          },
</span><del>-               setContent: function( html, callback, option ) {
-                       var self = this;
</del><ins>+                setContent: function( html, option ) {
+                       this.getNodes( function ( editor, node, content ) {
+                               var el = ( option === 'wrap' || option === 'replace' ) ? node : content,
+                                       insert = html;
</ins><span class="cx"> 
</span><del>-                       this.getNodes( function ( editor, element ) {
-                               var contentWrap = $( element ).find( '.wpview-content' ),
-                                       wrap = element;
-
-                               if ( contentWrap.length && option !== 'wrap' ) {
-                                       element = contentWrap = contentWrap[0];
</del><ins>+                                if ( _.isString( insert ) ) {
+                                       insert = editor.dom.createFragment( insert );
</ins><span class="cx">                           }
</span><span class="cx"> 
</span><del>-                               if ( _.isString( html ) ) {
-                                       if ( option === 'replace' ) {
-                                               element = editor.dom.replace( editor.dom.createFragment( html ), wrap );
-                                       } else {
-                                               editor.dom.setHTML( element, html );
-                                       }
</del><ins>+                                if ( option === 'replace' ) {
+                                       editor.dom.replace( insert, el );
</ins><span class="cx">                           } else {
</span><del>-                                       if ( option === 'replace' ) {
-                                               element = editor.dom.replace( html, wrap );
-                                       } else {
-                                               $( element ).empty().append( html );
-                                       }
</del><ins>+                                        el.innerHTML = '';
+                                       el.appendChild( insert );
</ins><span class="cx">                           }
</span><del>-
-                               if ( _.isFunction( callback ) ) {
-                                       callback( self, editor, $( element ).find( '.wpview-content' )[0] );
-                               }
</del><span class="cx">                   } );
</span><span class="cx">          },
</span><del>-
</del><span class="cx">           /* jshint scripturl: true */
</span><del>-               createIframe: function ( content ) {
</del><ins>+                setIframes: function ( html ) {
</ins><span class="cx">                   var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
</span><span class="cx"> 
</span><del>-                       if ( content.indexOf( '<script' ) !== -1 ) {
-                               this.getNodes( function ( editor, node ) {
</del><ins>+                        if ( html.indexOf( '<script' ) !== -1 ) {
+                               this.getNodes( function ( editor, node, content ) {
</ins><span class="cx">                                   var dom = editor.dom,
</span><span class="cx">                                          iframe, iframeDoc, i, resize;
</span><span class="cx"> 
</span><del>-                                       node = $( node ).find( '.wpview-content' )[0];
-                                       if ( ! node ) {
-                                               return;
-                                       }
-                                       node.innerHTML = '';
</del><ins>+                                        content.innerHTML = '';
</ins><span class="cx"> 
</span><del>-                                       iframe = dom.add( node, 'iframe', {
</del><ins>+                                        iframe = dom.add( content, 'iframe', {
</ins><span class="cx">                                           src: tinymce.Env.ie ? 'javascript:""' : '',
</span><span class="cx">                                          frameBorder: '0',
</span><span class="cx">                                          allowTransparency: 'true',
</span><span class="lines">@@ -155,7 +148,7 @@
</span><span class="cx">                                                          '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' +
</span><span class="cx">                                                  '</head>' +
</span><span class="cx">                                                  '<body data-context="iframe-sandbox" style="padding: 0; margin: 0;" class="' + editor.getBody().className + '">' +
</span><del>-                                                               content +
</del><ins>+                                                                html +
</ins><span class="cx">                                                   '</body>' +
</span><span class="cx">                                          '</html>'
</span><span class="cx">                                  );
</span><span class="lines">@@ -182,10 +175,9 @@
</span><span class="cx">                                  }
</span><span class="cx">                          });
</span><span class="cx">                  } else {
</span><del>-                               this.setContent( content );
</del><ins>+                                this.setContent( html );
</ins><span class="cx">                   }
</span><span class="cx">          },
</span><del>-
</del><span class="cx">           setError: function( message, dashicon ) {
</span><span class="cx">                  this.setContent(
</span><span class="cx">                          '<div class="wpview-error">' +
</span><span class="lines">@@ -507,28 +499,30 @@
</span><span class="cx">                  action: 'parse-media-shortcode',
</span><span class="cx"> 
</span><span class="cx">                  initialize: function( options ) {
</span><ins>+                               var self = this;
+
</ins><span class="cx">                           this.shortcode = options.shortcode;
</span><del>-                               this.fetching = false;
</del><span class="cx"> 
</span><del>-                               _.bindAll( this, 'createIframe', 'setNode', 'fetch', 'pausePlayers' );
-                               $( this ).on( 'ready', this.setNode );
</del><ins>+                                _.bindAll( this, 'setIframes', 'setNodes', 'fetch', 'pausePlayers' );
+                               $( this ).on( 'ready', this.setNodes );
</ins><span class="cx"> 
</span><span class="cx">                          $( document ).on( 'media:edit', this.pausePlayers );
</span><ins>+
+                               this.fetch();
+
+                               this.getEditors( function( editor ) {
+                                       editor.on( 'hide', self.pausePlayers );
+                               });
</ins><span class="cx">                   },
</span><span class="cx"> 
</span><del>-                       setNode: function ( event, editor ) {
-                               editor.on( 'hide', this.pausePlayers );
-
</del><ins>+                        setNodes: function () {
</ins><span class="cx">                           if ( this.parsed ) {
</span><del>-                                       this.createIframe( this.parsed );
-                               } else if ( ! this.fetching ) {
-                                       this.fetch();
</del><ins>+                                        this.setIframes( this.parsed );
</ins><span class="cx">                           }
</span><span class="cx">                  },
</span><span class="cx"> 
</span><span class="cx">                  fetch: function () {
</span><span class="cx">                          var self = this;
</span><del>-                               this.fetching = true;
</del><span class="cx"> 
</span><span class="cx">                          wp.ajax.send( this.action, {
</span><span class="cx">                                  data: {
</span><span class="lines">@@ -537,13 +531,10 @@
</span><span class="cx">                                          shortcode: this.shortcode.string()
</span><span class="cx">                                  }
</span><span class="cx">                          } )
</span><del>-                               .always( function() {
-                                       self.fetching = false;
-                               } )
</del><span class="cx">                           .done( function( response ) {
</span><span class="cx">                                  if ( response ) {
</span><span class="cx">                                          self.parsed = response;
</span><del>-                                               self.createIframe( response );
</del><ins>+                                                self.setIframes( response );
</ins><span class="cx">                                   }
</span><span class="cx">                          } )
</span><span class="cx">                          .fail( function( response ) {
</span><span class="lines">@@ -553,7 +544,7 @@
</span><span class="cx"> 
</span><span class="cx">                                                  self.setError( response.message, 'admin-media' );
</span><span class="cx">                                          } else {
</span><del>-                                                       self.setContent( '<p>' + self.original + '</p>', null, 'replace' );
</del><ins>+                                                        self.setContent( '<p>' + self.original + '</p>', 'replace' );
</ins><span class="cx">                                           }
</span><span class="cx">                                  } else if ( response && response.statusText ) {
</span><span class="cx">                                          self.setError( response.statusText, 'admin-media' );
</span><span class="lines">@@ -561,21 +552,9 @@
</span><span class="cx">                          } );
</span><span class="cx">                  },
</span><span class="cx"> 
</span><del>-                       /**
-                        * Return parsed response
-                        *
-                        * @returns {string}
-                        */
-                       getHtml: function() {
-                               if ( ! this.parsed ) {
-                                       return ' ';
-                               }
-                               return this.parsed;
-                       },
-
</del><span class="cx">                   pausePlayers: function() {
</span><del>-                               this.getNodes( function( editor, node ) {
-                                       var p, win = $( 'iframe', node ).get(0).contentWindow;
</del><ins>+                                this.getNodes( function( editor, node, content ) {
+                                       var p, win = $( 'iframe', content ).get(0).contentWindow;
</ins><span class="cx"> 
</span><span class="cx">                                  if ( win && win.mejs ) {
</span><span class="cx">                                          for ( p in win.mejs.players ) {
</span><span class="lines">@@ -586,8 +565,8 @@
</span><span class="cx">                  },
</span><span class="cx"> 
</span><span class="cx">                  unsetPlayers: function() {
</span><del>-                               this.getNodes( function( editor, node ) {
-                                       var p, win = $( 'iframe', node ).get(0).contentWindow;
</del><ins>+                                this.getNodes( function( editor, node, content ) {
+                                       var p, win = $( 'iframe', content ).get(0).contentWindow;
</ins><span class="cx"> 
</span><span class="cx">                                  if ( win && win.mejs ) {
</span><span class="cx">                                          for ( p in win.mejs.players ) {
</span><span class="lines">@@ -679,8 +658,6 @@
</span><span class="cx">                  action: 'parse-embed',
</span><span class="cx">                  initialize: function( options ) {
</span><span class="cx">                          this.content = options.content;
</span><del>-                               this.fetching = false;
-                               this.parsed = false;
</del><span class="cx">                           this.original = options.url || options.shortcode.string();
</span><span class="cx"> 
</span><span class="cx">                          if ( options.url ) {
</span><span class="lines">@@ -691,20 +668,10 @@
</span><span class="cx">                                  this.shortcode = options.shortcode;
</span><span class="cx">                          }
</span><span class="cx"> 
</span><del>-                               _.bindAll( this, 'createIframe', 'setNode', 'fetch' );
-                               $( this ).on( 'ready', this.setNode );
-                       },
</del><ins>+                                _.bindAll( this, 'setIframes', 'setNodes', 'fetch' );
+                               $( this ).on( 'ready', this.setNodes );
</ins><span class="cx"> 
</span><del>-                       /**
-                        * Return parsed response
-                        *
-                        * @returns {string}
-                        */
-                       getHtml: function() {
-                               if ( ! this.parsed ) {
-                                       return '';
-                               }
-                               return this.parsed;
</del><ins>+                                this.fetch();
</ins><span class="cx">                   }
</span><span class="cx">          } ),
</span><span class="cx">          edit: function( node ) {
</span></span></pre>
</div>
</div>

</body>
</html>