Skip to content

Commit

Permalink
Convert Event#keyIdentifier (deprecated) to Event#key
Browse files Browse the repository at this point in the history
BUG=607349
CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:closure_compilation
TBR=rockot@chromium.org

Review-Url: https://codereview.chromium.org/2104103002
Cr-Commit-Position: refs/heads/master@{#403715}
  • Loading branch information
dtapuska authored and Commit bot committed Jul 4, 2016
1 parent 1666624 commit 89d8be5
Show file tree
Hide file tree
Showing 29 changed files with 256 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<body>
<commands>
<command id="delete" i18n-values="label:deleteCommandLabel"
shortcut="U+007F"></command>
shortcut="Delete"></command>
</commands>

<cr-menu id="wallpaper-context-menu" class="chrome-menu"
Expand Down
44 changes: 11 additions & 33 deletions chrome/browser/resources/md_downloads/crisper.js
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ cr.define('cr.ui', function() {
function KeyboardShortcut(shortcut) {
var mods = {};
var ident = '';
shortcut.split('-').forEach(function(part) {
shortcut.split('|').forEach(function(part) {
var partLc = part.toLowerCase();
switch (partLc) {
case 'alt':
Expand All @@ -866,7 +866,7 @@ cr.define('cr.ui', function() {
* @return {boolean} Whether we found a match or not.
*/
matchesEvent: function(e) {
if (e.keyIdentifier == this.ident_) {
if (e.key == this.ident_) {
// All keyboard modifiers needs to match.
var mods = this.mods_;
return ['altKey', 'ctrlKey', 'metaKey', 'shiftKey'].every(function(k) {
Expand Down Expand Up @@ -927,17 +927,17 @@ cr.define('cr.ui', function() {

/**
* The keyboard shortcut that triggers the command. This is a string
* consisting of a keyIdentifier (as reported by WebKit in keydown) as
* well as optional key modifiers joinded with a '-'.
* consisting of a key (as reported by WebKit in keydown) as
* well as optional key modifiers joinded with a '|'.
*
* Multiple keyboard shortcuts can be provided by separating them by
* whitespace.
*
* For example:
* "F1"
* "U+0008-Meta" for Apple command backspace.
* "U+0041-Ctrl" for Control A
* "U+007F U+0008-Meta" for Delete and Command Backspace
* "Backspace-Meta" for Apple command backspace.
* "a-Ctrl" for Control A
* "Delete Backspace-Meta" for Delete and Command Backspace
*
* @type {string}
*/
Expand Down Expand Up @@ -1714,8 +1714,7 @@ function quoteString(str) {

/**
* Special table for KeyboardEvent.keyCode.
* KeyboardEvent.keyIdentifier is better, and KeyBoardEvent.key is even better
* than that.
* KeyBoardEvent.key is even better than that.
*
* Values from: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent.keyCode#Value_of_keyCode
*/
Expand Down Expand Up @@ -1758,11 +1757,6 @@ function quoteString(str) {
*/
var KEY_CHAR = /[a-z0-9*]/;

/**
* Matches a keyIdentifier string.
*/
var IDENT_CHAR = /U\+/;

/**
* Matches arrow keys in Gecko 27.0+
*/
Expand Down Expand Up @@ -1811,21 +1805,6 @@ function quoteString(str) {
return validKey;
}

function transformKeyIdentifier(keyIdent) {
var validKey = '';
if (keyIdent) {
if (keyIdent in KEY_IDENTIFIER) {
validKey = KEY_IDENTIFIER[keyIdent];
} else if (IDENT_CHAR.test(keyIdent)) {
keyIdent = parseInt(keyIdent.replace('U+', '0x'), 16);
validKey = String.fromCharCode(keyIdent).toLowerCase();
} else {
validKey = keyIdent.toLowerCase();
}
}
return validKey;
}

function transformKeyCode(keyCode) {
var validKey = '';
if (Number(keyCode)) {
Expand Down Expand Up @@ -1860,10 +1839,9 @@ function quoteString(str) {
* To get @ returned, set noSpecialChars = false
*/
function normalizedKeyForEvent(keyEvent, noSpecialChars) {
// Fall back from .key, to .keyIdentifier, to .keyCode, and then to
// Fall back from .key, to .keyCode, and then to
// .detail.key to support artificial keyboard events.
return transformKey(keyEvent.key, noSpecialChars) ||
transformKeyIdentifier(keyEvent.keyIdentifier) ||
transformKeyCode(keyEvent.keyCode) ||
transformKey(keyEvent.detail ? keyEvent.detail.key : keyEvent.detail, noSpecialChars) || '';
}
Expand Down Expand Up @@ -4178,7 +4156,7 @@ var ActionLink = document.registerElement('action-link', {
this.setAttribute('role', 'link');

this.addEventListener('keydown', function(e) {
if (!this.disabled && e.keyIdentifier == 'Enter' && !this.href) {
if (!this.disabled && e.key == 'Enter' && !this.href) {
// Schedule a click asynchronously because other 'keydown' handlers
// may still run later (e.g. document.addEventListener('keydown')).
// Specifically options dialogs break when this timeout isn't here.
Expand Down Expand Up @@ -11546,4 +11524,4 @@ cr.define('downloads', function() {
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

window.addEventListener('load', downloads.Manager.onLoad);
window.addEventListener('load', downloads.Manager.onLoad);
10 changes: 5 additions & 5 deletions chrome/browser/resources/md_downloads/downloads.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
</head>
<body>
<downloads-manager></downloads-manager>
<command id="clear-all-command" shortcut="Alt-U+0043"><!-- Alt+C -->
<command id="clear-all-command" shortcut="Alt|c">
<if expr="is_macosx">
<command id="undo-command" shortcut="Meta-U+005A"><!-- Command+Z -->
<command id="find-command" shortcut="Meta-U+0046"><!-- Command+F -->
<command id="undo-command" shortcut="Meta|z">
<command id="find-command" shortcut="Meta|f">
</if>
<if expr="not is_macosx">
<command id="undo-command" shortcut="Ctrl-U+005A"><!-- Ctrl+Z -->
<command id="find-command" shortcut="Ctrl-U+0046"><!-- Ctrl+F -->
<command id="undo-command" shortcut="Ctrl|z">
<command id="find-command" shortcut="Ctrl|f">
</if>
<link rel="import" href="chrome://resources/html/polymer.html">
<link rel="import" href="chrome://downloads/i18n_setup.html">
Expand Down
12 changes: 6 additions & 6 deletions chrome/browser/resources/md_downloads/vulcanized.html
Original file line number Diff line number Diff line change
Expand Up @@ -2772,14 +2772,14 @@ <h1>$i18n{title}</h1>
</dom-module>
</div>
<downloads-manager></downloads-manager>
<command id="clear-all-command" shortcut="Alt-U+0043"></command>
<command id="clear-all-command" shortcut="Alt|c"></command>
<if expr="is_macosx">
<command id="undo-command" shortcut="Meta-U+005A"></command>
<command id="find-command" shortcut="Meta-U+0046"></command>
<command id="undo-command" shortcut="Meta|z"></command>
<command id="find-command" shortcut="Meta|f"></command>
</if>
<if expr="not is_macosx">
<command id="undo-command" shortcut="Ctrl-U+005A"></command>
<command id="find-command" shortcut="Ctrl-U+0046"></command>
<command id="undo-command" shortcut="Ctrl|z"></command>
<command id="find-command" shortcut="Ctrl|f"></command>
</if>
<link rel="import" href="chrome://resources/html/polymer.html">
<script src="crisper.js"></script></body></html>
<script src="crisper.js"></script></body></html>
2 changes: 1 addition & 1 deletion chrome/browser/ui/webui/options/options_browsertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ TEST_F('OptionsWebUITest', 'EnterPreventsDefault', function() {
var event = new KeyboardEvent('keydown', {
'bubbles': true,
'cancelable': true,
'keyIdentifier': 'Enter'
'key': 'Enter'
});
assertFalse(event.defaultPrevented);
page.pageDiv.dispatchEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ cr.define('cr.ui', function() {
if (e.keyCode == SPACE_KEY_CODE)
return;
// Protect all but the most basic navigation commands in anything else.
} else if (e.keyIdentifier != 'Up' && e.keyIdentifier != 'Down') {
} else if (e.key != 'ArrowUp' && e.key != 'ArrowDown') {
return;
}
}
Expand Down Expand Up @@ -224,26 +224,26 @@ cr.define('cr.ui', function() {
}
}

switch (e.keyIdentifier) {
switch (e.key) {
case 'Home':
newIndex = this.getFirstIndex();
break;
case 'End':
newIndex = this.getLastIndex();
break;
case 'Up':
case 'ArrowUp':
newIndex = leadIndex == -1 ?
this.getLastIndex() : this.getIndexAbove(leadIndex);
break;
case 'Down':
case 'ArrowDown':
newIndex = leadIndex == -1 ?
this.getFirstIndex() : this.getIndexBelow(leadIndex);
break;
case 'Left':
case 'ArrrowLeft':
newIndex = leadIndex == -1 ?
this.getLastIndex() : this.getIndexBefore(leadIndex);
break;
case 'Right':
case 'ArrowRight':
newIndex = leadIndex == -1 ?
this.getFirstIndex() : this.getIndexAfter(leadIndex);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ cr.define('options', function() {
return;

var endEdit = false;
switch (e.keyIdentifier) {
case 'U+001B': // Esc
switch (e.key) {
case 'Escape':
this.editCancelled_ = true;
endEdit = true;
break;
Expand Down
2 changes: 1 addition & 1 deletion chrome/renderer/resources/plugins/plugin_poster.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
};

window.onkeydown = function(e) {
if (e.keyIdentifier == 'Enter' || e.keyIdentifier == 'U+0020') {
if (e.key == 'Enter' || e.key == ' ') {
plugin.load();
e.preventDefault();
}
Expand Down
38 changes: 16 additions & 22 deletions chrome/test/data/webui/history_browsertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,9 +818,8 @@ TEST_F('HistoryWebUIRealBackendTest',
visit.titleLink.focus();
assertEquals(visit.titleLink, document.activeElement);

var deleteKey = document.createEvent('KeyboardEvent');
deleteKey.initKeyboardEvent('keydown', true, true, window, 'U+007F');
assertEquals('U+007F', deleteKey.keyIdentifier);
var deleteKey = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'Delete'});

assertFalse(historyModel.isDeletingVisits());
expectFalse(visit.titleLink.dispatchEvent(deleteKey));
Expand Down Expand Up @@ -883,16 +882,14 @@ TEST_F('HistoryWebUIRealBackendTest', 'leftRightChangeFocus', function() {
visit.titleLink.focus();
assertEquals(visit.titleLink, document.activeElement);

var right = document.createEvent('KeyboardEvent');
right.initKeyboardEvent('keydown', true, true, window, 'Right');
assertEquals('Right', right.keyIdentifier);
var right = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'ArrowRight'});
expectFalse(visit.titleLink.dispatchEvent(right));

assertEquals(visit.dropDown, document.activeElement);

var left = document.createEvent('KeyboardEvent');
left.initKeyboardEvent('keydown', true, true, window, 'Left');
assertEquals('Left', left.keyIdentifier);
var left = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'ArrowLeft'});
expectFalse(visit.dropDown.dispatchEvent(left));

expectEquals(visit.titleLink, document.activeElement);
Expand All @@ -910,8 +907,8 @@ TEST_F('HistoryWebUIRealBackendTest', 'showConfirmDialogAndCancel', function() {
assertTrue($('alertOverlay').classList.contains('showing'));
assertFalse($('history-page').contains(document.activeElement));

var esc = document.createEvent('KeyboardEvent');
esc.initKeyboardEvent('keydown', true, true, window, 'U+001B');
var esc = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'Escape'});

document.documentElement.dispatchEvent(esc);
assertFalse($('alertOverlay').classList.contains('showing'));
Expand All @@ -928,8 +925,8 @@ TEST_F('HistoryWebUIRealBackendTest', 'showConfirmDialogAndRemove', function() {

waitForCallback('deleteComplete', testDone);

var enter = document.createEvent('KeyboardEvent');
enter.initKeyboardEvent('keydown', true, true, window, 'Enter');
var enter = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'Enter'});
document.documentElement.dispatchEvent(enter);
assertFalse($('alertOverlay').classList.contains('showing'));
});
Expand Down Expand Up @@ -1039,16 +1036,14 @@ TEST_F('HistoryWebUIDeleteProhibitedTest', 'leftRightChangeFocus', function() {
visit.titleLink.focus();
assertEquals(visit.titleLink, document.activeElement);

var right = document.createEvent('KeyboardEvent');
right.initKeyboardEvent('keydown', true, true, window, 'Right');
assertEquals('Right', right.keyIdentifier);
var right = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'ArrowRight'});
expectFalse(visit.titleLink.dispatchEvent(right));

assertEquals(visit.dropDown, document.activeElement);

var left = document.createEvent('KeyboardEvent');
left.initKeyboardEvent('keydown', true, true, window, 'Left');
assertEquals('Left', left.keyIdentifier);
var left = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'ArrowLeft'});
expectFalse(visit.dropDown.dispatchEvent(left));

expectEquals(visit.titleLink, document.activeElement);
Expand All @@ -1062,9 +1057,8 @@ TEST_F('HistoryWebUIDeleteProhibitedTest', 'deleteIgnored', function() {
visit.titleLink.focus();
assertEquals(visit.titleLink, document.activeElement);

var deleteKey = document.createEvent('KeyboardEvent');
deleteKey.initKeyboardEvent('keydown', true, true, window, 'U+007F');
assertEquals('U+007F', deleteKey.keyIdentifier);
var deleteKey = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: 'Delete'});

assertFalse(historyModel.isDeletingVisits());
expectTrue(visit.titleLink.dispatchEvent(deleteKey));
Expand Down
19 changes: 9 additions & 10 deletions chrome/test/data/webui/inline_editable_list_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@

/**
* @param {!EventTarget} target Where to send the event.
* @param {!string} keyIdentifier Which key to send.
* @param {!string} key Which key to send.
*/
function sendKeyDownEvent(target, keyIdentifier) {
var event = document.createEvent('KeyboardEvent');
event.initKeyboardEvent('keydown', true, true, window, keyIdentifier);
assertEquals(keyIdentifier, event.keyIdentifier);
function sendKeyDownEvent(target, key) {
var event = new KeyboardEvent('keydown',
{bubbles: true, cancelable: true, key: key});
target.dispatchEvent(event);
}

Expand All @@ -135,15 +134,15 @@
list.selectionModel.leadIndex = 0;

assertTrue(list.hasExactlyOneItemFocusable(0));
sendKeyDownEvent(list, 'Down');
sendKeyDownEvent(list, 'ArrowDown');
assertTrue(list.hasExactlyOneItemFocusable(1));
sendKeyDownEvent(list, 'Down');
sendKeyDownEvent(list, 'ArrowDown');
assertTrue(list.hasExactlyOneItemFocusable(2));
sendKeyDownEvent(list, 'Up');
sendKeyDownEvent(list, 'ArrowUp');
assertTrue(list.hasExactlyOneItemFocusable(1));
sendKeyDownEvent(list, 'Up');
sendKeyDownEvent(list, 'ArrowUp');
assertTrue(list.hasExactlyOneItemFocusable(0));
sendKeyDownEvent(list, 'Down');
sendKeyDownEvent(list, 'ArrowDown');
assertTrue(list.hasExactlyOneItemFocusable(1));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
-- found in the LICENSE file.
-->

<script>
function queryRequiredElement(selectors, opt_context) {
var element = (opt_context || document).querySelector(selectors);
return assertInstanceof(element, HTMLElement,
'Missing required element: ' + selectors);
}
</script>

<command id="share" label="Share"></command>
<command id="toggle-pinned" label="Toggle pinned"></command>
<command id="create-folder-shortcut" label="Create folder shortcut"></command>
Expand All @@ -20,7 +28,6 @@
<script src="../../../../../../ui/webui/resources/js/cr/ui/command.js"></script>
<script src="../../../../../../ui/webui/resources/js/cr/ui/menu_item.js"></script>
<script src="../../../../../../ui/webui/resources/js/cr/ui/menu.js"></script>
<script src="../../../../../../ui/webui/resources/js/util.js"></script>
<script src="../../../common/js/util.js"></script>
<script src="../actions_model.js"></script>
<script src="../mock_actions_model.js"></script>
Expand Down
Loading

0 comments on commit 89d8be5

Please sign in to comment.