tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
var toolbar, serializer, touchOnImage, pasteInCaption,
function isPlaceholder( node ) {
return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
editor.addButton( 'wp_img_remove', {
icon: 'dashicon dashicons-no',
removeImage( editor.selection.getNode() );
editor.addButton( 'wp_img_edit', {
tooltip: 'Edit|button', // '|button' is not displayed, only used for context.
icon: 'dashicon dashicons-edit',
editImage( editor.selection.getNode() );
aligncenter: 'Align center',
alignright: 'Align right',
alignnone: 'No alignment'
}, function( tooltip, name ) {
var direction = name.slice( 5 );
editor.addButton( 'wp_img_' + name, {
icon: 'dashicon dashicons-align-' + direction,
cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
onPostRender: function() {
editor.on( 'NodeChange', function( event ) {
if ( event.element.nodeName !== 'IMG' ) {
node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
if ( 'alignnone' === name ) {
self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
self.active( editor.dom.hasClass( node, name ) );
editor.once( 'preinit', function() {
if ( editor.wp && editor.wp._createToolbar ) {
toolbar = editor.wp._createToolbar( [
editor.on( 'wptoolbar', function( event ) {
if ( event.element.nodeName === 'IMG' && ! isPlaceholder( event.element ) ) {
function isNonEditable( node ) {
var parent = editor.$( node ).parents( '[contenteditable]' );
return parent && parent.attr( 'contenteditable' ) === 'false';
// Safari on iOS fails to select images in contentEditoble mode on touch.
editor.on( 'init', function() {
editor.on( 'touchstart', function( event ) {
if ( event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
editor.dom.bind( editor.getDoc(), 'touchmove', function() {
editor.on( 'touchend', function( event ) {
if ( touchOnImage && event.target.nodeName === 'IMG' && ! isNonEditable( event.target ) ) {
window.setTimeout( function() {
editor.selection.select( node );
function parseShortcode( content ) {
return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
var id, align, classes, caption, img, width;
id = b.match( /id=['"]([^'"]*)['"] ?/ );
b = b.replace( id[0], '' );
align = b.match( /align=['"]([^'"]*)['"] ?/ );
b = b.replace( align[0], '' );
classes = b.match( /class=['"]([^'"]*)['"] ?/ );
b = b.replace( classes[0], '' );
width = b.match( /width=['"]([0-9]*)['"] ?/ );
b = b.replace( width[0], '' );
img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
caption = trim( img[2] );
// Old captions shortcode style.
caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
align = ( align && align[1] ) ? align[1] : 'alignnone';
classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
width = img.match( /width=['"]([0-9]*)['"]/ );
if ( width && width[1] ) {
if ( ! width || ! caption ) {
width = parseInt( width, 10 );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
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">'+ caption +'</dd></dl></div>';
function getShortcode( content ) {
return content.replace( /(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g, function( all, dl ) {
if ( dl.indexOf('<img ') === -1 || dl.indexOf('</p>') !== -1 ) {
// Broken caption. The user managed to drag the image out or type in the wrapper div?
// Remove the <dl>, <dd> and <dt> and return the remaining text.
return dl.replace( /<d[ldt]( [^>]+)?>/g, '' ).replace( /<\/d[ldt]>/g, '' );
out = dl.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
var id, classes, align, width;
width = c.match( /width="([0-9]*)"/ );
width = ( width && width[1] ) ? width[1] : '';
classes = b.match( /class="([^"]*)"/ );
classes = ( classes && classes[1] ) ? classes[1] : '';
align = classes.match( /align[a-z]+/i ) || 'alignnone';
if ( ! width || ! caption ) {
if ( 'alignnone' !== align[0] ) {
c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
id = b.match( /id="([^"]*)"/ );
id = ( id && id[1] ) ? id[1] : '';
classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
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.
return a.replace( /[\r\n\t]+/, ' ' );
// Convert remaining line breaks to <br>.
caption = caption.replace( /\s*\n\s*/g, '<br />' );
return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
if ( out.indexOf('[caption') === -1 ) {
// The caption html seems broken, try to find the image that may be wrapped in a link
// and may be followed by <p> with the caption text.
out = dl.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
function extractImageData( imageNode ) {
var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
metadata.url = dom.getAttrib( imageNode, 'src' );
metadata.alt = dom.getAttrib( imageNode, 'alt' );
metadata.title = dom.getAttrib( imageNode, 'title' );
width = dom.getAttrib( imageNode, 'width' );
height = dom.getAttrib( imageNode, 'height' );
if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
width = imageNode.naturalWidth || imageNode.width;
if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
height = imageNode.naturalHeight || imageNode.height;
metadata.customWidth = metadata.width = width;
metadata.customHeight = metadata.height = height;
classes = tinymce.explode( imageNode.className, ' ' );
tinymce.each( classes, function( name ) {
if ( /^wp-image/.test( name ) ) {
metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
} else if ( /^align/.test( name ) ) {
metadata.align = name.replace( 'align', '' );
} else if ( /^size/.test( name ) ) {
metadata.size = name.replace( 'size-', '' );
extraClasses.push( name );
metadata.extraClasses = extraClasses.join( ' ' );
captionBlock = dom.getParents( imageNode, '.wp-caption' );
if ( captionBlock.length ) {
captionBlock = captionBlock[0];
classes = captionBlock.className.split( ' ' );
tinymce.each( classes, function( name ) {
if ( /^align/.test( name ) ) {
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 );
metadata.caption = editor.serializer.serialize( caption )
.replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
link = imageNode.parentNode;
metadata.linkUrl = dom.getAttrib( link, 'href' );
metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
metadata.linkRel = dom.getAttrib( link, 'rel' );
metadata.linkClassName = link.className;
function hasTextContent( node ) {
return node && !! ( node.textContent || node.innerText ).replace( /\ufeff/g, '' );
// Verify HTML in captions.
function verifyHTML( caption ) {
if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
serializer = new tinymce.html.Serializer( {}, editor.schema );
return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
function updateImage( $imageNode, imageData ) {
var classes, className, node, html, parent, wrap, linkNode, imageNode,
captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
if ( ! $imageNode || ! $imageNode.length ) {
imageNode = $imageNode[0];
classes = tinymce.explode( imageData.extraClasses, ' ' );
if ( ! imageData.caption ) {
classes.push( 'align' + imageData.align );
if ( imageData.attachment_id ) {
classes.push( 'wp-image-' + imageData.attachment_id );
if ( imageData.size && imageData.size !== 'custom' ) {
classes.push( 'size-' + imageData.size );
height = imageData.height;
if ( imageData.size === 'custom' ) {
width = imageData.customWidth;
height = imageData.customHeight;
title: imageData.title || null,
'class': classes.join( ' ' ) || null
dom.setAttribs( imageNode, attrs );
// Preserve empty alt attributes.
$imageNode.attr( 'alt', imageData.alt || '' );
rel: imageData.linkRel || null,
target: imageData.linkTargetBlank ? '_blank': null,
'class': imageData.linkClassName || null
if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
// Update or remove an existing link wrapped around the image.
if ( imageData.linkUrl ) {
dom.setAttribs( imageNode.parentNode, linkAttrs );
dom.remove( imageNode.parentNode, true );
} else if ( imageData.linkUrl ) {
if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
// The image is inside a link together with other nodes,
// or is nested in another node, move it out.
dom.insertAfter( imageNode, linkNode );
// Add link wrapped around the image.
linkNode = dom.create( 'a', linkAttrs );
imageNode.parentNode.insertBefore( linkNode, imageNode );
linkNode.appendChild( imageNode );
captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
node = imageNode.parentNode;
if ( imageData.caption ) {
imageData.caption = verifyHTML( imageData.caption );
id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
align = 'align' + ( imageData.align || 'none' );
className = 'wp-caption ' + align;
if ( imageData.captionClassName ) {
className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
width = parseInt( width, 10 );
dl = dom.select( 'dl.wp-caption', captionNode );
style: 'width: ' + width + 'px'
dd = dom.select( '.wp-caption-dd', captionNode );
dom.setHTML( dd[0], imageData.caption );
id = id ? 'id="'+ id +'" ' : '';
// Should create a new function for generating the caption markup.
html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
'<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
if ( parent = dom.getParent( node, 'p' ) ) {
parent.parentNode.insertBefore( wrap, parent );
node.parentNode.insertBefore( wrap, node );
editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
if ( parent && dom.isEmpty( parent ) ) {
} else if ( captionNode ) {
// Remove the caption wrapper and place the image in new paragraph.
parent = dom.create( 'p' );
captionNode.parentNode.insertBefore( parent, captionNode );
parent.appendChild( node );
dom.remove( captionNode );
$imageNode = editor.$( imageNode );
srcset = $imageNode.attr( 'srcset' );
src = $imageNode.attr( 'src' );
// Remove srcset and sizes if the image file was edited or the image was replaced.