Suppress enter shortcut when <a> is focused

The shouldSuppressKeyboardShortcut function exists to generally suppress
keyboard shortcuts when the browser native functionality shoud not be
overridden. One such case is on 'enter' when an anchor is focused.

Bug: Issue 9443
Change-Id: I49baf0ff828ba879d43d022ab0e8a6bae44bcd1d
This commit is contained in:
Kasper Nilsson
2018-08-06 10:58:01 -07:00
parent 55a9a94941
commit 5820b2f085
2 changed files with 15 additions and 1 deletions

View File

@@ -48,7 +48,9 @@ limitations under the License.
shouldSuppressKeyboardShortcut(e) {
e = getKeyboardEvent(e);
const tagName = Polymer.dom(e).rootTarget.tagName;
if (tagName === 'INPUT' || tagName === 'TEXTAREA') {
if (tagName === 'INPUT' || tagName === 'TEXTAREA' ||
(e.keyCode === 13 && tagName === 'A')) {
// Suppress shortcuts if the key is 'enter' and target is an anchor.
return true;
}
for (let i = 0; e.path && i < e.path.length; i++) {

View File

@@ -51,6 +51,7 @@ limitations under the License.
behaviors: [Gerrit.KeyboardShortcutBehavior],
keyBindings: {
k: '_handleKey',
enter: '_handleKey',
},
_handleKey() {},
});
@@ -107,6 +108,17 @@ limitations under the License.
MockInteractions.keyDownOn(divEl, 75, null, 'k');
});
test('blocks enter shortcut on an anchor', done => {
const anchorEl = document.createElement('a');
const element = overlay.querySelector('test-element');
element.appendChild(anchorEl);
element._handleKey = e => {
assert.isTrue(element.shouldSuppressKeyboardShortcut(e));
done();
};
MockInteractions.keyDownOn(anchorEl, 13, null, 'enter');
});
test('modifierPressed returns accurate values', () => {
const spy = sandbox.spy(element, 'modifierPressed');
element._handleKey = e => {