-
Notifications
You must be signed in to change notification settings - Fork 46.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve normalization of charCode, fix FF not reporting Enter #1956
Conversation
cc @zpao |
Pushed an update, can play around with it here http://dev.cetrez.com/jsx/2/indexx.html (KeyDown left, KeyPress right, type in the right-most input). I decided that it would be better to target any malfunctioning browser and not just rely on FF behaving in a specific way. Any function keys (all below 32 except Enter) are ignored for KeyPress (AFAIK only FF does this incorrectly, but now they should all behave the same). Also made a little precautionary change for IE8, AFAIK IE8 does not ever return 0 in keyCode, but if it would, "which" would have become null rather than 0. |
OK, there was a bunch of unexpected browser edge-case inconsistencies, but I think I've gotten all kinks out of |
|
||
"use strict"; | ||
|
||
var invariant = require('invariant'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self, remove this.
I think this should be it, it's hard to be absolutely certain, but as far as I'm able to discern it works as expected. As above, I think the code by itself makes sense so that's always a good sign. Test link is updated: http://dev.cetrez.com/jsx/2/indexx.html PS. I also removed an invariant from |
|
||
"use strict"; | ||
|
||
/** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe briefly mention that charCode
returns the keyCode
of printable characters (excluding Tab, including Enter).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm? I'm not sure if I fully understand, charCode
is "character code" (which is why we can use String.fromCharCode
) and keyCode
is "virtual key code". All similarities between the two are circumstancial and (theoretically) implementation-defined AFAIK, but for our purposes it's rather safe to assume that keyCode = 13
represents charCode = 13
(since keyboards at least agree on enter key being 13 and we "must" assume that it's a keyboard typing).
I should however explain some of that there I agree :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added doc comment.
The code makes sense to me. Thanks for digging into this, @syranide! |
@yungsters Np, I break it, I fix it :) |
*/ | ||
function getEventCharCode(nativeEvent) { | ||
var charCode, | ||
keyCode = nativeEvent.keyCode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: var
for each.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought "we" preferred this style, I hate it though so I'm all too happy to change it :)
Fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, we don't like this style. It shouldn't exist in any of the other code we have up here (maybe some build scripts or tests but we're a bit lax there)
Improve normalization of charCode, fix FF not reporting Enter
#1955
Haven't tested it yet (it's late), I'll run it through some manual tests tomorrow.