Commit 21d97eba authored by spqchan's avatar spqchan Committed by Commit Bot

[Mac] Fix Text Suggestions Touch Bar Out of Range Issue

It's possible for the TextSuggestionsTouchBarController to receive
a selection range that is out of the text bounds. This CL fixes the
issue by replacing the range with an empty one if it is out of bounds.

This CL also makes sure that gfx::Range::ToNSRange() gets used, since
gfx::Range() can potentially be reversed.

Bug: 880642, 872929
Change-Id: I4acc63b80cdf107de2ba340bb277e3e38084ff52
Reviewed-on: https://chromium-review.googlesource.com/1207575
Commit-Queue: Sarah Chan <spqchan@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#589247}
parent a26c1127
......@@ -173,19 +173,27 @@ class WebContentsTextObserver : public content::WebContentsObserver {
return;
}
if (!range.IsValid()) {
[self updateTextSelection:base::string16() range:gfx::Range() offset:0];
// TODO(crbug.com/880642): It's possible for a range out of the text bounds
// to be passed in. Investigate this.
if (range.start() - offset > text.length() ||
range.end() - offset > text.length()) {
text_.reset([[NSString alloc] init]);
selectionRange_ = NSMakeRange(0, 0);
editingWordRange_ = NSMakeRange(0, 0);
offsetEditingWordRange_ = NSMakeRange(0, 0);
return;
}
text_.reset([base::SysUTF16ToNSString(text) retain]);
selectionRange_ =
NSMakeRange(range.start() - offset, range.end() - range.start());
selectionRange_ = range.ToNSRange();
selectionRange_.location -= offset;
editingWordRange_ =
[self editingWordRangeFromText:text
cursorPosition:selectionRange_.location];
offsetEditingWordRange_ = NSMakeRange(editingWordRange_.location + offset,
editingWordRange_.length);
offsetEditingWordRange_ = editingWordRange_;
offsetEditingWordRange_.location += offset;
[self requestSuggestions];
}
}
......
......@@ -191,15 +191,24 @@ IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest,
}
}
// Tests that an invalid range does not crash the controller.
IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest, InvalidRange) {
// Tests that a range outside of the text bounds will set the selection range
// to empty.
IN_PROC_BROWSER_TEST_F(TextSuggestionsTouchBarControllerTest,
RangeOutOfTextBounds) {
FocusTextfield();
[touch_bar_controller_ setText:@""];
[touch_bar_controller_ setSelectionRange:gfx::Range()];
[touch_bar_controller_
updateTextSelection:base::string16(base::ASCIIToUTF16("text"))
range:gfx::Range::InvalidRange()
range:gfx::Range(4, 5)
offset:0];
EXPECT_STREQ("", [touch_bar_controller_ text].UTF8String);
EXPECT_EQ(gfx::Range(), [touch_bar_controller_ selectionRange]);
[touch_bar_controller_
updateTextSelection:base::string16(base::ASCIIToUTF16("text"))
range:gfx::Range(2, 5)
offset:0];
EXPECT_STREQ("", [touch_bar_controller_ text].UTF8String);
EXPECT_EQ(gfx::Range(), [touch_bar_controller_ selectionRange]);
......
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