From 9668cb850ef97e39822cb3ef0d0ea27ff0c1fe6e Mon Sep 17 00:00:00 2001 From: meh-cfl Date: Sat, 7 Apr 2012 09:07:21 -0400 Subject: [PATCH] Autocomplete: Don't invoke a search from arrow keys when the element can have multi-line text. Fixes #7639 - Key up/key down in textarea's should optionally not toggle auto-complete. --- tests/unit/autocomplete/autocomplete.html | 1 + tests/unit/autocomplete/autocomplete_core.js | 63 ++++++++++++++++++++ ui/jquery.ui.autocomplete.js | 17 ++++-- 3 files changed, 75 insertions(+), 6 deletions(-) diff --git a/tests/unit/autocomplete/autocomplete.html b/tests/unit/autocomplete/autocomplete.html index 1e7b58e80..88b7da4d8 100644 --- a/tests/unit/autocomplete/autocomplete.html +++ b/tests/unit/autocomplete/autocomplete.html @@ -38,6 +38,7 @@
+ diff --git a/tests/unit/autocomplete/autocomplete_core.js b/tests/unit/autocomplete/autocomplete_core.js index 84f26980f..fa7dce610 100644 --- a/tests/unit/autocomplete/autocomplete_core.js +++ b/tests/unit/autocomplete/autocomplete_core.js @@ -97,5 +97,68 @@ asyncTest( "handle race condition", function() { start(); } }); +test( "up arrow invokes search - input", function() { + arrowsInvokeSearch( "#autocomplete", true, true ); +}); + +test( "down arrow invokes search - input", function() { + arrowsInvokeSearch( "#autocomplete", false, true ); +}); + +test( "up arrow invokes search - textarea", function() { + arrowsInvokeSearch( "#autocomplete-textarea", true, false ); +}); + +test( "down arrow invokes search - textarea", function() { + arrowsInvokeSearch( "#autocomplete-textarea", false, false ); +}); + +test( "up arrow moves focus - input", function() { + arrowsMoveFocus( "#autocomplete", true ); +}); + +test( "down arrow moves focus - input", function() { + arrowsMoveFocus( "#autocomplete", false ); +}); + +test( "up arrow moves focus - textarea", function() { + arrowsMoveFocus( "#autocomplete-textarea", true ); +}); + +test( "down arrow moves focus - textarea", function() { + arrowsMoveFocus( "#autocomplete-textarea", false ); +}); + +function arrowsInvokeSearch( id, isKeyUp, shouldMove ) { + expect( 1 ); + + var didMove = false, + element = $( id ).autocomplete({ + source: [ "a" ], + delay: 0, + minLength: 0 + }); + element.data( "autocomplete" )._move = function() { + didMove = true; + }; + element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } ); + equal( didMove, shouldMove, "respond to arrow" ); +} + +function arrowsMoveFocus( id, isKeyUp ) { + expect( 1 ); + + var didMove = false, + element = $( id ).autocomplete({ + source: [ "a" ], + delay: 0, + minLength: 0 + }); + element.data( "autocomplete" )._move = function() { + ok( true, "repsond to arrow" ); + }; + element.autocomplete( "search" ); + element.simulate( "keydown", { keyCode: ( isKeyUp ? $.ui.keyCode.UP : $.ui.keyCode.DOWN ) } ); +} }( jQuery ) ); diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js index 5352c3432..3a6f34688 100644 --- a/ui/jquery.ui.autocomplete.js +++ b/ui/jquery.ui.autocomplete.js @@ -37,6 +37,7 @@ $.widget( "ui.autocomplete", { var self = this, doc = this.element[ 0 ].ownerDocument, suppressKeyPress; + this.isMultiLine = this.element.is( "textarea" ); this.element .addClass( "ui-autocomplete-input" ) @@ -62,14 +63,10 @@ $.widget( "ui.autocomplete", { self._move( "nextPage", event ); break; case keyCode.UP: - self._move( "previous", event ); - // prevent moving cursor to beginning of text field in some browsers - event.preventDefault(); + self._keyEvent( "previous", event ); break; case keyCode.DOWN: - self._move( "next", event ); - // prevent moving cursor to end of text field in some browsers - event.preventDefault(); + self._keyEvent( "next", event ); break; case keyCode.ENTER: case keyCode.NUMPAD_ENTER: @@ -424,6 +421,14 @@ $.widget( "ui.autocomplete", { widget: function() { return this.menu.element; + }, + _keyEvent: function( keyEvent, event ) { + if ( !this.isMultiLine || this.menu.element.is( ":visible" ) ) { + this._move( keyEvent, event ); + + // prevents moving cursor to beginning/end of the text field in some browsers + event.preventDefault(); + } } });