twbs / bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.
https://getbootstrap.com
MIT License
169.99k stars 78.75k forks source link

Typeahead confuses JS key codes with Unicode code points #6633

Closed jksmithing closed 11 years ago

jksmithing commented 11 years ago

In v2.2.2, in the move function, there's a switch for the event keyCode. What's problematic is that, at least in Chrome 24, the keyCode attribute of a keypress event is not the same as the keyCode attribute of a keydown or keyup event. See: http://msdn.microsoft.com/en-us/magazine/ff928319.aspx

The keyCode value in keypress events is actually the Unicode code point. For example, if I type a left parenthesis, I get a keyCode of 40 in the keypress event, which also happens to be the JS key code for the down arrow.

I haven't tested for other conflicts, but if you have an item in your Typeahead source with a left parenthesis (otherwise the menu won't be shown), then you will be prevented from typing a left parenthesis. Fiddle: http://jsfiddle.net/neonsilk/YMqRG/

jksmithing commented 11 years ago

Looked at this a bit more this morning... seems like detecting key presses in JS is a huge mess.

But I also noticed that fixing another issue fixed this one: https://github.com/twitter/bootstrap/issues/6635. (I just added a logical NOT before the bitwise NOT.)

fat commented 11 years ago

I think that's pretty minor (no one else seems to have ran into it) - if you've got a fix for it, id' be happy to merge so long as it doesn't break backwards compat

Barnabas commented 11 years ago

Issue not fixed as claimed; this still happens because move is called inside of keypress. This is a little hacky, and so I'm reluctant to submit this, but this is a sort of fix, inside the move function of bootstrap-typeahead.js:

        case 40: // down arrow
          if(e.type === 'keypress') break   // 40 is open paren inside a keypress
          e.preventDefault()
          this.next()
          break
      }

Blech.