From b5a3c5005701acc430e68f7df0d1ae02988726ef Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Tue, 28 Feb 2012 08:03:40 +0000 Subject: [PATCH] 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 git-svn-id: https://develop.svn.wordpress.org/trunk@20013 602fd350-edb4-49c9-b593-d223f7449a82 --- .../plugins/wpeditimage/editor_plugin_src.js | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js b/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js index 756154cf38..89cf947b34 100644 --- a/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js +++ b/wp-includes/js/tinymce/plugins/wpeditimage/editor_plugin_src.js @@ -141,13 +141,13 @@ return content.replace(/(?:

)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function(a,b,c){ var id, cls, w, cap, div_cls; - id = b.match(/id=['"]([^'"]+)['"] ?/); + id = b.match(/id=['"]([^'"]*)['"] ?/); b = b.replace(id[0], ''); - cls = b.match(/align=['"]([^'"]+)['"] ?/); + cls = b.match(/align=['"]([^'"]*)['"] ?/); b = b.replace(cls[0], ''); - w = b.match(/width=['"]([0-9]+)['"] ?/); + w = b.match(/width=['"]([0-9]*)['"] ?/); b = b.replace(w[0], ''); cap = tinymce.trim(b).replace(/caption=['"]/, '').replace(/['"]$/, ''); @@ -159,37 +159,52 @@ if ( !w || !cap ) return c; - div_cls = (cls == 'aligncenter') ? 'mceTemp mceIEcenter' : 'mceTemp'; + div_cls = 'mceTemp'; + if ( cls == 'aligncenter' ) + div_cls += ' mceIEcenter'; - return '

'+c+'
'+cap+'
'; }); }, _get_shcode : function(content) { - return content.replace(/
\s*
]+)>\s*
]+>([\s\S]+?)<\/dt>\s*
]+>([\s\S]+?)<\/dd>\s*<\/dl>\s*<\/div>/gi, function(a,b,c,cap){ - var id, cls, w; + return content.replace(/
]*>([\s\S]+?)<\/div>/g, function(a, b){ + var ret = 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]+)"/); - w = ( w && w[1] ) ? w[1] : ''; + w = c.match(/width="([0-9]*)"/); + w = ( w && w[1] ) ? w[1] : ''; - if ( !w || !cap ) - return c; + if ( !w || !cap ) + return c; - id = b.match(/id="([^"]+)"/); - id = ( id && id[1] ) ? id[1] : ''; + id = b.match(/id="([^"]*)"/); + id = ( id && id[1] ) ? id[1] : ''; - cls = b.match(/class="([^"]+)"/); - cls = ( cls && cls[1] ) ? cls[1] : ''; - cls = cls.match(/align[a-z]+/) || 'alignnone'; + cls = b.match(/class="([^"]*)"/); + cls = ( cls && cls[1] ) ? cls[1] : ''; + cls = cls.match(/align[a-z]+/) || 'alignnone'; - cap = cap.replace(/<[a-z][^<>]+>/g, function(a){ - return a.replace(/"/g, "'"); + cap = cap.replace(/<[a-z][^<>]+>/g, function(a){ + a = a.replace(/="[^"]+"/, function(b){ + return b.replace(/'/g, '''); + }); + return a.replace(/"/g, "'"); + }); + + cap = cap.replace(/"/g, '"'); + + return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]'; }); - cap = cap.replace(/"/g, '"'); + 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

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

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

$1

$2'); + } - return '[caption id="'+id+'" align="'+cls+'" width="'+w+'" caption="'+cap+'"]'+c+'[/caption]'; + return ret; }); },