Commit 2d57f47b authored by Gayane Petrosyan's avatar Gayane Petrosyan Committed by Commit Bot

[SH-Blink] Don't include next word when selection ends at the word

beginning.

Bug: 1132486
Change-Id: I7412855b1fc64613452d03517a5220850ffa7669
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2438881Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarDavid Bokan <bokan@chromium.org>
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812745}
parent 46cf2621
...@@ -195,20 +195,31 @@ void TextFragmentSelectorGenerator::CompleteSelection() { ...@@ -195,20 +195,31 @@ void TextFragmentSelectorGenerator::CompleteSelection() {
int selection_start_pos = int selection_start_pos =
ephemeral_range.StartPosition().ComputeOffsetInContainerNode(); ephemeral_range.StartPosition().ComputeOffsetInContainerNode();
start_text.Ensure16Bit(); start_text.Ensure16Bit();
int word_start = FindWordStartBoundary( int first_word_start = FindWordStartBoundary(
start_text.Characters16(), start_text.length(), selection_start_pos); start_text.Characters16(), start_text.length(), selection_start_pos);
String end_text = end_container->textContent(); String end_text = end_container->textContent();
int selection_end_pos = int selection_end_pos =
ephemeral_range.EndPosition().ComputeOffsetInContainerNode(); ephemeral_range.EndPosition().ComputeOffsetInContainerNode();
end_text.Ensure16Bit(); end_text.Ensure16Bit();
int word_end = FindWordEndBoundary(end_text.Characters16(), end_text.length(),
selection_end_pos); // If |selection_end_pos| is at the beginning of a new word then don't search
if (word_start != selection_start_pos || word_end != selection_end_pos) { // for the word end as it will be the end of the next word, which was not
// included in the selection.
int last_word_end = selection_end_pos;
if (selection_end_pos != FindWordStartBoundary(end_text.Characters16(),
end_text.length(),
selection_end_pos)) {
last_word_end = FindWordEndBoundary(end_text.Characters16(),
end_text.length(), selection_end_pos);
}
if (first_word_start != selection_start_pos ||
last_word_end != selection_end_pos) {
selection_range_ = selection_range_ =
MakeGarbageCollected<Range>(selection_range_->OwnerDocument(), MakeGarbageCollected<Range>(selection_range_->OwnerDocument(),
Position(start_container, word_start), Position(start_container, first_word_start),
Position(end_container, word_end)); Position(end_container, last_word_end));
} }
} }
......
...@@ -588,6 +588,27 @@ TEST_F(TextFragmentSelectorGeneratorTest, WordLimit_ExtraSpaces) { ...@@ -588,6 +588,27 @@ TEST_F(TextFragmentSelectorGeneratorTest, WordLimit_ExtraSpaces) {
"paragraph%20text%20that%20is%20longer"); "paragraph%20text%20that%20is%20longer");
} }
// When selection starts at the end of a word, selection shouldn't be
// autocompleted to contain extra words.
TEST_F(TextFragmentSelectorGeneratorTest,
WordLimit_SelectionStartsAndEndsAtWordLimit) {
SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html");
request.Complete(R"HTML(
<!DOCTYPE html>
<div>Test page</div>
<p id='first'>First paragraph text that is longer than 20 chars</p>
)HTML");
Node* first_paragraph = GetDocument().getElementById("first")->firstChild();
const auto& selected_start = Position(first_paragraph, 5);
const auto& selected_end = Position(first_paragraph, 37);
ASSERT_EQ(" paragraph text that is longer ",
PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end,
"paragraph%20text%20that%20is%20longer");
}
// Basic test case for |GetNextTextBlock|. // Basic test case for |GetNextTextBlock|.
TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock) { TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
......
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