JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / selection / plugin.js
index 190344a..17e2c52 100644 (file)
@@ -17,7 +17,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // In IE, the "selectionchange" event may still get thrown when\r
                        // releasing the WYSIWYG mode, so we need to check it first.\r
                        var sel = this.getSelection();\r
-                       if ( !sel )\r
+                       if ( !sel || !sel.document.getWindow().$ )\r
                                return;\r
 \r
                        var firstElement = sel.getStartElement();\r
@@ -105,7 +105,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        editor.on( 'contentDom', function()\r
                                {\r
                                        var doc = editor.document,\r
-                                               body = doc.getBody();\r
+                                               body = doc.getBody(),\r
+                                               html = doc.getDocumentElement();\r
 \r
                                        if ( CKEDITOR.env.ie )\r
                                        {\r
@@ -115,7 +116,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                // than firing the selection change event.\r
 \r
                                                var savedRange,\r
-                                                       saveEnabled;\r
+                                                       saveEnabled,\r
+                                                       restoreEnabled = 1;\r
 \r
                                                // "onfocusin" is fired before "onfocus". It makes it\r
                                                // possible to restore the selection before click\r
@@ -131,13 +133,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // point.\r
                                                                if ( savedRange )\r
                                                                {\r
-                                                                       // Well not break because of this.\r
-                                                                       try\r
+                                                                       // Range restored here might invalidate the DOM structure thus break up\r
+                                                                       // the locked selection, give it up. (#6083)\r
+                                                                       var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+                                                                       if ( restoreEnabled && !lockedSelection )\r
                                                                        {\r
-                                                                               savedRange.select();\r
+                                                                               // Well not break because of this.\r
+                                                                               try\r
+                                                                               {\r
+                                                                                       savedRange.select();\r
+                                                                               }\r
+                                                                               catch (e)\r
+                                                                               {}\r
                                                                        }\r
-                                                                       catch (e)\r
-                                                                       {}\r
 \r
                                                                        savedRange = null;\r
                                                                }\r
@@ -146,7 +154,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                body.on( 'focus', function()\r
                                                        {\r
                                                                // Enable selections to be saved.\r
-                                                               saveEnabled = true;\r
+                                                               saveEnabled = 1;\r
 \r
                                                                saveSelection();\r
                                                        });\r
@@ -159,26 +167,83 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                        return;\r
 \r
                                                                // Disable selections from being saved.\r
-                                                               saveEnabled = false;\r
+                                                               saveEnabled = 0;\r
+                                                               restoreEnabled = 1;\r
                                                        });\r
 \r
                                                // IE before version 8 will leave cursor blinking inside the document after\r
                                                // editor blurred unless we clean up the selection. (#4716)\r
                                                if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
                                                {\r
-                                                       doc.getWindow().on( 'blur', function( evt )\r
+                                                       editor.on( 'blur', function( evt )\r
                                                        {\r
-                                                               editor.document.$.selection.empty();\r
+                                                               // Try/Catch to avoid errors if the editor is hidden. (#6375)\r
+                                                               try\r
+                                                               {\r
+                                                                       editor.document && editor.document.$.selection.empty();\r
+                                                               }\r
+                                                               catch (e) {}\r
                                                        });\r
                                                }\r
 \r
+                                               // Listening on document element ensures that\r
+                                               // scrollbar is included. (#5280)\r
+                                               html.on( 'mousedown', function()\r
+                                               {\r
+                                                       // Lock restore selection now, as we have\r
+                                                       // a followed 'click' event which introduce\r
+                                                       // new selection. (#5735)\r
+                                                       restoreEnabled = 0;\r
+                                               });\r
+\r
+                                               html.on( 'mouseup', function()\r
+                                               {\r
+                                                       restoreEnabled = 1;\r
+                                               });\r
+\r
+                                               // In IE6/7 the blinking cursor appears, but contents are\r
+                                               // not editable. (#5634)\r
+                                               if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )\r
+                                               {\r
+                                                       // The 'click' event is not fired when clicking the\r
+                                                       // scrollbars, so we can use it to check whether\r
+                                                       // the empty space following <body> has been clicked.\r
+                                                       html.on( 'click', function( evt )\r
+                                                       {\r
+                                                               if ( evt.data.getTarget().getName() == 'html' )\r
+                                                                       editor.getSelection().getRanges()[ 0 ].select();\r
+                                                       });\r
+                                               }\r
+\r
+                                               var scroll;\r
                                                // IE fires the "selectionchange" event when clicking\r
                                                // inside a selection. We don't want to capture that.\r
-                                               body.on( 'mousedown', disableSave );\r
+                                               body.on( 'mousedown', function( evt )\r
+                                               {\r
+                                                       // IE scrolls document to top on right mousedown\r
+                                                       // when editor has no focus, remember this scroll\r
+                                                       // position and revert it before context menu opens. (#5778)\r
+                                                       if ( evt.data.$.button == 2 )\r
+                                                       {\r
+                                                               var sel = editor.document.$.selection;\r
+                                                               if ( sel.type == 'None' )\r
+                                                                       scroll = editor.window.getScrollPosition();\r
+                                                       }\r
+                                                       disableSave();\r
+                                               });\r
+\r
                                                body.on( 'mouseup',\r
-                                                       function()\r
+                                                       function( evt )\r
                                                        {\r
-                                                               saveEnabled = true;\r
+                                                               // Restore recorded scroll position when needed on right mouseup.\r
+                                                               if ( evt.data.$.button == 2 && scroll )\r
+                                                               {\r
+                                                                       editor.document.$.documentElement.scrollLeft = scroll.x;\r
+                                                                       editor.document.$.documentElement.scrollTop = scroll.y;\r
+                                                               }\r
+                                                               scroll = null;\r
+\r
+                                                               saveEnabled = 1;\r
                                                                setTimeout( function()\r
                                                                        {\r
                                                                                saveSelection( true );\r
@@ -190,7 +255,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                body.on( 'keyup',\r
                                                        function()\r
                                                        {\r
-                                                               saveEnabled = true;\r
+                                                               saveEnabled = 1;\r
                                                                saveSelection();\r
                                                        });\r
 \r
@@ -201,7 +266,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                function disableSave()\r
                                                {\r
-                                                       saveEnabled = false;\r
+                                                       saveEnabled = 0;\r
                                                }\r
 \r
                                                function saveSelection( testIt )\r
@@ -235,9 +300,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                                // Avoid saving selection from within text input. (#5747)\r
                                                                var parentTag;\r
-                                                               if ( nativeSel && nativeSel.type == 'Text'\r
-                                                                       && ( parentTag = nativeSel.createRange().parentElement().nodeName.toLowerCase() )\r
-                                                                       && parentTag in { input: 1, textarea : 1 } )\r
+                                                               if ( nativeSel && nativeSel.type && nativeSel.type != 'Control'\r
+                                                                       && ( parentTag = nativeSel.createRange() )\r
+                                                                       && ( parentTag = parentTag.parentElement() )\r
+                                                                       && ( parentTag = parentTag.nodeName )\r
+                                                                       && parentTag.toLowerCase() in { input: 1, textarea : 1 } )\r
                                                                {\r
                                                                        return;\r
                                                                }\r
@@ -341,7 +408,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return lockedSelection;\r
 \r
                this.document = document;\r
-               this.isLocked = false;\r
+               this.isLocked = 0;\r
                this._ =\r
                {\r
                        cache : {}\r
@@ -475,8 +542,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        return ( cache.type = type );\r
                                },\r
 \r
-               getRanges :\r
-                       CKEDITOR.env.ie ?\r
+               /**\r
+                * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.\r
+                * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns\r
+                * one range for each table cell when one or more table row is selected.\r
+                * @return {Array}\r
+                * @example\r
+                * var ranges = selection.getRanges();\r
+                * alert(ranges.length);\r
+                */\r
+               getRanges : (function()\r
+               {\r
+                       var func = CKEDITOR.env.ie ?\r
                                ( function()\r
                                {\r
                                        // Finds the container and offset for a specific boundary\r
@@ -564,10 +641,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        return function()\r
                                        {\r
-                                               var cache = this._.cache;\r
-                                               if ( cache.ranges )\r
-                                                       return cache.ranges;\r
-\r
                                                // IE doesn't have range support (in the W3C way), so we\r
                                                // need to do some magic to transform selections into\r
                                                // CKEDITOR.dom.range instances.\r
@@ -590,11 +663,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        boundaryInfo = getBoundaryInformation( nativeRange );\r
                                                        range.setEnd( new CKEDITOR.dom.node( boundaryInfo.container ), boundaryInfo.offset );\r
 \r
-                                                       return ( cache.ranges = [ range ] );\r
+                                                       // Correct an invalid IE range case on empty list item. (#5850)\r
+                                                       if ( range.endContainer.getPosition( range.startContainer ) & CKEDITOR.POSITION_PRECEDING\r
+                                                                       && range.endOffset <= range.startContainer.getIndex() )\r
+                                                       {\r
+                                                               range.collapse();\r
+                                                       }\r
+\r
+                                                       return [ range ];\r
                                                }\r
                                                else if ( type == CKEDITOR.SELECTION_ELEMENT )\r
                                                {\r
-                                                       var retval = this._.cache.ranges = [];\r
+                                                       var retval = [];\r
 \r
                                                        for ( var i = 0 ; i < nativeRange.length ; i++ )\r
                                                        {\r
@@ -615,38 +695,140 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        return retval;\r
                                                }\r
 \r
-                                               return ( cache.ranges = [] );\r
+                                               return [];\r
                                        };\r
                                })()\r
                        :\r
                                function()\r
                                {\r
-                                       var cache = this._.cache;\r
-                                       if ( cache.ranges )\r
-                                               return cache.ranges;\r
 \r
                                        // On browsers implementing the W3C range, we simply\r
                                        // tranform the native ranges in CKEDITOR.dom.range\r
                                        // instances.\r
 \r
-                                       var ranges = [];\r
-                                       var sel = this.getNative();\r
+                                       var ranges = [],\r
+                                               range,\r
+                                               doc = this.document,\r
+                                               sel = this.getNative();\r
 \r
                                        if ( !sel )\r
-                                               return [];\r
+                                               return ranges;\r
+\r
+                                       // On WebKit, it may happen that we'll have no selection\r
+                                       // available. We normalize it here by replicating the\r
+                                       // behavior of other browsers.\r
+                                       if ( !sel.rangeCount )\r
+                                       {\r
+                                               range = new CKEDITOR.dom.range( doc );\r
+                                               range.moveToElementEditStart( doc.getBody() );\r
+                                               ranges.push( range );\r
+                                       }\r
 \r
                                        for ( var i = 0 ; i < sel.rangeCount ; i++ )\r
                                        {\r
                                                var nativeRange = sel.getRangeAt( i );\r
-                                               var range = new CKEDITOR.dom.range( this.document );\r
+\r
+                                               range = new CKEDITOR.dom.range( doc );\r
 \r
                                                range.setStart( new CKEDITOR.dom.node( nativeRange.startContainer ), nativeRange.startOffset );\r
                                                range.setEnd( new CKEDITOR.dom.node( nativeRange.endContainer ), nativeRange.endOffset );\r
                                                ranges.push( range );\r
                                        }\r
+                                       return ranges;\r
+                               };\r
 \r
-                                       return ( cache.ranges = ranges );\r
-                               },\r
+                       return function( onlyEditables )\r
+                       {\r
+                               var cache = this._.cache;\r
+                               if ( cache.ranges && !onlyEditables )\r
+                                       return cache.ranges;\r
+                               else if ( !cache.ranges )\r
+                                       cache.ranges = new CKEDITOR.dom.rangeList( func.call( this ) );\r
+\r
+                               // Split range into multiple by read-only nodes.\r
+                               if ( onlyEditables )\r
+                               {\r
+                                       var ranges = cache.ranges;\r
+                                       for ( var i = 0; i < ranges.length; i++ )\r
+                                       {\r
+                                               var range = ranges[ i ];\r
+\r
+                                               // Drop range spans inside one ready-only node.\r
+                                               var parent = range.getCommonAncestor();\r
+                                               if ( parent.isReadOnly() )\r
+                                                       ranges.splice( i, 1 );\r
+\r
+                                               if ( range.collapsed )\r
+                                                       continue;\r
+\r
+                                               var startContainer = range.startContainer,\r
+                                                       endContainer = range.endContainer,\r
+                                                       startOffset = range.startOffset,\r
+                                                       endOffset = range.endOffset,\r
+                                                       walkerRange = range.clone();\r
+\r
+                                               // Range may start inside a non-editable element, restart range\r
+                                               // by the end of it.\r
+                                               var readOnly;\r
+                                               if ( ( readOnly = startContainer.isReadOnly() ) )\r
+                                                       range.setStartAfter( readOnly );\r
+\r
+                                               // Enlarge range start/end with text node to avoid walker\r
+                                               // being DOM destructive, it doesn't interfere our checking\r
+                                               // of elements below as well.\r
+                                               if ( startContainer && startContainer.type == CKEDITOR.NODE_TEXT )\r
+                                               {\r
+                                                       if ( startOffset >= startContainer.getLength() )\r
+                                                               walkerRange.setStartAfter( startContainer );\r
+                                                       else\r
+                                                               walkerRange.setStartBefore( startContainer );\r
+                                               }\r
+\r
+                                               if ( endContainer && endContainer.type == CKEDITOR.NODE_TEXT )\r
+                                               {\r
+                                                       if ( !endOffset )\r
+                                                               walkerRange.setEndBefore( endContainer );\r
+                                                       else\r
+                                                               walkerRange.setEndAfter( endContainer );\r
+                                               }\r
+\r
+                                               // Looking for non-editable element inside the range.\r
+                                               var walker = new CKEDITOR.dom.walker( walkerRange );\r
+                                               walker.evaluator = function( node )\r
+                                               {\r
+                                                       if ( node.type == CKEDITOR.NODE_ELEMENT\r
+                                                               && node.getAttribute( 'contenteditable' ) == 'false' )\r
+                                                       {\r
+                                                               var newRange = range.clone();\r
+                                                               range.setEndBefore( node );\r
+\r
+                                                               // Drop collapsed range around read-only elements,\r
+                                                               // it make sure the range list empty when selecting\r
+                                                               // only non-editable elements.\r
+                                                               if ( range.collapsed )\r
+                                                                       ranges.splice( i--, 1 );\r
+\r
+                                                               // Avoid creating invalid range.\r
+                                                               if ( !( node.getPosition( walkerRange.endContainer ) & CKEDITOR.POSITION_CONTAINS ) )\r
+                                                               {\r
+                                                                       newRange.setStartAfter( node );\r
+                                                                       if ( !newRange.collapsed )\r
+                                                                               ranges.splice( i + 1, 0, newRange );\r
+                                                               }\r
+\r
+                                                               return true;\r
+                                                       }\r
+\r
+                                                       return false;\r
+                                               };\r
+\r
+                                               walker.next();\r
+                                       }\r
+                               }\r
+\r
+                               return cache.ranges;\r
+                       };\r
+               })(),\r
 \r
                /**\r
                 * Gets the DOM element in which the selection starts.\r
@@ -683,7 +865,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // Decrease the range content to exclude particial\r
                                                        // selected node on the start which doesn't have\r
                                                        // visual impact. ( #3231 )\r
-                                                       while ( true )\r
+                                                       while ( 1 )\r
                                                        {\r
                                                                var startContainer = range.startContainer,\r
                                                                        startOffset = range.startOffset;\r
@@ -703,32 +885,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        node = node.getChild( range.startOffset );\r
 \r
                                                        if ( !node || node.type != CKEDITOR.NODE_ELEMENT )\r
-                                                               return range.startContainer;\r
-\r
-                                                       var child = node.getFirst();\r
-                                                       while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                                               node = range.startContainer;\r
+                                                       else\r
                                                        {\r
-                                                               node = child;\r
-                                                               child = child.getFirst();\r
+                                                               var child = node.getFirst();\r
+                                                               while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                                               {\r
+                                                                       node = child;\r
+                                                                       child = child.getFirst();\r
+                                                               }\r
                                                        }\r
-\r
-                                                       return node;\r
                                                }\r
-                                       }\r
-\r
-                                       if ( CKEDITOR.env.ie )\r
-                                       {\r
-                                               range = sel.createRange();\r
-                                               range.collapse( true );\r
-\r
-                                               node = range.parentElement();\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               node = sel.anchorNode;\r
+                                               else\r
+                                               {\r
+                                                       node = range.startContainer;\r
+                                                       if ( node.type != CKEDITOR.NODE_ELEMENT )\r
+                                                               node = node.getParent();\r
+                                               }\r
 \r
-                                               if ( node && node.nodeType != 1 )\r
-                                                       node = node.parentNode;\r
+                                               node = node.$;\r
                                        }\r
                        }\r
 \r
@@ -792,7 +967,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // The native selection is not available when locked.\r
                        this._.cache.nativeSel = {};\r
 \r
-                       this.isLocked = true;\r
+                       this.isLocked = 1;\r
 \r
                        // Save this selection inside the DOM document.\r
                        this.document.setCustomData( 'cke_locked_selection', this );\r
@@ -812,7 +987,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        var selectedElement = lockedSelection.getSelectedElement(),\r
                                                ranges = !selectedElement && lockedSelection.getRanges();\r
 \r
-                                       this.isLocked = false;\r
+                                       this.isLocked = 0;\r
                                        this.reset();\r
 \r
                                        doc.getBody().focus();\r
@@ -826,7 +1001,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        if  ( !lockedSelection || !restore )\r
                        {\r
-                               this.isLocked = false;\r
+                               this.isLocked = 0;\r
                                this.reset();\r
                        }\r
                },\r
@@ -836,6 +1011,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        this._.cache = {};\r
                },\r
 \r
+               /**\r
+                *  Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.\r
+                * @param element\r
+                */\r
                selectElement : function( element )\r
                {\r
                        if ( this.isLocked )\r
@@ -846,7 +1025,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                this._.cache.selectedElement = element;\r
                                this._.cache.startElement = element;\r
-                               this._.cache.ranges = [ range ];\r
+                               this._.cache.ranges = new CKEDITOR.dom.rangeList( range );\r
                                this._.cache.type = CKEDITOR.SELECTION_ELEMENT;\r
 \r
                                return;\r
@@ -863,7 +1042,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        range.addElement( element.$ );\r
                                        range.select();\r
                                }\r
-                               catch(e)\r
+                               catch( e )\r
                                {\r
                                        // If failed, select it as a text range.\r
                                        range = this.document.$.body.createTextRange();\r
@@ -892,13 +1071,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                },\r
 \r
+               /**\r
+                *  Adding the specified ranges to document selection preceding\r
+                * by clearing up the original selection.\r
+                * @param {CKEDITOR.dom.range} ranges\r
+                */\r
                selectRanges : function( ranges )\r
                {\r
                        if ( this.isLocked )\r
                        {\r
                                this._.cache.selectedElement = null;\r
-                               this._.cache.startElement = ranges[ 0 ].getTouchedStartNode();\r
-                               this._.cache.ranges = ranges;\r
+                               this._.cache.startElement = ranges[ 0 ] && ranges[ 0 ].getTouchedStartNode();\r
+                               this._.cache.ranges = new CKEDITOR.dom.rangeList( ranges );\r
                                this._.cache.type = CKEDITOR.SELECTION_TEXT;\r
 \r
                                return;\r
@@ -906,8 +1090,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        if ( CKEDITOR.env.ie )\r
                        {\r
-                               // IE doesn't accept multiple ranges selection, so we just\r
-                               // select the first one.\r
+                               if ( ranges.length > 1 )\r
+                               {\r
+                                       // IE doesn't accept multiple ranges selection, so we join all into one.\r
+                                       var last = ranges[ ranges.length -1 ] ;\r
+                                       ranges[ 0 ].setEnd( last.endContainer, last.endOffset );\r
+                                       ranges.length = 1;\r
+                               }\r
+\r
                                if ( ranges[ 0 ] )\r
                                        ranges[ 0 ].select();\r
 \r
@@ -916,10 +1106,35 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        else\r
                        {\r
                                var sel = this.getNative();\r
-                               sel.removeAllRanges();\r
+\r
+                               if ( ranges.length )\r
+                                       sel.removeAllRanges();\r
 \r
                                for ( var i = 0 ; i < ranges.length ; i++ )\r
                                {\r
+                                       // Joining sequential ranges introduced by\r
+                                       // readonly elements protection.\r
+                                       if ( i < ranges.length -1 )\r
+                                       {\r
+                                               var left = ranges[ i ], right = ranges[ i +1 ],\r
+                                                               between = left.clone();\r
+                                               between.setStart( left.endContainer, left.endOffset );\r
+                                               between.setEnd( right.startContainer, right.startOffset );\r
+\r
+                                               // Don't confused by Firefox adjancent multi-ranges\r
+                                               // introduced by table cells selection.\r
+                                               if ( !between.collapsed )\r
+                                               {\r
+                                                       between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+                                                       if ( between.getCommonAncestor().isReadOnly())\r
+                                                       {\r
+                                                               right.setStart( left.startContainer, left.startOffset );\r
+                                                               ranges.splice( i--, 1 );\r
+                                                               continue;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+\r
                                        var range = ranges[ i ];\r
                                        var nativeRange = this.document.$.createRange();\r
                                        var startContainer = range.startContainer;\r
@@ -927,8 +1142,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        // In FF2, if we have a collapsed range, inside an empty\r
                                        // element, we must add something to it otherwise the caret\r
                                        // will not be visible.\r
+                                       // In Opera instead, the selection will be moved out of the\r
+                                       // element. (#4657)\r
                                        if ( range.collapsed &&\r
-                                               ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&\r
+                                               ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&\r
                                                startContainer.type == CKEDITOR.NODE_ELEMENT &&\r
                                                !startContainer.getChildCount() )\r
                                        {\r
@@ -946,49 +1163,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                },\r
 \r
+               /**\r
+                *  Create bookmark for every single of this selection range (from #getRanges)\r
+                * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,\r
+                * with extra cares to avoid interferon among those ranges. Same arguments are\r
+                * received as with the underlay range method.\r
+                */\r
                createBookmarks : function( serializable )\r
                {\r
-                       var retval = [],\r
-                               ranges = this.getRanges(),\r
-                               length = ranges.length,\r
-                               bookmark;\r
-                       for ( var i = 0; i < length ; i++ )\r
-                       {\r
-                           retval.push( bookmark = ranges[ i ].createBookmark( serializable, true ) );\r
-\r
-                               serializable = bookmark.serializable;\r
-\r
-                               var bookmarkStart = serializable ? this.document.getById( bookmark.startNode ) : bookmark.startNode,\r
-                                       bookmarkEnd = serializable ? this.document.getById( bookmark.endNode ) : bookmark.endNode;\r
-\r
-                           // Updating the offset values for rest of ranges which have been mangled(#3256).\r
-                           for ( var j = i + 1 ; j < length ; j++ )\r
-                           {\r
-                               var dirtyRange = ranges[ j ],\r
-                                      rangeStart = dirtyRange.startContainer,\r
-                                      rangeEnd = dirtyRange.endContainer;\r
-\r
-                              rangeStart.equals( bookmarkStart.getParent() ) && dirtyRange.startOffset++;\r
-                              rangeStart.equals( bookmarkEnd.getParent() ) && dirtyRange.startOffset++;\r
-                              rangeEnd.equals( bookmarkStart.getParent() ) && dirtyRange.endOffset++;\r
-                              rangeEnd.equals( bookmarkEnd.getParent() ) && dirtyRange.endOffset++;\r
-                           }\r
-                       }\r
-\r
-                       return retval;\r
+                       return this.getRanges().createBookmarks( serializable );\r
                },\r
 \r
+               /**\r
+                *  Create bookmark for every single of this selection range (from #getRanges)\r
+                * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,\r
+                * with extra cares to avoid interferon among those ranges. Same arguments are\r
+                * received as with the underlay range method.\r
+                */\r
                createBookmarks2 : function( normalized )\r
                {\r
-                       var bookmarks = [],\r
-                               ranges = this.getRanges();\r
-\r
-                       for ( var i = 0 ; i < ranges.length ; i++ )\r
-                               bookmarks.push( ranges[i].createBookmark2( normalized ) );\r
-\r
-                       return bookmarks;\r
+                       return this.getRanges().createBookmarks2( normalized );\r
                },\r
 \r
+               /**\r
+                * Select the virtual ranges denote by the bookmarks by calling #selectRanges.\r
+                * @param bookmarks\r
+                */\r
                selectBookmarks : function( bookmarks )\r
                {\r
                        var ranges = [];\r
@@ -1002,6 +1202,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return this;\r
                },\r
 \r
+               /**\r
+                * Retrieve the common ancestor node of the first range and the last range.\r
+                */\r
                getCommonAncestor : function()\r
                {\r
                        var ranges = this.getRanges(),\r
@@ -1010,7 +1213,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return startNode.getCommonAncestor( endNode );\r
                },\r
 \r
-               // Moving scroll bar to the current selection's start position.\r
+               /**\r
+                * Moving scroll bar to the current selection's start position.\r
+                */\r
                scrollIntoView : function()\r
                {\r
                        // If we have split the block, adds a temporary span at the\r
@@ -1020,150 +1225,151 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                }\r
        };\r
 })();\r
+\r
 ( function()\r
 {\r
-var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true );\r
-var fillerTextRegex = /\ufeff|\u00a0/;\r
-var nonCells = { table:1,tbody:1,tr:1 };\r
-\r
-CKEDITOR.dom.range.prototype.select =\r
-       CKEDITOR.env.ie ?\r
-               // V2\r
-               function( forceExpand )\r
-               {\r
-                       var collapsed = this.collapsed;\r
-                       var isStartMarkerAlone;\r
-                       var dummySpan;\r
-\r
-                       // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.\r
-                       // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...\r
-                       if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
-                               || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
+       var notWhitespaces = CKEDITOR.dom.walker.whitespaces( true ),\r
+                       fillerTextRegex = /\ufeff|\u00a0/,\r
+                       nonCells = { table:1,tbody:1,tr:1 };\r
+\r
+       CKEDITOR.dom.range.prototype.select =\r
+               CKEDITOR.env.ie ?\r
+                       // V2\r
+                       function( forceExpand )\r
                        {\r
-                               this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
-                       }\r
+                               var collapsed = this.collapsed;\r
+                               var isStartMarkerAlone;\r
+                               var dummySpan;\r
+\r
+                               // IE doesn't support selecting the entire table row/cell, move the selection into cells, e.g.\r
+                               // <table><tbody><tr>[<td>cell</b></td>... => <table><tbody><tr><td>[cell</td>...\r
+                               if ( this.startContainer.type == CKEDITOR.NODE_ELEMENT && this.startContainer.getName() in nonCells\r
+                                       || this.endContainer.type == CKEDITOR.NODE_ELEMENT && this.endContainer.getName() in nonCells )\r
+                               {\r
+                                       this.shrink( CKEDITOR.NODE_ELEMENT, true );\r
+                               }\r
 \r
-                       var bookmark = this.createBookmark();\r
+                               var bookmark = this.createBookmark();\r
 \r
-                       // Create marker tags for the start and end boundaries.\r
-                       var startNode = bookmark.startNode;\r
+                               // Create marker tags for the start and end boundaries.\r
+                               var startNode = bookmark.startNode;\r
 \r
-                       var endNode;\r
-                       if ( !collapsed )\r
-                               endNode = bookmark.endNode;\r
+                               var endNode;\r
+                               if ( !collapsed )\r
+                                       endNode = bookmark.endNode;\r
 \r
-                       // Create the main range which will be used for the selection.\r
-                       var ieRange = this.document.$.body.createTextRange();\r
+                               // Create the main range which will be used for the selection.\r
+                               var ieRange = this.document.$.body.createTextRange();\r
 \r
-                       // Position the range at the start boundary.\r
-                       ieRange.moveToElementText( startNode.$ );\r
-                       ieRange.moveStart( 'character', 1 );\r
+                               // Position the range at the start boundary.\r
+                               ieRange.moveToElementText( startNode.$ );\r
+                               ieRange.moveStart( 'character', 1 );\r
 \r
-                       if ( endNode )\r
-                       {\r
-                               // Create a tool range for the end.\r
-                               var ieRangeEnd = this.document.$.body.createTextRange();\r
+                               if ( endNode )\r
+                               {\r
+                                       // Create a tool range for the end.\r
+                                       var ieRangeEnd = this.document.$.body.createTextRange();\r
 \r
-                               // Position the tool range at the end.\r
-                               ieRangeEnd.moveToElementText( endNode.$ );\r
+                                       // Position the tool range at the end.\r
+                                       ieRangeEnd.moveToElementText( endNode.$ );\r
 \r
-                               // Move the end boundary of the main range to match the tool range.\r
-                               ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
-                               ieRange.moveEnd( 'character', -1 );\r
-                       }\r
-                       else\r
-                       {\r
-                               // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
-                               // will expand and that the cursor will be blinking on the right place.\r
-                               // Actually, we are using this flag just to avoid using this hack in all\r
-                               // situations, but just on those needed.\r
-                               var next = startNode.getNext( notWhitespaces );\r
-                               isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) )     // already a filler there?\r
-                                                                         && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
-\r
-                               // Append a temporary <span>&#65279;</span> before the selection.\r
-                               // This is needed to avoid IE destroying selections inside empty\r
-                               // inline elements, like <b></b> (#253).\r
-                               // It is also needed when placing the selection right after an inline\r
-                               // element to avoid the selection moving inside of it.\r
-                               dummySpan = this.document.createElement( 'span' );\r
-                               dummySpan.setHtml( '&#65279;' );        // Zero Width No-Break Space (U+FEFF). See #1359.\r
-                               dummySpan.insertBefore( startNode );\r
-\r
-                               if ( isStartMarkerAlone )\r
+                                       // Move the end boundary of the main range to match the tool range.\r
+                                       ieRange.setEndPoint( 'EndToEnd', ieRangeEnd );\r
+                                       ieRange.moveEnd( 'character', -1 );\r
+                               }\r
+                               else\r
                                {\r
-                                       // To expand empty blocks or line spaces after <br>, we need\r
-                                       // instead to have any char, which will be later deleted using the\r
-                                       // selection.\r
-                                       // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
-                                       this.document.createText( '\ufeff' ).insertBefore( startNode );\r
+                                       // The isStartMarkerAlone logic comes from V2. It guarantees that the lines\r
+                                       // will expand and that the cursor will be blinking on the right place.\r
+                                       // Actually, we are using this flag just to avoid using this hack in all\r
+                                       // situations, but just on those needed.\r
+                                       var next = startNode.getNext( notWhitespaces );\r
+                                       isStartMarkerAlone = ( !( next && next.getText && next.getText().match( fillerTextRegex ) )     // already a filler there?\r
+                                                                                 && ( forceExpand || !startNode.hasPrevious() || ( startNode.getPrevious().is && startNode.getPrevious().is( 'br' ) ) ) );\r
+\r
+                                       // Append a temporary <span>&#65279;</span> before the selection.\r
+                                       // This is needed to avoid IE destroying selections inside empty\r
+                                       // inline elements, like <b></b> (#253).\r
+                                       // It is also needed when placing the selection right after an inline\r
+                                       // element to avoid the selection moving inside of it.\r
+                                       dummySpan = this.document.createElement( 'span' );\r
+                                       dummySpan.setHtml( '&#65279;' );        // Zero Width No-Break Space (U+FEFF). See #1359.\r
+                                       dummySpan.insertBefore( startNode );\r
+\r
+                                       if ( isStartMarkerAlone )\r
+                                       {\r
+                                               // To expand empty blocks or line spaces after <br>, we need\r
+                                               // instead to have any char, which will be later deleted using the\r
+                                               // selection.\r
+                                               // \ufeff = Zero Width No-Break Space (U+FEFF). (#1359)\r
+                                               this.document.createText( '\ufeff' ).insertBefore( startNode );\r
+                                       }\r
                                }\r
-                       }\r
 \r
-                       // Remove the markers (reset the position, because of the changes in the DOM tree).\r
-                       this.setStartBefore( startNode );\r
-                       startNode.remove();\r
+                               // Remove the markers (reset the position, because of the changes in the DOM tree).\r
+                               this.setStartBefore( startNode );\r
+                               startNode.remove();\r
 \r
-                       if ( collapsed )\r
-                       {\r
-                               if ( isStartMarkerAlone )\r
+                               if ( collapsed )\r
                                {\r
-                                       // Move the selection start to include the temporary \ufeff.\r
-                                       ieRange.moveStart( 'character', -1 );\r
+                                       if ( isStartMarkerAlone )\r
+                                       {\r
+                                               // Move the selection start to include the temporary \ufeff.\r
+                                               ieRange.moveStart( 'character', -1 );\r
 \r
-                                       ieRange.select();\r
+                                               ieRange.select();\r
+\r
+                                               // Remove our temporary stuff.\r
+                                               this.document.$.selection.clear();\r
+                                       }\r
+                                       else\r
+                                               ieRange.select();\r
 \r
-                                       // Remove our temporary stuff.\r
-                                       this.document.$.selection.clear();\r
+                                       this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
+                                       dummySpan.remove();\r
                                }\r
                                else\r
+                               {\r
+                                       this.setEndBefore( endNode );\r
+                                       endNode.remove();\r
                                        ieRange.select();\r
+                               }\r
 \r
-                               this.moveToPosition( dummySpan, CKEDITOR.POSITION_BEFORE_START );\r
-                               dummySpan.remove();\r
+                               this.document.fire( 'selectionchange' );\r
                        }\r
-                       else\r
+               :\r
+                       function()\r
                        {\r
-                               this.setEndBefore( endNode );\r
-                               endNode.remove();\r
-                               ieRange.select();\r
-                       }\r
-\r
-                       this.document.fire( 'selectionchange' );\r
-               }\r
-       :\r
-               function()\r
-               {\r
-                       var startContainer = this.startContainer;\r
+                               var startContainer = this.startContainer;\r
 \r
-                       // If we have a collapsed range, inside an empty element, we must add\r
-                       // something to it, otherwise the caret will not be visible.\r
-                       if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )\r
-                               startContainer.append( new CKEDITOR.dom.text( '' ) );\r
+                               // If we have a collapsed range, inside an empty element, we must add\r
+                               // something to it, otherwise the caret will not be visible.\r
+                               if ( this.collapsed && startContainer.type == CKEDITOR.NODE_ELEMENT && !startContainer.getChildCount() )\r
+                                       startContainer.append( new CKEDITOR.dom.text( '' ) );\r
 \r
-                       var nativeRange = this.document.$.createRange();\r
-                       nativeRange.setStart( startContainer.$, this.startOffset );\r
+                               var nativeRange = this.document.$.createRange();\r
+                               nativeRange.setStart( startContainer.$, this.startOffset );\r
 \r
-                       try\r
-                       {\r
-                               nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
-                       }\r
-                       catch ( e )\r
-                       {\r
-                               // There is a bug in Firefox implementation (it would be too easy\r
-                               // otherwise). The new start can't be after the end (W3C says it can).\r
-                               // So, let's create a new range and collapse it to the desired point.\r
-                               if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
+                               try\r
                                {\r
-                                       this.collapse( true );\r
                                        nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
                                }\r
-                               else\r
-                                       throw( e );\r
-                       }\r
+                               catch ( e )\r
+                               {\r
+                                       // There is a bug in Firefox implementation (it would be too easy\r
+                                       // otherwise). The new start can't be after the end (W3C says it can).\r
+                                       // So, let's create a new range and collapse it to the desired point.\r
+                                       if ( e.toString().indexOf( 'NS_ERROR_ILLEGAL_VALUE' ) >= 0 )\r
+                                       {\r
+                                               this.collapse( true );\r
+                                               nativeRange.setEnd( this.endContainer.$, this.endOffset );\r
+                                       }\r
+                                       else\r
+                                               throw( e );\r
+                               }\r
 \r
-                       var selection = this.document.getSelection().getNative();\r
-                       selection.removeAllRanges();\r
-                       selection.addRange( nativeRange );\r
-               };\r
+                               var selection = this.document.getSelection().getNative();\r
+                               selection.removeAllRanges();\r
+                               selection.addRange( nativeRange );\r
+                       };\r
 } )();\r