JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.1
[ckeditor.git] / _source / plugins / selection / plugin.js
index d4a92f8..d8888c1 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
@@ -70,38 +70,173 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
        // #### checkSelectionChange : END\r
 \r
+       function rangeRequiresFix( range )\r
+       {\r
+               function isInlineCt( node )\r
+               {\r
+                       return node && node.type == CKEDITOR.NODE_ELEMENT\r
+                                       && node.getName() in CKEDITOR.dtd.$removeEmpty;\r
+               }\r
+\r
+               function singletonBlock( node )\r
+               {\r
+                       var body = range.document.getBody();\r
+                       return !node.is( 'body' ) && body.getChildCount() == 1;\r
+               }\r
+\r
+               var start = range.startContainer,\r
+                       offset = range.startOffset;\r
+\r
+               if ( start.type == CKEDITOR.NODE_TEXT )\r
+                       return false;\r
+\r
+               // 1. Empty inline element. <span>^</span>\r
+               // 2. Adjoin to inline element. <p><strong>text</strong>^</p>\r
+               // 3. The only empty block in document. <body><p>^</p></body> (#7222)\r
+               return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) || singletonBlock( start )\r
+                               : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );\r
+       }\r
+\r
        var selectAllCmd =\r
        {\r
                modes : { wysiwyg : 1, source : 1 },\r
+               readOnly : CKEDITOR.env.ie || CKEDITOR.env.webkit,\r
                exec : function( editor )\r
                {\r
                        switch ( editor.mode )\r
                        {\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
        };\r
 \r
+       function createFillingChar( doc )\r
+       {\r
+               removeFillingChar( doc );\r
+\r
+               var fillingChar = doc.createText( '\u200B' );\r
+               doc.setCustomData( 'cke-fillingChar', fillingChar );\r
+\r
+               return fillingChar;\r
+       }\r
+\r
+       function getFillingChar( doc )\r
+       {\r
+               return doc && doc.getCustomData( 'cke-fillingChar' );\r
+       }\r
+\r
+       // Checks if a filling char has been used, eventualy removing it (#1272).\r
+       function checkFillingChar( doc )\r
+       {\r
+               var fillingChar = doc && getFillingChar( doc );\r
+               if ( fillingChar )\r
+               {\r
+                       // Use this flag to avoid removing the filling char right after\r
+                       // creating it.\r
+                       if ( fillingChar.getCustomData( 'ready' ) )\r
+                               removeFillingChar( doc );\r
+                       else\r
+                               fillingChar.setCustomData( 'ready', 1 );\r
+               }\r
+       }\r
+\r
+       function removeFillingChar( doc )\r
+       {\r
+               var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );\r
+               if ( fillingChar )\r
+               {\r
+                       // We can't simply remove the filling node because the user\r
+                       // will actually enlarge it when typing, so we just remove the\r
+                       // invisible char from it.\r
+                       fillingChar.setText( fillingChar.getText().replace( /\u200B/g, '' ) );\r
+                       fillingChar = 0;\r
+               }\r
+       }\r
+\r
        CKEDITOR.plugins.add( 'selection',\r
        {\r
                init : function( editor )\r
                {\r
+                       // On WebKit only, we need a special "filling" char on some situations\r
+                       // (#1272). Here we set the events that should invalidate that char.\r
+                       if ( CKEDITOR.env.webkit )\r
+                       {\r
+                               editor.on( 'selectionChange', function() { checkFillingChar( editor.document ); } );\r
+                               editor.on( 'beforeSetMode', function() { removeFillingChar( editor.document ); } );\r
+                               editor.on( 'key', function( e )\r
+                                       {\r
+                                               // Remove the filling char before some keys get\r
+                                               // executed, so they'll not get blocked by it.\r
+                                               switch ( e.data.keyCode )\r
+                                               {\r
+                                                       case 13 :       // ENTER\r
+                                                       case CKEDITOR.SHIFT + 13 :      // SHIFT-ENTER\r
+                                                       case 37 :       // LEFT-ARROW\r
+                                                       case 39 :       // RIGHT-ARROW\r
+                                                       case 8 :        // BACKSPACE\r
+                                                               removeFillingChar( editor.document );\r
+                                               }\r
+                                       }, null, null, 10 );\r
+\r
+                               var fillingCharBefore,\r
+                                       resetSelection;\r
+\r
+                               function beforeData()\r
+                               {\r
+                                       var doc = editor.document,\r
+                                               fillingChar = getFillingChar( doc );\r
+\r
+                                       if ( fillingChar )\r
+                                       {\r
+                                               // If cursor is right blinking by side of the filler node, save it for restoring,\r
+                                               // as the following text substitution will blind it. (#7437)\r
+                                               var sel = doc.$.defaultView.getSelection();\r
+                                               if ( sel.type == 'Caret' && sel.anchorNode == fillingChar.$ )\r
+                                                       resetSelection = 1;\r
+\r
+                                               fillingCharBefore = fillingChar.getText();\r
+                                               fillingChar.setText( fillingCharBefore.replace( /\u200B/g, '' ) );\r
+                                       }\r
+                               }\r
+                               function afterData()\r
+                               {\r
+                                       var doc = editor.document,\r
+                                               fillingChar = getFillingChar( doc );\r
+\r
+                                       if ( fillingChar )\r
+                                       {\r
+                                               fillingChar.setText( fillingCharBefore );\r
+\r
+                                               if ( resetSelection )\r
+                                               {\r
+                                                       doc.$.defaultView.getSelection().setPosition( fillingChar.$,fillingChar.getLength() );\r
+                                                       resetSelection = 0;\r
+                                               }\r
+                                       }\r
+                               }\r
+                               editor.on( 'beforeUndoImage', beforeData );\r
+                               editor.on( 'afterUndoImage', afterData );\r
+                               editor.on( 'beforeGetData', beforeData, null, null, 0 );\r
+                               editor.on( 'getData', afterData );\r
+                       }\r
+\r
                        editor.on( 'contentDom', function()\r
                                {\r
                                        var doc = editor.document,\r
@@ -142,6 +277,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                                }\r
                                                                                catch (e)\r
                                                                                {}\r
+\r
+                                                                               // Update locked selection because of the normalized text nodes. (#6083, #6987)\r
+                                                                               var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+                                                                               if ( lockedSelection )\r
+                                                                               {\r
+                                                                                       lockedSelection.unlock();\r
+                                                                                       lockedSelection.lock();\r
+                                                                               }\r
                                                                        }\r
 \r
                                                                        savedRange = null;\r
@@ -151,7 +294,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 +307,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 +317,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 +336,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 +355,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 +395,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 +406,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 +466,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
@@ -308,6 +477,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                });\r
 \r
                        editor.selectionChange = checkSelectionChangeTimeout;\r
+\r
+                       // IE9 might cease to work if there's an object selection inside the iframe (#7639).\r
+                       CKEDITOR.env.ie9Compat && editor.on( 'destroy', function()\r
+                       {\r
+                               var sel = editor.getSelection();\r
+                               sel && sel.getNative().clear();\r
+                       }, null, null, 9 );\r
                }\r
        });\r
 \r
@@ -382,7 +558,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
@@ -407,10 +583,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        };\r
 \r
        var styleObjectElements =\r
-       {\r
-               img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
-               a:1, input:1, form:1, select:1, textarea:1, button:1, fieldset:1, th:1, thead:1, tfoot:1\r
-       };\r
+               {\r
+                       img:1,hr:1,li:1,table:1,tr:1,td:1,th:1,embed:1,object:1,ol:1,ul:1,\r
+                       a:1,input:1,form:1,select:1,textarea:1,button:1,fieldset:1,thead:1,tfoot:1\r
+               };\r
 \r
        CKEDITOR.dom.selection.prototype =\r
        {\r
@@ -525,11 +701,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * var ranges = selection.getRanges();\r
                 * alert(ranges.length);\r
                 */\r
-               getRanges : (function ()\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,77 +717,121 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                range.collapse( start );\r
 \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
+                                               var parent = range.parentElement(),\r
+                                                       doc = parent.ownerDocument;\r
+\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
+                                                       sibling,\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
+                                                       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
                                                        {\r
-                                                               testRange = range.duplicate();\r
-\r
-                                                               testRange.moveToElementText( child );\r
-\r
-                                                               var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),\r
-                                                                       comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );\r
-\r
-                                                               testRange.collapse();\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
-\r
-                                                               testRange = null;\r
+                                                               // IE9 report wrong measurement with compareEndPoints when range anchors between two BRs.\r
+                                                               // e.g. <p>text<br />^<br /></p> (#7433)\r
+                                                               if ( CKEDITOR.env.ie9Compat && child.tagName == 'BR' )\r
+                                                               {\r
+                                                                       var bmId = 'cke_range_marker';\r
+                                                                       range.execCommand( 'CreateBookmark', false, bmId );\r
+                                                                       child = doc.getElementsByName( bmId )[ 0 ];\r
+                                                                       var offset = getNodeIndex( child );\r
+                                                                       parent.removeChild( child );\r
+                                                                       return { container : parent, offset : offset };\r
+                                                               }\r
+                                                               else\r
+                                                                       return { container : parent, offset : getNodeIndex( child ) };\r
                                                        }\r
                                                }\r
 \r
-                                               if ( !testRange )\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
-                                                       testRange = range.duplicate();\r
+                                                       // Adapt test range to embrace the entire parent contents.\r
                                                        testRange.moveToElementText( parent );\r
-                                                       testRange.collapse( false );\r
-                                               }\r
+                                                       testRange.setEndPoint( 'StartToStart', range );\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
+                                                       // 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
-                                               try\r
-                                               {\r
+                                                       siblings = parent.childNodes;\r
+\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
+                                                               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
+                                                       // 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
+                                                               try\r
+                                                               {\r
+                                                                       sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+                                                                       distance -= sibling.nodeValue.length;\r
+                                                                       child = sibling;\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
@@ -637,6 +859,13 @@ 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
+                                                       // 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
@@ -673,16 +902,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        // 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
@@ -709,7 +951,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // Drop range spans inside one ready-only node.\r
                                                var parent = range.getCommonAncestor();\r
-                                               if ( parent.isReadOnly())\r
+                                               if ( parent.isReadOnly() )\r
                                                        ranges.splice( i, 1 );\r
 \r
                                                if ( range.collapsed )\r
@@ -751,7 +993,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                walker.evaluator = function( node )\r
                                                {\r
                                                        if ( node.type == CKEDITOR.NODE_ELEMENT\r
-                                                               && node.getAttribute( 'contenteditable' ) == 'false' )\r
+                                                               && node.isReadOnly() )\r
                                                        {\r
                                                                var newRange = range.clone();\r
                                                                range.setEndBefore( node );\r
@@ -819,7 +1061,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
@@ -839,32 +1081,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
@@ -918,17 +1153,40 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return cache.selectedElement = ( node ? new CKEDITOR.dom.element( node ) : null );\r
                },\r
 \r
+               /**\r
+                * Retrieves the text contained within the range, empty string is returned for non-text selection.\r
+                * @returns {String} string of text within the current selection.\r
+                * @since 3.6.1\r
+                * @example\r
+                * var text = editor.getSelectedText();\r
+                * alert( text );\r
+                */\r
+               getSelectedText : function()\r
+               {\r
+                       var cache = this._.cache;\r
+                       if ( cache.selectedText !== undefined )\r
+                               return cache.selectedText;\r
+\r
+                       var text = '',\r
+                               nativeSel = this.getNative();\r
+                       if ( this.getType() == CKEDITOR.SELECTION_TEXT )\r
+                               text = CKEDITOR.env.ie ? nativeSel.createRange().text : nativeSel.toString();\r
+\r
+                       return ( cache.selectedText = text );\r
+               },\r
+\r
                lock : function()\r
                {\r
                        // Call all cacheable function.\r
                        this.getRanges();\r
                        this.getStartElement();\r
                        this.getSelectedElement();\r
+                       this.getSelectedText();\r
 \r
                        // 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
@@ -948,7 +1206,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
@@ -962,7 +1220,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
@@ -992,44 +1250,14 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                return;\r
                        }\r
 \r
-                       if ( CKEDITOR.env.ie )\r
-                       {\r
-                               this.getNative().empty();\r
-\r
-                               try\r
-                               {\r
-                                       // Try to select the node as a control.\r
-                                       range = this.document.$.body.createControlRange();\r
-                                       range.addElement( element.$ );\r
-                                       range.select();\r
-                               }\r
-                               catch(e)\r
-                               {\r
-                                       // If failed, select it as a text range.\r
-                                       range = this.document.$.body.createTextRange();\r
-                                       range.moveToElementText( element.$ );\r
-                                       range.select();\r
-                               }\r
-                               finally\r
-                               {\r
-                                       this.document.fire( 'selectionchange' );\r
-                               }\r
-\r
-                               this.reset();\r
-                       }\r
-                       else\r
-                       {\r
-                               // Create the range for the element.\r
-                               range = this.document.$.createRange();\r
-                               range.selectNode( element.$ );\r
+                       range = new CKEDITOR.dom.range( element.getDocument() );\r
+                       range.setStartBefore( element );\r
+                       range.setEndAfter( element );\r
+                       range.select();\r
 \r
-                               // Select the range.\r
-                               var sel = this.getNative();\r
-                               sel.removeAllRanges();\r
-                               sel.addRange( range );\r
+                       this.document.fire( 'selectionchange' );\r
+                       this.reset();\r
 \r
-                               this.reset();\r
-                       }\r
                },\r
 \r
                /**\r
@@ -1068,8 +1296,16 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                var sel = this.getNative();\r
 \r
+                               // getNative() returns null if iframe is "display:none" in FF. (#6577)\r
+                               if ( !sel )\r
+                                       return;\r
+\r
                                if ( ranges.length )\r
+                               {\r
                                        sel.removeAllRanges();\r
+                                       // Remove any existing filling char first.\r
+                                       CKEDITOR.env.webkit && removeFillingChar( this.document );\r
+                               }\r
 \r
                                for ( var i = 0 ; i < ranges.length ; i++ )\r
                                {\r
@@ -1087,7 +1323,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( !between.collapsed )\r
                                                {\r
                                                        between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
-                                                       if ( between.getCommonAncestor().isReadOnly())\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
@@ -1103,16 +1345,58 @@ 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
                                                startContainer.appendText( '' );\r
                                        }\r
 \r
-                                       nativeRange.setStart( startContainer.$, range.startOffset );\r
-                                       nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
+                                       if ( range.collapsed\r
+                                                       && CKEDITOR.env.webkit\r
+                                                       && rangeRequiresFix( range ) )\r
+                                       {\r
+                                               // Append a zero-width space so WebKit will not try to\r
+                                               // move the selection by itself (#1272).\r
+                                               var fillingChar = createFillingChar( this.document );\r
+                                               range.insertNode( fillingChar ) ;\r
+\r
+                                               var next = fillingChar.getNext();\r
+\r
+                                               // If the filling char is followed by a <br>, whithout\r
+                                               // having something before it, it'll not blink.\r
+                                               // Let's remove it in this case.\r
+                                               if ( next && !fillingChar.getPrevious() && next.type == CKEDITOR.NODE_ELEMENT && next.getName() == 'br' )\r
+                                               {\r
+                                                       removeFillingChar( this.document );\r
+                                                       range.moveToPosition( next, CKEDITOR.POSITION_BEFORE_START );\r
+                                               }\r
+                                               else\r
+                                                       range.moveToPosition( fillingChar, CKEDITOR.POSITION_AFTER_END );\r
+                                       }\r
+\r
+                                       nativeRange.setStart( range.startContainer.$, range.startOffset );\r
+\r
+                                       try\r
+                                       {\r
+                                               nativeRange.setEnd( range.endContainer.$, range.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
+                                               {\r
+                                                       range.collapse( 1 );\r
+                                                       nativeRange.setEnd( range.endContainer.$, range.endOffset );\r
+                                               }\r
+                                               else\r
+                                                       throw e;\r
+                                       }\r
 \r
                                        // Select the range.\r
                                        sel.addRange( nativeRange );\r
@@ -1196,9 +1480,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // V2\r
                        function( forceExpand )\r
                        {\r
-                               var collapsed = this.collapsed;\r
-                               var isStartMarkerAlone;\r
-                               var dummySpan;\r
+                               var collapsed = this.collapsed,\r
+                                       isStartMarkerAlone, dummySpan, ieRange;\r
+\r
+                               // Try to make a object selection.\r
+                               var selected = this.getEnclosedNode();\r
+                               if ( selected )\r
+                               {\r
+                                       try\r
+                                       {\r
+                                               ieRange = this.document.$.body.createControlRange();\r
+                                               ieRange.addElement( selected.$ );\r
+                                               ieRange.select();\r
+                                               return;\r
+                                       }\r
+                                       catch( er ) {}\r
+                               }\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
@@ -1218,7 +1515,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        endNode = bookmark.endNode;\r
 \r
                                // Create the main range which will be used for the selection.\r
-                               var ieRange = this.document.$.body.createTextRange();\r
+                               ieRange = this.document.$.body.createTextRange();\r
 \r
                                // Position the range at the start boundary.\r
                                ieRange.moveToElementText( startNode.$ );\r
@@ -1299,36 +1596,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                :\r
                        function()\r
                        {\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
-\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
-                                       {\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
+                               this.document.getSelection().selectRanges( [ this ] );\r
                        };\r
 } )();\r