JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / selection / plugin.js
index d4a92f8..17e2c52 100644 (file)
@@ -133,7 +133,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                // point.\r
                                                                if ( savedRange )\r
                                                                {\r
-                                                                       if ( restoreEnabled )\r
+                                                                       // Range restored here might invalidate the DOM structure thus break up\r
+                                                                       // the locked selection, give it up. (#6083)\r
+                                                                       var lockedSelection = doc.getCustomData( 'cke_locked_selection' );\r
+                                                                       if ( restoreEnabled && !lockedSelection )\r
                                                                        {\r
                                                                                // Well not break because of this.\r
                                                                                try\r
@@ -151,7 +154,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                body.on( 'focus', function()\r
                                                        {\r
                                                                // Enable selections to be saved.\r
-                                                               saveEnabled = true;\r
+                                                               saveEnabled = 1;\r
 \r
                                                                saveSelection();\r
                                                        });\r
@@ -164,7 +167,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 +177,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 +196,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 +215,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 +255,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                body.on( 'keyup',\r
                                                        function()\r
                                                        {\r
-                                                               saveEnabled = true;\r
+                                                               saveEnabled = 1;\r
                                                                saveSelection();\r
                                                        });\r
 \r
@@ -240,7 +266,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                function disableSave()\r
                                                {\r
-                                                       saveEnabled = false;\r
+                                                       saveEnabled = 0;\r
                                                }\r
 \r
                                                function saveSelection( testIt )\r
@@ -382,7 +408,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        return lockedSelection;\r
 \r
                this.document = document;\r
-               this.isLocked = false;\r
+               this.isLocked = 0;\r
                this._ =\r
                {\r
                        cache : {}\r
@@ -525,7 +551,7 @@ 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
@@ -637,6 +663,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 +706,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 +755,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
@@ -819,7 +865,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        // Decrease the range content to exclude particial\r
                                                        // selected node on the start which doesn't have\r
                                                        // visual impact. ( #3231 )\r
-                                                       while ( true )\r
+                                                       while ( 1 )\r
                                                        {\r
                                                                var startContainer = range.startContainer,\r
                                                                        startOffset = range.startOffset;\r
@@ -839,32 +885,25 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                        node = node.getChild( range.startOffset );\r
 \r
                                                        if ( !node || node.type != CKEDITOR.NODE_ELEMENT )\r
-                                                               return range.startContainer;\r
-\r
-                                                       var child = node.getFirst();\r
-                                                       while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                                               node = range.startContainer;\r
+                                                       else\r
                                                        {\r
-                                                               node = child;\r
-                                                               child = child.getFirst();\r
+                                                               var child = node.getFirst();\r
+                                                               while (  child && child.type == CKEDITOR.NODE_ELEMENT )\r
+                                                               {\r
+                                                                       node = child;\r
+                                                                       child = child.getFirst();\r
+                                                               }\r
                                                        }\r
-\r
-                                                       return node;\r
                                                }\r
-                                       }\r
-\r
-                                       if ( CKEDITOR.env.ie )\r
-                                       {\r
-                                               range = sel.createRange();\r
-                                               range.collapse( true );\r
-\r
-                                               node = range.parentElement();\r
-                                       }\r
-                                       else\r
-                                       {\r
-                                               node = sel.anchorNode;\r
+                                               else\r
+                                               {\r
+                                                       node = range.startContainer;\r
+                                                       if ( node.type != CKEDITOR.NODE_ELEMENT )\r
+                                                               node = node.getParent();\r
+                                               }\r
 \r
-                                               if ( node && node.nodeType != 1 )\r
-                                                       node = node.parentNode;\r
+                                               node = node.$;\r
                                        }\r
                        }\r
 \r
@@ -928,7 +967,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        // The native selection is not available when locked.\r
                        this._.cache.nativeSel = {};\r
 \r
-                       this.isLocked = true;\r
+                       this.isLocked = 1;\r
 \r
                        // Save this selection inside the DOM document.\r
                        this.document.setCustomData( 'cke_locked_selection', this );\r
@@ -948,7 +987,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        var selectedElement = lockedSelection.getSelectedElement(),\r
                                                ranges = !selectedElement && lockedSelection.getRanges();\r
 \r
-                                       this.isLocked = false;\r
+                                       this.isLocked = 0;\r
                                        this.reset();\r
 \r
                                        doc.getBody().focus();\r
@@ -962,7 +1001,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                        if  ( !lockedSelection || !restore )\r
                        {\r
-                               this.isLocked = false;\r
+                               this.isLocked = 0;\r
                                this.reset();\r
                        }\r
                },\r
@@ -1003,7 +1042,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        range.addElement( element.$ );\r
                                        range.select();\r
                                }\r
-                               catch(e)\r
+                               catch( e )\r
                                {\r
                                        // If failed, select it as a text range.\r
                                        range = this.document.$.body.createTextRange();\r
@@ -1103,8 +1142,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                        // In FF2, if we have a collapsed range, inside an empty\r
                                        // element, we must add something to it otherwise the caret\r
                                        // will not be visible.\r
+                                       // In Opera instead, the selection will be moved out of the\r
+                                       // element. (#4657)\r
                                        if ( range.collapsed &&\r
-                                               ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) &&\r
+                                               ( CKEDITOR.env.opera || ( CKEDITOR.env.gecko && CKEDITOR.env.version < 10900 ) ) &&\r
                                                startContainer.type == CKEDITOR.NODE_ELEMENT &&\r
                                                !startContainer.getChildCount() )\r
                                        {\r