<!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>[20013] trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js: HTML in image captions: improve converting the caption html elements to a shortcode, catch some rare cases where image with a caption is pasted in the visual editor, see #18311</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, #logmsg > ol { margin-left: 0; 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/20013">20013</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2012-02-28 08:03:40 +0000 (Tue, 28 Feb 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>HTML in image captions: improve converting the caption html elements to a shortcode, catch some rare cases where image with a caption is pasted in the visual editor, see <a href="http://core.trac.wordpress.org/ticket/18311">#18311</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpincludesjstinymcepluginswpeditimageeditor_plugin_srcjs">trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpincludesjstinymcepluginswpeditimageeditor_plugin_srcjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js (20012 => 20013)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js        2012-02-28 06:18:04 UTC (rev 20012)
+++ trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js        2012-02-28 08:03:40 UTC (rev 20013)
</span><span class="lines">@@ -141,13 +141,13 @@
</span><span class="cx">                         return content.replace(/(?:&lt;p&gt;)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:&lt;\/p&gt;)?/g, function(a,b,c){
</span><span class="cx">                                 var id, cls, w, cap, div_cls;
</span><span class="cx"> 
</span><del>-                                id = b.match(/id=['&quot;]([^'&quot;]+)['&quot;] ?/);
</del><ins>+                                id = b.match(/id=['&quot;]([^'&quot;]*)['&quot;] ?/);
</ins><span class="cx">                                 b = b.replace(id[0], '');
</span><span class="cx"> 
</span><del>-                                cls = b.match(/align=['&quot;]([^'&quot;]+)['&quot;] ?/);
</del><ins>+                                cls = b.match(/align=['&quot;]([^'&quot;]*)['&quot;] ?/);
</ins><span class="cx">                                 b = b.replace(cls[0], '');
</span><span class="cx"> 
</span><del>-                                w = b.match(/width=['&quot;]([0-9]+)['&quot;] ?/);
</del><ins>+                                w = b.match(/width=['&quot;]([0-9]*)['&quot;] ?/);
</ins><span class="cx">                                 b = b.replace(w[0], '');
</span><span class="cx"> 
</span><span class="cx">                                 cap = tinymce.trim(b).replace(/caption=['&quot;]/, '').replace(/['&quot;]$/, '');
</span><span class="lines">@@ -159,37 +159,52 @@
</span><span class="cx">                                 if ( !w || !cap )
</span><span class="cx">                                         return c;
</span><span class="cx"> 
</span><del>-                                div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp';
</del><ins>+                                div_cls = 'mceTemp';
+                                if ( cls == 'aligncenter' )
+                                        div_cls += ' mceIEcenter';
</ins><span class="cx"> 
</span><del>-                                return '&lt;div class=&quot;'+div_cls+'&quot; draggable=&quot;true&quot;&gt;&lt;dl id=&quot;'+id+'&quot; class=&quot;wp-caption '+cls+'&quot; style=&quot;width: '+( 10 + parseInt(w) )+
</del><ins>+                                return '&lt;div class=&quot;'+div_cls+'&quot;&gt;&lt;dl id=&quot;'+id+'&quot; class=&quot;wp-caption '+cls+'&quot; style=&quot;width: '+( 10 + parseInt(w) )+
</ins><span class="cx">                                 'px&quot;&gt;&lt;dt class=&quot;wp-caption-dt&quot;&gt;'+c+'&lt;/dt&gt;&lt;dd class=&quot;wp-caption-dd&quot;&gt;'+cap+'&lt;/dd&gt;&lt;/dl&gt;&lt;/div&gt;';
</span><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="cx"> 
</span><span class="cx">                 _get_shcode : function(content) {
</span><del>-                        return content.replace(/&lt;div class=&quot;mceTemp[^&quot;]*&quot;&gt;\s*&lt;dl ([^&gt;]+)&gt;\s*&lt;dt [^&gt;]+&gt;([\s\S]+?)&lt;\/dt&gt;\s*&lt;dd [^&gt;]+&gt;([\s\S]+?)&lt;\/dd&gt;\s*&lt;\/dl&gt;\s*&lt;\/div&gt;/gi, function(a,b,c,cap){
-                                var id, cls, w;
</del><ins>+                        return content.replace(/&lt;div (?:id=&quot;attachment_|class=&quot;mceTemp)[^&gt;]*&gt;([\s\S]+?)&lt;\/div&gt;/g, function(a, b){
+                                var ret = b.replace(/&lt;dl ([^&gt;]+)&gt;\s*&lt;dt [^&gt;]+&gt;([\s\S]+?)&lt;\/dt&gt;\s*&lt;dd [^&gt;]+&gt;([\s\S]*?)&lt;\/dd&gt;\s*&lt;\/dl&gt;/gi, function(a,b,c,cap){
+                                        var id, cls, w;
</ins><span class="cx"> 
</span><del>-                                w = c.match(/width=&quot;([0-9]+)&quot;/);
-                                w = ( w &amp;&amp; w[1] ) ? w[1] : '';
</del><ins>+                                        w = c.match(/width=&quot;([0-9]*)&quot;/);
+                                        w = ( w &amp;&amp; w[1] ) ? w[1] : '';
</ins><span class="cx"> 
</span><del>-                                if ( !w || !cap )
-                                        return c;
</del><ins>+                                        if ( !w || !cap )
+                                                return c;
</ins><span class="cx"> 
</span><del>-                                id = b.match(/id=&quot;([^&quot;]+)&quot;/);
-                                id = ( id &amp;&amp; id[1] ) ? id[1] : '';
</del><ins>+                                        id = b.match(/id=&quot;([^&quot;]*)&quot;/);
+                                        id = ( id &amp;&amp; id[1] ) ? id[1] : '';
</ins><span class="cx"> 
</span><del>-                                cls = b.match(/class=&quot;([^&quot;]+)&quot;/);
-                                cls = ( cls &amp;&amp; cls[1] ) ? cls[1] : '';
-                                cls = cls.match(/align[a-z]+/) || 'alignnone';
</del><ins>+                                        cls = b.match(/class=&quot;([^&quot;]*)&quot;/);
+                                        cls = ( cls &amp;&amp; cls[1] ) ? cls[1] : '';
+                                        cls = cls.match(/align[a-z]+/) || 'alignnone';
</ins><span class="cx"> 
</span><del>-                                cap = cap.replace(/&lt;[a-z][^&lt;&gt;]+&gt;/g, function(a){
-                                        return a.replace(/&quot;/g, &quot;'&quot;);
</del><ins>+                                        cap = cap.replace(/&lt;[a-z][^&lt;&gt;]+&gt;/g, function(a){
+                                                a = a.replace(/=&quot;[^&quot;]+&quot;/, function(b){
+                                                        return b.replace(/'/g, '&amp;#39;');
+                                                });
+                                                return a.replace(/&quot;/g, &quot;'&quot;);
+                                        });
+
+                                        cap = cap.replace(/&quot;/g, '&amp;quot;');
+
+                                        return '[caption id=&quot;'+id+'&quot; align=&quot;'+cls+'&quot; width=&quot;'+w+'&quot; caption=&quot;'+cap+'&quot;]'+c+'[/caption]';
</ins><span class="cx">                                 });
</span><span class="cx"> 
</span><del>-                                cap = cap.replace(/&quot;/g, '&amp;quot;');
</del><ins>+                                if ( ret.indexOf('[caption') !== 0 ) {
+                                        // the caption html seems brocken, try to find the image that may be wrapped in a link
+                                        // and may be followed by &lt;p&gt; with the caption text.
+                                        ret = b.replace(/[\s\S]*?((?:&lt;a [^&gt;]+&gt;)?&lt;img [^&gt;]+&gt;(?:&lt;\/a&gt;)?)(&lt;p&gt;[\s\S]*&lt;\/p&gt;)?[\s\S]*/gi, '&lt;p&gt;$1&lt;/p&gt;$2');
+                                }
</ins><span class="cx"> 
</span><del>-                                return '[caption id=&quot;'+id+'&quot; align=&quot;'+cls+'&quot; width=&quot;'+w+'&quot; caption=&quot;'+cap+'&quot;]'+c+'[/caption]';
</del><ins>+                                return ret;
</ins><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>