Skip to content

Commit

Permalink
fix charCode/keyCode for KeyPressEvent's when simulating Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Dec 12, 2021
1 parent ba008e3 commit 400ddab
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 55 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

<body>
<release version="2.56.0" date="December xx, 2021" description="Bugfixes, Chrome/Edge 96, Firefox 95, FF78 deprecated">
<action type="fix" dev="rbri">
Fix charCode/keyCode for KeyPressEvent's when simulating Firefox.
</action>
<action type="fix" dev="RhinoTeam">
core-js: Fix Array.from when called with a sparse array.
</action>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,10 +908,6 @@ public enum BrowserVersionFeatures {
@BrowserFeature({CHROME, EDGE, IE})
JS_ERROR_STACK_TRACE_LIMIT,

/** Javascript event.keyCode and event.charCode distinguish between printable and not printable keys. */
@BrowserFeature({FF, FF_ESR})
JS_EVENT_DISTINGUISH_PRINTABLE_KEY,

/** Javascript InputEvent reads the inputType property from data. */
@BrowserFeature({FF, FF_ESR})
JS_EVENT_INPUT_CTOR_INPUTTYPE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package com.gargoylesoftware.htmlunit.javascript.host.event;

import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_EVENT_DISTINGUISH_PRINTABLE_KEY;
import static com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_EVENT_KEYBOARD_CTOR_WHICH;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
Expand Down Expand Up @@ -918,30 +917,20 @@ public KeyboardEvent(final DomNode domNode, final String type, final char charac

if ('\n' == character) {
setKeyCode(DOM_VK_RETURN);
if (!getBrowserVersion().hasFeature(JS_EVENT_DISTINGUISH_PRINTABLE_KEY)) {
charCode_ = DOM_VK_RETURN;
}
charCode_ = DOM_VK_RETURN;
which_ = DOM_VK_RETURN;
return;
}

int keyCode = 0;
if (getType().equals(Event.TYPE_KEY_PRESS)) {
if (getBrowserVersion().hasFeature(JS_EVENT_DISTINGUISH_PRINTABLE_KEY)) {
if (character < 32 || character > 126) {
keyCode = Integer.valueOf(charToKeyCode(character));
}
}
else {
keyCode = Integer.valueOf(character);
}
keyCode = Integer.valueOf(character);
}
else {
keyCode = Integer.valueOf(charToKeyCode(character));
}
setKeyCode(keyCode);
if (getType().equals(Event.TYPE_KEY_PRESS) && (character >= 32 && character <= 126
|| !getBrowserVersion().hasFeature(JS_EVENT_DISTINGUISH_PRINTABLE_KEY))) {
if (getType().equals(Event.TYPE_KEY_PRESS)) {
charCode_ = character;
}
which_ = charCode_ == 0 ? keyCode : Integer.valueOf(charCode_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* And no keypress event in IE.
*
* @author Ahmed Ashour
* @author Ronald Brill
*/
@RunWith(BrowserRunner.class)
public class KeyboardEvent2Test extends SimpleWebTestCase {
Expand All @@ -39,9 +40,7 @@ public class KeyboardEvent2Test extends SimpleWebTestCase {
* @throws Exception if the test fails
*/
@Test
@Alerts(DEFAULT = "keydown:65,0,65 keypress:97,97,97 keyup:65,0,65",
FF = "keydown:65,0,65 keypress:0,97,97 keyup:65,0,65",
FF_ESR = "keydown:65,0,65 keypress:0,97,97 keyup:65,0,65")
@Alerts("keydown:65,0,65 keypress:97,97,97 keyup:65,0,65")
public void a() throws Exception {
final HtmlPage page = getHtmlPage();
page.getDocumentElement().type('a');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
package com.gargoylesoftware.htmlunit.javascript.host.event;

import static com.gargoylesoftware.htmlunit.junit.BrowserRunner.TestedBrowser.FF;
import static com.gargoylesoftware.htmlunit.junit.BrowserRunner.TestedBrowser.FF_ESR;

import java.util.Arrays;

import org.junit.Test;
Expand All @@ -26,13 +23,12 @@
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.gargoylesoftware.htmlunit.WebDriverTestCase;
import com.gargoylesoftware.htmlunit.html.HtmlPageTest;
import com.gargoylesoftware.htmlunit.junit.BrowserRunner;
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.Alerts;
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.BuggyWebDriver;
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.HtmlUnitNYI;
import com.gargoylesoftware.htmlunit.junit.BrowserRunner.NotYetImplemented;
import com.gargoylesoftware.htmlunit.WebDriverTestCase;

/**
* Tests for {@link KeyboardEvent}.
Expand Down Expand Up @@ -623,7 +619,6 @@ public void keyCodes2_keypress() throws Exception {
*/
@Test
@Alerts({"13", "13", "13"})
@NotYetImplemented({FF, FF_ESR})
public void keyCodeEnter_keypress() throws Exception {
final String html = "<html>\n"
+ "<head>\n"
Expand Down Expand Up @@ -667,34 +662,6 @@ public void keyCodeEnter_keypress() throws Exception {
"keydown:13,0,13,Enter,undefined,Enter,false",
"keypress:13,13,13,Enter,undefined,Enter,false",
"keyup:13,0,13,Enter,undefined,Enter,false"},
FF = {"keydown:16,0,16,Shift,undefined,ShiftLeft,true",
"keydown:65,0,65,A,undefined,KeyA,true",
"keypress:0,65,65,A,undefined,KeyA,true",
"keyup:65,0,65,A,undefined,KeyA,true",
"keyup:16,0,16,Shift,undefined,ShiftLeft,false",
"keydown:65,0,65,a,undefined,KeyA,false",
"keypress:0,97,97,a,undefined,KeyA,false",
"keyup:65,0,65,a,undefined,KeyA,false",
"keydown:190,0,190,.,undefined,Period,false",
"keypress:0,46,46,.,undefined,Period,false",
"keyup:190,0,190,.,undefined,Period,false",
"keydown:13,0,13,Enter,undefined,Enter,false",
"keypress:13,0,13,Enter,undefined,Enter,false",
"keyup:13,0,13,Enter,undefined,Enter,false"},
FF_ESR = {"keydown:16,0,16,Shift,undefined,ShiftLeft,true",
"keydown:65,0,65,A,undefined,KeyA,true",
"keypress:0,65,65,A,undefined,KeyA,true",
"keyup:65,0,65,A,undefined,KeyA,true",
"keyup:16,0,16,Shift,undefined,ShiftLeft,false",
"keydown:65,0,65,a,undefined,KeyA,false",
"keypress:0,97,97,a,undefined,KeyA,false",
"keyup:65,0,65,a,undefined,KeyA,false",
"keydown:190,0,190,.,undefined,Period,false",
"keypress:0,46,46,.,undefined,Period,false",
"keyup:190,0,190,.,undefined,Period,false",
"keydown:13,0,13,Enter,undefined,Enter,false",
"keypress:13,0,13,Enter,undefined,Enter,false",
"keyup:13,0,13,Enter,undefined,Enter,false"},
IE = {"keydown:16,0,16,Shift,,undefined,true",
"keydown:65,0,65,A,A,undefined,true",
"keypress:65,65,65,A,A,undefined,true",
Expand Down

0 comments on commit 400ddab

Please sign in to comment.