Merge "jquery.textSelection: Don't throw errors on empty collections"
authorMwalker <mwalker@wikimedia.org>
Fri, 25 Apr 2014 18:46:41 +0000 (18:46 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 25 Apr 2014 18:46:41 +0000 (18:46 +0000)
1  2 
resources/src/jquery/jquery.textSelection.js

@@@ -70,7 -70,7 +70,7 @@@
                                var retval, range,
                                        el = this.get( 0 );
  
-                               if ( $( el ).is( ':hidden' ) ) {
+                               if ( !el || $( el ).is( ':hidden' ) ) {
                                        retval = '';
                                } else if ( document.selection && document.selection.createRange ) {
                                        activateElementOnIE( el );
                         *
                         * @fixme document the options parameters
                         */
 -                       getCaretPosition: function ( options ) {
 +                      getCaretPosition: function ( options ) {
                                function getCaret( e ) {
                                        var caretPos = 0,
                                                endPos = 0,
                                                // Range containing text after the selection
                                                postRange;
  
-                                       if ( document.selection && document.selection.createRange ) {
+                                       if ( e && document.selection && document.selection.createRange ) {
                                                // IE doesn't properly report non-selected caret position through
                                                // the selection ranges when textarea isn't focused. This can
                                                // lead to saving a bogus empty selection, which then screws up
                                                } while ( ( !preFinished || !periFinished || !postFinished ) );
                                                caretPos = rawPreText.replace( /\r\n/g, '\n' ).length;
                                                endPos = caretPos + rawPeriText.replace( /\r\n/g, '\n' ).length;
-                                       } else if ( e.selectionStart || e.selectionStart === 0 ) {
+                                       } else if ( e && ( e.selectionStart || e.selectionStart === 0 ) ) {
                                                // Firefox support
                                                caretPos = e.selectionStart;
                                                endPos = e.selectionEnd;