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

Unified Diff: third_party/WebKit/Source/core/events/InputEvent.cpp

Issue 1752933002: [InputEvent] Fire 'beforeinput' during typing, pressing hot keys and IME composition (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Yosin's review 3 Created 4 years, 8 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: third_party/WebKit/Source/core/events/InputEvent.cpp
diff --git a/third_party/WebKit/Source/core/events/InputEvent.cpp b/third_party/WebKit/Source/core/events/InputEvent.cpp
index f7eb07a65d040c9cb0071ccc8a29dc660983bf09..c8cb04e80d1a1d9d6f41b3db2ffc47ea9ac75e17 100644
--- a/third_party/WebKit/Source/core/events/InputEvent.cpp
+++ b/third_party/WebKit/Source/core/events/InputEvent.cpp
@@ -5,9 +5,48 @@
#include "core/events/InputEvent.h"
#include "core/events/EventDispatcher.h"
+#include "public/platform/WebEditingCommandType.h"
namespace blink {
+namespace {
+
+const struct {
+ InputEvent::InputType inputType;
+ const char* stringName;
+} kInputTypeStringNameMap[] = {
+ {InputEvent::InputType::None, ""},
+ {InputEvent::InputType::InsertText, "insertText"},
+ {InputEvent::InputType::ReplaceContent, "replaceContent"},
+ {InputEvent::InputType::DeleteContent, "deleteContent"},
+ {InputEvent::InputType::DeleteComposedCharacter, "deleteComposedCharacter"},
+ {InputEvent::InputType::Undo, "undo"},
+ {InputEvent::InputType::Redo, "redo"},
ojan 2016/04/16 00:11:46 We will need the spec to make sure to list all the
chongz 2016/04/18 22:54:47 Posted question in https://github.com/w3c/editing/
+};
+
+static_assert(arraysize(kInputTypeStringNameMap) == static_cast<size_t>(InputEvent::InputType::NumberOfInputTypes),
+ "must handle all InputEvent::InputType");
+
+String convertInputTypeToString(InputEvent::InputType inputType)
+{
+ const auto& it = std::begin(kInputTypeStringNameMap) + static_cast<size_t>(inputType);
+ if (it >= std::begin(kInputTypeStringNameMap) && it < std::end(kInputTypeStringNameMap))
+ return AtomicString(it->stringName);
+ return emptyString();
+}
+
+InputEvent::InputType convertStringToInputType(const String& stringName)
+{
+ // TODO(chongz): Use binary search if the map goes larger.
+ for (const auto& entry : kInputTypeStringNameMap) {
+ if (codePointCompareIgnoringASCIICase(stringName, entry.stringName) == 0)
ojan 2016/04/16 00:11:46 Why do we need to ignore case?
chongz 2016/04/18 22:54:48 I was trying to match the style of |execCommand()|
+ return entry.inputType;
+ }
+ return InputEvent::InputType::None;
+}
+
+} // anonymous namespace
+
InputEvent::InputEvent()
{
}
@@ -15,6 +54,54 @@ InputEvent::InputEvent()
InputEvent::InputEvent(const AtomicString& type, const InputEventInit& initializer)
: UIEvent(type, initializer)
{
+ if (initializer.hasInputType())
+ m_inputType = convertStringToInputType(initializer.inputType());
ojan 2016/04/16 00:11:45 TODO: ojan
chongz 2016/04/18 22:54:47 Done.
+ if (initializer.hasData())
+ m_data = initializer.data();
+}
+
+/* static */
+InputEvent* InputEvent::createBeforeInputTyping(InputType inputType, const String& data)
+{
+ InputEventInit inputEventInit;
+
+ inputEventInit.setBubbles(true);
+ inputEventInit.setCancelable(true);
+ inputEventInit.setInputType(convertInputTypeToString(inputType));
ojan 2016/04/16 00:11:46 It seems pretty silly for us to convert the enum t
chongz 2016/04/18 22:54:47 Done.
+ inputEventInit.setData(data);
+
+ return InputEvent::create(EventTypeNames::beforeinput, inputEventInit);
+}
+
+/* static */
+InputEvent* InputEvent::createBeforeInputFromComposition(InputType inputType, const String& data)
+{
+ InputEventInit inputEventInit;
+
+ inputEventInit.setBubbles(true);
+ inputEventInit.setCancelable(false);
+ inputEventInit.setInputType(convertInputTypeToString(inputType));
+ inputEventInit.setData(data);
+
+ return InputEvent::create(EventTypeNames::beforeinput, inputEventInit);
+}
+
+/* static */
+InputEvent* InputEvent::createBeforeInputEditorCommand(InputType inputType, const String& data)
ojan 2016/04/16 00:11:46 This looks the same as createBeforeInputTyping. Ca
chongz 2016/04/18 22:54:47 Done.
+{
+ InputEventInit inputEventInit;
+
+ inputEventInit.setBubbles(true);
+ inputEventInit.setCancelable(true);
+ inputEventInit.setInputType(convertInputTypeToString(inputType));
+ inputEventInit.setData(data);
+
+ return InputEvent::create(EventTypeNames::beforeinput, inputEventInit);
+}
+
+String InputEvent::inputType() const
+{
+ return convertInputTypeToString(m_inputType);
}
bool InputEvent::isInputEvent() const

Powered by Google App Engine
This is Rietveld 408576698