Closed
Bug 1479964
Opened 6 years ago
Closed 6 years ago
Tracking event.keyCode issue due to the implementation of window.event
Categories
(Core :: DOM: Events, defect, P2)
Tracking
()
RESOLVED
FIXED
mozilla64
Tracking | Status | |
---|---|---|
firefox-esr60 | --- | unaffected |
firefox62 | --- | unaffected |
firefox63 | --- | unaffected |
firefox64 | --- | disabled |
People
(Reporter: karlcow, Assigned: masayuki)
References
()
Details
(Keywords: regression, site-compat, Whiteboard: [webcompat:p1])
Attachments
(1 file)
(deleted),
text/x-phabricator-request
|
Details |
Recently, Firefox landed Bug 218415 on supported window.event
We start to see regression in websites (through webcompat.com reports)
which uses window.event to determine if the browser should use event.keyCode or event.which for getting the key reference on keypress events.
This bug is here to track the regressions for now and determine eventually what will be the best course (curse) of actions.
It seems to revolve on a variation of
var num;
var char;
if (window.event) {
num = e.keyCode
}
else if (e.which) {
num = e.which
}
char = String.fromCharCode(num)
or
var keyCode = window.event ? event.keyCode : event.which;
event.keyCode, event.which and event.charCode are antiquated constructs.
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/which
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/charCode
event.charCode has a slightly larger support across browsers.
event.keyCode returns 0 in Gecko and the character reference in other browsers
event.which returns the character reference in Gecko (Netscapism)
The modern way to get the key is event.key
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key
Flags: webcompat?
Assignee | ||
Updated•6 years ago
|
Component: Event Handling → DOM: Events
Assignee | ||
Comment 1•6 years ago
|
||
If we'd set KeyboardEvent.keyCode to the which value, we'd break following implementations:
foo.addEventListener("keypress", (aEvent) => {
if (!aEvent.keyCode) {
// do something for character input.
} else {
// do something for non-printable keys like Arrow keys, Fn keys, etc.
}
});
So, I don't think that there are something which we can do for the odd apps since non-undefined window.event does NOT mean specific web browser now.
Assignee | ||
Comment 2•6 years ago
|
||
Ah, but my example will be broken by fix of bug 354358.
Assignee | ||
Comment 3•6 years ago
|
||
https://cs.chromium.org/chromium/src/third_party/blink/renderer/core/events/keyboard_event.cc?l=117-120&rcl=9eb9a910e8f9354912a72a8f2e54fe0584ed7bf7
> if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
> key_code_ = key.windows_key_code;
> else
> key_code_ = char_code_;
Chromium always sets keyCode value to charCode value when it's keypress event representing native input. Perhaps, we should follow the same behavior after (or at same time) fixing bug 354358?
Flags: needinfo?(bugs)
Comment 4•6 years ago
|
||
That sounds reasonable.
But I'm not sure what to do with this issue currently. Perhaps we should disable window.event for now.
Flags: needinfo?(bugs)
Assignee | ||
Comment 5•6 years ago
|
||
(In reply to Olli Pettay [:smaug] from comment #4)
> That sounds reasonable.
Sure, I'll try to create an experimental patch.
> But I'm not sure what to do with this issue currently. Perhaps we should
> disable window.event for now.
Hmm, looks like that it is not controlled by pref.
https://reviewboard.mozilla.org/r/233638/diff/17#index_header
Will you back it out completely? (Although the assignee, hsivonen is away until 8/6.)
Comment 6•6 years ago
|
||
No particular hurry, next merge will be early September, so there is still time to figure out other approaches.
Would it be bad to so change keyCode handling before bug 354358?
Assignee | ||
Comment 7•6 years ago
|
||
(In reply to Olli Pettay [:smaug] from comment #6)
> Would it be bad to so change keyCode handling before bug 354358?
I'm still not sure. However, according to the broken websites of our keypress event dispatching changes, that indicate that such websites refer keyCode value of keypress event on Firefox. So, I guess that this behavior change has similar impact as bug 354358.
Reporter | ||
Comment 8•6 years ago
|
||
(In reply to Olli Pettay [:smaug] from comment #4)
> But I'm not sure what to do with this issue currently. Perhaps we should
> disable window.event for now.
My impression for now is that we benefit more of the addition of window.event than the new issues being created (and reported so far). I would like to keep it for now. We didn't have any top100 site breaking yet. And keeping it might help us discover new patterns.
Updated•6 years ago
|
Priority: -- → P2
Assignee | ||
Comment 9•6 years ago
|
||
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/18073
Comment 10•6 years ago
|
||
Posted the site compatibility note: https://www.fxsitecompat.com/en-CA/docs/2018/window-event-has-been-added-for-compatibility-but-some-browser-detections-are-broken/
Keywords: site-compat
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/18584
Reporter | ||
Comment 11•6 years ago
|
||
function digitos(n) {
return (window.event ? key = n.keyCode : n.which && (key = n.which), key != 8 || key != 13 || key < 48 || key > 57) ? key > 47 && key < 58 || key == 8 || key == 13 : !0
}
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/18573
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/18684
Reporter | ||
Comment 12•6 years ago
|
||
This latest report https://webcompat.com/issues/18684 is for a government website in India.
People in India can't login and do their tax with Firefox.
I guess this raises a bit the priority for a fix.
Flags: needinfo?(miket)
Updated•6 years ago
|
Flags: webcompat?
Flags: webcompat+
Flags: needinfo?(miket)
Whiteboard: [webcompat] → [webcompat:p1]
Comment 13•6 years ago
|
||
(In reply to Masayuki Nakano [:masayuki] (JST, +0900) (offline: 9/21-9/30) from comment #3)
> https://cs.chromium.org/chromium/src/third_party/blink/renderer/core/events/
> keyboard_event.cc?l=117-120&rcl=9eb9a910e8f9354912a72a8f2e54fe0584ed7bf7
> > if (type() == EventTypeNames::keydown || type() == EventTypeNames::keyup)
> > key_code_ = key.windows_key_code;
> > else
> > key_code_ = char_code_;
>
> Chromium always sets keyCode value to charCode value when it's keypress
> event representing native input. Perhaps, we should follow the same behavior
> after (or at same time) fixing bug 354358?
Following Chrome, assuming it's safe enough, and speccing this seems like a good path forward (unfortunately...).
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/18953
Comment 14•6 years ago
|
||
Bug 1493098 breaks wikipedia donation. We can try outreach there, but it seems like we just need to implement and spec event.keyCode mirroring charCode for keypress event.
What do you think, Masayuki?
Flags: needinfo?(masayuki)
Updated•6 years ago
|
Comment 15•6 years ago
|
||
Updated•6 years ago
|
Updated•6 years ago
|
Updated•6 years ago
|
Comment 16•6 years ago
|
||
(In reply to Mike Taylor [:miketaylr] (62 Regression Engineering Owner) from comment #15)
> Filed https://github.com/whatwg/dom/issues/701.
...in the wrong place. New bug: https://github.com/w3c/uievents/issues/213
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/19132
Updated•6 years ago
|
Keywords: regression
Comment 17•6 years ago
|
||
(marking 63 as disabled, because this will only be seen in Nightly, given 1493869)
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/19150
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/19158
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/19139
Updated•6 years ago
|
Updated•6 years ago
|
Assignee | ||
Comment 18•6 years ago
|
||
(In reply to Mike Taylor [:miketaylr] (62 Regression Engineering Owner) from comment #14)
> Bug 1493098 breaks wikipedia donation. We can try outreach there, but it
> seems like we just need to implement and spec event.keyCode mirroring
> charCode for keypress event.
>
> What do you think, Masayuki?
Yeah, I think so. However, 0 keyCode/charCode value is currently important since we're dispatching non-printable keypress events until fixing bug 354358 and web apps need to distinguish whether coming keypress events are printable or not. After we stop dispatching keypress events for non-printable keys, web apps become not necessary to check keypress events.
On the other hand, I think that there is only one risk. If web apps ignore keypress events whose keyCode value is non-zero only when Gecko, we'll meet such another web-compat issue.
Flags: needinfo?(masayuki)
Comment 19•6 years ago
|
||
(In reply to Masayuki Nakano [:masayuki] (JST, +0900) (offline: 9/21-9/30) from comment #18)
> Yeah, I think so. However, 0 keyCode/charCode value is currently important
> since we're dispatching non-printable keypress events until fixing bug
> 354358 and web apps need to distinguish whether coming keypress events are
> printable or not.
After discussing with Masayuki, the references to bug 354358 here should actually refer to bug 968056. That is, Masayuki is concerned that we should ship bug 968056 before or at the same time as changing this, otherwise code such as the following will break:
foo.addEventListener("keypress", aEvent => {
if (aEvent.charCode) {
// Do something for char input
} else {
// Ignore non-printable key.
}
});
Regarding the test failures in the try run from comment 9, it looks like at least some are from DevTools where it's not yet clear what DevTools is doing.
Assignee | ||
Comment 20•6 years ago
|
||
Assignee | ||
Comment 21•6 years ago
|
||
Assignee | ||
Comment 22•6 years ago
|
||
I realize that if we do this hack only in web content, we can minimize the regression risk in our source code.
QA Contact: overholt
Assignee | ||
Comment 23•6 years ago
|
||
Updated•6 years ago
|
QA Contact: overholt
Assignee | ||
Comment 24•6 years ago
|
||
Assignee | ||
Comment 25•6 years ago
|
||
Chrome sets both KeyboardEvent.keyCode and KeyboardEvent.charCode of "keypress"
event to same value. On the other hand, our traditional behavior is, sets
one of them to 0.
Therefore, we need to set keyCode value to charCode value if the keypress
event is caused by a non-function key, i.e., it may be a printable key with
specific modifier state and/or different keyboard layout for compatibility
with Chrome. Similarly, we need to set charCode value to keyCode value if
the keypress event is caused by a function key which is not mapped to producing
a character.
Note that this hack is for compatibility with Chrome. So, for now, it's enough
to change the behavior only for "keypress" event handlers in web content. If
we completely change the behavior, we need to fix a lot of default handlers
and mochitests too. However, it's really difficult because default handlers
check whether keypress events are printable or not with following code:
> if (event.charCode &&
> !event.altKey && !event.ctrlKey && !event.metaKey) {
or
> if (!event.keyCode &&
> !event.altKey && !event.ctrlKey && !event.metaKey) {
So, until we stop dispatching "keypress" events for non-printable keys,
we need complicated check in each of them.
And also note that this patch changes the behavior of KeyboardEvent::KeyCode()
when spoofing is enabled and the instance is initialized by initKeyEvent() or
initKeyboardEvent(). That was changed by bug 1222285 unexpectedly and keeping
the behavior makes patched code really ugly. Therefore, this takes back the
old behavior even if spoofing is enabled.
Assignee | ||
Comment 26•6 years ago
|
||
Comment 27•6 years ago
|
||
Pushed by masayuki@d-toybox.com:
https://hg.mozilla.org/integration/autoland/rev/a9db3033d9cb
Set KeyboardEvent.keyCode and KeyboardEvent.charCode to same value if the event is "keypress" event r=smaug
Assignee | ||
Updated•6 years ago
|
Assignee: nobody → masayuki
Status: NEW → ASSIGNED
Comment 28•6 years ago
|
||
bugherder |
Status: ASSIGNED → RESOLVED
Closed: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla64
Assignee | ||
Comment 29•6 years ago
|
||
Oh... The reports in web-compat do not reference whether keyCode or charCode is referred wrongly by the web app...
Assignee | ||
Comment 30•6 years ago
|
||
If we stop mirroring charCode to keyCode, the broken web apps (probably based on Google Closure) work fine. However, according to comment 0, we need to do it.
This is really hacky approach though, we might be able to switch behavior with checking whether window.event is referred or not before first keyCode/charCode value access on the document? Of course, I don't like this approach, but as far as I've heard, this is important bug and should be fixed ASAP.
Flags: needinfo?(bugs)
Assignee | ||
Comment 31•6 years ago
|
||
Ah, no. Cannot fix this case:
https://webcompat.com/issues/18073
> function onlyNumbersCharWithDot(e) {
>
> var keynum;
>
> if (e.keyCode == 13 ||e.which == 13 ) {
> return true;
> }
>
> if (e.keyCode == 9 ||e.which == 9 ) {
> return true;
> }
> if (e.which == 0 ) {
> return true;
> }
>
> if (e.keyCode == 8 ||e.which == 8 ) {
> return true;
> }
>
> if (e.keyCode == 32 ||e.which == 32) {
> return false;
> }
>
> if (window.event) // IE
> {
> keynum = e.keyCode;
> } else if (e.which) // Netscape/Firefox/Opera
Comment 32•6 years ago
|
||
Tricky. Do we know why exactly Google Closure is broken?
Flags: needinfo?(bugs)
Assignee | ||
Comment 33•6 years ago
|
||
I've still not found exact point. However, key handling code in rememberthemilk (I assume that is generated by Closure) checks UA string around checking keyCode.
I wonder, if we release bug 968056 before changing this behavior, any web apps can fix them easier since they don't need to check whether keypress event is caused by printable key or non-printable key anymore. (Although bug 968056 is still blocked by medium.com.)
Assignee | ||
Comment 34•6 years ago
|
||
https://github.com/google/closure-library/blob/719529feafc9d60eecd2f620756d3e5cc84a9250/closure/goog/events/keyhandler.js#L400-L413
https://github.com/google/closure-library/blob/719529feafc9d60eecd2f620756d3e5cc84a9250/closure/goog/events/keyhandler.js#L439-L449
https://github.com/google/closure-library/blob/719529feafc9d60eecd2f620756d3e5cc84a9250/closure/goog/events/keycodes.js#L413-L415
At least those code checks both UA string and keyCode value.
Updated•6 years ago
|
status-firefox62:
--- → unaffected
status-firefox-esr60:
--- → unaffected
Reporter | ||
Updated•6 years ago
|
See Also: → https://webcompat.com/issues/19842
Comment 35•6 years ago
|
||
(Correcting flags as per Bug 1497546)
Updated•6 years ago
|
Assignee | ||
Comment 36•6 years ago
|
||
You need to log in
before you can comment on or make changes to this bug.
Description
•