From 2828669dd40f63f60c2391b6d5ee30a3e255f3db Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bartosz=20Dziewo=C5=84ski?= Date: Fri, 25 Apr 2014 15:13:56 +0200 Subject: [PATCH] jquery.textSelection: Don't throw errors on empty collections 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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/resources/src/jquery/jquery.textSelection.js b/resources/src/jquery/jquery.textSelection.js index 156b3149ef..6d87ffd37a 100644 --- a/resources/src/jquery/jquery.textSelection.js +++ b/resources/src/jquery/jquery.textSelection.js @@ -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 ); @@ -279,7 +279,7 @@ // 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 @@ -349,7 +349,7 @@ } 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; -- 2.20.1