2 tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
3 var toolbar, serializer, touchOnImage,
8 function isPlaceholder( node ) {
9 return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
12 editor.addButton( 'wp_img_remove', {
14 icon: 'dashicon dashicons-no',
16 removeImage( editor.selection.getNode() );
20 editor.addButton( 'wp_img_edit', {
21 tooltip: 'Edit ', // trailing space is needed, used for context
22 icon: 'dashicon dashicons-edit',
24 editImage( editor.selection.getNode() );
29 alignleft: 'Align left',
30 aligncenter: 'Align center',
31 alignright: 'Align right',
32 alignnone: 'No alignment'
33 }, function( tooltip, name ) {
34 var direction = name.slice( 5 );
36 editor.addButton( 'wp_img_' + name, {
38 icon: 'dashicon dashicons-align-' + direction,
39 cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
40 onPostRender: function() {
43 editor.on( 'NodeChange', function( event ) {
47 if ( event.element.nodeName !== 'IMG' ) {
51 node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
53 if ( 'alignnone' === name ) {
54 self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
56 self.active( editor.dom.hasClass( node, name ) );
63 editor.once( 'preinit', function() {
64 if ( editor.wp && editor.wp._createToolbar ) {
65 toolbar = editor.wp._createToolbar( [
76 editor.on( 'wptoolbar', function( event ) {
77 if ( event.element.nodeName === 'IMG' && ! isPlaceholder( event.element ) ) {
78 event.toolbar = toolbar;
82 // Safari on iOS fails to select images in contentEditoble mode on touch.
85 editor.on( 'init', function() {
86 editor.on( 'touchstart', function( event ) {
87 if ( event.target.nodeName === 'IMG' ) {
92 editor.dom.bind( editor.getDoc(), 'touchmove', function( event ) {
93 if ( event.target.nodeName === 'IMG' ) {
98 editor.on( 'touchend', function( event ) {
99 if ( touchOnImage && event.target.nodeName === 'IMG' ) {
100 var node = event.target;
102 touchOnImage = false;
104 window.setTimeout( function() {
105 editor.selection.select( node );
106 editor.nodeChanged();
108 } else if ( toolbar ) {
115 function parseShortcode( content ) {
116 return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
117 var id, align, classes, caption, img, width;
119 id = b.match( /id=['"]([^'"]*)['"] ?/ );
121 b = b.replace( id[0], '' );
124 align = b.match( /align=['"]([^'"]*)['"] ?/ );
126 b = b.replace( align[0], '' );
129 classes = b.match( /class=['"]([^'"]*)['"] ?/ );
131 b = b.replace( classes[0], '' );
134 width = b.match( /width=['"]([0-9]*)['"] ?/ );
136 b = b.replace( width[0], '' );
140 img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
142 if ( img && img[2] ) {
143 caption = trim( img[2] );
144 img = trim( img[1] );
146 // old captions shortcode style
147 caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
151 id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
152 align = ( align && align[1] ) ? align[1] : 'alignnone';
153 classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
155 if ( ! width && img ) {
156 width = img.match( /width=['"]([0-9]*)['"]/ );
159 if ( width && width[1] ) {
163 if ( ! width || ! caption ) {
167 width = parseInt( width, 10 );
168 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
172 return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
173 '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
177 function getShortcode( content ) {
178 return content.replace( /(?:<div [^>]+mceTemp[^>]+>)?\s*(<dl [^>]+wp-caption[^>]+>[\s\S]+?<\/dl>)\s*(?:<\/div>)?/g, function( all, dl ) {
181 if ( dl.indexOf('<img ') === -1 ) {
182 // Broken caption. The user managed to drag the image out?
183 // Try to return the caption text as a paragraph.
184 out = dl.match( /<dd [^>]+>([\s\S]+?)<\/dd>/i );
186 if ( out && out[1] ) {
187 return '<p>' + out[1] + '</p>';
193 out = dl.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
194 var id, classes, align, width;
196 width = c.match( /width="([0-9]*)"/ );
197 width = ( width && width[1] ) ? width[1] : '';
199 classes = b.match( /class="([^"]*)"/ );
200 classes = ( classes && classes[1] ) ? classes[1] : '';
201 align = classes.match( /align[a-z]+/i ) || 'alignnone';
203 if ( ! width || ! caption ) {
204 if ( 'alignnone' !== align[0] ) {
205 c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
210 id = b.match( /id="([^"]*)"/ );
211 id = ( id && id[1] ) ? id[1] : '';
213 classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
216 classes = ' class="' + classes + '"';
219 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
220 // no line breaks inside HTML tags
221 return a.replace( /[\r\n\t]+/, ' ' );
224 // convert remaining line breaks to <br>
225 caption = caption.replace( /\s*\n\s*/g, '<br />' );
227 return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
230 if ( out.indexOf('[caption') === -1 ) {
231 // the caption html seems broken, try to find the image that may be wrapped in a link
232 // and may be followed by <p> with the caption text.
233 out = dl.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
240 function extractImageData( imageNode ) {
241 var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
242 captionClassName = [],
244 isIntRegExp = /^\d+$/;
246 // default attributes
248 attachment_id: false,
256 linkTargetBlank: false,
261 metadata.url = dom.getAttrib( imageNode, 'src' );
262 metadata.alt = dom.getAttrib( imageNode, 'alt' );
263 metadata.title = dom.getAttrib( imageNode, 'title' );
265 width = dom.getAttrib( imageNode, 'width' );
266 height = dom.getAttrib( imageNode, 'height' );
268 if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
269 width = imageNode.naturalWidth || imageNode.width;
272 if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
273 height = imageNode.naturalHeight || imageNode.height;
276 metadata.customWidth = metadata.width = width;
277 metadata.customHeight = metadata.height = height;
279 classes = tinymce.explode( imageNode.className, ' ' );
282 tinymce.each( classes, function( name ) {
284 if ( /^wp-image/.test( name ) ) {
285 metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
286 } else if ( /^align/.test( name ) ) {
287 metadata.align = name.replace( 'align', '' );
288 } else if ( /^size/.test( name ) ) {
289 metadata.size = name.replace( 'size-', '' );
291 extraClasses.push( name );
296 metadata.extraClasses = extraClasses.join( ' ' );
299 captionBlock = dom.getParents( imageNode, '.wp-caption' );
301 if ( captionBlock.length ) {
302 captionBlock = captionBlock[0];
304 classes = captionBlock.className.split( ' ' );
305 tinymce.each( classes, function( name ) {
306 if ( /^align/.test( name ) ) {
307 metadata.align = name.replace( 'align', '' );
308 } else if ( name && name !== 'wp-caption' ) {
309 captionClassName.push( name );
313 metadata.captionClassName = captionClassName.join( ' ' );
315 caption = dom.select( 'dd.wp-caption-dd', captionBlock );
316 if ( caption.length ) {
317 caption = caption[0];
319 metadata.caption = editor.serializer.serialize( caption )
320 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
325 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
326 link = imageNode.parentNode;
327 metadata.linkUrl = dom.getAttrib( link, 'href' );
328 metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
329 metadata.linkRel = dom.getAttrib( link, 'rel' );
330 metadata.linkClassName = link.className;
336 function hasTextContent( node ) {
337 return node && !! ( node.textContent || node.innerText );
340 // Verify HTML in captions
341 function verifyHTML( caption ) {
342 if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
346 if ( ! serializer ) {
347 serializer = new tinymce.html.Serializer( {}, editor.schema );
350 return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
353 function updateImage( imageNode, imageData ) {
354 var classes, className, node, html, parent, wrap, linkNode,
355 captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
356 $imageNode, srcset, src,
359 classes = tinymce.explode( imageData.extraClasses, ' ' );
365 if ( ! imageData.caption ) {
366 classes.push( 'align' + imageData.align );
369 if ( imageData.attachment_id ) {
370 classes.push( 'wp-image-' + imageData.attachment_id );
371 if ( imageData.size && imageData.size !== 'custom' ) {
372 classes.push( 'size-' + imageData.size );
376 width = imageData.width;
377 height = imageData.height;
379 if ( imageData.size === 'custom' ) {
380 width = imageData.customWidth;
381 height = imageData.customHeight;
386 width: width || null,
387 height: height || null,
389 title: imageData.title || null,
390 'class': classes.join( ' ' ) || null
393 dom.setAttribs( imageNode, attrs );
396 href: imageData.linkUrl,
397 rel: imageData.linkRel || null,
398 target: imageData.linkTargetBlank ? '_blank': null,
399 'class': imageData.linkClassName || null
402 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
403 // Update or remove an existing link wrapped around the image
404 if ( imageData.linkUrl ) {
405 dom.setAttribs( imageNode.parentNode, linkAttrs );
407 dom.remove( imageNode.parentNode, true );
409 } else if ( imageData.linkUrl ) {
410 if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
411 // The image is inside a link together with other nodes,
412 // or is nested in another node, move it out
413 dom.insertAfter( imageNode, linkNode );
416 // Add link wrapped around the image
417 linkNode = dom.create( 'a', linkAttrs );
418 imageNode.parentNode.insertBefore( linkNode, imageNode );
419 linkNode.appendChild( imageNode );
422 captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
424 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
425 node = imageNode.parentNode;
430 if ( imageData.caption ) {
431 imageData.caption = verifyHTML( imageData.caption );
433 id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
434 align = 'align' + ( imageData.align || 'none' );
435 className = 'wp-caption ' + align;
437 if ( imageData.captionClassName ) {
438 className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
441 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
442 width = parseInt( width, 10 );
447 dl = dom.select( 'dl.wp-caption', captionNode );
450 dom.setAttribs( dl, {
453 style: 'width: ' + width + 'px'
457 dd = dom.select( '.wp-caption-dd', captionNode );
460 dom.setHTML( dd[0], imageData.caption );
464 id = id ? 'id="'+ id +'" ' : '';
466 // should create a new function for generating the caption markup
467 html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
468 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
470 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
472 if ( parent = dom.getParent( node, 'p' ) ) {
473 parent.parentNode.insertBefore( wrap, parent );
475 node.parentNode.insertBefore( wrap, node );
478 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
480 if ( parent && dom.isEmpty( parent ) ) {
481 dom.remove( parent );
484 } else if ( captionNode ) {
485 // Remove the caption wrapper and place the image in new paragraph
486 parent = dom.create( 'p' );
487 captionNode.parentNode.insertBefore( parent, captionNode );
488 parent.appendChild( node );
489 dom.remove( captionNode );
492 $imageNode = editor.$( imageNode );
493 srcset = $imageNode.attr( 'srcset' );
494 src = $imageNode.attr( 'src' );
496 // Remove srcset and sizes if the image file was edited or the image was replaced.
497 if ( srcset && src ) {
498 src = src.replace( /[?#].*/, '' );
500 if ( srcset.indexOf( src ) === -1 ) {
501 $imageNode.attr( 'srcset', null ).attr( 'sizes', null );
505 if ( wp.media.events ) {
506 wp.media.events.trigger( 'editor:image-update', {
513 editor.nodeChanged();
516 function editImage( img ) {
517 var frame, callback, metadata;
519 if ( typeof wp === 'undefined' || ! wp.media ) {
520 editor.execCommand( 'mceImage' );
524 metadata = extractImageData( img );
526 // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
527 wp.media.events.trigger( 'editor:image-edit', {
535 state: 'image-details',
539 wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
541 callback = function( imageData ) {
543 editor.undoManager.transact( function() {
544 updateImage( img, imageData );
549 frame.state('image-details').on( 'update', callback );
550 frame.state('replace-image').on( 'replace', callback );
551 frame.on( 'close', function() {
559 function removeImage( node ) {
560 var wrap = editor.dom.getParent( node, 'div.mceTemp' );
562 if ( ! wrap && node.nodeName === 'IMG' ) {
563 wrap = editor.dom.getParent( node, 'a' );
567 if ( wrap.nextSibling ) {
568 editor.selection.select( wrap.nextSibling );
569 } else if ( wrap.previousSibling ) {
570 editor.selection.select( wrap.previousSibling );
572 editor.selection.select( wrap.parentNode );
575 editor.selection.collapse( true );
576 editor.dom.remove( wrap );
578 editor.dom.remove( node );
581 editor.nodeChanged();
582 editor.undoManager.add();
585 editor.on( 'init', function() {
586 var dom = editor.dom,
587 captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
589 dom.addClass( editor.getBody(), captionClass );
591 // Add caption field to the default image dialog
592 editor.on( 'wpLoadImageForm', function( event ) {
593 if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
604 label: 'Image caption'
607 event.data.splice( event.data.length - 1, 0, captionField );
610 // Fix caption parent width for images added from URL
611 editor.on( 'wpNewImageRefresh', function( event ) {
612 var parent, captionWidth;
614 if ( parent = dom.getParent( event.node, 'dl.wp-caption' ) ) {
615 if ( ! parent.style.width ) {
616 captionWidth = parseInt( event.node.clientWidth, 10 ) + 10;
617 captionWidth = captionWidth ? captionWidth + 'px' : '50%';
618 dom.setStyle( parent, 'width', captionWidth );
623 editor.on( 'wpImageFormSubmit', function( event ) {
624 var data = event.imgData.data,
625 imgNode = event.imgData.node,
626 caption = event.imgData.wpcaption,
630 wrap, parent, node, html, imgId;
632 // Temp image id so we can find the node later
633 data.id = '__wp-temp-img-id';
634 // Cancel the original callback
635 event.imgData.cancel = true;
637 if ( ! data.style ) {
642 // Delete the image and the caption
644 if ( wrap = dom.getParent( imgNode, 'div.mceTemp' ) ) {
646 } else if ( imgNode.parentNode.nodeName === 'A' ) {
647 dom.remove( imgNode.parentNode );
649 dom.remove( imgNode );
652 editor.nodeChanged();
658 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
659 // No line breaks inside HTML tags
660 return a.replace( /[\r\n\t]+/, ' ' );
663 // Convert remaining line breaks to <br>
664 caption = caption.replace( /(<br[^>]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '<br />' );
665 caption = verifyHTML( caption );
669 // New image inserted
670 html = dom.createHTML( 'img', data );
673 node = editor.selection.getNode();
676 captionWidth = parseInt( data.width, 10 );
678 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
682 captionWidth = ' style="width: ' + captionWidth + 'px"';
685 html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
686 '<dt class="wp-caption-dt">'+ html +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
688 if ( node.nodeName === 'P' ) {
691 parent = dom.getParent( node, 'p' );
694 if ( parent && parent.nodeName === 'P' ) {
695 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
696 parent.parentNode.insertBefore( wrap, parent );
697 editor.selection.select( wrap );
698 editor.nodeChanged();
700 if ( dom.isEmpty( parent ) ) {
701 dom.remove( parent );
704 editor.selection.setContent( '<div class="mceTemp">' + html + '</div>' );
707 editor.selection.setContent( html );
710 // Edit existing image
712 // Store the original image id if any
713 imgId = imgNode.id || null;
714 // Update the image node
715 dom.setAttribs( imgNode, data );
716 wrap = dom.getParent( imgNode, 'dl.wp-caption' );
720 if ( parent = dom.select( 'dd.wp-caption-dd', wrap )[0] ) {
721 parent.innerHTML = caption;
724 if ( imgNode.className ) {
725 captionId = imgNode.className.match( /wp-image-([0-9]+)/ );
726 captionAlign = imgNode.className.match( /align(left|right|center|none)/ );
729 if ( captionAlign ) {
730 captionAlign = captionAlign[0];
731 imgNode.className = imgNode.className.replace( /align(left|right|center|none)/g, '' );
733 captionAlign = 'alignnone';
736 captionAlign = ' class="wp-caption ' + captionAlign + '"';
739 captionId = ' id="attachment_' + captionId[1] + '"';
742 captionWidth = data.width || imgNode.clientWidth;
744 if ( captionWidth ) {
745 captionWidth = parseInt( captionWidth, 10 );
747 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
751 captionWidth = ' style="width: '+ captionWidth +'px"';
754 if ( imgNode.parentNode && imgNode.parentNode.nodeName === 'A' ) {
755 node = imgNode.parentNode;
760 html = '<dl ' + captionId + captionAlign + captionWidth + '>' +
761 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
763 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
765 if ( parent = dom.getParent( node, 'p' ) ) {
766 parent.parentNode.insertBefore( wrap, parent );
768 node.parentNode.insertBefore( wrap, node );
771 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
773 if ( parent && dom.isEmpty( parent ) ) {
774 dom.remove( parent );
779 // Remove the caption wrapper and place the image in new paragraph
780 if ( imgNode.parentNode.nodeName === 'A' ) {
781 html = dom.getOuterHTML( imgNode.parentNode );
783 html = dom.getOuterHTML( imgNode );
786 parent = dom.create( 'p', {}, html );
787 dom.insertAfter( parent, wrap.parentNode );
788 editor.selection.select( parent );
789 editor.nodeChanged();
790 dom.remove( wrap.parentNode );
795 imgNode = dom.get('__wp-temp-img-id');
796 dom.setAttrib( imgNode, 'id', imgId );
797 event.imgData.node = imgNode;
800 editor.on( 'wpLoadImageData', function( event ) {
802 data = event.imgData.data,
803 imgNode = event.imgData.node;
805 if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) {
806 parent = dom.select( 'dd.wp-caption-dd', parent )[0];
809 data.wpcaption = editor.serializer.serialize( parent )
810 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
815 // Prevent IE11 from making dl.wp-caption resizable
816 if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
817 // The 'mscontrolselect' event is supported only in IE11+
818 dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
819 if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
820 // Hide the thick border with resize handles around dl.wp-caption
821 editor.getBody().focus(); // :(
822 } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
823 // Trigger the thick border with resize handles...
824 // This will make the caption text editable.
825 event.target.focus();
831 editor.on( 'ObjectResized', function( event ) {
832 var node = event.target;
834 if ( node.nodeName === 'IMG' ) {
835 editor.undoManager.transact( function() {
839 node.className = node.className.replace( /\bsize-[^ ]+/, '' );
841 if ( parent = dom.getParent( node, '.wp-caption' ) ) {
842 width = event.width || dom.getAttrib( node, 'width' );
845 width = parseInt( width, 10 );
847 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
851 dom.setStyle( parent, 'width', width + 'px' );
858 editor.on( 'BeforeExecCommand', function( event ) {
859 var node, p, DL, align, replacement,
863 if ( cmd === 'mceInsertContent' ) {
864 // When inserting content, if the caret is inside a caption create new paragraph under
865 // and move the caret there
866 if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
867 p = dom.create( 'p' );
868 dom.insertAfter( p, node );
869 editor.selection.setCursorLocation( p, 0 );
870 editor.nodeChanged();
872 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
873 node = editor.selection.getNode();
874 align = 'align' + cmd.slice( 7 ).toLowerCase();
875 DL = editor.dom.getParent( node, '.wp-caption' );
877 if ( node.nodeName !== 'IMG' && ! DL ) {
883 if ( editor.dom.hasClass( node, align ) ) {
884 replacement = ' alignnone';
886 replacement = ' ' + align;
889 node.className = trim( node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement );
891 editor.nodeChanged();
892 event.preventDefault();
895 toolbar.reposition();
898 editor.fire( 'ExecCommand', {
906 editor.on( 'keydown', function( event ) {
907 var node, wrap, P, spacer,
908 selection = editor.selection,
909 keyCode = event.keyCode,
911 VK = tinymce.util.VK;
913 if ( keyCode === VK.ENTER ) {
914 // When pressing Enter inside a caption move the caret to a new parapraph under it
915 node = selection.getNode();
916 wrap = dom.getParent( node, 'div.mceTemp' );
919 dom.events.cancel( event ); // Doesn't cancel all :(
921 // Remove any extra dt and dd cleated on pressing Enter...
922 tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
923 if ( dom.isEmpty( element ) ) {
924 dom.remove( element );
928 spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
929 P = dom.create( 'p', null, spacer );
931 if ( node.nodeName === 'DD' ) {
932 dom.insertAfter( P, wrap );
934 wrap.parentNode.insertBefore( P, wrap );
937 editor.nodeChanged();
938 selection.setCursorLocation( P, 0 );
940 } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
941 node = selection.getNode();
943 if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
945 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
946 wrap = dom.getParent( node, 'div.mceTemp' );
950 dom.events.cancel( event );
957 // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
958 // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
959 // Collapse the selection to remove the resize handles.
960 if ( tinymce.Env.gecko ) {
961 editor.on( 'undo redo', function() {
962 if ( editor.selection.getNode().nodeName === 'IMG' ) {
963 editor.selection.collapse();
968 editor.wpSetImgCaption = function( content ) {
969 return parseShortcode( content );
972 editor.wpGetImgCaption = function( content ) {
973 return getShortcode( content );
976 editor.on( 'BeforeSetContent', function( event ) {
977 if ( event.format !== 'raw' ) {
978 event.content = editor.wpSetImgCaption( event.content );
982 editor.on( 'PostProcess', function( event ) {
984 event.content = editor.wpGetImgCaption( event.content );
991 editor.on( 'dragstart', function() {
992 var node = editor.selection.getNode();
994 if ( node.nodeName === 'IMG' ) {
995 wrap = editor.dom.getParent( node, '.mceTemp' );
997 if ( ! wrap && node.parentNode.nodeName === 'A' && ! hasTextContent( node.parentNode ) ) {
998 wrap = node.parentNode;
1003 editor.on( 'drop', function( event ) {
1004 var dom = editor.dom,
1005 rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint( event.clientX, event.clientY, editor.getDoc() );
1007 // Don't allow anything to be dropped in a captioned image.
1008 if ( rng && dom.getParent( rng.startContainer, '.mceTemp' ) ) {
1009 event.preventDefault();
1010 } else if ( wrap ) {
1011 event.preventDefault();
1013 editor.undoManager.transact( function() {
1015 editor.selection.setRng( rng );
1018 editor.selection.setNode( wrap );
1028 editor.wp = editor.wp || {};
1029 editor.wp.isPlaceholder = isPlaceholder;
1033 _do_shcode: parseShortcode,
1034 _get_shcode: getShortcode