Commit 040e9749 authored by Alex Keng's avatar Alex Keng Committed by Chromium LUCI CQ

EditContext: enable English typing

This CL enables English typing for EditContext by redirecting inserted
text in Editor::HandleEditingKeyboardEvent if EditContext is active.

A test is also added in edit-context.html.

Change-Id: I0c63371385556869ab71652d6857d18d985260d9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626528Reviewed-by: default avatarAnupam Snigdha <snianu@microsoft.com>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Alex Keng <shihken@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#843605}
parent 4e759602
......@@ -32,6 +32,8 @@
#include "third_party/blink/renderer/core/editing/editing_behavior.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/ime/edit_context.h"
#include "third_party/blink/renderer/core/editing/ime/input_method_controller.h"
#include "third_party/blink/renderer/core/events/keyboard_event.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
......@@ -60,7 +62,20 @@ bool Editor::HandleEditingKeyboardEvent(KeyboardEvent* evt) {
if (command.Execute(evt))
return true;
if (!Behavior().ShouldInsertCharacter(*evt) || !CanEdit())
if (!Behavior().ShouldInsertCharacter(*evt))
return false;
// If EditContext is active, redirect text to EditContext, otherwise, send
// text to the focused element.
auto* edit_context =
GetFrame().GetInputMethodController().GetActiveEditContext();
if (edit_context) {
WebString text(WTF::String(key_event->text));
edit_context->InsertText(text);
return true;
}
if (!CanEdit())
return false;
const Element* const focused_element =
......
......@@ -457,6 +457,20 @@ bool EditContext::SetCompositionFromExistingText(
return true;
}
bool EditContext::InsertText(const WebString& text) {
String update_text(text);
text_ = text_.Substring(0, selection_start_) + update_text +
text_.Substring(selection_end_);
uint32_t update_range_start = selection_start_;
uint32_t update_range_end = selection_end_;
selection_start_ = selection_start_ + text.length();
selection_end_ = selection_start_;
DispatchTextUpdateEvent(update_text, update_range_start, update_range_end,
selection_start_, selection_end_);
return true;
}
bool EditContext::CommitText(const WebString& text,
const WebVector<ui::ImeTextSpan>& ime_text_spans,
const WebRange& replacement_range,
......
......@@ -174,6 +174,9 @@ class CORE_EXPORT EditContext final : public EventTargetWithInlineData,
int composition_end,
const WebVector<ui::ImeTextSpan>& ime_text_spans);
// For English typing.
bool InsertText(const WebString& text);
bool IsVirtualKeyboardPolicyManual() const override;
bool IsEditContextActive() const override;
// Returns whether show()/hide() API is called from virtualkeyboard or not.
......
......@@ -28,6 +28,19 @@ test(function() {
assert_equals(editContext.enterKeyHint, "enter");
}, 'Testing EditContext Dictionary Init');
test(function() {
const editContext = new EditContext();
assert_not_equals(editContext, null);
const test = document.getElementById('test');
test.innerHTML = "";
editContext.addEventListener("textupdate", e => {
test.innerHTML = e.updateText;
});
editContext.focus();
eventSender.keyDown('a');
assert_equals(test.innerHTML, "a");
}, 'Testing EditContext English typing');
test(function() {
const editContext = new EditContext();
assert_not_equals(editContext, null);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment