<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[41645] trunk/src/wp-admin/js/editor.js: Editor: Improve keeping text selection when switching between Visual and Text modes.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/41645">41645</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/41645","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2017-09-29 17:49:43 +0000 (Fri, 29 Sep 2017)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Editor: Improve keeping text selection when switching between Visual and Text modes.

Props biskobe.
See <a href="https://core.trac.wordpress.org/ticket/42029">#42029</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjseditorjs">trunk/src/wp-admin/js/editor.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjseditorjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/editor.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/editor.js   2017-09-29 15:09:39 UTC (rev 41644)
+++ trunk/src/wp-admin/js/editor.js     2017-09-29 17:49:43 UTC (rev 41645)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -133,10 +133,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                         */
</span><span class="cx" style="display: block; padding: 0 10px">                                        var tinyMCEConfig = $.extend(
</span><span class="cx" style="display: block; padding: 0 10px">                                                {},
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                window.tinyMCEPreInit.mceInit[id],
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         window.tinyMCEPreInit.mceInit[ id ],
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                        setup: function(editor) {
-                                                               editor.on('init', function(event) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                                 setup: function( editor ) {
+                                                               editor.on( 'init', function( event ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                                                         focusHTMLBookmarkInVisualEditor( event.target );
</span><span class="cx" style="display: block; padding: 0 10px">                                                                });
</span><span class="cx" style="display: block; padding: 0 10px">                                                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -210,72 +210,156 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * @returns {(null|Object)} Null if cursor is not in a tag, Object if the cursor is inside a tag.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                function getContainingTagInfo( content, cursorPosition ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var lastLtPos = content.lastIndexOf( '<', cursorPosition ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var lastLtPos = content.lastIndexOf( '<', cursorPosition - 1 ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 lastGtPos = content.lastIndexOf( '>', cursorPosition );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( lastLtPos > lastGtPos || content.substr( cursorPosition, 1 ) === '>' ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // find what the tag is
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                var tagContent = content.substr( lastLtPos );
-                               var tagMatch = tagContent.match( /<\s*(\/)?(\w+)/ );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         var tagContent = content.substr( lastLtPos ),
+                                       tagMatch = tagContent.match( /<\s*(\/)?(\w+)/ );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( ! tagMatch ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        return null;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                var tagType = tagMatch[ 2 ];
-                               var closingGt = tagContent.indexOf( '>' );
-                               var isClosingTag = ! ! tagMatch[ 1 ];
-                               var shortcodeWrapperInfo = getShortcodeWrapperInfo( content, lastLtPos );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         var tagType = tagMatch[2],
+                                       closingGt = tagContent.indexOf( '>' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                return {
</span><span class="cx" style="display: block; padding: 0 10px">                                        ltPos: lastLtPos,
</span><span class="cx" style="display: block; padding: 0 10px">                                        gtPos: lastLtPos + closingGt + 1, // offset by one to get the position _after_ the character,
</span><span class="cx" style="display: block; padding: 0 10px">                                        tagType: tagType,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        isClosingTag: isClosingTag,
-                                       shortcodeTagInfo: shortcodeWrapperInfo
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 isClosingTag: !! tagMatch[1]
</ins><span class="cx" style="display: block; padding: 0 10px">                                 };
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        return null;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @summary Check if a given HTML tag is enclosed in a shortcode tag
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @summary Check if the cursor is inside a shortcode
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><span class="cx" style="display: block; padding: 0 10px">                 * If the cursor is inside a shortcode wrapping tag, e.g. `[caption]` it's better to
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * move the selection marker to before the short tag.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * move the selection marker to before or after the shortcode.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><span class="cx" style="display: block; padding: 0 10px">                 * For example `[caption]` rewrites/removes anything that's between the `[caption]` tag and the
</span><span class="cx" style="display: block; padding: 0 10px">                 * `<img/>` tag inside.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 *      `[caption]<span>ThisIsGone</span><img .../>[caption]`
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * `[caption]<span>ThisIsGone</span><img .../>[caption]`
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 *      Moving the selection to before the short code is better, since it allows to select
-                *      something, instead of just losing focus and going to the start of the content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Moving the selection to before or after the short code is better, since it allows to select
+                * something, instead of just losing focus and going to the start of the content.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 *      @param {string} content The text content to check against
-                *      @param {number} cursorPosition  The cursor position to check from. Usually this is the opening symbol of
-                *                                                                      an HTML tag.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @param {string} content The text content to check against.
+                * @param {number} cursorPosition    The cursor position to check.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @return {(null|Object)}      Null if the oject is not wrapped in a shortcode tag.
-                *                                                      Information about the wrapping shortcode tag if it's wrapped in one.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @return {(undefined|Object)} Undefined if the cursor is not wrapped in a shortcode tag.
+                *                                Information about the wrapping shortcode tag if it's wrapped in one.
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><span class="cx" style="display: block; padding: 0 10px">                function getShortcodeWrapperInfo( content, cursorPosition ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( content.substr( cursorPosition - 1, 1 ) === ']' ) {
-                               var shortTagStart = content.lastIndexOf( '[', cursorPosition );
-                               var shortTagContent = content.substr(shortTagStart, cursorPosition - shortTagStart);
-                               var shortTag = content.match( /\[\s*(\/)?(\w+)/ );
-                               var tagType = shortTag[ 2 ];
-                               var closingGt = shortTagContent.indexOf( '>' );
-                               var isClosingTag = ! ! shortTag[ 1 ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var contentShortcodes = getShortCodePositionsInText( content );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                return {
-                                       openingBracket: shortTagStart,
-                                       shortcode: tagType,
-                                       closingBracket: closingGt,
-                                       isClosingTag: isClosingTag
-                               };
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return _.find( contentShortcodes, function( element ) {
+                               return cursorPosition >= element.startIndex && cursorPosition <= element.endIndex;
+                       } );
+               }
+
+               /**
+                * Gets a list of unique shortcodes or shortcode-look-alikes in the content.
+                *
+                * @param {string} content The content we want to scan for shortcodes.
+                */
+               function getShortcodesInText( content ) {
+                       var shortcodes = content.match( /\[+([\w_-])+/g );
+
+                       return _.uniq(
+                               _.map( shortcodes, function( element ) {
+                                       return element.replace( /^\[+/g, '' );
+                               } )
+                       );
+               }
+
+               /**
+                * @summary Check if a shortcode has Live Preview enabled for it.
+                *
+                * Previewable shortcodes here refers to shortcodes that have Live Preview enabled.
+                *
+                * These shortcodes get rewritten when the editor is in Visual mode, which means that
+                * we don't want to change anything inside them, i.e. inserting a selection marker
+                * inside the shortcode will break it :(
+                *
+                * @link wp-includes/js/mce-view.js
+                *
+                * @param {string} shortcode The shortcode to check.
+                * @return {boolean} If a shortcode has Live Preview or not
+                */
+               function isShortcodePreviewable( shortcode ) {
+                       var defaultPreviewableShortcodes = [ 'caption' ];
+
+                       return (
+                               defaultPreviewableShortcodes.indexOf( shortcode ) !== -1 ||
+                               wp.mce.views.get( shortcode ) !== undefined
+                       );
+
+               }
+
+               /**
+                * @summary Get all shortcodes and their positions in the content
+                *
+                * This function returns all the shortcodes that could be found in the textarea content
+                * along with their character positions and boundaries.
+                *
+                * This is used to check if the selection cursor is inside the boundaries of a shortcode
+                * and move it accordingly, to avoid breakage.
+                *
+                * @link adjustTextAreaSelectionCursors
+                *
+                * The information can also be used in other cases when we need to lookup shortcode data,
+                * as it's already structured!
+                *
+                * @param {string} content The content we want to scan for shortcodes
+                */
+               function getShortCodePositionsInText( content ) {
+                       var allShortcodes = getShortcodesInText( content );
+
+                       if ( allShortcodes.length === 0 ) {
+                               return [];
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return null;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var shortcodeDetailsRegexp = wp.shortcode.regexp( allShortcodes.join( '|' ) ),
+                               shortcodeMatch, // Define local scope for the variable to be used in the loop below.
+                               shortcodesDetails = [];
+
+                       while ( shortcodeMatch = shortcodeDetailsRegexp.exec( content ) ) {
+                               /**
+                                * Check if the shortcode should be shown as plain text.
+                                *
+                                * This corresponds to the [[shortcode]] syntax, which doesn't parse the shortcode
+                                * and just shows it as text.
+                                */
+                               var showAsPlainText = shortcodeMatch[1] === '[';
+
+                               /**
+                                * For more context check the docs for:
+                                *
+                                * @link isShortcodePreviewable
+                                *
+                                * In addition, if the shortcode will get rendered as plain text ( see above ),
+                                * we can treat it as text and use the selection markers in it.
+                                */
+                               var isPreviewable = ! showAsPlainText && isShortcodePreviewable( shortcodeMatch[2] ),
+                                       shortcodeInfo = {
+                                               shortcodeName: shortcodeMatch[2],
+                                               showAsPlainText: showAsPlainText,
+                                               startIndex: shortcodeMatch.index,
+                                               endIndex: shortcodeMatch.index + shortcodeMatch[0].length,
+                                               length: shortcodeMatch[0].length,
+                                               isPreviewable: isPreviewable
+                                       };
+
+                               shortcodesDetails.push( shortcodeInfo );
+                       }
+
+                       return shortcodesDetails;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -299,27 +383,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @summary Adds text selection markers in the editor textarea.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @summary Get adjusted selection cursor positions according to HTML tags/shortcodes
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * Adds selection markers in the content of the editor `textarea`.
-                * The method directly manipulates the `textarea` content, to allow TinyMCE plugins
-                * to run after the markers are added.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * Shortcodes and HTML codes are a bit of a special case when selecting, since they may render
+                * content in Visual mode. If we insert selection markers somewhere inside them, it's really possible
+                * to break the syntax and render the HTML tag or shortcode broken.
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @param {object} $textarea TinyMCE's textarea wrapped as a DomQuery object
-                * @param {object} jQuery A jQuery instance
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @link getShortcodeWrapperInfo
+                *
+                * @param {string} content Textarea content that the cursors are in
+                * @param {{cursorStart: number, cursorEnd: number}} cursorPositions Cursor start and end positions
+                *
+                * @return {{cursorStart: number, cursorEnd: number}}
</ins><span class="cx" style="display: block; padding: 0 10px">                  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                function addHTMLBookmarkInTextAreaContent( $textarea, jQuery ) {
-                       var textArea = $textarea[ 0 ], // TODO add error checking
-                               htmlModeCursorStartPosition = textArea.selectionStart,
-                               htmlModeCursorEndPosition = textArea.selectionEnd;
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         function adjustTextAreaSelectionCursors( content, cursorPositions ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         var voidElements = [
</span><span class="cx" style="display: block; padding: 0 10px">                                'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input',
</span><span class="cx" style="display: block; padding: 0 10px">                                'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
</span><span class="cx" style="display: block; padding: 0 10px">                        ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // check if the cursor is in a tag and if so, adjust it
-                       var isCursorStartInTag = getContainingTagInfo( textArea.value, htmlModeCursorStartPosition );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var cursorStart = cursorPositions.cursorStart,
+                               cursorEnd = cursorPositions.cursorEnd,
+                               // check if the cursor is in a tag and if so, adjust it
+                               isCursorStartInTag = getContainingTagInfo( content, cursorStart );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( isCursorStartInTag ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                /**
</span><span class="cx" style="display: block; padding: 0 10px">                                 * Only move to the start of the HTML tag (to select the whole element) if the tag
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -334,78 +421,74 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                 * In cases where the tag is not a void element, the cursor is put to the end of the tag,
</span><span class="cx" style="display: block; padding: 0 10px">                                 * so it's either between the opening and closing tag elements or after the closing tag.
</span><span class="cx" style="display: block; padding: 0 10px">                                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( voidElements.indexOf( isCursorStartInTag.tagType ) !== - 1 ) {
-                                       htmlModeCursorStartPosition = isCursorStartInTag.ltPos;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( voidElements.indexOf( isCursorStartInTag.tagType ) !== -1 ) {
+                                       cursorStart = isCursorStartInTag.ltPos;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                                else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        htmlModeCursorStartPosition = isCursorStartInTag.gtPos;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 cursorStart = isCursorStartInTag.gtPos;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var isCursorEndInTag = getContainingTagInfo( textArea.value, htmlModeCursorEndPosition );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var isCursorEndInTag = getContainingTagInfo( content, cursorEnd );
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( isCursorEndInTag ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                htmlModeCursorEndPosition = isCursorEndInTag.gtPos;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         cursorEnd = isCursorEndInTag.gtPos;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var mode = htmlModeCursorStartPosition !== htmlModeCursorEndPosition ? 'range' : 'single';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var isCursorStartInShortcode = getShortcodeWrapperInfo( content, cursorStart );
+                       if ( isCursorStartInShortcode && isCursorStartInShortcode.isPreviewable ) {
+                               cursorStart = isCursorStartInShortcode.startIndex;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var selectedText = null;
-                       var cursorMarkerSkeleton = getCursorMarkerSpan( { $: jQuery }, '&#65279;' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var isCursorEndInShortcode = getShortcodeWrapperInfo( content, cursorEnd );
+                       if ( isCursorEndInShortcode && isCursorEndInShortcode.isPreviewable ) {
+                               cursorEnd = isCursorEndInShortcode.endIndex;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( mode === 'range' ) {
-                               var markedText = textArea.value.slice( htmlModeCursorStartPosition, htmlModeCursorEndPosition );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 return {
+                               cursorStart: cursorStart,
+                               cursorEnd: cursorEnd
+                       };
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                /**
-                                * Since the shortcodes convert the tags in them a bit, we need to mark the tag itself,
-                                * and not rely on the cursor marker.
-                                *
-                                * @see getShortcodeWrapperInfo
-                                */
-                               if ( isCursorStartInTag && isCursorStartInTag.shortcodeTagInfo ) {
-                                       // Get the tag on the cursor start
-                                       var tagEndPosition = isCursorStartInTag.gtPos - isCursorStartInTag.ltPos;
-                                       var tagContent = markedText.slice( 0, tagEndPosition );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /**
+                * @summary Adds text selection markers in the editor textarea.
+                *
+                * Adds selection markers in the content of the editor `textarea`.
+                * The method directly manipulates the `textarea` content, to allow TinyMCE plugins
+                * to run after the markers are added.
+                *
+                * @param {object} $textarea TinyMCE's textarea wrapped as a DomQuery object
+                * @param {object} jQuery A jQuery instance
+                */
+               function addHTMLBookmarkInTextAreaContent( $textarea, jQuery ) {
+                       if ( ! $textarea || ! $textarea.length ) {
+                               // If no valid $textarea object is provided, there's nothing we can do.
+                               return;
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // Check if the tag already has a `class` attribute.
-                                       var classMatch = /class=(['"])([^$1]*?)\1/;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var textArea = $textarea[0],
+                               textAreaContent = textArea.value,
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        /**
-                                        * Add a marker class to the selected tag, to be used later.
-                                        *
-                                        * @see focusHTMLBookmarkInVisualEditor
-                                        */
-                                       if ( tagContent.match( classMatch ) ) {
-                                               tagContent = tagContent.replace( classMatch, 'class=$1$2 mce_SELRES_start_target$1' );
-                                       }
-                                       else {
-                                               tagContent = tagContent.replace( /(<\w+)/, '$1 class="mce_SELRES_start_target" ' );
-                                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         adjustedCursorPositions = adjustTextAreaSelectionCursors( textAreaContent, {
+                                       cursorStart: textArea.selectionStart,
+                                       cursorEnd: textArea.selectionEnd
+                               } ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // Update the selected text content with the marked tag above
-                                       markedText = [
-                                               tagContent,
-                                               markedText.substr( tagEndPosition )
-                                       ].join( '' );
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         htmlModeCursorStartPosition = adjustedCursorPositions.cursorStart,
+                               htmlModeCursorEndPosition = adjustedCursorPositions.cursorEnd,
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                var bookMarkEnd = cursorMarkerSkeleton.clone()
-                                       .addClass( 'mce_SELRES_end' )[ 0 ].outerHTML;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         mode = htmlModeCursorStartPosition !== htmlModeCursorEndPosition ? 'range' : 'single',
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                /**
-                                * A small workaround when selecting just a single HTML tag inside a shortcode.
-                                *
-                                * This removes the end selection marker, to make sure the HTML tag is the only selected
-                                * thing. This prevents the selection to appear like it contains multiple items in it (i.e.
-                                * all highlighted blue)
-                                */
-                               if ( isCursorStartInTag && isCursorStartInTag.shortcodeTagInfo && isCursorEndInTag &&
-                                               isCursorStartInTag.ltPos === isCursorEndInTag.ltPos ) {
-                                       bookMarkEnd = '';
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         selectedText = null,
+                               cursorMarkerSkeleton = getCursorMarkerSpan( { $: jQuery }, '&#65279;' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if ( mode === 'range' ) {
+                               var markedText = textArea.value.slice( htmlModeCursorStartPosition, htmlModeCursorEndPosition ),
+                                       bookMarkEnd = cursorMarkerSkeleton.clone().addClass( 'mce_SELRES_end' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 selectedText = [
</span><span class="cx" style="display: block; padding: 0 10px">                                        markedText,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        bookMarkEnd
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 bookMarkEnd[0].outerHTML
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ].join( '' );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -432,34 +515,66 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        var startNode = editor.$( '.mce_SELRES_start' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                endNode = editor.$( '.mce_SELRES_end' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! startNode.length ) {
-                               startNode = editor.$( '.mce_SELRES_start_target' );
-                       }
-
</del><span class="cx" style="display: block; padding: 0 10px">                         if ( startNode.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                editor.focus();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! endNode.length ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        editor.selection.select( startNode[ 0 ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 editor.selection.select( startNode[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                        var selection = editor.getDoc().createRange();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        selection.setStartAfter( startNode[ 0 ] );
-                                       selection.setEndBefore( endNode[ 0 ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 selection.setStartAfter( startNode[0] );
+                                       selection.setEndBefore( endNode[0] );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        editor.selection.setRng( selection );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                               scrollVisualModeToStartElement( editor, startNode );
</del><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( startNode.hasClass( 'mce_SELRES_start_target' ) ) {
-                               startNode.removeClass( 'mce_SELRES_start_target' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 scrollVisualModeToStartElement( editor, startNode );
+
+
+                       removeSelectionMarker( editor, startNode );
+                       removeSelectionMarker( editor, endNode );
+               }
+
+               /**
+                * @summary Remove selection marker with optional `<p>` parent.
+                *
+                * By default TinyMCE puts every inline node at the main level in a `<p>` wrapping tag.
+                *
+                * In the case with selection markers, when removed they leave an empty `<p>` behind,
+                * which adds an empty paragraph line with `&nbsp;` when switched to Text mode.
+                *
+                * In order to prevent that the wrapping `<p>` needs to be removed when removing the
+                * selection marker.
+                *
+                * @param {object} editor The TinyMCE Editor instance
+                * @param {object} marker The marker to be removed from the editor DOM
+                */
+               function removeSelectionMarker( editor, marker ) {
+                       var markerParent = editor.$( marker ).parent();
+
+                       if (
+                               ! markerParent.length ||
+                               markerParent.prop('tagName').toLowerCase() !== 'p' ||
+                               markerParent[0].childNodes.length > 1 ||
+                               ! markerParent.prop('outerHTML').match(/^<p>/)
+                       ) {
+                               /**
+                                * The selection marker is not self-contained in a <p>.
+                                * In this case only the selection marker is removed, since
+                                * it will affect the content.
+                                */
+                               marker.remove();
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                        else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                startNode.remove();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         /**
+                                * The marker is self-contained in an blank `<p>` tag.
+                                *
+                                * This is usually inserted by TinyMCE
+                                */
+                               markerParent.remove();
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        endNode.remove();
</del><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -476,23 +591,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param {Object} element HTMLElement that should be scrolled into view.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                function scrollVisualModeToStartElement( editor, element ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        /**
-                        * TODO:
-                        *  * Decide if we should animate the transition or not ( motion sickness/accessibility )
-                        */
-                       var elementTop = editor.$( element ).offset().top;
-                       var TinyMCEContentAreaTop = editor.$( editor.getContentAreaContainer() ).offset().top;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var elementTop = editor.$( element ).offset().top,
+                               TinyMCEContentAreaTop = editor.$( editor.getContentAreaContainer() ).offset().top,
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var edTools = $('#wp-content-editor-tools');
-                       var edToolsHeight = edTools.height();
-                       var edToolsOffsetTop = edTools.offset().top;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         edTools = $( '#wp-content-editor-tools' ),
+                               edToolsHeight = edTools.height(),
+                               edToolsOffsetTop = edTools.offset().top,
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var toolbarHeight = getToolbarHeight( editor );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         toolbarHeight = getToolbarHeight( editor ),
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var selectionPosition = TinyMCEContentAreaTop + elementTop;
-                       var visibleAreaHeight = windowHeight - ( edToolsHeight + toolbarHeight );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         selectionPosition = TinyMCEContentAreaTop + elementTop,
+                               visibleAreaHeight = windowHeight - ( edToolsHeight + toolbarHeight );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * The minimum scroll height should be to the top of the editor, to offer a consistent
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -502,10 +613,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * subtracting the height. This gives the scroll position where the top of the editor tools aligns with
</span><span class="cx" style="display: block; padding: 0 10px">                         * the top of the viewport (under the Master Bar)
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var adjustedScroll = Math.max(selectionPosition - visibleAreaHeight / 2, edToolsOffsetTop - edToolsHeight);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var adjustedScroll = Math.max( selectionPosition - visibleAreaHeight / 2, edToolsOffsetTop - edToolsHeight );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       $( 'body' ).animate( {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $( 'html,body' ).animate( {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 scrollTop: parseInt( adjustedScroll, 10 )
</span><span class="cx" style="display: block; padding: 0 10px">                        }, 100 );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -560,11 +670,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * The elements have hardcoded style that makes them invisible. This is done to avoid seeing
</span><span class="cx" style="display: block; padding: 0 10px">                         * random content flickering in the editor when switching between modes.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var spanSkeleton = getCursorMarkerSpan(editor, selectionID);
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var spanSkeleton = getCursorMarkerSpan( editor, selectionID ),
+                               startElement = spanSkeleton.clone().addClass( 'mce_SELRES_start' ),
+                               endElement = spanSkeleton.clone().addClass( 'mce_SELRES_end' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var startElement = spanSkeleton.clone().addClass('mce_SELRES_start');
-                       var endElement = spanSkeleton.clone().addClass('mce_SELRES_end');
-
</del><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Inspired by:
</span><span class="cx" style="display: block; padding: 0 10px">                         * @link https://stackoverflow.com/a/17497803/153310
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -598,37 +707,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                startOffset = range.startOffset,
</span><span class="cx" style="display: block; padding: 0 10px">                                boundaryRange = range.cloneRange();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        boundaryRange.collapse( false );
-                       boundaryRange.insertNode( endElement[0] );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                         * Sometimes the selection starts at the `<img>` tag, which makes the
-                        * boundary range `insertNode` insert `startElement` inside the `<img>` tag itself, i.e.:
-                        *
-                        * `<img><span class="mce_SELRES_start"...>...</span></img>`
-                        *
-                        * As this is an invalid syntax, it breaks the selection.
-                        *
-                        * The conditional below checks if `startNode` is a tag that suffer from that and
-                        * manually inserts the selection start maker before it.
-                        *
-                        * In the future this will probably include a list of tags, not just `<img>`, depending on the needs.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                  * If the selection is on a shortcode with Live View, TinyMCE creates a bogus markup,
+                        * which we have to account for.
</ins><span class="cx" style="display: block; padding: 0 10px">                          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( startNode && startNode.tagName && startNode.tagName.toLowerCase() === 'img' ) {
-                               editor.$( startNode ).before( startElement[ 0 ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( editor.$( startNode ).parents( '.mce-offscreen-selection' ).length > 0 ) {
+                               startNode = editor.$( '[data-mce-selected]' )[0];
+
+                               /**
+                                * Marking the start and end element with `data-mce-object-selection` helps
+                                * discern when the selected object is a Live Preview selection.
+                                *
+                                * This way we can adjust the selection to properly select only the content, ignoring
+                                * whitespace inserted around the selected object by the Editor.
+                                */
+                               startElement.attr('data-mce-object-selection', 'true');
+                               endElement.attr('data-mce-object-selection', 'true');
+
+                               editor.$( startNode ).before( startElement[0] );
+                               editor.$( startNode ).after( endElement[0] );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                        else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                boundaryRange.collapse( false );
+                               boundaryRange.insertNode( endElement[0] );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 boundaryRange.setStart( startNode, startOffset );
</span><span class="cx" style="display: block; padding: 0 10px">                                boundaryRange.collapse( true );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                boundaryRange.insertNode( startElement[ 0 ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         boundaryRange.insertNode( startElement[0] );
+
+                               range.setStartAfter( startElement[0] );
+                               range.setEndBefore( endElement[0] );
+                               selection.removeAllRanges();
+                               selection.addRange( range );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       range.setStartAfter( startElement[0] );
-                       range.setEndBefore( endElement[0] );
-                       selection.removeAllRanges();
-                       selection.addRange( range );
-
</del><span class="cx" style="display: block; padding: 0 10px">                         /**
</span><span class="cx" style="display: block; padding: 0 10px">                         * Now the editor's content has the start/end nodes.
</span><span class="cx" style="display: block; padding: 0 10px">                         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -645,24 +757,47 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        endElement.remove();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        var startRegex = new RegExp(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                '<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         '<span[^>]*\\s*class="mce_SELRES_start"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>(\\s*)'
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        var endRegex = new RegExp(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                '<span[^>]*\\s*class="mce_SELRES_end"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>'
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         '(\\s*)<span[^>]*\\s*class="mce_SELRES_end"[^>]+>\\s*' + selectionID + '[^<]*<\\/span>'
</ins><span class="cx" style="display: block; padding: 0 10px">                         );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        var startMatch = content.match( startRegex );
-                       var endMatch = content.match( endRegex );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var startMatch = content.match( startRegex ),
+                               endMatch = content.match( endRegex );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! startMatch ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return null;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        return {
-                               start: startMatch.index,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 var startIndex = startMatch.index,
+                               startMatchLength = startMatch[0].length,
+                               endIndex = null;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        if (endMatch) {
+                               /**
+                                * Adjust the selection index, if the selection contains a Live Preview object or not.
+                                *
+                                * Check where the `data-mce-object-selection` attribute is set above for more context.
+                                */
+                               if ( startMatch[0].indexOf( 'data-mce-object-selection' ) !== -1 ) {
+                                       startMatchLength -= startMatch[1].length;
+                               }
+
+                               var endMatchIndex = endMatch.index;
+
+                               if ( endMatch[0].indexOf( 'data-mce-object-selection' ) !== -1 ) {
+                                       endMatchIndex -= endMatch[1].length;
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 // We need to adjust the end position to discard the length of the range start marker
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                end: endMatch ? endMatch.index - startMatch[ 0 ].length : null
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         endIndex = endMatchIndex - startMatchLength;
+                       }
+
+                       return {
+                               start: startIndex,
+                               end: endIndex
</ins><span class="cx" style="display: block; padding: 0 10px">                         };
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -672,7 +807,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * Selects the text in TinyMCE's textarea that's between `selection.start` and `selection.end`.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * For `selection` parameter:
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * @see findBookmarkedPosition
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+          * @link findBookmarkedPosition
</ins><span class="cx" style="display: block; padding: 0 10px">                  *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param {Object} editor TinyMCE's editor instance.
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param {Object} selection Selection data.
</span></span></pre>
</div>
</div>

</body>
</html>