]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Have Carousel ignore keyboard events from <input>s or <textarea>s; fixes #14991 14993/head
authorChris Rebert <code@rebertia.com>
Tue, 4 Nov 2014 02:35:35 +0000 (18:35 -0800)
committerChris Rebert <code@rebertia.com>
Wed, 5 Nov 2014 00:23:44 +0000 (16:23 -0800)
js/carousel.js
js/tests/unit/carousel.js

index 4e4e47797d056e970eeace6e18083fbfcef9a0a8..82106d278ac9cb4104a12c34b0b78f9bf90bc6b3 100644 (file)
@@ -42,6 +42,7 @@
   }
 
   Carousel.prototype.keydown = function (e) {
+    if (/input|textarea/i.test(e.target.tagName)) return
     switch (e.which) {
       case 37: this.prev(); break
       case 39: this.next(); break
index 6f0b9642fc7c4a385fd8fe016678395d74e8f9f2..5998abe6af8365a1cbbd4f63585d4d022f671d64 100644 (file)
@@ -478,6 +478,48 @@ $(function () {
     strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press')
   })
 
+  test('should ignore keyboard events within <input>s and <textarea>s', function () {
+    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
+        + '<div class="carousel-inner">'
+        + '<div id="first" class="item active">'
+        + '<img alt="">'
+        + '<input type="text" id="in-put">'
+        + '<textarea id="text-area"></textarea>'
+        + '</div>'
+        + '<div id="second" class="item">'
+        + '<img alt="">'
+        + '</div>'
+        + '<div id="third" class="item">'
+        + '<img alt="">'
+        + '</div>'
+        + '</div>'
+        + '</div>'
+    var $template = $(templateHTML)
+    var $input = $template.find('#in-put')
+    var $textarea = $template.find('#text-area')
+
+    strictEqual($input.length, 1, 'found <input>')
+    strictEqual($textarea.length, 1, 'found <textarea>')
+
+    $template.bootstrapCarousel()
+
+    strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
+
+
+    $input.trigger($.Event('keydown', { which: 39 }))
+    strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <input>')
+
+    $input.trigger($.Event('keydown', { which: 37 }))
+    strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>')
+
+
+    $textarea.trigger($.Event('keydown', { which: 39 }))
+    strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <textarea>')
+
+    $textarea.trigger($.Event('keydown', { which: 37 }))
+    strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>')
+  })
+
   test('should only add mouseenter and mouseleave listeners when not on mobile', function () {
     var isMobile     = 'ontouchstart' in document.documentElement
     var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-pause="hover">'