In $.textSelection, treat <input> elements differently from other elements (such...
[lhc/web/wiklou.git] / resources / jquery / jquery.textSelection.js
index ebbfebc..22ee092 100644 (file)
@@ -3,6 +3,20 @@
  */
 ( function( $ ) {
 $.fn.textSelection = function( command, options ) {
+
+/**
+ * Helper function to get an IE TextRange object for an element
+ */
+function rangeForElementIE( e ) {
+       if ( e.nodeName.toLowerCase() == 'input' ) {
+               return e.createTextRange();
+       } else {
+               var sel = document.body.createTextRange();
+               sel.moveToElementText( e );
+               return sel;
+       }
+}
+
 var fn = {
 /**
  * Get the contents of the textarea
@@ -34,6 +48,8 @@ getSelection: function() {
  *
  * Inserts text at the begining and end of a text selection, optionally
  * inserting text at the caret when selection is empty.
+ *
+ * @fixme document the options parameters
  */
 encapsulateSelection: function( options ) {
        return this.each( function() {
@@ -92,7 +108,7 @@ encapsulateSelection: function( options ) {
                        // IE
                        $(this).focus();
                        if ( context ) {
-                               context.fn.restoreStuffForIE();
+                               context.fn.restoreCursorAndScrollTop();
                        }
                        var selText = $(this).textSelection( 'getSelection' );
                        var scrollTop = this.scrollTop;
@@ -134,6 +150,8 @@ encapsulateSelection: function( options ) {
  *
  * Get the position (in resolution of bytes not nessecarily characters)
  * in a textarea
+ *
+ * @fixme document the options parameters
  */
  getCaretPosition: function( options ) {
        function getCaret( e ) {
@@ -148,15 +166,11 @@ encapsulateSelection: function( options ) {
                        // Create range containing text in the selection
                        var periRange = document.selection.createRange().duplicate();
                        // Create range containing text before the selection
-                       var preRange = document.body.createTextRange();
-                       // Select all the text
-                       preRange.moveToElementText(e);
+                       var preRange = rangeForElementIE( e );
                        // Move the end where we need it
                        preRange.setEndPoint("EndToStart", periRange);
                        // Create range containing text after the selection
-                       var postRange = document.body.createTextRange();
-                       // Select all the text
-                       postRange.moveToElementText(e);
+                       var postRange = rangeForElementIE( e );
                        // Move the start where we need it
                        postRange.setEndPoint("StartToEnd", periRange);
                        // Load the text values we need to compare
@@ -173,7 +187,7 @@ encapsulateSelection: function( options ) {
                                        if ( preRange.compareEndPoints( "StartToEnd", preRange ) == 0 ) {
                                                preFinished = true;
                                        } else {
-                                               preRange.moveEnd( "character", -1 )
+                                               preRange.moveEnd( "character", -1 );
                                                if ( preRange.text == preText ) {
                                                        rawPreText += "\r\n";
                                                } else {
@@ -217,6 +231,9 @@ encapsulateSelection: function( options ) {
        }
        return getCaret( this.get( 0 ) );
 },
+/**
+ * @fixme document the options parameters
+ */
 setSelection: function( options ) {
        return this.each( function() {
                if ( $(this).is( ':hidden' ) ) {
@@ -233,8 +250,7 @@ setSelection: function( options ) {
                                this.selectionEnd = options.end;
                        }
                } else if ( document.body.createTextRange ) {
-                       var selection = document.body.createTextRange();
-                       selection.moveToElementText( this );
+                       var selection = rangeForElementIE( this );
                        var length = this.value.length;
                        // IE doesn't count \n when computing the offset, so we won't either
                        var newLines = this.value.match( /\n/g );
@@ -258,10 +274,12 @@ setSelection: function( options ) {
  * position with setSelection()
  * @param options boolean Whether to force a scroll even if the caret position
  *  is already visible. Defaults to false
+ *
+ * @fixme document the options parameters (function body suggests options.force is a boolean, not options itself)
  */
 scrollToCaretPosition: function( options ) {
        function getLineLength( e ) {
-               return Math.floor( e.scrollWidth / ( $.os.name == 'linux' ? 7 : 8 ) );
+               return Math.floor( e.scrollWidth / ( $.client.profile().platform == 'linux' ? 7 : 8 ) );
        }
        function getCaretScrollPosition( e ) {
                // FIXME: This functions sucks and is off by a few lines most
@@ -304,7 +322,7 @@ scrollToCaretPosition: function( options ) {
                        charInLine = caret - lastSpaceInLine;
                        row++;
                }
-               return ( $.os.name == 'mac' ? 13 : ( $.os.name == 'linux' ? 15 : 16 ) ) * row;
+               return ( $.client.profile().platform == 'mac' ? 13 : ( $.client.profile().platform == 'linux' ? 15 : 16 ) ) * row;
        }
        return this.each(function() {
                if ( $(this).is( ':hidden' ) ) {
@@ -386,7 +404,7 @@ scrollToCaretPosition: function( options ) {
                        break;
        }
        var context = $(this).data( 'wikiEditor-context' );
-       var hasIframe = context !== undefined && context.$iframe !== undefined;
+       var hasIframe = typeof context !== 'undefined' && context && typeof context.$iframe !== 'undefined';
        
        // IE selection restore voodoo
        var needSave = false;
@@ -400,5 +418,4 @@ scrollToCaretPosition: function( options ) {
        }
        return retval;
 };
-
 } )( jQuery );