Index: content/renderer/render_widget.cc |
diff --git a/content/renderer/render_widget.cc b/content/renderer/render_widget.cc |
index ee171f595d950efcb2d85b3a3de5361b38a6517a..187edac700de34a8a7379c5d3125d7a8e429f802 100644 |
--- a/content/renderer/render_widget.cc |
+++ b/content/renderer/render_widget.cc |
@@ -1508,6 +1508,32 @@ WebRect RenderWidget::windowResizerRect() { |
return resizer_rect_; |
} |
+std::pair<int, int> RenderWidget::adjustSelectionForBoundary( |
+ int selectionStart, |
+ int selectionEnd, |
+ const int textLength) const { |
+ int compositionLength = |
+ text_input_info_.compositionEnd - text_input_info_.compositionStart; |
+ int beforeCompositionLength; |
+ if (compositionLength != 0) |
+ beforeCompositionLength = text_input_info_.compositionStart; |
+ else |
+ beforeCompositionLength = text_input_info_.selectionStart; |
+ int afterCompositionLength = |
+ static_cast<int>(text_input_info_.value.length()) - compositionLength - |
+ beforeCompositionLength; |
+ |
+ // In case of exceeding the left boundary. |
+ selectionStart = std::max(selectionStart, -beforeCompositionLength); |
+ selectionEnd = std::max(selectionEnd, selectionStart); |
+ |
+ // In case of exceeding the right boundary. |
+ selectionEnd = std::min(selectionEnd, textLength + afterCompositionLength); |
+ selectionStart = std::min(selectionStart, selectionEnd); |
+ |
+ return std::make_pair(selectionStart, selectionEnd); |
+} |
+ |
void RenderWidget::OnImeSetComposition( |
const base::string16& text, |
const std::vector<WebCompositionUnderline>& underlines, |
@@ -1516,9 +1542,13 @@ void RenderWidget::OnImeSetComposition( |
if (!ShouldHandleImeEvent()) |
return; |
ImeEventGuard guard(this); |
+ |
+ std::pair<int, int> selectionPair = |
+ adjustSelectionForBoundary(selection_start, selection_end, text.length()); |
+ |
if (!webwidget_->setComposition( |
text, WebVector<WebCompositionUnderline>(underlines), |
- selection_start, selection_end)) { |
+ selectionPair.first, selectionPair.second)) { |
// If we failed to set the composition text, then we need to let the browser |
// process to cancel the input method's ongoing composition session, to make |
// sure we are in a consistent state. |