JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.3
[ckeditor.git] / _source / core / dom / range.js
index 5b1681f..4c0067b 100644 (file)
@@ -1,16 +1,90 @@
 /*\r
-Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
+/**\r
+ * Creates a CKEDITOR.dom.range instance that can be used inside a specific\r
+ * DOM Document.\r
+ * @class Represents a delimited piece of content in a DOM Document.\r
+ * It is contiguous in the sense that it can be characterized as selecting all\r
+ * of the content between a pair of boundary-points.<br>\r
+ * <br>\r
+ * This class shares much of the W3C\r
+ * <a href="http://www.w3.org/TR/DOM-Level-2-Traversal-Range/ranges.html">Document Object Model Range</a>\r
+ * ideas and features, adding several range manipulation tools to it, but it's\r
+ * not intended to be compatible with it.\r
+ * @param {CKEDITOR.dom.document} document The document into which the range\r
+ *             features will be available.\r
+ * @example\r
+ * // Create a range for the entire contents of the editor document body.\r
+ * var range = new CKEDITOR.dom.range( editor.document );\r
+ * range.selectNodeContents( editor.document.getBody() );\r
+ * // Delete the contents.\r
+ * range.deleteContents();\r
+ */\r
 CKEDITOR.dom.range = function( document )\r
 {\r
+       /**\r
+        * Node within which the range begins.\r
+        * @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}\r
+        * @example\r
+        * var range = new CKEDITOR.dom.range( editor.document );\r
+        * range.selectNodeContents( editor.document.getBody() );\r
+        * alert( range.startContainer.getName() );  // "body"\r
+        */\r
        this.startContainer     = null;\r
+\r
+       /**\r
+        * Offset within the starting node of the range.\r
+        * @type {Number}\r
+        * @example\r
+        * var range = new CKEDITOR.dom.range( editor.document );\r
+        * range.selectNodeContents( editor.document.getBody() );\r
+        * alert( range.startOffset );  // "0"\r
+        */\r
        this.startOffset        = null;\r
+\r
+       /**\r
+        * Node within which the range ends.\r
+        * @type {CKEDITOR.NODE_ELEMENT|CKEDITOR.NODE_TEXT}\r
+        * @example\r
+        * var range = new CKEDITOR.dom.range( editor.document );\r
+        * range.selectNodeContents( editor.document.getBody() );\r
+        * alert( range.endContainer.getName() );  // "body"\r
+        */\r
        this.endContainer       = null;\r
+\r
+       /**\r
+        * Offset within the ending node of the range.\r
+        * @type {Number}\r
+        * @example\r
+        * var range = new CKEDITOR.dom.range( editor.document );\r
+        * range.selectNodeContents( editor.document.getBody() );\r
+        * alert( range.endOffset );  // == editor.document.getBody().getChildCount()\r
+        */\r
        this.endOffset          = null;\r
+\r
+       /**\r
+        * Indicates that this is a collapsed range. A collapsed range has it's\r
+        * start and end boudaries at the very same point so nothing is contained\r
+        * in it.\r
+        * @example\r
+        * var range = new CKEDITOR.dom.range( editor.document );\r
+        * range.selectNodeContents( editor.document.getBody() );\r
+        * alert( range.collapsed );  // "false"\r
+        * range.collapse();\r
+        * alert( range.collapsed );  // "true"\r
+        */\r
        this.collapsed          = true;\r
 \r
+       /**\r
+        * The document within which the range can be used.\r
+        * @type {CKEDITOR.dom.document}\r
+        * @example\r
+        * // Selects the body contents of the range document.\r
+        * range.selectNodeContents( range.document.getBody() );\r
+        */\r
        this.document = document;\r
 };\r
 \r
@@ -29,7 +103,7 @@ CKEDITOR.dom.range = function( document )
        // This is a shared function used to delete, extract and clone the range\r
        // contents.\r
        // V2\r
-       var execContentsAction = function( range, action, docFrag )\r
+       var execContentsAction = function( range, action, docFrag, mergeThen )\r
        {\r
                range.optimizeBookmark();\r
 \r
@@ -133,7 +207,7 @@ CKEDITOR.dom.range = function( document )
 \r
                        currentNode = levelStartNode.getNext();\r
 \r
-                       while( currentNode )\r
+                       while ( currentNode )\r
                        {\r
                                // Stop processing when the current node matches a node in the\r
                                // endParents tree or if it is the endNode.\r
@@ -180,7 +254,7 @@ CKEDITOR.dom.range = function( document )
                        {\r
                                currentNode = levelStartNode.getPrevious();\r
 \r
-                               while( currentNode )\r
+                               while ( currentNode )\r
                                {\r
                                        // Stop processing when the current node matches a node in the\r
                                        // startParents tree or if it is the startNode.\r
@@ -244,7 +318,17 @@ CKEDITOR.dom.range = function( document )
                                if ( removeStartNode && topEnd.$.parentNode == startNode.$.parentNode )\r
                                        endIndex--;\r
 \r
-                               range.setStart( topEnd.getParent(), endIndex );\r
+                               // Merge splitted parents.\r
+                               if ( mergeThen && topStart.type == CKEDITOR.NODE_ELEMENT )\r
+                               {\r
+                                       var span = CKEDITOR.dom.element.createFromHtml( '<span ' +\r
+                                               'data-cke-bookmark="1" style="display:none">&nbsp;</span>', range.document );\r
+                                       span.insertAfter( topStart );\r
+                                       topStart.mergeSiblings( false );\r
+                                       range.moveToBookmark( { startNode : span } );\r
+                               }\r
+                               else\r
+                                       range.setStart( topEnd.getParent(), endIndex );\r
                        }\r
 \r
                        // Collapse it to the start.\r
@@ -252,10 +336,10 @@ CKEDITOR.dom.range = function( document )
                }\r
 \r
                // Cleanup any marked node.\r
-               if( removeStartNode )\r
+               if ( removeStartNode )\r
                        startNode.remove();\r
 \r
-               if( removeEndNode && endNode.$.parentNode )\r
+               if ( removeEndNode && endNode.$.parentNode )\r
                        endNode.remove();\r
        };\r
 \r
@@ -277,8 +361,8 @@ CKEDITOR.dom.range = function( document )
                                // If there's any visible text, then we're not at the start.\r
                                if ( CKEDITOR.tools.trim( node.getText() ).length )\r
                                        return false;\r
-                               }\r
-                       else\r
+                       }\r
+                       else if ( node.type == CKEDITOR.NODE_ELEMENT )\r
                        {\r
                                // If there are non-empty inline elements (e.g. <img />), then we're not\r
                                // at the start.\r
@@ -305,7 +389,16 @@ CKEDITOR.dom.range = function( document )
                return node.type != CKEDITOR.NODE_TEXT\r
                            && node.getName() in CKEDITOR.dtd.$removeEmpty\r
                            || !CKEDITOR.tools.trim( node.getText() )\r
-                           || node.getParent().hasAttribute( '_fck_bookmark' );\r
+                           || !!node.getParent().data( 'cke-bookmark' );\r
+       }\r
+\r
+       var whitespaceEval = new CKEDITOR.dom.walker.whitespaces(),\r
+               bookmarkEval = new CKEDITOR.dom.walker.bookmark();\r
+\r
+       function nonWhitespaceOrBookmarkEval( node )\r
+       {\r
+               // Whitespaces and bookmark nodes are to be ignored.\r
+               return !whitespaceEval( node ) && !bookmarkEval( node );\r
        }\r
 \r
        CKEDITOR.dom.range.prototype =\r
@@ -339,7 +432,10 @@ CKEDITOR.dom.range = function( document )
                        this.collapsed = true;\r
                },\r
 \r
-               // The selection may be lost when cloning (due to the splitText() call).\r
+               /**\r
+                *  The content nodes of the range are cloned and added to a document fragment, which is returned.\r
+                *  <strong> Note: </strong> Text selection may lost after invoking this method. (caused by text node splitting).\r
+                */\r
                cloneContents : function()\r
                {\r
                        var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
@@ -350,20 +446,29 @@ CKEDITOR.dom.range = function( document )
                        return docFrag;\r
                },\r
 \r
-               deleteContents : function()\r
+               /**\r
+                * Deletes the content nodes of the range permanently from the DOM tree.\r
+                * @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.\r
+                */\r
+               deleteContents : function( mergeThen )\r
                {\r
                        if ( this.collapsed )\r
                                return;\r
 \r
-                       execContentsAction( this, 0 );\r
+                       execContentsAction( this, 0, null, mergeThen );\r
                },\r
 \r
-               extractContents : function()\r
+               /**\r
+                *  The content nodes of the range are cloned and added to a document fragment,\r
+                * meanwhile they're removed permanently from the DOM tree.\r
+                * @param {Boolean} [mergeThen] Merge any splitted elements result in DOM true due to partial selection.\r
+                */\r
+               extractContents : function( mergeThen )\r
                {\r
                        var docFrag = new CKEDITOR.dom.documentFragment( this.document );\r
 \r
                        if ( !this.collapsed )\r
-                               execContentsAction( this, 1, docFrag );\r
+                               execContentsAction( this, 1, docFrag, mergeThen );\r
 \r
                        return docFrag;\r
                },\r
@@ -388,9 +493,10 @@ CKEDITOR.dom.range = function( document )
                        var startNode, endNode;\r
                        var baseId;\r
                        var clone;\r
+                       var collapsed = this.collapsed;\r
 \r
                        startNode = this.document.createElement( 'span' );\r
-                       startNode.setAttribute( '_fck_bookmark', 1 );\r
+                       startNode.data( 'cke-bookmark', 1 );\r
                        startNode.setStyle( 'display', 'none' );\r
 \r
                        // For IE, it must have something inside, otherwise it may be\r
@@ -404,7 +510,7 @@ CKEDITOR.dom.range = function( document )
                        }\r
 \r
                        // If collapsed, the endNode will not be created.\r
-                       if ( !this.collapsed )\r
+                       if ( !collapsed )\r
                        {\r
                                endNode = startNode.clone();\r
                                endNode.setHtml( '&nbsp;' );\r
@@ -433,7 +539,8 @@ CKEDITOR.dom.range = function( document )
                        return {\r
                                startNode : serializable ? baseId + 'S' : startNode,\r
                                endNode : serializable ? baseId + 'E' : endNode,\r
-                               serializable : serializable\r
+                               serializable : serializable,\r
+                               collapsed : collapsed\r
                        };\r
                },\r
 \r
@@ -456,6 +563,8 @@ CKEDITOR.dom.range = function( document )
                        var startOffset = this.startOffset,\r
                                endOffset       = this.endOffset;\r
 \r
+                       var collapsed = this.collapsed;\r
+\r
                        var child, previous;\r
 \r
                        // If there is no range then get out of here.\r
@@ -480,6 +589,10 @@ CKEDITOR.dom.range = function( document )
                                                startContainer = child;\r
                                                startOffset = 0;\r
                                        }\r
+\r
+                                       // Get the normalized offset.\r
+                                       if ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                               startOffset = child.getIndex( 1 );\r
                                }\r
 \r
                                // Normalize the start.\r
@@ -492,7 +605,7 @@ CKEDITOR.dom.range = function( document )
                                }\r
 \r
                                // Process the end only if not normalized.\r
-                               if ( !this.isCollapsed )\r
+                               if ( !collapsed )\r
                                {\r
                                        // Find out if the start is pointing to a text node that\r
                                        // will be normalized.\r
@@ -508,6 +621,10 @@ CKEDITOR.dom.range = function( document )
                                                        endContainer = child;\r
                                                        endOffset = 0;\r
                                                }\r
+\r
+                                               // Get the normalized offset.\r
+                                               if ( child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                                       endOffset = child.getIndex( 1 );\r
                                        }\r
 \r
                                        // Normalize the end.\r
@@ -523,10 +640,11 @@ CKEDITOR.dom.range = function( document )
 \r
                        return {\r
                                start           : startContainer.getAddress( normalized ),\r
-                               end                     : this.isCollapsed ? null : endContainer.getAddress( normalized ),\r
+                               end                     : collapsed ? null : endContainer.getAddress( normalized ),\r
                                startOffset     : startOffset,\r
                                endOffset       : endOffset,\r
                                normalized      : normalized,\r
+                               collapsed       : collapsed,\r
                                is2                     : true          // It's a createBookmark2 bookmark.\r
                        };\r
                },\r
@@ -688,7 +806,7 @@ CKEDITOR.dom.range = function( document )
                },\r
 \r
                /**\r
-                * Move the range out of bookmark nodes if they're been the container.\r
+                * Move the range out of bookmark nodes if they'd been the container.\r
                 */\r
                optimizeBookmark: function()\r
                {\r
@@ -696,10 +814,10 @@ CKEDITOR.dom.range = function( document )
                                endNode = this.endContainer;\r
 \r
                        if ( startNode.is && startNode.is( 'span' )\r
-                               && startNode.hasAttribute( '_fck_bookmark' ) )\r
+                               && startNode.data( 'cke-bookmark' ) )\r
                                this.setStartAt( startNode, CKEDITOR.POSITION_BEFORE_START );\r
                        if ( endNode && endNode.is && endNode.is( 'span' )\r
-                               && endNode.hasAttribute( '_fck_bookmark' ) )\r
+                               && endNode.data( 'cke-bookmark' ) )\r
                                this.setEndAt( endNode,  CKEDITOR.POSITION_AFTER_END );\r
                },\r
 \r
@@ -733,15 +851,21 @@ CKEDITOR.dom.range = function( document )
 \r
                                        startOffset = startContainer.getIndex() + 1;\r
                                        startContainer = startContainer.getParent();\r
-                                       // Check if it is necessary to update the end boundary.\r
-                                       if ( !collapsed && this.startContainer.equals( this.endContainer ) )\r
+\r
+                                       // Check all necessity of updating the end boundary.\r
+                                       if ( this.startContainer.equals( this.endContainer ) )\r
                                                this.setEnd( nextText, this.endOffset - this.startOffset );\r
+                                       else if ( startContainer.equals( this.endContainer ) )\r
+                                               this.endOffset += 1;\r
                                }\r
 \r
                                this.setStart( startContainer, startOffset );\r
 \r
                                if ( collapsed )\r
+                               {\r
                                        this.collapse( true );\r
+                                       return;\r
+                               }\r
                        }\r
 \r
                        var endContainer = this.endContainer;\r
@@ -778,7 +902,12 @@ CKEDITOR.dom.range = function( document )
                        }\r
                },\r
 \r
-               enlarge : function( unit )\r
+               /**\r
+                * Expands the range so that partial units are completely contained.\r
+                * @param unit {Number} The unit type to expand with.\r
+                * @param {Boolean} [excludeBrs=false] Whether include line-breaks when expanding.\r
+                */\r
+               enlarge : function( unit, excludeBrs )\r
                {\r
                        switch ( unit )\r
                        {\r
@@ -899,7 +1028,8 @@ CKEDITOR.dom.range = function( document )
                                                                // If this is a visible element.\r
                                                                // We need to check for the bookmark attribute because IE insists on\r
                                                                // rendering the display:none nodes we use for bookmarks. (#3363)\r
-                                                               if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
+                                                               // Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)\r
+                                                               if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )\r
                                                                {\r
                                                                        // We'll accept it only if we need\r
                                                                        // whitespace, and this is an inline\r
@@ -910,7 +1040,7 @@ CKEDITOR.dom.range = function( document )
 \r
                                                                                siblingText = sibling.getText();\r
 \r
-                                                                               if ( !(/[^\s\ufeff]/).test( siblingText ) )     // Spaces + Zero Width No-Break Space (U+FEFF)\r
+                                                                               if ( (/[^\s\ufeff]/).test( siblingText ) )      // Spaces + Zero Width No-Break Space (U+FEFF)\r
                                                                                        sibling = null;\r
                                                                                else\r
                                                                                {\r
@@ -1058,7 +1188,8 @@ CKEDITOR.dom.range = function( document )
                                                                // If this is a visible element.\r
                                                                // We need to check for the bookmark attribute because IE insists on\r
                                                                // rendering the display:none nodes we use for bookmarks. (#3363)\r
-                                                               if ( sibling.$.offsetWidth > 0 && !sibling.getAttribute( '_fck_bookmark' ) )\r
+                                                               // Line-breaks (br) are rendered with zero width, which we don't want to include. (#7041)\r
+                                                               if ( ( sibling.$.offsetWidth > 0 || excludeBrs && sibling.is( 'br' ) ) && !sibling.data( 'cke-bookmark' ) )\r
                                                                {\r
                                                                        // We'll accept it only if we need\r
                                                                        // whitespace, and this is an inline\r
@@ -1069,7 +1200,7 @@ CKEDITOR.dom.range = function( document )
 \r
                                                                                siblingText = sibling.getText();\r
 \r
-                                                                               if ( !(/[^\s\ufeff]/).test( siblingText ) )\r
+                                                                               if ( (/[^\s\ufeff]/).test( siblingText ) )\r
                                                                                        sibling = null;\r
                                                                                else\r
                                                                                {\r
@@ -1151,13 +1282,13 @@ CKEDITOR.dom.range = function( document )
 \r
                                        var walker = new CKEDITOR.dom.walker( walkerRange ),\r
                                            blockBoundary,  // The node on which the enlarging should stop.\r
-                                               tailBr, //\r
-                                           defaultGuard = CKEDITOR.dom.walker.blockBoundary(\r
+                                               tailBr, // In case BR as block boundary.\r
+                                           notBlockBoundary = CKEDITOR.dom.walker.blockBoundary(\r
                                                                ( unit == CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS ) ? { br : 1 } : null ),\r
                                                // Record the encountered 'blockBoundary' for later use.\r
                                                boundaryGuard = function( node )\r
                                                {\r
-                                                       var retval = defaultGuard( node );\r
+                                                       var retval = notBlockBoundary( node );\r
                                                        if ( !retval )\r
                                                                blockBoundary = node;\r
                                                        return retval;\r
@@ -1178,12 +1309,14 @@ CKEDITOR.dom.range = function( document )
                                        // It's the body which stop the enlarging if no block boundary found.\r
                                        blockBoundary = blockBoundary || body;\r
 \r
-                                       // Start the range at different position by comparing\r
-                                       // the document position of it with 'enlargeable' node.\r
+                                       // Start the range either after the end of found block (<p>...</p>[text)\r
+                                       // or at the start of block (<p>[text...), by comparing the document position\r
+                                       // with 'enlargeable' node.\r
                                        this.setStartAt(\r
                                                        blockBoundary,\r
                                                        !blockBoundary.is( 'br' ) &&\r
-                                                       ( !enlargeable || blockBoundary.contains( enlargeable ) ) ?\r
+                                                       ( !enlargeable && this.checkStartOfBlock()\r
+                                                         || enlargeable && blockBoundary.contains( enlargeable ) ) ?\r
                                                                CKEDITOR.POSITION_AFTER_START :\r
                                                                CKEDITOR.POSITION_AFTER_END );\r
 \r
@@ -1204,12 +1337,12 @@ CKEDITOR.dom.range = function( document )
                                        // It's the body which stop the enlarging if no block boundary found.\r
                                        blockBoundary = blockBoundary || body;\r
 \r
-                                       // Start the range at different position by comparing\r
-                                       // the document position of it with 'enlargeable' node.\r
+                                       // Close the range either before the found block start (text]<p>...</p>) or at the block end (...text]</p>)\r
+                                       // by comparing the document position with 'enlargeable' node.\r
                                        this.setEndAt(\r
                                                        blockBoundary,\r
-                                                       !blockBoundary.is( 'br' ) &&\r
-                                                       ( !enlargeable || blockBoundary.contains( enlargeable ) ) ?\r
+                                                       ( !enlargeable && this.checkEndOfBlock()\r
+                                                         || enlargeable && blockBoundary.contains( enlargeable ) ) ?\r
                                                                CKEDITOR.POSITION_BEFORE_END :\r
                                                                CKEDITOR.POSITION_BEFORE_START );\r
                                        // We must include the <br> at the end of range if there's\r
@@ -1220,6 +1353,111 @@ CKEDITOR.dom.range = function( document )
                },\r
 \r
                /**\r
+                *  Descrease the range to make sure that boundaries\r
+               *  always anchor beside text nodes or innermost element.\r
+                * @param {Number} mode  ( CKEDITOR.SHRINK_ELEMENT | CKEDITOR.SHRINK_TEXT ) The shrinking mode.\r
+                * <dl>\r
+                *       <dt>CKEDITOR.SHRINK_ELEMENT</dt>\r
+                *       <dd>Shrink the range boundaries to the edge of the innermost element.</dd>\r
+                *       <dt>CKEDITOR.SHRINK_TEXT</dt>\r
+                *       <dd>Shrink the range boudaries to anchor by the side of enclosed text  node, range remains if there's no text nodes on boundaries at all.</dd>\r
+                 * </dl>\r
+                * @param {Boolean} selectContents Whether result range anchors at the inner OR outer boundary of the node.\r
+                */\r
+               shrink : function( mode, selectContents )\r
+               {\r
+                       // Unable to shrink a collapsed range.\r
+                       if ( !this.collapsed )\r
+                       {\r
+                               mode = mode || CKEDITOR.SHRINK_TEXT;\r
+\r
+                               var walkerRange = this.clone();\r
+\r
+                               var startContainer = this.startContainer,\r
+                                       endContainer = this.endContainer,\r
+                                       startOffset = this.startOffset,\r
+                                       endOffset = this.endOffset,\r
+                                       collapsed = this.collapsed;\r
+\r
+                               // Whether the start/end boundary is moveable.\r
+                               var moveStart = 1,\r
+                                               moveEnd = 1;\r
+\r
+                               if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
+                               {\r
+                                       if ( !startOffset )\r
+                                               walkerRange.setStartBefore( startContainer );\r
+                                       else if ( startOffset >= startContainer.getLength( ) )\r
+                                               walkerRange.setStartAfter( startContainer );\r
+                                       else\r
+                                       {\r
+                                               // Enlarge the range properly to avoid walker making\r
+                                               // DOM changes caused by triming the text nodes later.\r
+                                               walkerRange.setStartBefore( startContainer );\r
+                                               moveStart = 0;\r
+                                       }\r
+                               }\r
+\r
+                               if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
+                               {\r
+                                       if ( !endOffset )\r
+                                               walkerRange.setEndBefore( endContainer );\r
+                                       else if ( endOffset >= endContainer.getLength( ) )\r
+                                               walkerRange.setEndAfter( endContainer );\r
+                                       else\r
+                                       {\r
+                                               walkerRange.setEndAfter( endContainer );\r
+                                               moveEnd = 0;\r
+                                       }\r
+                               }\r
+\r
+                               var walker = new CKEDITOR.dom.walker( walkerRange ),\r
+                                       isBookmark = CKEDITOR.dom.walker.bookmark();\r
+\r
+                               walker.evaluator = function( node )\r
+                               {\r
+                                       return node.type == ( mode == CKEDITOR.SHRINK_ELEMENT ?\r
+                                               CKEDITOR.NODE_ELEMENT : CKEDITOR.NODE_TEXT );\r
+                               };\r
+\r
+                               var currentElement;\r
+                               walker.guard = function( node, movingOut )\r
+                               {\r
+                                       if ( isBookmark( node ) )\r
+                                               return true;\r
+\r
+                                       // Stop when we're shrink in element mode while encountering a text node.\r
+                                       if ( mode == CKEDITOR.SHRINK_ELEMENT && node.type == CKEDITOR.NODE_TEXT )\r
+                                               return false;\r
+\r
+                                       // Stop when we've already walked "through" an element.\r
+                                       if ( movingOut && node.equals( currentElement ) )\r
+                                               return false;\r
+\r
+                                       if ( !movingOut && node.type == CKEDITOR.NODE_ELEMENT )\r
+                                               currentElement = node;\r
+\r
+                                       return true;\r
+                               };\r
+\r
+                               if ( moveStart )\r
+                               {\r
+                                       var textStart = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastForward' : 'next']();\r
+                                       textStart && this.setStartAt( textStart, selectContents ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_START );\r
+                               }\r
+\r
+                               if ( moveEnd )\r
+                               {\r
+                                       walker.reset();\r
+                                       var textEnd = walker[ mode == CKEDITOR.SHRINK_ELEMENT ? 'lastBackward' : 'previous']();\r
+                                       textEnd && this.setEndAt( textEnd, selectContents ? CKEDITOR.POSITION_BEFORE_END : CKEDITOR.POSITION_AFTER_END );\r
+                               }\r
+\r
+                               return !!( moveStart || moveEnd );\r
+                       }\r
+               },\r
+\r
+               /**\r
                 * Inserts a node at the start of the range. The range will be expanded\r
                 * the contain the node.\r
                 */\r
@@ -1272,6 +1510,11 @@ CKEDITOR.dom.range = function( document )
                        // we will not need this check for our use of this class so we can\r
                        // ignore it for now.\r
 \r
+                       // Fixing invalid range start inside dtd empty elements.\r
+                       if( startNode.type == CKEDITOR.NODE_ELEMENT\r
+                               && CKEDITOR.dtd.$empty[ startNode.getName() ] )\r
+                               startOffset = startNode.getIndex(), startNode = startNode.getParent();\r
+\r
                        this.startContainer     = startNode;\r
                        this.startOffset        = startOffset;\r
 \r
@@ -1298,6 +1541,11 @@ CKEDITOR.dom.range = function( document )
                        // will not need this check for our use of this class so we can ignore\r
                        // it for now.\r
 \r
+                       // Fixing invalid range end inside dtd empty elements.\r
+                       if( endNode.type == CKEDITOR.NODE_ELEMENT\r
+                               && CKEDITOR.dtd.$empty[ endNode.getName() ] )\r
+                               endOffset = endNode.getIndex() + 1, endNode = endNode.getParent();\r
+\r
                        this.endContainer       = endNode;\r
                        this.endOffset          = endOffset;\r
 \r
@@ -1457,18 +1705,7 @@ CKEDITOR.dom.range = function( document )
                                }\r
                                else\r
                                {\r
-                                       // Extract the contents of the block from the selection point to the end\r
-                                       // of its contents.\r
-                                       this.setEndAt( startBlock, CKEDITOR.POSITION_BEFORE_END );\r
-                                       var documentFragment = this.extractContents();\r
-\r
-                                       // Duplicate the block element after it.\r
-                                       endBlock = startBlock.clone( false );\r
-\r
-                                       // Place the extracted contents into the duplicated block.\r
-                                       documentFragment.appendTo( endBlock );\r
-                                       endBlock.insertAfter( startBlock );\r
-                                       this.moveToPosition( startBlock, CKEDITOR.POSITION_AFTER_END );\r
+                                       endBlock = this.splitElement( startBlock );\r
 \r
                                        // In Gecko, the last child node must be a bogus <br>.\r
                                        // Note: bogus <br> added under <ul> or <ol> would cause\r
@@ -1488,26 +1725,63 @@ CKEDITOR.dom.range = function( document )
                },\r
 \r
                /**\r
-                * Check whether current range is on the inner edge of the specified element.\r
-                * @param {Number} checkType ( CKEDITOR.START | CKEDITOR.END ) The checking side.\r
+                * Branch the specified element from the collapsed range position and\r
+                * place the caret between the two result branches.\r
+                * Note: The range must be collapsed and been enclosed by this element.\r
+                * @param {CKEDITOR.dom.element} element\r
+                * @return {CKEDITOR.dom.element} Root element of the new branch after the split.\r
+                */\r
+               splitElement : function( toSplit )\r
+               {\r
+                       if ( !this.collapsed )\r
+                               return null;\r
+\r
+                       // Extract the contents of the block from the selection point to the end\r
+                       // of its contents.\r
+                       this.setEndAt( toSplit, CKEDITOR.POSITION_BEFORE_END );\r
+                       var documentFragment = this.extractContents();\r
+\r
+                       // Duplicate the element after it.\r
+                       var clone = toSplit.clone( false );\r
+\r
+                       // Place the extracted contents into the duplicated element.\r
+                       documentFragment.appendTo( clone );\r
+                       clone.insertAfter( toSplit );\r
+                       this.moveToPosition( toSplit, CKEDITOR.POSITION_AFTER_END );\r
+                       return clone;\r
+               },\r
+\r
+               /**\r
+                * Check whether a range boundary is at the inner boundary of a given\r
+                * element.\r
                 * @param {CKEDITOR.dom.element} element The target element to check.\r
+                * @param {Number} checkType The boundary to check for both the range\r
+                *              and the element. It can be CKEDITOR.START or CKEDITOR.END.\r
+                * @returns {Boolean} "true" if the range boundary is at the inner\r
+                *              boundary of the element.\r
                 */\r
                checkBoundaryOfElement : function( element, checkType )\r
                {\r
+                       var checkStart = ( checkType == CKEDITOR.START );\r
+\r
+                       // Create a copy of this range, so we can manipulate it for our checks.\r
                        var walkerRange = this.clone();\r
+\r
+                       // Collapse the range at the proper size.\r
+                       walkerRange.collapse( checkStart );\r
+\r
                        // Expand the range to element boundary.\r
-                       walkerRange[ checkType == CKEDITOR.START ?\r
-                        'setStartAt' : 'setEndAt' ]\r
-                        ( element, checkType == CKEDITOR.START ?\r
-                          CKEDITOR.POSITION_AFTER_START\r
-                          : CKEDITOR.POSITION_BEFORE_END );\r
+                       walkerRange[ checkStart ? 'setStartAt' : 'setEndAt' ]\r
+                        ( element, checkStart ? CKEDITOR.POSITION_AFTER_START : CKEDITOR.POSITION_BEFORE_END );\r
 \r
-                       var walker = new CKEDITOR.dom.walker( walkerRange ),\r
-                        retval = false;\r
+                       // Create the walker, which will check if we have anything useful\r
+                       // in the range.\r
+                       var walker = new CKEDITOR.dom.walker( walkerRange );\r
                        walker.evaluator = elementBoundaryEval;\r
-                       return walker[ checkType == CKEDITOR.START ?\r
-                               'checkBackward' : 'checkForward' ]();\r
+\r
+                       return walker[ checkStart ? 'checkBackward' : 'checkForward' ]();\r
                },\r
+\r
                // Calls to this function may produce changes to the DOM. The range may\r
                // be updated to reflect such changes.\r
                checkStartOfBlock : function()\r
@@ -1579,34 +1853,114 @@ CKEDITOR.dom.range = function( document )
                },\r
 \r
                /**\r
-                * Moves the range boundaries to the first editing point inside an\r
+                * Check if elements at which the range boundaries anchor are read-only,\r
+                * with respect to "contenteditable" attribute.\r
+                */\r
+               checkReadOnly : ( function()\r
+               {\r
+                       function checkNodesEditable( node, anotherEnd )\r
+                       {\r
+                               while( node )\r
+                               {\r
+                                       if ( node.type == CKEDITOR.NODE_ELEMENT )\r
+                                       {\r
+                                               if ( node.getAttribute( 'contentEditable' ) == 'false'\r
+                                                       && !node.data( 'cke-editable' ) )\r
+                                               {\r
+                                                       return 0;\r
+                                               }\r
+                                               // Range enclosed entirely in an editable element.\r
+                                               else if ( node.is( 'body' )\r
+                                                       || node.getAttribute( 'contentEditable' ) == 'true'\r
+                                                       && ( node.contains( anotherEnd ) || node.equals( anotherEnd ) ) )\r
+                                               {\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                                       node = node.getParent();\r
+                               }\r
+\r
+                               return 1;\r
+                       }\r
+\r
+                       return function()\r
+                       {\r
+                               var startNode = this.startContainer,\r
+                                       endNode = this.endContainer;\r
+\r
+                               // Check if elements path at both boundaries are editable.\r
+                               return !( checkNodesEditable( startNode, endNode ) && checkNodesEditable( endNode, startNode ) );\r
+                       };\r
+               })(),\r
+\r
+               /**\r
+                * Moves the range boundaries to the first/end editing point inside an\r
                 * element. For example, in an element tree like\r
                 * "&lt;p&gt;&lt;b&gt;&lt;i&gt;&lt;/i&gt;&lt;/b&gt; Text&lt;/p&gt;", the start editing point is\r
                 * "&lt;p&gt;&lt;b&gt;&lt;i&gt;^&lt;/i&gt;&lt;/b&gt; Text&lt;/p&gt;" (inside &lt;i&gt;).\r
-                * @param {CKEDITOR.dom.element} targetElement The element into which\r
-                *              look for the editing spot.\r
+                * @param {CKEDITOR.dom.element} el The element into which look for the\r
+                *              editing spot.\r
+                * @param {Boolean} isMoveToEnd Whether move to the end editable position.\r
                 */\r
-               moveToElementEditStart : function( targetElement )\r
+               moveToElementEditablePosition : function( el, isMoveToEnd )\r
                {\r
-                       var editableElement;\r
+                       var isEditable;\r
 \r
-                       while ( targetElement && targetElement.type == CKEDITOR.NODE_ELEMENT )\r
+                       // Empty elements are rejected.\r
+                       if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
+                               return false;\r
+\r
+                       while ( el && el.type == CKEDITOR.NODE_ELEMENT )\r
                        {\r
-                               if ( targetElement.isEditable() )\r
-                                       editableElement = targetElement;\r
-                               else if ( editableElement )\r
-                                       break ;         // If we already found an editable element, stop the loop.\r
+                               isEditable = el.isEditable();\r
+\r
+                               // If an editable element is found, move inside it.\r
+                               if ( isEditable )\r
+                                       this.moveToPosition( el, isMoveToEnd ?\r
+                                                                CKEDITOR.POSITION_BEFORE_END :\r
+                                                                CKEDITOR.POSITION_AFTER_START );\r
+                               // Stop immediately if we've found a non editable inline element (e.g <img>).\r
+                               else if ( CKEDITOR.dtd.$inline[ el.getName() ] )\r
+                               {\r
+                                       this.moveToPosition( el, isMoveToEnd ?\r
+                                                                CKEDITOR.POSITION_AFTER_END :\r
+                                                                CKEDITOR.POSITION_BEFORE_START );\r
+                                       return true;\r
+                               }\r
 \r
-                               targetElement = targetElement.getFirst();\r
-                       }\r
+                               // Non-editable non-inline elements are to be bypassed, getting the next one.\r
+                               if ( CKEDITOR.dtd.$empty[ el.getName() ] )\r
+                                       el = el[ isMoveToEnd ? 'getPrevious' : 'getNext' ]( nonWhitespaceOrBookmarkEval );\r
+                               else\r
+                                       el = el[ isMoveToEnd ? 'getLast' : 'getFirst' ]( nonWhitespaceOrBookmarkEval );\r
 \r
-                       if ( editableElement )\r
-                       {\r
-                               this.moveToPosition(editableElement, CKEDITOR.POSITION_AFTER_START);\r
-                               return true;\r
+                               // Stop immediately if we've found a text node.\r
+                               if ( el && el.type == CKEDITOR.NODE_TEXT )\r
+                               {\r
+                                       this.moveToPosition( el, isMoveToEnd ?\r
+                                                                CKEDITOR.POSITION_AFTER_END :\r
+                                                                CKEDITOR.POSITION_BEFORE_START );\r
+                                       return true;\r
+                               }\r
                        }\r
-                       else\r
-                               return false;\r
+\r
+                       return isEditable;\r
+               },\r
+\r
+               /**\r
+                *@see {CKEDITOR.dom.range.moveToElementEditablePosition}\r
+                */\r
+               moveToElementEditStart : function( target )\r
+               {\r
+                       return this.moveToElementEditablePosition( target );\r
+               },\r
+\r
+               /**\r
+                *@see {CKEDITOR.dom.range.moveToElementEditablePosition}\r
+                */\r
+               moveToElementEditEnd : function( target )\r
+               {\r
+                       return this.moveToElementEditablePosition( target, true );\r
                },\r
 \r
                /**\r
@@ -1614,8 +1968,15 @@ CKEDITOR.dom.range = function( document )
                 */\r
                getEnclosedNode : function()\r
                {\r
-                       var walkerRange = this.clone(),\r
-                               walker = new CKEDITOR.dom.walker( walkerRange ),\r
+                       var walkerRange = this.clone();\r
+\r
+                       // Optimize and analyze the range to avoid DOM destructive nature of walker. (#5780)\r
+                       walkerRange.optimize();\r
+                       if ( walkerRange.startContainer.type != CKEDITOR.NODE_ELEMENT\r
+                                       || walkerRange.endContainer.type != CKEDITOR.NODE_ELEMENT )\r
+                               return null;\r
+\r
+                       var walker = new CKEDITOR.dom.walker( walkerRange ),\r
                                isNotBookmarks = CKEDITOR.dom.walker.bookmark( true ),\r
                                isNotWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
                                evaluator = function( node )\r
@@ -1659,10 +2020,13 @@ CKEDITOR.ENLARGE_ELEMENT = 1;
 CKEDITOR.ENLARGE_BLOCK_CONTENTS = 2;\r
 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS = 3;\r
 \r
-/**\r
- * Check boundary types.\r
- * @see CKEDITOR.dom.range::checkBoundaryOfElement\r
- */\r
+// Check boundary types.\r
+// @see CKEDITOR.dom.range.prototype.checkBoundaryOfElement\r
 CKEDITOR.START = 1;\r
 CKEDITOR.END = 2;\r
 CKEDITOR.STARTEND = 3;\r
+\r
+// Shrink range types.\r
+// @see CKEDITOR.dom.range.prototype.shrink\r
+CKEDITOR.SHRINK_ELEMENT = 1;\r
+CKEDITOR.SHRINK_TEXT = 2;\r