Skip to content

Commit a3456a9

Browse files
committed
Bug 1055085 - Clear input search on esc keydown. r=smaug,masayuki
This matches Blink and WebKit, with the difference that this preserves the undo stack (which I think is useful and mitigates a bit the double-esc concerns Masayuki had in bug 1936648). Keydown because it matches both other engines, though I think keypress would've been more consistent... Differential Revision: https://phabricator.services.mozilla.com/D251563
1 parent c21daa2 commit a3456a9

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

browser/components/search/content/searchbar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@
795795
} else {
796796
this.textbox.select();
797797
}
798+
aEvent.preventDefault();
798799
return true;
799800
}
800801
return false;

dom/html/HTMLInputElement.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,6 +3839,18 @@ nsresult HTMLInputElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
38393839
EventStateManager::SetActiveManager(
38403840
aVisitor.mPresContext->EventStateManager(), this);
38413841
}
3842+
3843+
if (keyEvent->mKeyCode == NS_VK_ESCAPE && keyEvent->IsTrusted() &&
3844+
!keyEvent->DefaultPrevented() && !keyEvent->mIsComposing &&
3845+
mType == FormControlType::InputSearch &&
3846+
StaticPrefs::dom_forms_search_esc() && !IsDisabledOrReadOnly() &&
3847+
!IsValueEmpty()) {
3848+
// WebKit and Blink both also do this on keydown, see:
3849+
// https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/html/forms/search_input_type.cc;l=82;drc=04f1f437aaefbd3bb4e0cdb5911c1ea1e3eb3557;bpv=1;bpt=1
3850+
// https://searchfox.org/wubkat/rev/717f9adc97dd16bf639d27addbe0faf420f7dfce/Source/WebCore/html/SearchInputType.cpp#145
3851+
SetUserInput(EmptyString(), *NodePrincipal());
3852+
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
3853+
}
38423854
break;
38433855
}
38443856

modules/libpref/init/StaticPrefList.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,6 +3042,13 @@
30423042
value: false
30433043
mirror: always
30443044

3045+
# This pref controls whether <input type=search> clears its value on Esc
3046+
# keydown.
3047+
- name: dom.forms.search.esc
3048+
type: bool
3049+
value: true
3050+
mirror: always
3051+
30453052
# The interval in milliseconds between two Escape key events where the second
30463053
# key event will exit fullscreen, even if it is consumed.
30473054
- name: dom.fullscreen.force_exit_on_multiple_escape_interval
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!doctype html>
2+
<meta charset=utf-8>
3+
<title>Esc clears search input</title>
4+
<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
5+
<link rel="author" href="https://mozilla.org" title="Mozilla">
6+
<script src="/resources/testharness.js"></script>
7+
<script src="/resources/testharnessreport.js"></script>
8+
<script src="/resources/testdriver.js"></script>
9+
<script src="/resources/testdriver-vendor.js"></script>
10+
<script src="/resources/testdriver-actions.js"></script>
11+
<input type=search>
12+
<script>
13+
const kEsc = '\uE00C';
14+
let input = document.querySelector("input");
15+
promise_test(async function() {
16+
input.focus();
17+
await test_driver.send_keys(input, "abc");
18+
assert_equals(input.value, "abc");
19+
let inputEvent = false;
20+
let changeEvent = false;
21+
input.addEventListener("input", function() {
22+
inputEvent = true;
23+
}, { once: true });
24+
await test_driver.send_keys(input, kEsc);
25+
assert_equals(input.value, "", "should've been cleared");
26+
assert_true(inputEvent, "input event should've fired");
27+
await test_driver.send_keys(input, "abc");
28+
assert_equals(input.value, "abc", "Should've been able to type again");
29+
input.readOnly = true;
30+
await test_driver.send_keys(input, kEsc);
31+
assert_equals(input.value, "abc", "Shouldn't have cleared on readonly input");
32+
});
33+
</script>

0 commit comments

Comments
 (0)