jquery.textSelection: Don't throw errors on empty collections
authorBartosz Dziewoński <matma.rex@gmail.com>
Fri, 25 Apr 2014 13:13:56 +0000 (15:13 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Fri, 25 Apr 2014 13:38:11 +0000 (15:38 +0200)
All jQuery functions just do nothing or return "empty" values when
called on an empty collection (e.g. `$()`), the ones defined in this
module should behave in the same way.

This came to light when a change in WikiEditor combined with lousy
coding caused this code path to be called, breaking various gadgets
and extensions like SemanticForms.

Bug: 64289
Change-Id: Ib97f47ef1d66420682bd429c9c12e66c3392e77d

resources/src/jquery/jquery.textSelection.js

index 156b314..6d87ffd 100644 (file)
@@ -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 );
                                                // 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;