<!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>[20679] trunk: Change the image caption shortcode format to [caption ...]<a><img /></a> caption text + html[/caption].</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/20679">20679</a></dd>
<dt>Author</dt> <dd>azaozz</dd>
<dt>Date</dt> <dd>2012-05-02 01:14:52 +0000 (Wed, 02 May 2012)</dd>
</dl>
<h3>Log Message</h3>
<pre>Change the image caption shortcode format to [caption ...]<a><img /></a> caption text + html[/caption]. That way HTML tags in captions are better supported and the shortcode wouldn't break when using the wrong quotes. Props sushkov, nacin, fixes <a href="http://core.trac.wordpress.org/ticket/18311">#18311</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesmediaphp">trunk/wp-admin/includes/media.php</a></li>
<li><a href="#trunkwpadminjseditordevjs">trunk/wp-admin/js/editor.dev.js</a></li>
<li><a href="#trunkwpincludesjstinymcepluginswpeditimageeditor_plugin_srcjs">trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js</a></li>
<li><a href="#trunkwpincludesjstinymcepluginswpeditimagejseditimagedevjs">trunk/wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js</a></li>
<li><a href="#trunkwpincludesmediaphp">trunk/wp-includes/media.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/media.php (20678 => 20679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/media.php        2012-05-01 22:58:23 UTC (rev 20678)
+++ trunk/wp-admin/includes/media.php        2012-05-02 01:14:52 UTC (rev 20679)
</span><span class="lines">@@ -145,14 +145,14 @@
</span><span class="cx">
</span><span class="cx">         $caption = str_replace( array("\r\n", "\r"), "\n", $caption);
</span><span class="cx">         $caption = preg_replace_callback( '/<[a-zA-Z0-9]+(?: [^<>]+>)*/', '_cleanup_image_add_caption', $caption );
</span><del>-        $caption = preg_replace( '/\n+/', '<br />', str_replace('"', '&quot;', $caption) );
</del><ins>+        // convert any remaining line breaks to <br>
+        $caption = preg_replace( '/[ \n\t]*\n[ \t]*/', '<br />', $caption );
</ins><span class="cx">
</span><span class="cx">         $html = preg_replace( '/(class=["\'][^\'"]*)align(none|left|right|center)\s?/', '$1', $html );
</span><span class="cx">         if ( empty($align) )
</span><span class="cx">                 $align = 'none';
</span><span class="cx">
</span><del>-        $shcode = '[caption id="' . $id . '" align="align' . $align
-        . '" width="' . $width . '" caption="' . $caption . '"]' . $html . '[/caption]';
</del><ins>+        $shcode = '[caption id="' . $id . '" align="align' . $align        . '" width="' . $width . '"]' . $html . ' ' . $caption . '[/caption]';
</ins><span class="cx">
</span><span class="cx">         return apply_filters( 'image_add_caption_shortcode', $shcode, $html );
</span><span class="cx"> }
</span><span class="lines">@@ -166,23 +166,10 @@
</span><span class="cx"> */
</span><span class="cx"> function _cleanup_image_add_caption( $matches ) {
</span><span class="cx">         // remove any line breaks from inside the tags
</span><del>-        $s = preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
-        // look for single quotes inside html attributes (for example in title)
-        $s = preg_replace_callback( '/="[^"]+"/', '_cleanup_image_add_caption_callback', $s );
-        return str_replace( '"', "'", $s );
</del><ins>+        return preg_replace( '/[\r\n\t]+/', ' ', $matches[0] );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Private preg_replace callback used in _cleanup_image_add_caption()
- *
- * @access private
- * @since 3.4.0
- */
-function _cleanup_image_add_caption_callback( $matches ) {
-        return str_replace( "'", '&#39;', $matches[0] );
-}
-
-/**
</del><span class="cx"> * {@internal Missing Short Description}}
</span><span class="cx"> *
</span><span class="cx"> * @since 2.5.0
</span><span class="lines">@@ -1541,13 +1528,10 @@
</span><span class="cx">                 if ( f.caption.value ) {
</span><span class="cx">                         caption = f.caption.value.replace(/\r\n|\r/g, '\n');
</span><span class="cx">                         caption = caption.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
</span><del>-                                a = a.replace(/[\r\n\t]+/, ' ').replace(/="[^"]+"/, function(b){
-                                        return b.replace(/'/g, '&#39;');
-                                });
-                                return a.replace(/"/g, "'");
</del><ins>+                                return a.replace(/[\r\n\t]+/, ' ');
</ins><span class="cx">                         });
</span><span class="cx">
</span><del>-                        caption = caption.replace(/\n+/g, '<br />').replace(/"/g, '&quot;');
</del><ins>+                        caption = caption.replace(/\s*\n\s*/g, '<br />');
</ins><span class="cx">                 }
</span><span class="cx"> <?php } ?>
</span><span class="cx">
</span><span class="lines">@@ -1561,7 +1545,7 @@
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 if ( caption )
</span><del>-                        html = '[caption id="" align="'+t.align+'" width="'+t.width+'" caption="'+caption+'"]'+html+'[/caption]';
</del><ins>+                        html = '[caption id="" align="'+t.align+'" width="'+t.width+'"]'+html+caption+'[/caption]';
</ins><span class="cx">
</span><span class="cx">                 var win = window.dialogArguments || opener || parent || top;
</span><span class="cx">                 win.send_to_editor(html);
</span></span></pre></div>
<a id="trunkwpadminjseditordevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/js/editor.dev.js (20678 => 20679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/js/editor.dev.js        2012-05-01 22:58:23 UTC (rev 20678)
+++ trunk/wp-admin/js/editor.dev.js        2012-05-02 01:14:52 UTC (rev 20679)
</span><span class="lines">@@ -73,11 +73,11 @@
</span><span class="cx">                         });
</span><span class="cx">                 }
</span><span class="cx">
</span><del>-                // keep <br> tags inside captions
</del><ins>+                // keep <br> tags inside captions and remove line breaks
</ins><span class="cx">                 if ( content.indexOf('[caption') != -1 ) {
</span><span class="cx">                         preserve_br = true;
</span><del>-                        content = content.replace(/\[caption[^\]]+\]/g, function(a) {
-                                return a.replace(/<br([^>]*)>[\r\n]*/g, '<wp-temp-br$1>');
</del><ins>+                        content = content.replace(/\[caption[\s\S]+?\[\/caption\]/g, function(a) {
+                                return a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>').replace(/[\r\n\t]+/, '');
</ins><span class="cx">                         });
</span><span class="cx">                 }
</span><span class="cx">
</span><span class="lines">@@ -139,7 +139,8 @@
</span><span class="cx">         },
</span><span class="cx">
</span><span class="cx">         _wp_Autop : function(pee) {
</span><del>-                var blocklist = 'table|thead|tfoot|tbody|tr|td|th|caption|col|colgroup|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6]|fieldset|legend|hr|noscript|menu|samp|header|footer|article|section|hgroup|nav|aside|details|summary';
</del><ins>+                var preserve_linebreaks = false, preserve_br = false,
+                        blocklist = 'table|thead|tfoot|tbody|tr|td|th|caption|col|colgroup|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|address|math|p|h[1-6]|fieldset|legend|hr|noscript|menu|samp|header|footer|article|section|hgroup|nav|aside|details|summary';
</ins><span class="cx">
</span><span class="cx">                 if ( pee.indexOf('<object') != -1 ) {
</span><span class="cx">                         pee = pee.replace(/<object[\s\S]+?<\/object>/g, function(a){
</span><span class="lines">@@ -153,11 +154,27 @@
</span><span class="cx">
</span><span class="cx">                 // Protect pre|script tags
</span><span class="cx">                 if ( pee.indexOf('<pre') != -1 || pee.indexOf('<script') != -1 ) {
</span><ins>+                        preserve_linebreaks = true;
</ins><span class="cx">                         pee = pee.replace(/<(pre|script)[^>]*>[\s\S]+?<\/\1>/g, function(a) {
</span><del>-                                return a.replace(/(\r\n|\n)/g, '<wp_temp_br>');
</del><ins>+                                return a.replace(/(\r\n|\n)/g, '<wp-temp-lb>');
</ins><span class="cx">                         });
</span><span class="cx">                 }
</span><span class="cx">
</span><ins>+                // keep <br> tags inside captions and convert line breaks
+                if ( pee.indexOf('[caption') != -1 ) {
+                        preserve_br = true;
+                        pee = pee.replace(/\[caption[\s\S]+?\[\/caption\]/g, function(a) {
+                                // keep existing <br>
+                                a = a.replace(/<br([^>]*)>/g, '<wp-temp-br$1>');
+                                // no line breaks inside HTML tags
+                                a = a.replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(b){
+                                        return b.replace(/[\r\n\t]+/, ' ');
+                                });
+                                // convert remaining line breaks to <br>
+                                return a.replace(/\s*\n\s*/g, '<wp-temp-br />');
+                        });
+                }
+
</ins><span class="cx">                 pee = pee + '\n\n';
</span><span class="cx">                 pee = pee.replace(/<br \/>\s*<br \/>/gi, '\n\n');
</span><span class="cx">                 pee = pee.replace(new RegExp('(<(?:'+blocklist+')(?: [^>]*)?>)', 'gi'), '\n$1');
</span><span class="lines">@@ -186,8 +203,12 @@
</span><span class="cx">                 });
</span><span class="cx">
</span><span class="cx">                 // put back the line breaks in pre|script
</span><del>-                pee = pee.replace(/<wp_temp_br>/g, '\n');
</del><ins>+                if ( preserve_linebreaks )
+                        pee = pee.replace(/<wp-temp-lb>/g, '\n');
</ins><span class="cx">
</span><ins>+                if ( preserve_br )
+                        pee = pee.replace(/<wp-temp-br([^>]*)>/g, '<br$1>');
+
</ins><span class="cx">                 return pee;
</span><span class="cx">         },
</span><span class="cx">
</span></span></pre></div>
<a id="trunkwpincludesjstinymcepluginswpeditimageeditor_plugin_srcjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js (20678 => 20679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js        2012-05-01 22:58:23 UTC (rev 20678)
+++ trunk/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js        2012-05-02 01:14:52 UTC (rev 20679)
</span><span class="lines">@@ -139,7 +139,7 @@
</span><span class="cx">
</span><span class="cx">                 _do_shcode : function(content) {
</span><span class="cx">                         return content.replace(/(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){
</span><del>-                                var id, cls, w, cap, div_cls;
</del><ins>+                                var id, cls, w, cap, div_cls, img, trim = tinymce.trim;
</ins><span class="cx">
</span><span class="cx">                                 id = b.match(/id=['"]([^'"]*)['"] ?/);
</span><span class="cx">                                 b = b.replace(id[0], '');
</span><span class="lines">@@ -150,8 +150,18 @@
</span><span class="cx">                                 w = b.match(/width=['"]([0-9]*)['"] ?/);
</span><span class="cx">                                 b = b.replace(w[0], '');
</span><span class="cx">
</span><del>-                                cap = tinymce.trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
</del><ins>+                                c = trim(c);
+                                img = c.match(/((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i);
</ins><span class="cx">
</span><ins>+                                if ( img && img[2] ) {
+                                        cap = trim( img[2] );
+                                        img = trim( img[1] );
+                                } else {
+                                        // old captions shortcode style
+                                        cap = trim(b).replace(/caption=['"]/, '').replace(/['"]$/, '');
+                                        img = c;
+                                }
+
</ins><span class="cx">                                 id = ( id && id[1] ) ? id[1] : '';
</span><span class="cx">                                 cls = ( cls && cls[1] ) ? cls[1] : 'alignnone';
</span><span class="cx">                                 w = ( w && w[1] ) ? w[1] : '';
</span><span class="lines">@@ -164,7 +174,7 @@
</span><span class="cx">                                         div_cls += ' mceIEcenter';
</span><span class="cx">
</span><span class="cx">                                 return '<div class="'+div_cls+'"><dl id="'+id+'" class="wp-caption '+cls+'" style="width: '+( 10 + parseInt(w) )+
</span><del>-                                'px"><dt class="wp-caption-dt">'+c+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
</del><ins>+                                'px"><dt class="wp-caption-dt">'+img+'</dt><dd class="wp-caption-dd">'+cap+'</dd></dl></div>';
</ins><span class="cx">                         });
</span><span class="cx">                 },
</span><span class="cx">
</span><span class="lines">@@ -187,15 +197,14 @@
</span><span class="cx">                                         cls = cls.match(/align[a-z]+/) || 'alignnone';
</span><span class="cx">
</span><span class="cx">                                         cap = cap.replace(/\r\n|\r/g, '\n').replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
</span><del>-                                                a = a.replace(/[\r\n\t]+/, ' ').replace(/="[^"]+"/, function(b){
-                                                        return b.replace(/'/g, '&#39;');
-                                                });
-                                                return a.replace(/"/g, "'");
</del><ins>+                                                // no line breaks inside HTML tags
+                                                return a.replace(/[\r\n\t]+/, ' ');
</ins><span class="cx">                                         });
</span><span class="cx">
</span><del>-                                        cap = cap.replace(/\n+/g, '<br />').replace(/"/g, '&quot;');
</del><ins>+                                        // convert remaining line breaks to <br>
+                                        cap = cap.replace(/\s*\n\s*/g, '<br />');
</ins><span class="cx">
</span><del>-                                        return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]';
</del><ins>+                                        return '[caption id="'+id+'" align="'+cls+'" width="'+w+'"]'+c+' '+cap+'[/caption]';
</ins><span class="cx">                                 });
</span><span class="cx">
</span><span class="cx">                                 if ( ret.indexOf('[caption') !== 0 ) {
</span></span></pre></div>
<a id="trunkwpincludesjstinymcepluginswpeditimagejseditimagedevjs"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js (20678 => 20679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js        2012-05-01 22:58:23 UTC (rev 20678)
+++ trunk/wp-includes/js/tinymce/plugins/wpeditimage/js/editimage.dev.js        2012-05-02 01:14:52 UTC (rev 20679)
</span><span class="lines">@@ -419,13 +419,10 @@
</span><span class="cx">                         caption = f.img_cap_text.value;
</span><span class="cx">
</span><span class="cx">                         caption = caption.replace(/\r\n|\r/g, '\n').replace(/<[a-zA-Z0-9]+( [^<>]+)?>/g, function(a){
</span><del>-                                a = a.replace(/[\r\n\t]+/, ' ').replace(/="[^"]+"/, function(b){
-                                        return b.replace(/'/g, '&#39;');
-                                });
-                                return a.replace(/"/g, "'");
</del><ins>+                                return a.replace(/[\r\n\t]+/, ' ');
</ins><span class="cx">                         });
</span><span class="cx">
</span><del>-                        caption = caption.replace(/\n+/g, '<br />').replace(/"/g, '&quot;');
</del><ins>+                        caption = caption.replace(/\s*\n\s*/g, '<br />');
</ins><span class="cx">
</span><span class="cx">                         if ( DL ) {
</span><span class="cx">                                 ed.dom.setAttribs(DL, {
</span></span></pre></div>
<a id="trunkwpincludesmediaphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/media.php (20678 => 20679)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/media.php        2012-05-01 22:58:23 UTC (rev 20678)
+++ trunk/wp-includes/media.php        2012-05-02 01:14:52 UTC (rev 20679)
</span><span class="lines">@@ -724,6 +724,13 @@
</span><span class="cx"> * @return string
</span><span class="cx"> */
</span><span class="cx"> function img_caption_shortcode($attr, $content = null) {
</span><ins>+        // New-style shortcode with the caption inside the shortcode with the link and image tags.
+        if ( ! isset( $attr['caption'] ) ) {
+                if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
+                        $content = $matches[1];
+                        $attr['caption'] = trim( $matches[2] );
+                }
+        }
</ins><span class="cx">
</span><span class="cx">         // Allow plugins/themes to override the default caption template.
</span><span class="cx">         $output = apply_filters('img_caption_shortcode', '', $attr, $content);
</span></span></pre>
</div>
</div>
</body>
</html>