Commit a1f57882 authored by Anupam Snigdha's avatar Anupam Snigdha Committed by Commit Bot

Fixed SetComposition text updates in EditContext.

There were few bugs related to how the composition and selection
ranges were modified when SetComposition call was executed in
EditContext. This patch fixes those and also added unit tests for it.

Bug: 999184
Change-Id: If354e99b2f9ac0fd455a97a849062ced4c457759
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2023676Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Commit-Queue: Anupam Snigdha <snianu@microsoft.com>
Cr-Commit-Position: refs/heads/master@{#735718}
parent d4a518da
......@@ -380,22 +380,15 @@ bool EditContext::SetComposition(
}
// Update the selection and buffer if the composition range has changed.
String update_text(text);
if (composition_range_start_ != selection_start_ &&
composition_range_end_ != selection_end_) {
selection_start_ = composition_range_end_;
selection_end_ = composition_range_end_;
}
text_ = text_.Substring(0, selection_start_) + update_text +
text_.Substring(selection_end_);
text_ = text_.Substring(0, composition_range_start_) + update_text +
text_.Substring(composition_range_end_);
// Fire textupdate and textformatupdate events to JS.
const uint32_t update_range_start = composition_range_start_;
const uint32_t update_range_end = composition_range_end_;
if (has_composition_) {
// Replace the existing composition with empty string.
composition_range_start_ = selection_start_;
composition_range_end_ = selection_end_;
}
// Update the new selection.
selection_start_ = composition_range_start_ + selection_end;
selection_end_ = composition_range_start_ + selection_end;
DispatchTextUpdateEvent(update_text, update_range_start, update_range_end,
selection_start_, selection_end_);
composition_range_end_ = composition_range_start_ + selection_end;
......
......@@ -174,6 +174,50 @@ test(function() {
assert_equals(compositionEndFired, 1);
}, 'Testing EditContext Composition Event');
test(function() {
const editContext = new EditContext();
assert_not_equals(editContext, null);
const test = document.getElementById('test');
test.innerHTML = "";
// Add EditContext event listeners
editContext.addEventListener("textupdate", e => {
// Update the text in the div
const buffer = test.innerText;
test.innerHTML = buffer.substr(0, e.updateRangeStart) + e.updateText + buffer.substr(e.updateRangeEnd);
});
test.focus();
editContext.focus();
textInputController.setComposition("f");
assert_equals(test.innerHTML, "f");
textInputController.setComposition("foo");
assert_equals(test.innerHTML, "foo");
textInputController.insertText("foobar");
assert_equals(test.innerHTML, "foobar");
}, 'Testing EditContext Text updates');
test(function() {
const editContext = new EditContext();
assert_not_equals(editContext, null);
const test = document.getElementById('test');
test.innerHTML = "";
// Add EditContext event listeners
editContext.addEventListener("textupdate", e => {
// Update the text in the div
const buffer = test.innerText;
test.innerHTML = buffer.substr(0, e.updateRangeStart) + e.updateText + buffer.substr(e.updateRangeEnd);
});
test.focus();
editContext.focus();
textInputController.setComposition("f");
assert_equals(test.innerHTML, "f");
textInputController.setComposition("");
assert_equals(test.innerHTML, "");
textInputController.insertText("foobar");
assert_equals(test.innerHTML, "foobar");
}, 'Testing EditContext Text updates with empty text');
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