TinyMCE: add support for `class` attribute on the caption shortcode. Part props knutsp, kadamwhite, fixes #28951.

Built from https://develop.svn.wordpress.org/trunk@29380


git-svn-id: http://core.svn.wordpress.org/trunk@29158 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Ozz 2014-08-06 00:58:16 +00:00
parent 8fc01225ac
commit b61903f2bb
4 changed files with 56 additions and 34 deletions

View File

@ -4,7 +4,7 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
function parseShortcode( content ) { function parseShortcode( content ) {
return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) { return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
var id, cls, w, cap, img, width, var id, align, classes, caption, img, width,
trim = tinymce.trim; trim = tinymce.trim;
id = b.match( /id=['"]([^'"]*)['"] ?/ ); id = b.match( /id=['"]([^'"]*)['"] ?/ );
@ -12,50 +12,56 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
b = b.replace( id[0], '' ); b = b.replace( id[0], '' );
} }
cls = b.match( /align=['"]([^'"]*)['"] ?/ ); align = b.match( /align=['"]([^'"]*)['"] ?/ );
if ( cls ) { if ( align ) {
b = b.replace( cls[0], '' ); b = b.replace( align[0], '' );
} }
w = b.match( /width=['"]([0-9]*)['"] ?/ ); classes = b.match( /class=['"]([^'"]*)['"] ?/ );
if ( w ) { if ( classes ) {
b = b.replace( w[0], '' ); b = b.replace( classes[0], '' );
}
width = b.match( /width=['"]([0-9]*)['"] ?/ );
if ( width ) {
b = b.replace( width[0], '' );
} }
c = trim( c ); c = trim( c );
img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i ); img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
if ( img && img[2] ) { if ( img && img[2] ) {
cap = trim( img[2] ); caption = trim( img[2] );
img = trim( img[1] ); img = trim( img[1] );
} else { } else {
// old captions shortcode style // old captions shortcode style
cap = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' ); caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
img = c; img = c;
} }
id = ( id && id[1] ) ? id[1] : ''; id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
cls = ( cls && cls[1] ) ? cls[1] : 'alignnone'; align = ( align && align[1] ) ? align[1] : 'alignnone';
classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
if ( ! w && img ) { if ( ! width && img ) {
w = img.match( /width=['"]([0-9]*)['"]/ ); width = img.match( /width=['"]([0-9]*)['"]/ );
} }
if ( w && w[1] ) { if ( width && width[1] ) {
w = w[1]; width = width[1];
} }
if ( ! w || ! cap ) { if ( ! width || ! caption ) {
return c; return c;
} }
width = parseInt( w, 10 ); width = parseInt( width, 10 );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) { if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width += 10; width += 10;
} }
return '<div class="mceTemp"><dl id="'+ id +'" class="wp-caption '+ cls +'" style="width: '+ width +'px">' + return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
'<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ cap +'</dd></dl></div>'; '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
}); });
} }
@ -75,32 +81,38 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
return ''; return '';
} }
out = b.replace( /<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function( a, b, c, cap ) { out = b.replace( /<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>/gi, function( a, b, c, caption ) {
var id, cls, w; var id, classes, align, width;
w = c.match( /width="([0-9]*)"/ ); width = c.match( /width="([0-9]*)"/ );
w = ( w && w[1] ) ? w[1] : ''; width = ( width && width[1] ) ? width[1] : '';
if ( ! w || ! cap ) { if ( ! width || ! caption ) {
return c; return c;
} }
id = b.match( /id="([^"]*)"/ ); id = b.match( /id="([^"]*)"/ );
id = ( id && id[1] ) ? id[1] : ''; id = ( id && id[1] ) ? id[1] : '';
cls = b.match( /class="([^"]*)"/ ); classes = b.match( /class="([^"]*)"/ );
cls = ( cls && cls[1] ) ? cls[1] : ''; classes = ( classes && classes[1] ) ? classes[1] : '';
cls = cls.match( /align[a-z]+/ ) || 'alignnone';
cap = cap.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) { align = classes.match( /align[a-z]+/i ) || 'alignnone';
classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
if ( classes ) {
classes = ' class="' + classes + '"';
}
caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
// no line breaks inside HTML tags // no line breaks inside HTML tags
return a.replace( /[\r\n\t]+/, ' ' ); return a.replace( /[\r\n\t]+/, ' ' );
}); });
// convert remaining line breaks to <br> // convert remaining line breaks to <br>
cap = cap.replace( /\s*\n\s*/g, '<br />' ); caption = caption.replace( /\s*\n\s*/g, '<br />' );
return '[caption id="'+ id +'" align="'+ cls +'" width="'+ w +'"]'+ c +' '+ cap +'[/caption]'; return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
}); });
if ( out.indexOf('[caption') !== 0 ) { if ( out.indexOf('[caption') !== 0 ) {
@ -115,6 +127,7 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
function extractImageData( imageNode ) { function extractImageData( imageNode ) {
var classes, extraClasses, metadata, captionBlock, caption, link, width, height, var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
captionClassName = [],
dom = editor.dom, dom = editor.dom,
isIntRegExp = /^\d+$/; isIntRegExp = /^\d+$/;
@ -180,9 +193,13 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
tinymce.each( classes, function( name ) { tinymce.each( classes, function( name ) {
if ( /^align/.test( name ) ) { if ( /^align/.test( name ) ) {
metadata.align = name.replace( 'align', '' ); metadata.align = name.replace( 'align', '' );
} else if ( name && name !== 'wp-caption' ) {
captionClassName.push( name );
} }
} ); } );
metadata.captionClassName = captionClassName.join( ' ' );
caption = dom.select( 'dd.wp-caption-dd', captionBlock ); caption = dom.select( 'dd.wp-caption-dd', captionBlock );
if ( caption.length ) { if ( caption.length ) {
caption = caption[0]; caption = caption[0];
@ -210,7 +227,7 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
function updateImage( imageNode, imageData ) { function updateImage( imageNode, imageData ) {
var classes, className, node, html, parent, wrap, linkNode, var classes, className, node, html, parent, wrap, linkNode,
captionNode, dd, dl, id, attrs, linkAttrs, width, height, captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
dom = editor.dom; dom = editor.dom;
classes = tinymce.explode( imageData.extraClasses, ' ' ); classes = tinymce.explode( imageData.extraClasses, ' ' );
@ -287,7 +304,12 @@ tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
if ( imageData.caption ) { if ( imageData.caption ) {
id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null; id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
className = 'wp-caption align' + ( imageData.align || 'none' ); align = 'align' + ( imageData.align || 'none' );
className = 'wp-caption ' + align;
if ( imageData.captionClassName ) {
className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
}
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) { if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width = parseInt( width, 10 ); width = parseInt( width, 10 );

File diff suppressed because one or more lines are too long

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.0-beta2-20140805'; $wp_version = '4.0-beta2-20140806';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.