JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.6.3
[ckeditor.git] / _source / plugins / selection / plugin.js
index 39c6db7..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
@@ -162,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
@@ -180,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
@@ -264,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
@@ -311,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
@@ -341,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
@@ -399,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
@@ -465,6 +548,31 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                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
                        // Clear the cached range path before unload. (#7174)\r
@@ -573,10 +681,18 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                 */\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
@@ -737,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
@@ -758,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
@@ -790,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
@@ -798,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
@@ -826,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
@@ -836,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
@@ -1157,7 +1283,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                        for ( var t in tags )\r
                                        {\r
-                                               if ( root = ancestor.getAscendant( t, 1 ) )\r
+                                               if ( ( root = ancestor.getAscendant( t, 1 ) ) )\r
                                                        break;\r
                                        }\r
 \r