JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.2
[ckeditor.git] / _source / plugins / selection / plugin.js
index 3d707fc..fd8332e 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, 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
@@ -79,20 +79,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                case 'wysiwyg' :\r
                                        editor.document.$.execCommand( 'SelectAll', false, null );\r
+                                       // Force triggering selectionChange (#7008)\r
+                                       editor.forceNextSelectionCheck();\r
+                                       editor.selectionChange();\r
                                        break;\r
                                case 'source' :\r
                                        // Select the contents of the textarea\r
-                                       var textarea = editor.textarea.$ ;\r
+                                       var textarea = editor.textarea.$;\r
                                        if ( CKEDITOR.env.ie )\r
-                                       {\r
-                                               textarea.createTextRange().execCommand( 'SelectAll' ) ;\r
-                                       }\r
+                                               textarea.createTextRange().execCommand( 'SelectAll' );\r
                                        else\r
                                        {\r
-                                               textarea.selectionStart = 0 ;\r
-                                               textarea.selectionEnd = textarea.value.length ;\r
+                                               textarea.selectionStart = 0;\r
+                                               textarea.selectionEnd = textarea.value.length;\r
                                        }\r
-                                       textarea.focus() ;\r
+                                       textarea.focus();\r
                        }\r
                },\r
                canUndo : false\r
@@ -133,7 +134,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // point.\r
                                                                if ( savedRange )\r
                                                                {\r
-                                                                       if ( restoreEnabled )\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
                                                                                // Well not break because of this.\r
                                                                                try\r
@@ -151,7 +155,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
@@ -164,7 +168,7 @@ 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
@@ -174,13 +178,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                {\r
                                                        editor.on( 'blur', function( evt )\r
                                                        {\r
-                                                               editor.document && 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
+                                               html.on( 'mousedown', function()\r
                                                {\r
                                                        // Lock restore selection now, as we have\r
                                                        // a followed 'click' event which introduce\r
@@ -188,7 +197,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        restoreEnabled = 0;\r
                                                });\r
 \r
-                                               html.on( 'mouseup', function ()\r
+                                               html.on( 'mouseup', function()\r
                                                {\r
                                                        restoreEnabled = 1;\r
                                                });\r
@@ -207,17 +216,35 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        });\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', function ()\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
@@ -229,7 +256,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
@@ -240,7 +267,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
@@ -300,6 +327,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        }\r
                                });\r
 \r
+                       // Clear the cached range path before unload. (#7174)\r
+                       editor.on( 'contentDomUnload', editor.forceNextSelectionCheck, editor );\r
+\r
                        editor.addCommand( 'selectAll', selectAllCmd );\r
                        editor.ui.addButton( 'SelectAll',\r
                                {\r
@@ -382,7 +412,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
@@ -525,10 +555,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * var ranges = selection.getRanges();\r
                 * alert(ranges.length);\r
                 */\r
-               getRanges :\r
-                       CKEDITOR.env.ie ?\r
+               getRanges : (function()\r
+               {\r
+                       var func = CKEDITOR.env.ie ?\r
                                ( function()\r
                                {\r
+                                       function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }\r
+\r
                                        // Finds the container and offset for a specific boundary\r
                                        // of an IE range.\r
                                        var getBoundaryInformation = function( range, start )\r
@@ -539,85 +572,108 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // Gets the element that encloses the range entirely.\r
                                                var parent = range.parentElement();\r
-                                               var siblings = parent.childNodes;\r
-\r
-                                               var testRange;\r
 \r
-                                               for ( var i = 0 ; i < siblings.length ; i++ )\r
+                                               // Empty parent element, e.g. <i>^</i>\r
+                                               if ( !parent.hasChildNodes() )\r
+                                                       return  { container : parent, offset : 0 };\r
+\r
+                                               var siblings = parent.children,\r
+                                                       child,\r
+                                                       testRange = range.duplicate(),\r
+                                                       startIndex = 0,\r
+                                                       endIndex = siblings.length - 1,\r
+                                                       index = -1,\r
+                                                       position,\r
+                                                       distance;\r
+\r
+                                               // Binary search over all element childs to test the range to see whether\r
+                                               // range is right on the boundary of one element.\r
+                                               while ( startIndex <= endIndex )\r
                                                {\r
-                                                       var child = siblings[ i ];\r
-                                                       if ( child.nodeType == 1 )\r
-                                                       {\r
-                                                               testRange = range.duplicate();\r
+                                                       index = Math.floor( ( startIndex + endIndex ) / 2 );\r
+                                                       child = siblings[ index ];\r
+                                                       testRange.moveToElementText( child );\r
+                                                       position = testRange.compareEndPoints( 'StartToStart', range );\r
+\r
+                                                       if ( position > 0 )\r
+                                                               endIndex = index - 1;\r
+                                                       else if ( position < 0 )\r
+                                                               startIndex = index + 1;\r
+                                                       else\r
+                                                               return { container : parent, offset : getNodeIndex( child ) };\r
+                                               }\r
 \r
-                                                               testRange.moveToElementText( child );\r
+                                               // All childs are text nodes,\r
+                                               // or to the right hand of test range are all text nodes. (#6992)\r
+                                               if ( index == -1 || index == siblings.length - 1 && position < 0 )\r
+                                               {\r
+                                                       // Adapt test range to embrace the entire parent contents.\r
+                                                       testRange.moveToElementText( parent );\r
+                                                       testRange.setEndPoint( 'StartToStart', range );\r
 \r
-                                                               var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),\r
-                                                                       comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );\r
+                                                       // IE report line break as CRLF with range.text but\r
+                                                       // only LF with textnode.nodeValue, normalize them to avoid\r
+                                                       // breaking character counting logic below. (#3949)\r
+                                                       distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
 \r
-                                                               testRange.collapse();\r
+                                                       siblings = parent.childNodes;\r
 \r
-                                                               if ( comparisonStart > 0 )\r
-                                                                       break;\r
-                                                               // When selection stay at the side of certain self-closing elements, e.g. BR,\r
-                                                               // our comparison will never shows an equality. (#4824)\r
-                                                               else if ( !comparisonStart\r
-                                                                       || comparisonEnd == 1 && comparisonStart == -1 )\r
-                                                                       return { container : parent, offset : i };\r
-                                                               else if ( !comparisonEnd )\r
-                                                                       return { container : parent, offset : i + 1 };\r
+                                                       // Actual range anchor right beside test range at the boundary of text node.\r
+                                                       if ( !distance )\r
+                                                       {\r
+                                                               child = siblings[ siblings.length - 1 ];\r
 \r
-                                                               testRange = null;\r
+                                                               if ( child.nodeType == CKEDITOR.NODE_ELEMENT )\r
+                                                                       return { container : parent, offset : siblings.length };\r
+                                                               else\r
+                                                                       return { container : child, offset : child.nodeValue.length };\r
                                                        }\r
-                                               }\r
 \r
-                                               if ( !testRange )\r
-                                               {\r
-                                                       testRange = range.duplicate();\r
-                                                       testRange.moveToElementText( parent );\r
-                                                       testRange.collapse( false );\r
-                                               }\r
-\r
-                                               testRange.setEndPoint( 'StartToStart', range );\r
-                                               // IE report line break as CRLF with range.text but\r
-                                               // only LF with textnode.nodeValue, normalize them to avoid\r
-                                               // breaking character counting logic below. (#3949)\r
-                                               var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
-\r
-                                               try\r
-                                               {\r
+                                                       // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+                                                       var i = siblings.length;\r
                                                        while ( distance > 0 )\r
                                                                distance -= siblings[ --i ].nodeValue.length;\r
-                                               }\r
-                                               // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
-                                               catch( e )\r
-                                               {\r
-                                                       distance = 0;\r
-                                               }\r
 \r
-\r
-                                               if ( distance === 0 )\r
-                                               {\r
-                                                       return {\r
-                                                               container : parent,\r
-                                                               offset : i\r
-                                                       };\r
+                                                       return  { container : siblings[ i ], offset : -distance };\r
                                                }\r
+                                               // Test range was one offset beyond OR behind the anchored text node.\r
                                                else\r
                                                {\r
-                                                       return {\r
-                                                               container : siblings[ i ],\r
-                                                               offset : -distance\r
-                                                       };\r
+                                                       // Adapt one side of test range to the actual range\r
+                                                       // for measuring the offset between them.\r
+                                                       testRange.collapse( position > 0 ? true : false );\r
+                                                       testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );\r
+\r
+                                                       // IE report line break as CRLF with range.text but\r
+                                                       // only LF with textnode.nodeValue, normalize them to avoid\r
+                                                       // breaking character counting logic below. (#3949)\r
+                                                       distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
+\r
+                                                       // Actual range anchor right beside test range at the inner boundary of text node.\r
+                                                       if ( !distance )\r
+                                                               return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };\r
+\r
+                                                       // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+                                                       while ( distance > 0 )\r
+                                                       {\r
+                                                               child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+                                                               try\r
+                                                               {\r
+                                                                       distance -= child.nodeValue.length;\r
+                                                               }\r
+                                                               // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
+                                                               catch( e )\r
+                                                               {\r
+                                                                       return { container : parent, offset : getNodeIndex( child ) };\r
+                                                               }\r
+                                                       }\r
+\r
+                                                       return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };\r
                                                }\r
                                        };\r
 \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
@@ -640,11 +696,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
@@ -665,38 +728,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.isReadOnly() )\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
@@ -733,7 +898,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
@@ -753,32 +918,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
@@ -842,7 +1000,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
@@ -862,7 +1020,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
@@ -876,7 +1034,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
@@ -900,7 +1058,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
@@ -917,7 +1075,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
@@ -956,8 +1114,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        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
@@ -965,8 +1123,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
@@ -975,10 +1139,41 @@ 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
+                                                       var ancestor = between.getCommonAncestor(),\r
+                                                               enclosed = between.getEnclosedNode();\r
+\r
+                                                       // The following cases has to be considered:\r
+                                                       // 1. <span contenteditable="false">[placeholder]</span>\r
+                                                       // 2. <input contenteditable="false"  type="radio"/> (#6621)\r
+                                                       if ( ancestor.isReadOnly() || enclosed && enclosed.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
@@ -986,8 +1181,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
@@ -1013,34 +1210,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 */\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
@@ -1051,13 +1221,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 */\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
@@ -1244,7 +1408,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\r
 \r
                                var selection = this.document.getSelection().getNative();\r
-                               selection.removeAllRanges();\r
-                               selection.addRange( nativeRange );\r
+                               // getSelection() returns null in case when iframe is "display:none" in FF. (#6577)\r
+                               if ( selection )\r
+                               {\r
+                                       selection.removeAllRanges();\r
+                                       selection.addRange( nativeRange );\r
+                               }\r
                        };\r
 } )();\r