2 tinymce.PluginManager.add( 'wpeditimage', function( editor ) {
3 var toolbar, serializer,
7 function isPlaceholder( node ) {
8 return !! ( editor.dom.getAttrib( node, 'data-mce-placeholder' ) || editor.dom.getAttrib( node, 'data-mce-object' ) );
11 editor.addButton( 'wp_img_remove', {
13 icon: 'dashicon dashicons-no',
15 removeImage( editor.selection.getNode() );
19 editor.addButton( 'wp_img_edit', {
20 tooltip: 'Edit ', // trailing space is needed, used for context
21 icon: 'dashicon dashicons-edit',
23 editImage( editor.selection.getNode() );
28 alignleft: 'Align left',
29 aligncenter: 'Align center',
30 alignright: 'Align right',
31 alignnone: 'No alignment'
32 }, function( tooltip, name ) {
33 var direction = name.slice( 5 );
35 editor.addButton( 'wp_img_' + name, {
37 icon: 'dashicon dashicons-align-' + direction,
38 cmd: 'alignnone' === name ? 'wpAlignNone' : 'Justify' + direction.slice( 0, 1 ).toUpperCase() + direction.slice( 1 ),
39 onPostRender: function() {
42 editor.on( 'NodeChange', function( event ) {
46 if ( event.element.nodeName !== 'IMG' ) {
50 node = editor.dom.getParent( event.element, '.wp-caption' ) || event.element;
52 if ( 'alignnone' === name ) {
53 self.active( ! /\balign(left|center|right)\b/.test( node.className ) );
55 self.active( editor.dom.hasClass( node, name ) );
62 editor.once( 'preinit', function() {
63 toolbar = editor.wp._createToolbar( [
73 editor.on( 'wptoolbar', function( event ) {
74 if ( event.element.nodeName === 'IMG' && ! isPlaceholder( event.element ) ) {
75 event.toolbar = toolbar;
79 // Safari on iOS fails to select image nodes in contentEditoble mode on touch/click.
82 editor.on( 'click', function( event ) {
83 if ( event.target.nodeName === 'IMG' ) {
84 var node = event.target;
86 window.setTimeout( function() {
87 editor.selection.select( node );
96 function parseShortcode( content ) {
97 return content.replace( /(?:<p>)?\[(?:wp_)?caption([^\]]+)\]([\s\S]+?)\[\/(?:wp_)?caption\](?:<\/p>)?/g, function( a, b, c ) {
98 var id, align, classes, caption, img, width,
101 id = b.match( /id=['"]([^'"]*)['"] ?/ );
103 b = b.replace( id[0], '' );
106 align = b.match( /align=['"]([^'"]*)['"] ?/ );
108 b = b.replace( align[0], '' );
111 classes = b.match( /class=['"]([^'"]*)['"] ?/ );
113 b = b.replace( classes[0], '' );
116 width = b.match( /width=['"]([0-9]*)['"] ?/ );
118 b = b.replace( width[0], '' );
122 img = c.match( /((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)([\s\S]*)/i );
124 if ( img && img[2] ) {
125 caption = trim( img[2] );
126 img = trim( img[1] );
128 // old captions shortcode style
129 caption = trim( b ).replace( /caption=['"]/, '' ).replace( /['"]$/, '' );
133 id = ( id && id[1] ) ? id[1].replace( /[<>&]+/g, '' ) : '';
134 align = ( align && align[1] ) ? align[1] : 'alignnone';
135 classes = ( classes && classes[1] ) ? ' ' + classes[1].replace( /[<>&]+/g, '' ) : '';
137 if ( ! width && img ) {
138 width = img.match( /width=['"]([0-9]*)['"]/ );
141 if ( width && width[1] ) {
145 if ( ! width || ! caption ) {
149 width = parseInt( width, 10 );
150 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
154 return '<div class="mceTemp"><dl id="' + id + '" class="wp-caption ' + align + classes + '" style="width: ' + width + 'px">' +
155 '<dt class="wp-caption-dt">'+ img +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl></div>';
159 function getShortcode( content ) {
160 return content.replace( /<div (?:id="attachment_|class="mceTemp)[^>]*>([\s\S]+?)<\/div>/g, function( a, b ) {
163 if ( b.indexOf('<img ') === -1 ) {
164 // Broken caption. The user managed to drag the image out?
165 // Try to return the caption text as a paragraph.
166 out = b.match( /<dd [^>]+>([\s\S]+?)<\/dd>/i );
168 if ( out && out[1] ) {
169 return '<p>' + out[1] + '</p>';
175 out = b.replace( /\s*<dl ([^>]+)>\s*<dt [^>]+>([\s\S]+?)<\/dt>\s*<dd [^>]+>([\s\S]*?)<\/dd>\s*<\/dl>\s*/gi, function( a, b, c, caption ) {
176 var id, classes, align, width;
178 width = c.match( /width="([0-9]*)"/ );
179 width = ( width && width[1] ) ? width[1] : '';
181 classes = b.match( /class="([^"]*)"/ );
182 classes = ( classes && classes[1] ) ? classes[1] : '';
183 align = classes.match( /align[a-z]+/i ) || 'alignnone';
185 if ( ! width || ! caption ) {
186 if ( 'alignnone' !== align[0] ) {
187 c = c.replace( /><img/, ' class="' + align[0] + '"><img' );
192 id = b.match( /id="([^"]*)"/ );
193 id = ( id && id[1] ) ? id[1] : '';
195 classes = classes.replace( /wp-caption ?|align[a-z]+ ?/gi, '' );
198 classes = ' class="' + classes + '"';
201 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
202 // no line breaks inside HTML tags
203 return a.replace( /[\r\n\t]+/, ' ' );
206 // convert remaining line breaks to <br>
207 caption = caption.replace( /\s*\n\s*/g, '<br />' );
209 return '[caption id="' + id + '" align="' + align + '" width="' + width + '"' + classes + ']' + c + ' ' + caption + '[/caption]';
212 if ( out.indexOf('[caption') === -1 ) {
213 // the caption html seems broken, try to find the image that may be wrapped in a link
214 // and may be followed by <p> with the caption text.
215 out = b.replace( /[\s\S]*?((?:<a [^>]+>)?<img [^>]+>(?:<\/a>)?)(<p>[\s\S]*<\/p>)?[\s\S]*/gi, '<p>$1</p>$2' );
222 function extractImageData( imageNode ) {
223 var classes, extraClasses, metadata, captionBlock, caption, link, width, height,
224 captionClassName = [],
226 isIntRegExp = /^\d+$/;
228 // default attributes
230 attachment_id: false,
238 linkTargetBlank: false,
243 metadata.url = dom.getAttrib( imageNode, 'src' );
244 metadata.alt = dom.getAttrib( imageNode, 'alt' );
245 metadata.title = dom.getAttrib( imageNode, 'title' );
247 width = dom.getAttrib( imageNode, 'width' );
248 height = dom.getAttrib( imageNode, 'height' );
250 if ( ! isIntRegExp.test( width ) || parseInt( width, 10 ) < 1 ) {
251 width = imageNode.naturalWidth || imageNode.width;
254 if ( ! isIntRegExp.test( height ) || parseInt( height, 10 ) < 1 ) {
255 height = imageNode.naturalHeight || imageNode.height;
258 metadata.customWidth = metadata.width = width;
259 metadata.customHeight = metadata.height = height;
261 classes = tinymce.explode( imageNode.className, ' ' );
264 tinymce.each( classes, function( name ) {
266 if ( /^wp-image/.test( name ) ) {
267 metadata.attachment_id = parseInt( name.replace( 'wp-image-', '' ), 10 );
268 } else if ( /^align/.test( name ) ) {
269 metadata.align = name.replace( 'align', '' );
270 } else if ( /^size/.test( name ) ) {
271 metadata.size = name.replace( 'size-', '' );
273 extraClasses.push( name );
278 metadata.extraClasses = extraClasses.join( ' ' );
281 captionBlock = dom.getParents( imageNode, '.wp-caption' );
283 if ( captionBlock.length ) {
284 captionBlock = captionBlock[0];
286 classes = captionBlock.className.split( ' ' );
287 tinymce.each( classes, function( name ) {
288 if ( /^align/.test( name ) ) {
289 metadata.align = name.replace( 'align', '' );
290 } else if ( name && name !== 'wp-caption' ) {
291 captionClassName.push( name );
295 metadata.captionClassName = captionClassName.join( ' ' );
297 caption = dom.select( 'dd.wp-caption-dd', captionBlock );
298 if ( caption.length ) {
299 caption = caption[0];
301 metadata.caption = editor.serializer.serialize( caption )
302 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
307 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' ) {
308 link = imageNode.parentNode;
309 metadata.linkUrl = dom.getAttrib( link, 'href' );
310 metadata.linkTargetBlank = dom.getAttrib( link, 'target' ) === '_blank' ? true : false;
311 metadata.linkRel = dom.getAttrib( link, 'rel' );
312 metadata.linkClassName = link.className;
318 function hasTextContent( node ) {
319 return node && !! ( node.textContent || node.innerText );
322 // Verify HTML in captions
323 function verifyHTML( caption ) {
324 if ( ! caption || ( caption.indexOf( '<' ) === -1 && caption.indexOf( '>' ) === -1 ) ) {
328 if ( ! serializer ) {
329 serializer = new tinymce.html.Serializer( {}, editor.schema );
332 return serializer.serialize( editor.parser.parse( caption, { forced_root_block: false } ) );
335 function updateImage( imageNode, imageData ) {
336 var classes, className, node, html, parent, wrap, linkNode,
337 captionNode, dd, dl, id, attrs, linkAttrs, width, height, align,
340 classes = tinymce.explode( imageData.extraClasses, ' ' );
346 if ( ! imageData.caption ) {
347 classes.push( 'align' + imageData.align );
350 if ( imageData.attachment_id ) {
351 classes.push( 'wp-image-' + imageData.attachment_id );
352 if ( imageData.size && imageData.size !== 'custom' ) {
353 classes.push( 'size-' + imageData.size );
357 width = imageData.width;
358 height = imageData.height;
360 if ( imageData.size === 'custom' ) {
361 width = imageData.customWidth;
362 height = imageData.customHeight;
367 width: width || null,
368 height: height || null,
370 title: imageData.title || null,
371 'class': classes.join( ' ' ) || null
374 dom.setAttribs( imageNode, attrs );
377 href: imageData.linkUrl,
378 rel: imageData.linkRel || null,
379 target: imageData.linkTargetBlank ? '_blank': null,
380 'class': imageData.linkClassName || null
383 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
384 // Update or remove an existing link wrapped around the image
385 if ( imageData.linkUrl ) {
386 dom.setAttribs( imageNode.parentNode, linkAttrs );
388 dom.remove( imageNode.parentNode, true );
390 } else if ( imageData.linkUrl ) {
391 if ( linkNode = dom.getParent( imageNode, 'a' ) ) {
392 // The image is inside a link together with other nodes,
393 // or is nested in another node, move it out
394 dom.insertAfter( imageNode, linkNode );
397 // Add link wrapped around the image
398 linkNode = dom.create( 'a', linkAttrs );
399 imageNode.parentNode.insertBefore( linkNode, imageNode );
400 linkNode.appendChild( imageNode );
403 captionNode = editor.dom.getParent( imageNode, '.mceTemp' );
405 if ( imageNode.parentNode && imageNode.parentNode.nodeName === 'A' && ! hasTextContent( imageNode.parentNode ) ) {
406 node = imageNode.parentNode;
411 if ( imageData.caption ) {
412 imageData.caption = verifyHTML( imageData.caption );
414 id = imageData.attachment_id ? 'attachment_' + imageData.attachment_id : null;
415 align = 'align' + ( imageData.align || 'none' );
416 className = 'wp-caption ' + align;
418 if ( imageData.captionClassName ) {
419 className += ' ' + imageData.captionClassName.replace( /[<>&]+/g, '' );
422 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
423 width = parseInt( width, 10 );
428 dl = dom.select( 'dl.wp-caption', captionNode );
431 dom.setAttribs( dl, {
434 style: 'width: ' + width + 'px'
438 dd = dom.select( '.wp-caption-dd', captionNode );
441 dom.setHTML( dd[0], imageData.caption );
445 id = id ? 'id="'+ id +'" ' : '';
447 // should create a new function for generating the caption markup
448 html = '<dl ' + id + 'class="' + className +'" style="width: '+ width +'px">' +
449 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ imageData.caption +'</dd></dl>';
451 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
453 if ( parent = dom.getParent( node, 'p' ) ) {
454 parent.parentNode.insertBefore( wrap, parent );
456 node.parentNode.insertBefore( wrap, node );
459 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
461 if ( parent && dom.isEmpty( parent ) ) {
462 dom.remove( parent );
465 } else if ( captionNode ) {
466 // Remove the caption wrapper and place the image in new paragraph
467 parent = dom.create( 'p' );
468 captionNode.parentNode.insertBefore( parent, captionNode );
469 parent.appendChild( node );
470 dom.remove( captionNode );
473 if ( wp.media.events ) {
474 wp.media.events.trigger( 'editor:image-update', {
481 editor.nodeChanged();
484 function editImage( img ) {
485 var frame, callback, metadata;
487 if ( typeof wp === 'undefined' || ! wp.media ) {
488 editor.execCommand( 'mceImage' );
492 metadata = extractImageData( img );
494 // Manipulate the metadata by reference that is fed into the PostImage model used in the media modal
495 wp.media.events.trigger( 'editor:image-edit', {
503 state: 'image-details',
507 wp.media.events.trigger( 'editor:frame-create', { frame: frame } );
509 callback = function( imageData ) {
511 editor.undoManager.transact( function() {
512 updateImage( img, imageData );
517 frame.state('image-details').on( 'update', callback );
518 frame.state('replace-image').on( 'replace', callback );
519 frame.on( 'close', function() {
527 function removeImage( node ) {
530 if ( node.nodeName === 'DIV' && editor.dom.hasClass( node, 'mceTemp' ) ) {
532 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
533 wrap = editor.dom.getParent( node, 'div.mceTemp' );
537 if ( wrap.nextSibling ) {
538 editor.selection.select( wrap.nextSibling );
539 } else if ( wrap.previousSibling ) {
540 editor.selection.select( wrap.previousSibling );
542 editor.selection.select( wrap.parentNode );
545 editor.selection.collapse( true );
546 editor.dom.remove( wrap );
548 editor.dom.remove( node );
551 editor.nodeChanged();
552 editor.undoManager.add();
555 editor.on( 'init', function() {
556 var dom = editor.dom,
557 captionClass = editor.getParam( 'wpeditimage_html5_captions' ) ? 'html5-captions' : 'html4-captions';
559 dom.addClass( editor.getBody(), captionClass );
561 // Add caption field to the default image dialog
562 editor.on( 'wpLoadImageForm', function( event ) {
563 if ( editor.getParam( 'wpeditimage_disable_captions' ) ) {
574 label: 'Image caption'
577 event.data.splice( event.data.length - 1, 0, captionField );
580 // Fix caption parent width for images added from URL
581 editor.on( 'wpNewImageRefresh', function( event ) {
582 var parent, captionWidth;
584 if ( parent = dom.getParent( event.node, 'dl.wp-caption' ) ) {
585 if ( ! parent.style.width ) {
586 captionWidth = parseInt( event.node.clientWidth, 10 ) + 10;
587 captionWidth = captionWidth ? captionWidth + 'px' : '50%';
588 dom.setStyle( parent, 'width', captionWidth );
593 editor.on( 'wpImageFormSubmit', function( event ) {
594 var data = event.imgData.data,
595 imgNode = event.imgData.node,
596 caption = event.imgData.caption,
600 wrap, parent, node, html, imgId;
602 // Temp image id so we can find the node later
603 data.id = '__wp-temp-img-id';
604 // Cancel the original callback
605 event.imgData.cancel = true;
607 if ( ! data.style ) {
612 // Delete the image and the caption
614 if ( wrap = dom.getParent( imgNode, 'div.mceTemp' ) ) {
616 } else if ( imgNode.parentNode.nodeName === 'A' ) {
617 dom.remove( imgNode.parentNode );
619 dom.remove( imgNode );
622 editor.nodeChanged();
628 caption = caption.replace( /\r\n|\r/g, '\n' ).replace( /<\/?[a-zA-Z0-9]+( [^<>]+)?>/g, function( a ) {
629 // No line breaks inside HTML tags
630 return a.replace( /[\r\n\t]+/, ' ' );
633 // Convert remaining line breaks to <br>
634 caption = caption.replace( /(<br[^>]*>)\s*\n\s*/g, '$1' ).replace( /\s*\n\s*/g, '<br />' );
635 caption = verifyHTML( caption );
639 // New image inserted
640 html = dom.createHTML( 'img', data );
643 node = editor.selection.getNode();
646 captionWidth = parseInt( data.width, 10 );
648 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
652 captionWidth = ' style="width: ' + captionWidth + 'px"';
655 html = '<dl class="wp-caption alignnone"' + captionWidth + '>' +
656 '<dt class="wp-caption-dt">'+ html +'</dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
658 if ( node.nodeName === 'P' ) {
661 parent = dom.getParent( node, 'p' );
664 if ( parent && parent.nodeName === 'P' ) {
665 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
666 parent.parentNode.insertBefore( wrap, parent );
667 editor.selection.select( wrap );
668 editor.nodeChanged();
670 if ( dom.isEmpty( parent ) ) {
671 dom.remove( parent );
674 editor.selection.setContent( '<div class="mceTemp">' + html + '</div>' );
677 editor.selection.setContent( html );
680 // Edit existing image
682 // Store the original image id if any
683 imgId = imgNode.id || null;
684 // Update the image node
685 dom.setAttribs( imgNode, data );
686 wrap = dom.getParent( imgNode, 'dl.wp-caption' );
690 if ( parent = dom.select( 'dd.wp-caption-dd', wrap )[0] ) {
691 parent.innerHTML = caption;
694 if ( imgNode.className ) {
695 captionId = imgNode.className.match( /wp-image-([0-9]+)/ );
696 captionAlign = imgNode.className.match( /align(left|right|center|none)/ );
699 if ( captionAlign ) {
700 captionAlign = captionAlign[0];
701 imgNode.className = imgNode.className.replace( /align(left|right|center|none)/g, '' );
703 captionAlign = 'alignnone';
706 captionAlign = ' class="wp-caption ' + captionAlign + '"';
709 captionId = ' id="attachment_' + captionId[1] + '"';
712 captionWidth = data.width || imgNode.clientWidth;
714 if ( captionWidth ) {
715 captionWidth = parseInt( captionWidth, 10 );
717 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
721 captionWidth = ' style="width: '+ captionWidth +'px"';
724 if ( imgNode.parentNode && imgNode.parentNode.nodeName === 'A' ) {
725 node = imgNode.parentNode;
730 html = '<dl ' + captionId + captionAlign + captionWidth + '>' +
731 '<dt class="wp-caption-dt"></dt><dd class="wp-caption-dd">'+ caption +'</dd></dl>';
733 wrap = dom.create( 'div', { 'class': 'mceTemp' }, html );
735 if ( parent = dom.getParent( node, 'p' ) ) {
736 parent.parentNode.insertBefore( wrap, parent );
738 node.parentNode.insertBefore( wrap, node );
741 editor.$( wrap ).find( 'dt.wp-caption-dt' ).append( node );
743 if ( parent && dom.isEmpty( parent ) ) {
744 dom.remove( parent );
749 // Remove the caption wrapper and place the image in new paragraph
750 if ( imgNode.parentNode.nodeName === 'A' ) {
751 html = dom.getOuterHTML( imgNode.parentNode );
753 html = dom.getOuterHTML( imgNode );
756 parent = dom.create( 'p', {}, html );
757 dom.insertAfter( parent, wrap.parentNode );
758 editor.selection.select( parent );
759 editor.nodeChanged();
760 dom.remove( wrap.parentNode );
765 imgNode = dom.get('__wp-temp-img-id');
766 dom.setAttrib( imgNode, 'id', imgId );
767 event.imgData.node = imgNode;
770 editor.on( 'wpLoadImageData', function( event ) {
772 data = event.imgData.data,
773 imgNode = event.imgData.node;
775 if ( parent = dom.getParent( imgNode, 'dl.wp-caption' ) ) {
776 parent = dom.select( 'dd.wp-caption-dd', parent )[0];
779 data.caption = editor.serializer.serialize( parent )
780 .replace( /<br[^>]*>/g, '$&\n' ).replace( /^<p>/, '' ).replace( /<\/p>$/, '' );
785 dom.bind( editor.getDoc(), 'dragstart', function( event ) {
786 var node = editor.selection.getNode();
788 // Prevent dragging images out of the caption elements
789 if ( node.nodeName === 'IMG' && dom.getParent( node, '.wp-caption' ) ) {
790 event.preventDefault();
794 // Prevent IE11 from making dl.wp-caption resizable
795 if ( tinymce.Env.ie && tinymce.Env.ie > 10 ) {
796 // The 'mscontrolselect' event is supported only in IE11+
797 dom.bind( editor.getBody(), 'mscontrolselect', function( event ) {
798 if ( event.target.nodeName === 'IMG' && dom.getParent( event.target, '.wp-caption' ) ) {
799 // Hide the thick border with resize handles around dl.wp-caption
800 editor.getBody().focus(); // :(
801 } else if ( event.target.nodeName === 'DL' && dom.hasClass( event.target, 'wp-caption' ) ) {
802 // Trigger the thick border with resize handles...
803 // This will make the caption text editable.
804 event.target.focus();
810 editor.on( 'ObjectResized', function( event ) {
811 var node = event.target;
813 if ( node.nodeName === 'IMG' ) {
814 editor.undoManager.transact( function() {
818 node.className = node.className.replace( /\bsize-[^ ]+/, '' );
820 if ( parent = dom.getParent( node, '.wp-caption' ) ) {
821 width = event.width || dom.getAttrib( node, 'width' );
824 width = parseInt( width, 10 );
826 if ( ! editor.getParam( 'wpeditimage_html5_captions' ) ) {
830 dom.setStyle( parent, 'width', width + 'px' );
837 editor.on( 'BeforeExecCommand', function( event ) {
838 var node, p, DL, align, replacement,
842 if ( cmd === 'mceInsertContent' ) {
843 // When inserting content, if the caret is inside a caption create new paragraph under
844 // and move the caret there
845 if ( node = dom.getParent( editor.selection.getNode(), 'div.mceTemp' ) ) {
846 p = dom.create( 'p' );
847 dom.insertAfter( p, node );
848 editor.selection.setCursorLocation( p, 0 );
849 editor.nodeChanged();
851 } else if ( cmd === 'JustifyLeft' || cmd === 'JustifyRight' || cmd === 'JustifyCenter' || cmd === 'wpAlignNone' ) {
852 node = editor.selection.getNode();
853 align = 'align' + cmd.slice( 7 ).toLowerCase();
854 DL = editor.dom.getParent( node, '.wp-caption' );
856 if ( node.nodeName !== 'IMG' && ! DL ) {
862 if ( editor.dom.hasClass( node, align ) ) {
863 replacement = ' alignnone';
865 replacement = ' ' + align;
868 node.className = node.className.replace( / ?align(left|center|right|none)/g, '' ) + replacement;
870 editor.nodeChanged();
871 event.preventDefault();
874 toolbar.reposition();
877 editor.fire( 'ExecCommand', {
885 editor.on( 'keydown', function( event ) {
886 var node, wrap, P, spacer,
887 selection = editor.selection,
888 keyCode = event.keyCode,
890 VK = tinymce.util.VK;
892 if ( keyCode === VK.ENTER ) {
893 // When pressing Enter inside a caption move the caret to a new parapraph under it
894 node = selection.getNode();
895 wrap = dom.getParent( node, 'div.mceTemp' );
898 dom.events.cancel( event ); // Doesn't cancel all :(
900 // Remove any extra dt and dd cleated on pressing Enter...
901 tinymce.each( dom.select( 'dt, dd', wrap ), function( element ) {
902 if ( dom.isEmpty( element ) ) {
903 dom.remove( element );
907 spacer = tinymce.Env.ie && tinymce.Env.ie < 11 ? '' : '<br data-mce-bogus="1" />';
908 P = dom.create( 'p', null, spacer );
910 if ( node.nodeName === 'DD' ) {
911 dom.insertAfter( P, wrap );
913 wrap.parentNode.insertBefore( P, wrap );
916 editor.nodeChanged();
917 selection.setCursorLocation( P, 0 );
919 } else if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
920 node = selection.getNode();
922 if ( node.nodeName === 'DIV' && dom.hasClass( node, 'mceTemp' ) ) {
924 } else if ( node.nodeName === 'IMG' || node.nodeName === 'DT' || node.nodeName === 'A' ) {
925 wrap = dom.getParent( node, 'div.mceTemp' );
929 dom.events.cancel( event );
936 // After undo/redo FF seems to set the image height very slowly when it is set to 'auto' in the CSS.
937 // This causes image.getBoundingClientRect() to return wrong values and the resize handles are shown in wrong places.
938 // Collapse the selection to remove the resize handles.
939 if ( tinymce.Env.gecko ) {
940 editor.on( 'undo redo', function() {
941 if ( editor.selection.getNode().nodeName === 'IMG' ) {
942 editor.selection.collapse();
947 editor.wpSetImgCaption = function( content ) {
948 return parseShortcode( content );
951 editor.wpGetImgCaption = function( content ) {
952 return getShortcode( content );
955 editor.on( 'BeforeSetContent', function( event ) {
956 if ( event.format !== 'raw' ) {
957 event.content = editor.wpSetImgCaption( event.content );
961 editor.on( 'PostProcess', function( event ) {
963 event.content = editor.wpGetImgCaption( event.content );
968 editor.wp = editor.wp || {};
969 editor.wp.isPlaceholder = isPlaceholder;
973 _do_shcode: parseShortcode,
974 _get_shcode: getShortcode