From 83509b3969a161d07c2c0034ba7f4c6c80e8bb62 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Tue, 31 Dec 2013 03:09:34 +0000 Subject: [PATCH] TinyMCE: - Fix removing leftovers from broken captions. - Better filter for line breaks and
tags when editing captions. See #24067. git-svn-id: https://develop.svn.wordpress.org/trunk@26882 602fd350-edb4-49c9-b593-d223f7449a82 --- .../js/tinymce/plugins/wpeditimage/plugin.js | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js b/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js index 1bc5a6cfeb..b3eae05d48 100644 --- a/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js +++ b/src/wp-includes/js/tinymce/plugins/wpeditimage/plugin.js @@ -49,12 +49,21 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { function getShortcode( content ) { return content.replace( /
]*>([\s\S]+?)<\/div>/g, function( a, b ) { + var out = ''; + if ( b.indexOf(']+>([\s\S]+?)<\/dd>/i ); + + if ( out && out[1] ) { + return '

' + out[1] + '

' + } + return ''; } - var ret = b.replace( /
]+)>\s*
]+>([\s\S]+?)<\/dt>\s*
]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function( a, b, c, cap ) { + out = b.replace( /
]+)>\s*
]+>([\s\S]+?)<\/dt>\s*
]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function( a, b, c, cap ) { var id, cls, w; w = c.match( /width="([0-9]*)"/ ); @@ -82,13 +91,13 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { return '[caption id="'+ id +'" align="'+ cls +'" width="'+ w +'"]'+ c +' '+ cap +'[/caption]'; }); - if ( ret.indexOf('[caption') !== 0 ) { + if ( out.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

with the caption text. - ret = b.replace( /[\s\S]*?((?:]+>)?]+>(?:<\/a>)?)(

[\s\S]*<\/p>)?[\s\S]*/gi, '

$1

$2' ); + out = b.replace( /[\s\S]*?((?:]+>)?]+>(?:<\/a>)?)(

[\s\S]*<\/p>)?[\s\S]*/gi, '

$1

$2' ); } - return ret; + return out; }); } @@ -161,6 +170,16 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { return; } + if ( caption ) { + caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) { + // No line breaks inside HTML tags + return a.replace( /[\r\n\t]+/, ' ' ); + }); + + // Convert remaining line breaks to
+ caption = caption.replace( /(]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '
' ); + } + if ( ! imgNode ) { // New image inserted html = dom.createHTML( 'img', data ); @@ -294,7 +313,11 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) { if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) { parent = dom.select( 'dd.wp-caption-dd', parent )[0]; - data.caption = parent ? parent.innerHTML : ''; + + if ( parent ) { + data.caption = editor.serializer.serialize( parent ) + .replace( /]*>/g, '$&\n' ).replace( /^

/, '' ).replace( /<\/p>$/, '' ); + } } });