JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.4.2
[ckeditor.git] / _source / plugins / selection / plugin.js
index f0d2c44..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
@@ -235,7 +243,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                                }\r
                                                                scroll = null;\r
 \r
-                                                               saveEnabled = true;\r
+                                                               saveEnabled = 1;\r
                                                                setTimeout( function()\r
                                                                        {\r
                                                                                saveSelection( true );\r
@@ -247,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
@@ -258,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
@@ -400,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
@@ -543,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
@@ -655,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
@@ -740,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
@@ -850,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
@@ -870,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
@@ -959,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
@@ -979,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
@@ -993,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
@@ -1034,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