Reviewers: Yuta Kitamura, yoichio, Takayoshi Kochi,
Message:
Could you review this patch?
Thanks in advance.
Description:
Make "compositionstart" event cancelable
This patch makes "compositionstart" event cancelable as DOM Level 3 event
specification[1].
[1]
http://www.w3.org/TR/DOM-Level-3-Events/#events-compositionevents
BUG=228440
TEST=Source/core/editing/InputMethodControllerTest.cpp
Please review this at
https://codereview.chromium.org/311053008/
SVN Base:
https://chromium.googlesource.com/chromium/blink.git@master
Affected files (+76, -1 lines):
M Source/core/core.gypi
M Source/core/editing/InputMethodController.cpp
A Source/core/editing/InputMethodControllerTest.cpp
Index: Source/core/core.gypi
diff --git a/Source/core/core.gypi b/Source/core/core.gypi
index
5b8c58f59f6e12fa711694e4b76c10a14c4234af..439f3e967fe20a2f4e79b9a62af4fb9476d424a1
100644
--- a/Source/core/core.gypi
+++ b/Source/core/core.gypi
@@ -3385,6 +3385,7 @@
'dom/MainThreadTaskRunnerTest.cpp',
'dom/RangeTest.cpp',
'dom/TreeScopeTest.cpp',
+ 'editing/InputMethodControllerTest.cpp',
'editing/TextIteratorTest.cpp',
'editing/VisibleSelectionTest.cpp',
'fetch/CachingCorrectnessTest.cpp',
Index: Source/core/editing/InputMethodController.cpp
diff --git a/Source/core/editing/InputMethodController.cpp
b/Source/core/editing/InputMethodController.cpp
index
3609e75405069a27fc42c957cd5e295daa8802a6..f50dc088a4f953724c8e2fe5acd37c8a47ed1cf5
100644
--- a/Source/core/editing/InputMethodController.cpp
+++ b/Source/core/editing/InputMethodController.cpp
@@ -259,7 +259,9 @@ void InputMethodController::setComposition(const
String& text, const Vector<Comp
// We should send a compositionstart event only when the given
text is not empty because this
// function doesn't create a composition node when the text is
empty.
if (!text.isEmpty()) {
-
target->dispatchEvent(CompositionEvent::create(EventTypeNames::compositionstart,
m_frame.domWindow(), m_frame.selectedText(), underlines));
+ RefPtrWillBeRawPtr<CompositionEvent> compositionStartEvent
= CompositionEvent::create(EventTypeNames::compositionstart,
m_frame.domWindow(), m_frame.selectedText(), underlines);
+ if (!target->dispatchEvent(compositionStartEvent))
+ return;
event =
CompositionEvent::create(EventTypeNames::compositionupdate,
m_frame.domWindow(), text, underlines);
}
} else {
Index: Source/core/editing/InputMethodControllerTest.cpp
diff --git a/Source/core/editing/InputMethodControllerTest.cpp
b/Source/core/editing/InputMethodControllerTest.cpp
new file mode 100644
index
0000000000000000000000000000000000000000..b432aefc0ec7d1d83da129ebc5b0f8b12b28371b
--- /dev/null
+++ b/Source/core/editing/InputMethodControllerTest.cpp
@@ -0,0 +1,72 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/editing/InputMethodController.h"
+
+#include "core/dom/Text.h"
+#include "core/editing/FrameSelection.h"
+#include "core/frame/LocalFrame.h"
+#include "core/frame/Settings.h"
+#include "core/html/HTMLDocument.h"
+#include "core/html/HTMLElement.h"
+#include "core/testing/DummyPageHolder.h"
+#include <gtest/gtest.h>
+
+using namespace WebCore;
+
+namespace WebCore {
+
+class InputMethodControllerTest : public ::testing::Test {
+protected:
+ HTMLDocument& document() const { return *m_document; }
+ LocalFrame& frame() const { return m_dummyPageHolder->frame(); }
+
+private:
+ virtual void SetUp() OVERRIDE;
+
+ OwnPtr<DummyPageHolder> m_dummyPageHolder;
+ HTMLDocument* m_document;
+};
+
+void InputMethodControllerTest::SetUp()
+{
+ m_dummyPageHolder = DummyPageHolder::create(IntSize(800, 600));
+ m_document = toHTMLDocument(&m_dummyPageHolder->document());
+ ASSERT(m_document);
+}
+
+} // namespace WebCore
+
+namespace {
+
+using namespace WebCore;
+
+TEST_F(InputMethodControllerTest, setComposition)
+{
+ document().write("<div contenteditable='true' id='target'>foo</div>");
+ RefPtrWillBeRawPtr<Element> input =
document().getElementById("target");
+ frame().selection().moveTo(Position(toText(input->firstChild()), 0),
DOWNSTREAM);
+ frame().inputMethodController().setComposition("bar",
Vector<CompositionUnderline>(), 0, 0);
+ EXPECT_STREQ("barfoo", input->textContent().utf8().data());
+}
+
+TEST_F(InputMethodControllerTest, setCompositionCancelCompositionStart)
+{
+ frame().settings()->setScriptEnabled(true);
+ document().write("<div contenteditable='true' id='target'>foo</div>"
+ "<script>"
+ "document.getElementById('target').addEventListener('compositionstart',
function(event)
{"
+ " event.preventDefault();"
+ "});"
+ "</script>");
+ document().updateLayout();
+ RefPtrWillBeRawPtr<Element> input =
document().getElementById("target");
+ document().setFocusedElement(input);
+ frame().selection().moveTo(Position(toText(input->firstChild()), 0),
DOWNSTREAM);
+ frame().inputMethodController().setComposition("bar",
Vector<CompositionUnderline>(), 0, 0);
+ EXPECT_STREQ("foo", input->textContent().utf8().data());
+}
+
+} // namespace WebCore