JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / selection / plugin.js
index a42ef66..aff5ba2 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -78,6 +78,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        && 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
@@ -86,7 +92,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                // 1. Empty inline element. <span>^</span>\r
                // 2. Adjoin to inline element. <p><strong>text</strong>^</p>\r
-               return !CKEDITOR.tools.trim( start.getHtml() ) ? isInlineCt( start ) : isInlineCt( start.getChild( offset - 1 ) ) || isInlineCt( start.getChild( offset ) );\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
@@ -154,11 +162,54 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                var fillingChar = doc && doc.removeCustomData( 'cke-fillingChar' );\r
                if ( fillingChar )\r
                {\r
+                       var bm,\r
+                       sel = doc.getSelection().getNative(),\r
+                       // Be error proof.\r
+                       range = sel && sel.type != 'None' && sel.getRangeAt( 0 );\r
+\r
+                       // Text selection position might get mangled by\r
+                       // subsequent dom modification, save it now for restoring. (#8617)\r
+                       if ( fillingChar.getLength() > 1\r
+                                && range && range.intersectsNode( fillingChar.$ ) )\r
+                       {\r
+                               bm = [ sel.anchorOffset, sel.focusOffset ];\r
+\r
+                               // Anticipate the offset change brought by the removed char.\r
+                               var startAffected = sel.anchorNode == fillingChar.$ && sel.anchorOffset > 0,\r
+                                       endAffected = sel.focusNode == fillingChar.$ && sel.focusOffset > 0;\r
+                               startAffected && bm[ 0 ]--;\r
+                               endAffected && bm[ 1 ]--;\r
+\r
+                               // Revert the bookmark order on reverse selection.\r
+                               isReversedSelection( sel ) && bm.unshift( bm.pop() );\r
+                       }\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
+                       // Restore the bookmark.\r
+                       if ( bm )\r
+                       {\r
+                               var rng = sel.getRangeAt( 0 );\r
+                               rng.setStart( rng.startContainer, bm[ 0 ] );\r
+                               rng.setEnd( rng.startContainer, bm[ 1 ] );\r
+                               sel.removeAllRanges();\r
+                               sel.addRange( rng );\r
+                       }\r
+               }\r
+       }\r
+\r
+       function isReversedSelection( sel )\r
+       {\r
+               if ( !sel.isCollapsed )\r
+               {\r
+                       var range = sel.getRangeAt( 0 );\r
+                       // Potentially alter an reversed selection range.\r
+                       range.setStart( sel.anchorNode, sel.anchorOffset );\r
+                       range.setEnd( sel.focusNode, sel.focusOffset );\r
+                       return range.collapsed;\r
                }\r
        }\r
 \r
@@ -172,20 +223,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\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
@@ -256,29 +293,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                if ( evt.data.$.srcElement.nodeName != 'BODY' )\r
                                                                        return;\r
 \r
-                                                               // If we have saved a range, restore it at this\r
+                                                               // Give the priority to locked selection since it probably\r
+                                                               // reflects the actual situation, besides locked selection\r
+                                                               // could be interfered because of text nodes normalizing.\r
+                                                               // (#6083, #6987)\r
+                                                               var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+                                                               if ( lockedSelection )\r
+                                                               {\r
+                                                                       lockedSelection.unlock( 1 );\r
+                                                                       lockedSelection.lock();\r
+                                                               }\r
+                                                               // Then check ff we have saved a range, restore it at this\r
                                                                // point.\r
-                                                               if ( savedRange )\r
+                                                               else if ( savedRange && restoreEnabled )\r
                                                                {\r
-                                                                       if ( restoreEnabled )\r
-                                                                       {\r
-                                                                               // Well not break because of this.\r
-                                                                               try\r
-                                                                               {\r
-                                                                                       savedRange.select();\r
-                                                                               }\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
+                                                                       // Well not break because of this.\r
+                                                                       try { savedRange.select(); } catch (e) {}\r
                                                                        savedRange = null;\r
                                                                }\r
                                                        });\r
@@ -303,20 +333,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                restoreEnabled = 1;\r
                                                        });\r
 \r
-                                               // IE before version 8 will leave cursor blinking inside the document after\r
-                                               // editor blurred unless we clean up the selection. (#4716)\r
-                                               if ( CKEDITOR.env.ie && CKEDITOR.env.version < 8 )\r
+                                               // [IE] Iframe will still keep the selection when blurred, if\r
+                                               // focus is moved onto a non-editing host, e.g. link or button, but\r
+                                               // it becomes a problem for the object type selection, since the resizer\r
+                                               // handler attached on it will mark other part of the UI, especially\r
+                                               // for the dialog. (#8157)\r
+                                               // [IE<8] Even worse For old IEs, the cursor will not vanish even if\r
+                                               // the selection has been moved to another text input in some cases. (#4716)\r
+                                               //\r
+                                               // Now the range restore is disabled, so we simply force IE to clean\r
+                                               // up the selection before blur.\r
+                                               CKEDITOR.env.ie && editor.on( 'blur', function()\r
                                                {\r
-                                                       editor.on( 'blur', function( evt )\r
-                                                       {\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
+                                                       // Error proof when the editor is not visible. (#6375)\r
+                                                       try{ doc.$.selection.empty(); } catch ( er){}\r
+                                               });\r
 \r
                                                // Listening on document element ensures that\r
                                                // scrollbar is included. (#5280)\r
@@ -333,20 +364,6 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        restoreEnabled = 1;\r
                                                });\r
 \r
-                                               // In IE6/7 the blinking cursor appears, but contents are\r
-                                               // not editable. (#5634)\r
-                                               if ( CKEDITOR.env.ie && ( CKEDITOR.env.ie7Compat || CKEDITOR.env.version < 8 || CKEDITOR.env.quirks ) )\r
-                                               {\r
-                                                       // The 'click' event is not fired when clicking the\r
-                                                       // scrollbars, so we can use it to check whether\r
-                                                       // the empty space following <body> has been clicked.\r
-                                                       html.on( 'click', function( evt )\r
-                                                       {\r
-                                                               if ( evt.data.getTarget().getName() == 'html' )\r
-                                                                       editor.getSelection().getRanges()[ 0 ].select();\r
-                                                       });\r
-                                               }\r
-\r
                                                var scroll;\r
                                                // IE fires the "selectionchange" event when clicking\r
                                                // inside a selection. We don't want to capture that.\r
@@ -391,6 +408,80 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                saveSelection();\r
                                                        });\r
 \r
+                                               // When content doc is in standards mode, IE doesn't focus the editor when\r
+                                               // clicking at the region below body (on html element) content, we emulate\r
+                                               // the normal behavior on old IEs. (#1659, #7932)\r
+                                               if ( ( CKEDITOR.env.ie7Compat || CKEDITOR.env.ie6Compat )\r
+                                                        && doc.$.compatMode != 'BackCompat' )\r
+                                               {\r
+                                                       html.on( 'mousedown', function( evt )\r
+                                                       {\r
+                                                               evt = evt.data.$;\r
+\r
+                                                               // Expand the text range along with mouse move.\r
+                                                               function onHover( evt )\r
+                                                               {\r
+                                                                       evt = evt.data.$;\r
+                                                                       if ( textRng )\r
+                                                                       {\r
+                                                                               // Read the current cursor.\r
+                                                                               var rngEnd = body.$.createTextRange();\r
+                                                                               rngEnd.moveToPoint( evt.x, evt.y );\r
+\r
+                                                                               // Handle drag directions.\r
+                                                                               textRng.setEndPoint(\r
+                                                                                       textRng.compareEndPoints( 'StartToStart', rngEnd ) < 0 ?\r
+                                                                                       'EndToEnd' :\r
+                                                                                       'StartToStart',\r
+                                                                                       rngEnd );\r
+\r
+                                                                               // Update selection with new range.\r
+                                                                               textRng.select();\r
+                                                                       }\r
+                                                               }\r
+\r
+                                                               // We're sure that the click happens at the region\r
+                                                               // below body, but not on scrollbar.\r
+                                                               if ( evt.y < html.$.clientHeight\r
+                                                                        && evt.y > body.$.offsetTop + body.$.clientHeight\r
+                                                                        && evt.x < html.$.clientWidth )\r
+                                                               {\r
+                                                                       // Start to build the text range.\r
+                                                                       var textRng = body.$.createTextRange();\r
+                                                                       textRng.moveToPoint( evt.x, evt.y );\r
+                                                                       textRng.select();\r
+\r
+                                                                       html.on( 'mousemove', onHover );\r
+\r
+                                                                       html.on( 'mouseup', function( evt )\r
+                                                                       {\r
+                                                                               html.removeListener( 'mousemove', onHover );\r
+                                                                               evt.removeListener();\r
+                                                                               textRng.select();\r
+                                                                               textRng = null;\r
+                                                                       } );\r
+                                                               }\r
+                                                       });\r
+                                               }\r
+\r
+                                               // It's much simpler for IE8, we just need to reselect the reported range.\r
+                                               if ( CKEDITOR.env.ie8 )\r
+                                               {\r
+                                                       html.on( 'mouseup', function( evt )\r
+                                                       {\r
+                                                               // The event is not fired when clicking on the scrollbars,\r
+                                                               // so we can safely check the following to understand\r
+                                                               // whether the empty space following <body> has been clicked.\r
+                                                               if ( evt.data.getTarget().getName() == 'html' )\r
+                                                               {\r
+                                                                       var sel = CKEDITOR.document.$.selection,\r
+                                                                               range = sel.createRange();\r
+                                                                       // The selection range is reported on host, but actually it should applies to the content doc.\r
+                                                                       if ( sel.type != 'None' && range.parentElement().ownerDocument == doc.$ )\r
+                                                                               range.select();\r
+                                                               }\r
+                                                       } );\r
+                                               }\r
 \r
                                                // IE is the only to provide the "selectionchange"\r
                                                // event.\r
@@ -455,6 +546,32 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                doc.on( 'mouseup', checkSelectionChangeTimeout, editor );\r
                                                doc.on( 'keyup', checkSelectionChangeTimeout, editor );\r
+                                               doc.on( 'selectionchange', checkSelectionChangeTimeout, editor );\r
+                                       }\r
+\r
+                                       if ( CKEDITOR.env.webkit )\r
+                                       {\r
+                                               doc.on( 'keydown', function( evt )\r
+                                               {\r
+                                                       var key = evt.data.getKey();\r
+                                                       // Remove the filling char before some keys get\r
+                                                       // executed, so they'll not get blocked by it.\r
+                                                       switch ( key )\r
+                                                       {\r
+                                                               case 13 :       // ENTER\r
+                                                               case 33 :       // PAGEUP\r
+                                                               case 34 :       // PAGEDOWN\r
+                                                               case 35 :       // HOME\r
+                                                               case 36 :       // END\r
+                                                               case 37 :       // LEFT-ARROW\r
+                                                               case 39 :       // RIGHT-ARROW\r
+                                                               case 8 :        // BACKSPACE\r
+                                                               case 45 :       // INS\r
+                                                               case 46 :       // DEl\r
+                                                                       removeFillingChar( editor.document );\r
+                                                       }\r
+\r
+                                               }, null, null, 10 );\r
                                        }\r
                                });\r
 \r
@@ -469,15 +586,22 @@ 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
        /**\r
         * Gets the current selection from the editing area when in WYSIWYG mode.\r
-        * @returns {CKEDITOR.dom.selection} A selection object or null if not on\r
+        * @returns {CKEDITOR.dom.selection} A selection object or null if not in\r
         *              WYSIWYG mode or no selection is available.\r
         * @example\r
-        * var selection = CKEDITOR.instances.editor1.<b>getSelection()</b>;\r
+        * var selection = CKEDITOR.instances.editor1.<strong>getSelection()</strong>;\r
         * alert( selection.getType() );\r
         */\r
        CKEDITOR.editor.prototype.getSelection = function()\r
@@ -494,7 +618,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
         * Gets the current selection from the document.\r
         * @returns {CKEDITOR.dom.selection} A selection object.\r
         * @example\r
-        * var selection = CKEDITOR.instances.editor1.document.<b>getSelection()</b>;\r
+        * var selection = CKEDITOR.instances.editor1.document.<strong>getSelection()</strong>;\r
         * alert( selection.getType() );\r
         */\r
        CKEDITOR.dom.document.prototype.getSelection = function()\r
@@ -513,11 +637,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        CKEDITOR.SELECTION_NONE         = 1;\r
 \r
        /**\r
-        * Text or collapsed selection.\r
+        * A text or a collapsed selection.\r
         * @constant\r
         * @example\r
         * if ( editor.getSelection().getType() == CKEDITOR.SELECTION_TEXT )\r
-        *     alert( 'Text is selected' );\r
+        *     alert( 'A text is selected' );\r
         */\r
        CKEDITOR.SELECTION_TEXT         = 2;\r
 \r
@@ -533,7 +657,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
        /**\r
         * Manipulates the selection in a DOM document.\r
         * @constructor\r
+        * @param {CKEDITOR.dom.document} document The DOM document that contains the selection.\r
         * @example\r
+        * var sel = new <strong>CKEDITOR.dom.selection( CKEDITOR.document )</strong>;\r
         */\r
        CKEDITOR.dom.selection = function( document )\r
        {\r
@@ -551,14 +677,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                /**\r
                 * IE BUG: The selection's document may be a different document than the\r
-                * editor document. Return null if that's the case.\r
+                * editor document. Return null if that is the case.\r
                 */\r
                if ( CKEDITOR.env.ie )\r
                {\r
-                       var range = this.getNative().createRange();\r
-                       if ( !range\r
-                               || ( range.item && range.item(0).ownerDocument != this.document.$ )\r
-                               || ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
+                       // Avoid breaking because of it. (#8836)\r
+                       try\r
+                       {\r
+                               var range = this.getNative().createRange();\r
+                               if ( !range ||\r
+                                        ( range.item && range.item( 0 ).ownerDocument != this.document.$ ) ||\r
+                                        ( range.parentElement && range.parentElement().ownerDocument != this.document.$ ) )\r
+                               {\r
+                                       throw 0;\r
+                               }\r
+                       }\r
+                       catch ( e )\r
                        {\r
                                this.isInvalid = true;\r
                        }\r
@@ -578,9 +712,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                /**\r
                 * Gets the native selection object from the browser.\r
                 * @function\r
-                * @returns {Object} The native selection object.\r
+                * @returns {Object} The native browser selection object.\r
                 * @example\r
-                * var selection = editor.getSelection().<b>getNative()</b>;\r
+                * var selection = editor.getSelection().<strong>getNative()</strong>;\r
                 */\r
                getNative :\r
                        CKEDITOR.env.ie ?\r
@@ -598,19 +732,19 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * Gets the type of the current selection. The following values are\r
                 * available:\r
                 * <ul>\r
-                *              <li>{@link CKEDITOR.SELECTION_NONE} (1): No selection.</li>\r
-                *              <li>{@link CKEDITOR.SELECTION_TEXT} (2): Text is selected or\r
-                *                      collapsed selection.</li>\r
-                *              <li>{@link CKEDITOR.SELECTION_ELEMENT} (3): A element\r
-                *                      selection.</li>\r
+                *              <li><code>{@link CKEDITOR.SELECTION_NONE}</code> (1): No selection.</li>\r
+                *              <li><code>{@link CKEDITOR.SELECTION_TEXT}</code> (2): A text or a collapsed\r
+                *                      selection is selected.</li>\r
+                *              <li><code>{@link CKEDITOR.SELECTION_ELEMENT}</code> (3): An element is\r
+                *                      selected.</li>\r
                 * </ul>\r
                 * @function\r
                 * @returns {Number} One of the following constant values:\r
-                *              {@link CKEDITOR.SELECTION_NONE}, {@link CKEDITOR.SELECTION_TEXT} or\r
-                *              {@link CKEDITOR.SELECTION_ELEMENT}.\r
+                *              <code>{@link CKEDITOR.SELECTION_NONE}</code>, <code>{@link CKEDITOR.SELECTION_TEXT}</code>, or\r
+                *              <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
                 * @example\r
-                * if ( editor.getSelection().<b>getType()</b> == CKEDITOR.SELECTION_TEXT )\r
-                *     alert( 'Text is selected' );\r
+                * if ( editor.getSelection().<strong>getType()</strong> == CKEDITOR.SELECTION_TEXT )\r
+                *     alert( 'A text is selected' );\r
                 */\r
                getType :\r
                        CKEDITOR.env.ie ?\r
@@ -678,13 +812,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                },\r
 \r
                /**\r
-                * Retrieve the {@link CKEDITOR.dom.range} instances that represent the current selection.\r
-                * Note: Some browsers returns multiple ranges even on a sequent selection, e.g. Firefox returns\r
-                * one range for each table cell when one or more table row is selected.\r
-                * @return {Array}\r
+                * Retrieves the <code>{@link CKEDITOR.dom.range}</code> instances that represent the current selection.\r
+                * Note: Some browsers return multiple ranges even for a continuous selection. Firefox, for example, returns\r
+                * one range for each table cell when one or more table rows are selected.\r
+                * @function\r
+                * @param {Boolean} [onlyEditables] If set to <code>true</code>, this function retrives editable ranges only.\r
+                * @return {Array} Range instances that represent the current selection.\r
                 * @example\r
-                * var ranges = selection.getRanges();\r
-                * alert(ranges.length);\r
+                * var ranges = selection.<strong>getRanges()</strong>;\r
+                * alert( ranges.length );\r
                 */\r
                getRanges : (function()\r
                {\r
@@ -717,7 +853,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        endIndex = siblings.length - 1,\r
                                                        index = -1,\r
                                                        position,\r
-                                                       distance;\r
+                                                       distance,\r
+                                                       container;\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
@@ -738,12 +875,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // 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
+                                                                       // "Fall back" to w3c selection.\r
+                                                                       var sel = doc.defaultView.getSelection();\r
+                                                                       return { container : sel[ start ? 'anchorNode' : 'focusNode' ],\r
+                                                                               offset : sel[ start ? 'anchorOffset' : 'focusOffset' ] };\r
                                                                }\r
                                                                else\r
                                                                        return { container : parent, offset : getNodeIndex( child ) };\r
@@ -770,7 +905,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        {\r
                                                                child = siblings[ siblings.length - 1 ];\r
 \r
-                                                               if ( child.nodeType == CKEDITOR.NODE_ELEMENT )\r
+                                                               if ( child.nodeType != CKEDITOR.NODE_TEXT )\r
                                                                        return { container : parent, offset : siblings.length };\r
                                                                else\r
                                                                        return { container : child, offset : child.nodeValue.length };\r
@@ -778,10 +913,17 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \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
+                                                       while ( distance > 0 && i > 0 )\r
+                                                       {\r
+                                                               sibling = siblings[ --i ];\r
+                                                               if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
+                                                               {\r
+                                                                       container = sibling;\r
+                                                                       distance -= sibling.nodeValue.length;\r
+                                                               }\r
+                                                       }\r
 \r
-                                                       return  { container : siblings[ i ], offset : -distance };\r
+                                                       return  { container : container, offset : -distance };\r
                                                }\r
                                                // Test range was one offset beyond OR behind the anchored text node.\r
                                                else\r
@@ -806,7 +948,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                try\r
                                                                {\r
                                                                        sibling = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
-                                                                       distance -= sibling.nodeValue.length;\r
+                                                                       if ( sibling.nodeType == CKEDITOR.NODE_TEXT )\r
+                                                                       {\r
+                                                                               distance -= sibling.nodeValue.length;\r
+                                                                               container = sibling;\r
+                                                                       }\r
                                                                        child = sibling;\r
                                                                }\r
                                                                // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
@@ -816,7 +962,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                        }\r
 \r
-                                                       return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };\r
+                                                       return { container : container, offset : position > 0 ? -distance : container.nodeValue.length + distance };\r
                                                }\r
                                        };\r
 \r
@@ -942,18 +1088,30 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( range.collapsed )\r
                                                        continue;\r
 \r
+                                               // Range may start inside a non-editable element,\r
+                                               // replace the range start after it.\r
+                                               if ( range.startContainer.isReadOnly() )\r
+                                               {\r
+                                                       var current = range.startContainer;\r
+                                                       while( current )\r
+                                                       {\r
+                                                               if ( current.is( 'body' ) || !current.isReadOnly() )\r
+                                                                       break;\r
+\r
+                                                               if ( current.type == CKEDITOR.NODE_ELEMENT\r
+                                                                               && current.getAttribute( 'contentEditable' ) == 'false' )\r
+                                                                       range.setStartAfter( current );\r
+\r
+                                                               current = current.getParent();\r
+                                                       }\r
+                                               }\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
@@ -1016,7 +1174,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 * @returns {CKEDITOR.dom.element} The element at the beginning of the\r
                 *              selection.\r
                 * @example\r
-                * var element = editor.getSelection().<b>getStartElement()</b>;\r
+                * var element = editor.getSelection().<strong>getStartElement()</strong>;\r
                 * alert( element.getName() );\r
                 */\r
                getStartElement : function()\r
@@ -1092,12 +1250,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                * Gets the current selected element.\r
+                * Gets the currently selected element.\r
                 * @returns {CKEDITOR.dom.element} The selected element. Null if no\r
                 *              selection is available or the selection type is not\r
-                *              {@link CKEDITOR.SELECTION_ELEMENT}.\r
+                *              <code>{@link CKEDITOR.SELECTION_ELEMENT}</code>.\r
                 * @example\r
-                * var element = editor.getSelection().<b>getSelectedElement()</b>;\r
+                * var element = editor.getSelection().<strong>getSelectedElement()</strong>;\r
                 * alert( element.getName() );\r
                 */\r
                getSelectedElement : function()\r
@@ -1114,6 +1272,74 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                {\r
                                        return self.getNative().createRange().item( 0 );\r
                                },\r
+                               // If a table or list is fully selected.\r
+                               function()\r
+                               {\r
+                                       var root,\r
+                                               retval,\r
+                                               range  = self.getRanges()[ 0 ],\r
+                                               ancestor = range.getCommonAncestor( 1, 1 ),\r
+                                               tags = { table:1,ul:1,ol:1,dl:1 };\r
+\r
+                                       for ( var t in tags )\r
+                                       {\r
+                                               if ( ( root = ancestor.getAscendant( t, 1 ) ) )\r
+                                                       break;\r
+                                       }\r
+\r
+                                       if ( root )\r
+                                       {\r
+                                               // Enlarging the start boundary.\r
+                                               var testRange = new CKEDITOR.dom.range( this.document );\r
+                                               testRange.setStartAt( root, CKEDITOR.POSITION_AFTER_START );\r
+                                               testRange.setEnd( range.startContainer, range.startOffset );\r
+\r
+                                               var enlargeables = CKEDITOR.tools.extend( tags, CKEDITOR.dtd.$listItem, CKEDITOR.dtd.$tableContent ),\r
+                                                       walker = new CKEDITOR.dom.walker( testRange ),\r
+                                                       // Check the range is at the inner boundary of the structural element.\r
+                                                       guard = function( walker, isEnd )\r
+                                                       {\r
+                                                               return function( node, isWalkOut )\r
+                                                               {\r
+                                                                       if ( node.type == CKEDITOR.NODE_TEXT && ( !CKEDITOR.tools.trim( node.getText() ) || node.getParent().data( 'cke-bookmark' ) ) )\r
+                                                                               return true;\r
+\r
+                                                                       var tag;\r
+                                                                       if ( node.type == CKEDITOR.NODE_ELEMENT )\r
+                                                                       {\r
+                                                                               tag = node.getName();\r
+\r
+                                                                               // Bypass bogus br at the end of block.\r
+                                                                               if ( tag == 'br' && isEnd && node.equals( node.getParent().getBogus() ) )\r
+                                                                                       return true;\r
+\r
+                                                                               if ( isWalkOut && tag in enlargeables || tag in CKEDITOR.dtd.$removeEmpty )\r
+                                                                                       return true;\r
+                                                                       }\r
+\r
+                                                                       walker.halted = 1;\r
+                                                                       return false;\r
+                                                               };\r
+                                                       };\r
+\r
+                                               walker.guard = guard( walker );\r
+\r
+                                               if ( walker.checkBackward() && !walker.halted )\r
+                                               {\r
+                                                       walker = new CKEDITOR.dom.walker( testRange );\r
+                                                       testRange.setStart( range.endContainer, range.endOffset );\r
+                                                       testRange.setEndAt( root, CKEDITOR.POSITION_BEFORE_END );\r
+                                                       walker.guard = guard( walker, 1 );\r
+                                                       if ( walker.checkForward() && !walker.halted )\r
+                                                               retval = root.$;\r
+                                               }\r
+                                       }\r
+\r
+                                       if ( !retval )\r
+                                               throw 0;\r
+\r
+                                       return retval;\r
+                               },\r
                                // Figure it out by checking if there's a single enclosed\r
                                // node of the range.\r
                                function()\r
@@ -1138,12 +1364,43 @@ 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. An empty string is returned for non-text selection.\r
+                * @returns {String} A string of text within the current selection.\r
+                * @since 3.6.1\r
+                * @example\r
+                * var text = editor.getSelection().<strong>getSelectedText()</strong>;\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
+               /**\r
+                * Locks the selection made in the editor in order to make it possible to\r
+                * manipulate it without browser interference. A locked selection is\r
+                * cached and remains unchanged until it is released with the <code>#unlock</code>\r
+                * method.\r
+                * @example\r
+                * editor.getSelection().<strong>lock()</strong>;\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
@@ -1154,6 +1411,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        this.document.setCustomData( 'cke_locked_selection', this );\r
                },\r
 \r
+               /**\r
+                * Unlocks the selection made in the editor and locked with the <code>#lock</code> method.\r
+                * An unlocked selection is no longer cached and can be changed.\r
+                * @param {Boolean} [restore] If set to <code>true</code>, the selection is restored back to the selection saved earlier by using the <code>#lock</code> method.\r
+                * @example\r
+                * editor.getSelection().<strong>unlock()</strong>;\r
+                */\r
                unlock : function( restore )\r
                {\r
                        var doc = this.document,\r
@@ -1187,14 +1451,22 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        }\r
                },\r
 \r
+               /**\r
+                * Clears the selection cache.\r
+                * @example\r
+                * editor.getSelection().<strong>reset()</strong>;\r
+                */\r
                reset : function()\r
                {\r
                        this._.cache = {};\r
                },\r
 \r
                /**\r
-                *  Make the current selection of type {@link CKEDITOR.SELECTION_ELEMENT} by enclosing the specified element.\r
-                * @param element\r
+                * Makes the current selection of type <code>{@link CKEDITOR.SELECTION_ELEMENT}</code> by enclosing the specified element.\r
+                * @param {CKEDITOR.dom.element} element The element to enclose in the selection.\r
+                * @example\r
+                * var element = editor.document.getById( 'sampleElement' );\r
+                * editor.getSelection.<strong>selectElement( element )</strong>;\r
                 */\r
                selectElement : function( element )\r
                {\r
@@ -1223,9 +1495,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                *  Adding the specified ranges to document selection preceding\r
-                * by clearing up the original selection.\r
-                * @param {CKEDITOR.dom.range} ranges\r
+                *  Clears the original selection and adds the specified ranges\r
+                * to the document selection.\r
+                * @param {Array} ranges An array of <code>{@link CKEDITOR.dom.range}</code> instances representing ranges to be added to the document.\r
+                * @example\r
+                * var ranges = new CKEDITOR.dom.range( editor.document );\r
+                * editor.getSelection().<strong>selectRanges( [ ranges ] )</strong>;\r
                 */\r
                selectRanges : function( ranges )\r
                {\r
@@ -1364,15 +1639,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        sel.addRange( nativeRange );\r
                                }\r
 \r
+                               // Don't miss selection change event for non-IEs.\r
+                               this.document.fire( 'selectionchange' );\r
                                this.reset();\r
                        }\r
                },\r
 \r
                /**\r
-                *  Create bookmark for every single of this selection range (from #getRanges)\r
-                * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark} method,\r
-                * with extra cares to avoid interferon among those ranges. Same arguments are\r
-                * received as with the underlay range method.\r
+                *  Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
+                * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark}</code> method,\r
+                * with extra care taken to avoid interference among those ranges. The arguments\r
+                * received are the same as with the underlying range method.\r
+                * @returns {Array} Array of bookmarks for each range.\r
+                * @example\r
+                * var bookmarks = editor.getSelection().<strong>createBookmarks()</strong>;\r
                 */\r
                createBookmarks : function( serializable )\r
                {\r
@@ -1380,10 +1660,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                *  Create bookmark for every single of this selection range (from #getRanges)\r
-                * by calling the {@link CKEDITOR.dom.range.prototype.createBookmark2} method,\r
-                * with extra cares to avoid interferon among those ranges. Same arguments are\r
-                * received as with the underlay range method.\r
+                *  Creates a bookmark for each range of this selection (from <code>#getRanges</code>)\r
+                * by calling the <code>{@link CKEDITOR.dom.range.prototype.createBookmark2}</code> method,\r
+                * with extra care taken to avoid interference among those ranges. The arguments\r
+                * received are the same as with the underlying range method.\r
+                * @returns {Array} Array of bookmarks for each range.\r
+                * @example\r
+                * var bookmarks = editor.getSelection().<strong>createBookmarks2()</strong>;\r
                 */\r
                createBookmarks2 : function( normalized )\r
                {\r
@@ -1391,8 +1674,12 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                * Select the virtual ranges denote by the bookmarks by calling #selectRanges.\r
-                * @param bookmarks\r
+                * Selects the virtual ranges denoted by the bookmarks by calling <code>#selectRanges</code>.\r
+                * @param {Array} bookmarks The bookmarks representing ranges to be selected.\r
+                * @returns {CKEDITOR.dom.selection} This selection object, after the ranges were selected.\r
+                * @example\r
+                * var bookmarks = editor.getSelection().createBookmarks();\r
+                * editor.getSelection().<strong>selectBookmarks( bookmarks )</strong>;\r
                 */\r
                selectBookmarks : function( bookmarks )\r
                {\r
@@ -1408,7 +1695,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                * Retrieve the common ancestor node of the first range and the last range.\r
+                * Retrieves the common ancestor node of the first range and the last range.\r
+                * @returns {CKEDITOR.dom.element} The common ancestor of the selection.\r
+                * @example\r
+                * var ancestor = editor.getSelection().<strong>getCommonAncestor()</strong>;\r
                 */\r
                getCommonAncestor : function()\r
                {\r
@@ -1419,7 +1709,9 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                },\r
 \r
                /**\r
-                * Moving scroll bar to the current selection's start position.\r
+                * Moves the scrollbar to the starting position of the current selection.\r
+                * @example\r
+                * editor.getSelection().<strong>scrollIntoView()</strong>;\r
                 */\r
                scrollIntoView : function()\r
                {\r