JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
vanilla ckeditor-3.5.1
[ckeditor.git] / _source / plugins / selection / plugin.js
index 17e2c52..3a4eb80 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
 For licensing, see LICENSE.html or http://ckeditor.com/license\r
 */\r
 \r
@@ -79,20 +79,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        {\r
                                case 'wysiwyg' :\r
                                        editor.document.$.execCommand( 'SelectAll', false, null );\r
+                                       // Force triggering selectionChange (#7008)\r
+                                       editor.forceNextSelectionCheck();\r
+                                       editor.selectionChange();\r
                                        break;\r
                                case 'source' :\r
                                        // Select the contents of the textarea\r
-                                       var textarea = editor.textarea.$ ;\r
+                                       var textarea = editor.textarea.$;\r
                                        if ( CKEDITOR.env.ie )\r
-                                       {\r
-                                               textarea.createTextRange().execCommand( 'SelectAll' ) ;\r
-                                       }\r
+                                               textarea.createTextRange().execCommand( 'SelectAll' );\r
                                        else\r
                                        {\r
-                                               textarea.selectionStart = 0 ;\r
-                                               textarea.selectionEnd = textarea.value.length ;\r
+                                               textarea.selectionStart = 0;\r
+                                               textarea.selectionEnd = textarea.value.length;\r
                                        }\r
-                                       textarea.focus() ;\r
+                                       textarea.focus();\r
                        }\r
                },\r
                canUndo : false\r
@@ -556,6 +557,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                        var func = CKEDITOR.env.ie ?\r
                                ( function()\r
                                {\r
+                                       function getNodeIndex( node ) { return new CKEDITOR.dom.node( node ).getIndex(); }\r
+\r
                                        // Finds the container and offset for a specific boundary\r
                                        // of an IE range.\r
                                        var getBoundaryInformation = function( range, start )\r
@@ -566,76 +569,103 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
 \r
                                                // Gets the element that encloses the range entirely.\r
                                                var parent = range.parentElement();\r
-                                               var siblings = parent.childNodes;\r
 \r
-                                               var testRange;\r
-\r
-                                               for ( var i = 0 ; i < siblings.length ; i++ )\r
+                                               // Empty parent element, e.g. <i>^</i>\r
+                                               if ( !parent.hasChildNodes() )\r
+                                                       return  { container : parent, offset : 0 };\r
+\r
+                                               var siblings = parent.children,\r
+                                                       child,\r
+                                                       testRange = range.duplicate(),\r
+                                                       startIndex = 0,\r
+                                                       endIndex = siblings.length - 1,\r
+                                                       index = -1,\r
+                                                       position,\r
+                                                       distance;\r
+\r
+                                               // Binary search over all element childs to test the range to see whether\r
+                                               // range is right on the boundary of one element.\r
+                                               while ( startIndex <= endIndex )\r
                                                {\r
-                                                       var child = siblings[ i ];\r
-                                                       if ( child.nodeType == 1 )\r
-                                                       {\r
-                                                               testRange = range.duplicate();\r
+                                                       index = Math.floor( ( startIndex + endIndex ) / 2 );\r
+                                                       child = siblings[ index ];\r
+                                                       testRange.moveToElementText( child );\r
+                                                       position = testRange.compareEndPoints( 'StartToStart', range );\r
+\r
+                                                       if ( position > 0 )\r
+                                                               endIndex = index - 1;\r
+                                                       else if ( position < 0 )\r
+                                                               startIndex = index + 1;\r
+                                                       else\r
+                                                               return { container : parent, offset : getNodeIndex( child ) };\r
+                                               }\r
 \r
-                                                               testRange.moveToElementText( child );\r
+                                               // All childs are text nodes,\r
+                                               // or to the right hand of test range are all text nodes. (#6992)\r
+                                               if ( index == -1 || index == siblings.length - 1 && position < 0 )\r
+                                               {\r
+                                                       // Adapt test range to embrace the entire parent contents.\r
+                                                       testRange.moveToElementText( parent );\r
+                                                       testRange.setEndPoint( 'StartToStart', range );\r
 \r
-                                                               var comparisonStart = testRange.compareEndPoints( 'StartToStart', range ),\r
-                                                                       comparisonEnd = testRange.compareEndPoints( 'EndToStart', range );\r
+                                                       // IE report line break as CRLF with range.text but\r
+                                                       // only LF with textnode.nodeValue, normalize them to avoid\r
+                                                       // breaking character counting logic below. (#3949)\r
+                                                       distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
 \r
-                                                               testRange.collapse();\r
+                                                       siblings = parent.childNodes;\r
 \r
-                                                               if ( comparisonStart > 0 )\r
-                                                                       break;\r
-                                                               // When selection stay at the side of certain self-closing elements, e.g. BR,\r
-                                                               // our comparison will never shows an equality. (#4824)\r
-                                                               else if ( !comparisonStart\r
-                                                                       || comparisonEnd == 1 && comparisonStart == -1 )\r
-                                                                       return { container : parent, offset : i };\r
-                                                               else if ( !comparisonEnd )\r
-                                                                       return { container : parent, offset : i + 1 };\r
+                                                       // Actual range anchor right beside test range at the boundary of text node.\r
+                                                       if ( !distance )\r
+                                                       {\r
+                                                               child = siblings[ siblings.length - 1 ];\r
 \r
-                                                               testRange = null;\r
+                                                               if ( child.nodeType == CKEDITOR.NODE_ELEMENT )\r
+                                                                       return { container : parent, offset : siblings.length };\r
+                                                               else\r
+                                                                       return { container : child, offset : child.nodeValue.length };\r
                                                        }\r
-                                               }\r
-\r
-                                               if ( !testRange )\r
-                                               {\r
-                                                       testRange = range.duplicate();\r
-                                                       testRange.moveToElementText( parent );\r
-                                                       testRange.collapse( false );\r
-                                               }\r
 \r
-                                               testRange.setEndPoint( 'StartToStart', range );\r
-                                               // IE report line break as CRLF with range.text but\r
-                                               // only LF with textnode.nodeValue, normalize them to avoid\r
-                                               // breaking character counting logic below. (#3949)\r
-                                               var distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
-\r
-                                               try\r
-                                               {\r
+                                                       // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+                                                       var i = siblings.length;\r
                                                        while ( distance > 0 )\r
                                                                distance -= siblings[ --i ].nodeValue.length;\r
-                                               }\r
-                                               // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
-                                               catch( e )\r
-                                               {\r
-                                                       distance = 0;\r
-                                               }\r
-\r
 \r
-                                               if ( distance === 0 )\r
-                                               {\r
-                                                       return {\r
-                                                               container : parent,\r
-                                                               offset : i\r
-                                                       };\r
+                                                       return  { container : siblings[ i ], offset : -distance };\r
                                                }\r
+                                               // Test range was one offset beyond OR behind the anchored text node.\r
                                                else\r
                                                {\r
-                                                       return {\r
-                                                               container : siblings[ i ],\r
-                                                               offset : -distance\r
-                                                       };\r
+                                                       // Adapt one side of test range to the actual range\r
+                                                       // for measuring the offset between them.\r
+                                                       testRange.collapse( position > 0 ? true : false );\r
+                                                       testRange.setEndPoint( position > 0 ? 'StartToStart' : 'EndToStart', range );\r
+\r
+                                                       // IE report line break as CRLF with range.text but\r
+                                                       // only LF with textnode.nodeValue, normalize them to avoid\r
+                                                       // breaking character counting logic below. (#3949)\r
+                                                       distance = testRange.text.replace( /(\r\n|\r)/g, '\n' ).length;\r
+\r
+                                                       // Actual range anchor right beside test range at the inner boundary of text node.\r
+                                                       if ( !distance )\r
+                                                               return { container : parent, offset : getNodeIndex( child ) + ( position > 0 ? 0 : 1 ) };\r
+\r
+                                                       // Start the measuring until distance overflows, meanwhile count the text nodes.\r
+                                                       while ( distance > 0 )\r
+                                                       {\r
+                                                               child = child[ position > 0 ? 'previousSibling' : 'nextSibling' ];\r
+                                                               try\r
+                                                               {\r
+                                                                       distance -= child.nodeValue.length;\r
+                                                               }\r
+                                                               // Measurement in IE could be somtimes wrong because of <select> element. (#4611)\r
+                                                               catch( e )\r
+                                                               {\r
+                                                                       return { container : parent, offset : getNodeIndex( child ) };\r
+                                                               }\r
+                                                       }\r
+\r
+                                                       return { container : child, offset : position > 0 ? -distance : child.nodeValue.length + distance };\r
                                                }\r
                                        };\r
 \r
@@ -797,7 +827,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                walker.evaluator = function( node )\r
                                                {\r
                                                        if ( node.type == CKEDITOR.NODE_ELEMENT\r
-                                                               && node.getAttribute( 'contenteditable' ) == 'false' )\r
+                                                               && node.isReadOnly() )\r
                                                        {\r
                                                                var newRange = range.clone();\r
                                                                range.setEndBefore( node );\r
@@ -1126,7 +1156,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                                if ( !between.collapsed )\r
                                                {\r
                                                        between.shrink( CKEDITOR.NODE_ELEMENT, true );\r
-                                                       if ( between.getCommonAncestor().isReadOnly())\r
+                                                       var ancestor = between.getCommonAncestor(),\r
+                                                               enclosed = between.getEnclosedNode();\r
+\r
+                                                       // The following cases has to be considered:\r
+                                                       // 1. <span contenteditable="false">[placeholder]</span>\r
+                                                       // 2. <input contenteditable="false"  type="radio"/> (#6621)\r
+                                                       if ( ancestor.isReadOnly() || enclosed && enclosed.isReadOnly() )\r
                                                        {\r
                                                                right.setStart( left.startContainer, left.startOffset );\r
                                                                ranges.splice( i--, 1 );\r
@@ -1369,7 +1405,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license
                                }\r
 \r
                                var selection = this.document.getSelection().getNative();\r
-                               selection.removeAllRanges();\r
-                               selection.addRange( nativeRange );\r
+                               // getSelection() returns null in case when iframe is "display:none" in FF. (#6577)\r
+                               if ( selection )\r
+                               {\r
+                                       selection.removeAllRanges();\r
+                                       selection.addRange( nativeRange );\r
+                               }\r
                        };\r
 } )();\r