Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1140)

Unified Diff: ui/events/keycodes/dom/keycode_converter.cc

Issue 1284433002: Revise ui::DomKey to unify character and non-character codes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/events/keycodes/dom/keycode_converter.cc
diff --git a/ui/events/keycodes/dom/keycode_converter.cc b/ui/events/keycodes/dom/keycode_converter.cc
index 9aef53f29716fb41f263f52538ee9179a0a32699..5c8aa6052bcf60715bfa05b1f3d7cab2e4e8c211 100644
--- a/ui/events/keycodes/dom/keycode_converter.cc
+++ b/ui/events/keycodes/dom/keycode_converter.cc
@@ -5,6 +5,7 @@
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "base/logging.h"
+#include "base/strings/utf_string_conversion_utils.h"
#include "ui/events/keycodes/dom/dom_code.h"
#include "ui/events/keycodes/dom/dom_key.h"
@@ -36,11 +37,17 @@ struct DomKeyMapEntry {
const char* string;
};
-#define DOM_KEY_MAP(key, id) {DomKey::id, key}
#define DOM_KEY_MAP_DECLARATION const DomKeyMapEntry dom_key_map[] =
+#define DOM_KEY_UNI(key, id, value) {DomKey::id, key}
+#define DOM_KEY_MAP_BEGIN
+#define DOM_KEY_MAP(key, id) {DomKey::id, key}
+#define DOM_KEY_MAP_END
#include "ui/events/keycodes/dom/dom_key_data.inc"
-#undef DOM_KEY_MAP
#undef DOM_KEY_MAP_DECLARATION
+#undef DOM_KEY_MAP_BEGIN
+#undef DOM_KEY_MAP
+#undef DOM_KEY_MAP_END
+#undef DOM_KEY_UNI
const size_t kDomKeyMapEntries = arraysize(dom_key_map);
@@ -166,20 +173,49 @@ DomKeyLocation KeycodeConverter::DomCodeToLocation(DomCode dom_code) {
DomKey KeycodeConverter::KeyStringToDomKey(const char* key) {
if (!key || !*key)
return DomKey::NONE;
+ // Check for standard key names.
for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
- if (dom_key_map[i].string &&
- strcmp(dom_key_map[i].string, key) == 0) {
+ if (dom_key_map[i].string && strcmp(dom_key_map[i].string, key) == 0) {
return dom_key_map[i].dom_key;
}
}
+ if (strcmp(key, "Dead") == 0) {
+ // The web KeyboardEvent string does not encode the combining character,
+ // so we just set it to the Unicode designated non-character 0xFFFF.
+ // This will round-trip convert back to 'Dead' but take no part in
+ // character composition.
+ return DomKey::Dead(0xFFFF);
+ }
+ // Otherwise, if the string contains a single Unicode character,
+ // the key value is that character.
+ int32_t char_index = 0;
+ uint32_t character;
+ if (base::ReadUnicodeCharacter(key, static_cast<int32_t>(strlen(key)),
+ &char_index, &character) &&
+ key[++char_index] == 0) {
+ return character;
+ }
return DomKey::NONE;
}
// static
kpschoedel 2015/08/07 20:42:12 This now returns a std::string; there are no curre
-const char* KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
+std::string KeycodeConverter::DomKeyToKeyString(DomKey dom_key) {
+ if (dom_key.IsDead()) {
+ // All dead-key combining codes collapse to 'Dead', as UI Events
+ // KeyboardEvent represents the combining character separately.
+ return "Dead";
+ }
for (size_t i = 0; i < kDomKeyMapEntries; ++i) {
dtapuska 2015/08/13 14:47:26 Can this not use bsearch? The DomKeyMap is an ord
kpschoedel 2015/08/13 15:46:06 I've generally been leaving these things alone, as
- if (dom_key_map[i].dom_key == dom_key)
- return dom_key_map[i].string;
+ if (dom_key_map[i].dom_key == dom_key) {
+ if (dom_key_map[i].string)
+ return dom_key_map[i].string;
+ return "";
dtapuska 2015/08/13 14:47:26 I believe its preferred to return std::string(); s
kpschoedel 2015/08/13 15:46:06 Will do.
+ }
+ }
+ if (dom_key.IsUnicode()) {
+ std::string s;
+ base::WriteUnicodeCharacter(dom_key, &s);
+ return s;
}
return "";
}

Powered by Google App Engine
This is Rietveld 408576698