Commit 90c2cf68 authored by Gayane Petrosyan's avatar Gayane Petrosyan Committed by Commit Bot

[SH-Blink] Add ranges to text fragment selector

Add range to text fragment selector when selection is too long is spans
across block boundaries.
Gradually add words from beginning and end of the selection until unique
match is found.

Bug: 1102382
Change-Id: I71ce2623dae410aa7f21861bf2a6de6911cfe692
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2412698
Commit-Queue: Gayane Petrosyan <gayane@chromium.org>
Reviewed-by: default avatarYoshifumi Inoue <yosin@chromium.org>
Reviewed-by: default avatarDavid Bokan <bokan@chromium.org>
Reviewed-by: default avatarTommy Martino <tmartino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#808404}
parent a9262353
...@@ -156,6 +156,7 @@ String GetWordsFromStart(String text, int word_num) { ...@@ -156,6 +156,7 @@ String GetWordsFromStart(String text, int word_num) {
constexpr int kExactTextMaxChars = 300; constexpr int kExactTextMaxChars = 300;
constexpr int kNoContextMinChars = 20; constexpr int kNoContextMinChars = 20;
constexpr int kMaxContextWords = 10; constexpr int kMaxContextWords = 10;
constexpr int kMaxRangeWords = 10;
void TextFragmentSelectorGenerator::UpdateSelection( void TextFragmentSelectorGenerator::UpdateSelection(
LocalFrame* selection_frame, LocalFrame* selection_frame,
...@@ -324,8 +325,73 @@ void TextFragmentSelectorGenerator::GenerateExactSelector() { ...@@ -324,8 +325,73 @@ void TextFragmentSelectorGenerator::GenerateExactSelector() {
} }
void TextFragmentSelectorGenerator::ExtendRangeSelector() { void TextFragmentSelectorGenerator::ExtendRangeSelector() {
// TODO(gayane): Generate range selector. DCHECK_EQ(kRange, step_);
state_ = kFailure; DCHECK_EQ(kNeedsNewCandidate, state_);
// Give up if range is already too long.
if (num_range_start_words_ == kMaxRangeWords ||
num_range_end_words_ == kMaxRangeWords) {
step_ = kContext;
return;
}
// Initialize range start and end, if needed.
if (max_available_range_start_.IsEmpty() &&
max_available_range_end_.IsEmpty()) {
EphemeralRangeInFlatTree ephemeral_range(selection_range_);
Node& start_first_block_ancestor =
FindBuffer::GetFirstBlockLevelAncestorInclusive(
*ephemeral_range.StartPosition().ComputeContainerNode());
Node& end_first_block_ancestor =
FindBuffer::GetFirstBlockLevelAncestorInclusive(
*ephemeral_range.EndPosition().ComputeContainerNode());
// If selection starts and ends in the same block, then split selected text
// roughly in the middle.
// TODO(gayane): Should also check that there are no nested blocks.
if (start_first_block_ancestor.isSameNode(&end_first_block_ancestor)) {
String selection_text = PlainText(ephemeral_range);
selection_text.Ensure16Bit();
int selection_length = selection_text.length();
int mid_point =
FindNextWordForward(selection_text.Characters16(), selection_length,
selection_length / 2);
max_available_range_start_ = selection_text.Left(mid_point);
// If from middle till end of selection there is no word break, then we
// cannot use it for range end.
if (mid_point == selection_length) {
state_ = kFailure;
return;
}
max_available_range_end_ =
selection_text.Right(selection_text.length() - mid_point - 1);
} else {
// If not the same node, then we use first and last block of the selection
// range.
max_available_range_start_ =
GetNextTextBlock(selection_range_->StartPosition());
max_available_range_end_ =
GetPreviousTextBlock(selection_range_->EndPosition());
}
}
String start =
GetWordsFromStart(max_available_range_start_, ++num_range_start_words_);
String end =
GetWordsFromEnd(max_available_range_end_, ++num_range_end_words_);
// If the start and end didn't change, it means we exhausted the selected
// text and should try adding context.
if (selector_ && start == selector_->Start() && end == selector_->End()) {
step_ = kContext;
return;
}
selector_ = std::make_unique<TextFragmentSelector>(
TextFragmentSelector::SelectorType::kRange, start, end, "", "");
state_ = kTestCandidate;
} }
void TextFragmentSelectorGenerator::ExtendContext() { void TextFragmentSelectorGenerator::ExtendContext() {
...@@ -347,9 +413,8 @@ void TextFragmentSelectorGenerator::ExtendContext() { ...@@ -347,9 +413,8 @@ void TextFragmentSelectorGenerator::ExtendContext() {
// Try initiating properties necessary for calculating prefix and suffix. // Try initiating properties necessary for calculating prefix and suffix.
if (max_available_prefix_.IsEmpty() && max_available_suffix_.IsEmpty()) { if (max_available_prefix_.IsEmpty() && max_available_suffix_.IsEmpty()) {
max_available_prefix_ = max_available_prefix_ =
GetAvailablePrefixAsText(selection_range_->StartPosition()); GetPreviousTextBlock(selection_range_->StartPosition());
max_available_suffix_ = max_available_suffix_ = GetNextTextBlock(selection_range_->EndPosition());
GetAvailableSuffixAsText(selection_range_->EndPosition());
} }
if (max_available_prefix_.IsEmpty() && max_available_suffix_.IsEmpty()) { if (max_available_prefix_.IsEmpty() && max_available_suffix_.IsEmpty()) {
...@@ -371,7 +436,7 @@ void TextFragmentSelectorGenerator::ExtendContext() { ...@@ -371,7 +436,7 @@ void TextFragmentSelectorGenerator::ExtendContext() {
state_ = kTestCandidate; state_ = kTestCandidate;
} }
String TextFragmentSelectorGenerator::GetAvailablePrefixAsText( String TextFragmentSelectorGenerator::GetPreviousTextBlock(
const Position& prefix_end_position) { const Position& prefix_end_position) {
Node* prefix_end = prefix_end_position.ComputeContainerNode(); Node* prefix_end = prefix_end_position.ComputeContainerNode();
unsigned prefix_end_offset = unsigned prefix_end_offset =
...@@ -399,7 +464,7 @@ String TextFragmentSelectorGenerator::GetAvailablePrefixAsText( ...@@ -399,7 +464,7 @@ String TextFragmentSelectorGenerator::GetAvailablePrefixAsText(
return PlainText(EphemeralRange(range_start, range_end)).StripWhiteSpace(); return PlainText(EphemeralRange(range_start, range_end)).StripWhiteSpace();
} }
String TextFragmentSelectorGenerator::GetAvailableSuffixAsText( String TextFragmentSelectorGenerator::GetNextTextBlock(
const Position& suffix_start_position) { const Position& suffix_start_position) {
Node* suffix_start = suffix_start_position.ComputeContainerNode(); Node* suffix_start = suffix_start_position.ComputeContainerNode();
unsigned suffix_start_offset = unsigned suffix_start_offset =
......
...@@ -54,11 +54,11 @@ class CORE_EXPORT TextFragmentSelectorGenerator final ...@@ -54,11 +54,11 @@ class CORE_EXPORT TextFragmentSelectorGenerator final
void NotifySelectorReady(const TextFragmentSelector& selector); void NotifySelectorReady(const TextFragmentSelector& selector);
// Wrappers for tests. // Wrappers for tests.
String GetAvailablePrefixAsTextForTesting(const Position& position) { String GetPreviousTextBlockForTesting(const Position& position) {
return GetAvailablePrefixAsText(position); return GetPreviousTextBlock(position);
} }
String GetAvailableSuffixAsTextForTesting(const Position& position) { String GetNextTextBlockForTesting(const Position& position) {
return GetAvailableSuffixAsText(position); return GetNextTextBlock(position);
} }
// Releases members if necessary. // Releases members if necessary.
...@@ -94,11 +94,11 @@ class CORE_EXPORT TextFragmentSelectorGenerator final ...@@ -94,11 +94,11 @@ class CORE_EXPORT TextFragmentSelectorGenerator final
// Returns max text preceding given position that doesn't cross block // Returns max text preceding given position that doesn't cross block
// boundaries. // boundaries.
String GetAvailablePrefixAsText(const Position& position); String GetPreviousTextBlock(const Position& position);
// Returns max text following given position that doesn't cross block // Returns max text following given position that doesn't cross block
// boundaries. // boundaries.
String GetAvailableSuffixAsText(const Position& position); String GetNextTextBlock(const Position& position);
void GenerateExactSelector(); void GenerateExactSelector();
void ExtendRangeSelector(); void ExtendRangeSelector();
...@@ -124,11 +124,17 @@ class CORE_EXPORT TextFragmentSelectorGenerator final ...@@ -124,11 +124,17 @@ class CORE_EXPORT TextFragmentSelectorGenerator final
String max_available_prefix_; String max_available_prefix_;
String max_available_suffix_; String max_available_suffix_;
String max_available_range_start_;
String max_available_range_end_;
// Indicates a number of words used from |max_available_prefix_| and // Indicates a number of words used from |max_available_prefix_| and
// |max_available_suffix_| for the current |selector_|. // |max_available_suffix_| for the current |selector_|.
int num_prefix_words_ = 0; int num_prefix_words_ = 0;
int num_suffix_words_ = 0; int num_suffix_words_ = 0;
int num_range_start_words_ = 0;
int num_range_end_words_ = 0;
DISALLOW_COPY_AND_ASSIGN(TextFragmentSelectorGenerator); DISALLOW_COPY_AND_ASSIGN(TextFragmentSelectorGenerator);
}; };
......
...@@ -426,8 +426,8 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -426,8 +426,8 @@ TEST_F(TextFragmentSelectorGeneratorTest,
GenerateAndVerifySelector(selected_start, selected_end, GenerateAndVerifySelector(selected_start, selected_end,
"with-,not%20unique%20snippet,-text"); "with-,not%20unique%20snippet,-text");
} }
// Multi-block selection is currently not implemented.
TEST_F(TextFragmentSelectorGeneratorTest, MultiblockSelection) { TEST_F(TextFragmentSelectorGeneratorTest, RangeSelector) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -443,11 +443,112 @@ TEST_F(TextFragmentSelectorGeneratorTest, MultiblockSelection) { ...@@ -443,11 +443,112 @@ TEST_F(TextFragmentSelectorGeneratorTest, MultiblockSelection) {
ASSERT_EQ("First paragraph text that is longer than 20 chars\n\nSecond", ASSERT_EQ("First paragraph text that is longer than 20 chars\n\nSecond",
PlainText(EphemeralRange(selected_start, selected_end))); PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end, "First,Second");
}
// It should be more than 300 characters selected from the same node so that
// ranges are used.
TEST_F(TextFragmentSelectorGeneratorTest, RangeSelector_SameNode) {
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 text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text text text text text
text text text text text text text text text and last text</p>
)HTML");
Node* first_paragraph = GetDocument().getElementById("first")->firstChild();
const auto& selected_start = Position(first_paragraph, 0);
const auto& selected_end = Position(first_paragraph, 320);
ASSERT_EQ(
"First paragraph text text text text text text text \
text text text text text text text text text text text text text \
text text text text text text text text text text text text text \
text text text text text text text text text text text text text \
text text text text text text text text text and last text",
PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end,
"First%20paragraph,last%20text");
}
// When using all the selected text for the range is not enough for unique
// match, context should be added.
TEST_F(TextFragmentSelectorGeneratorTest, RangeSelector_RangeNotUnique) {
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</p><p id='text1'>text</p>
<p id='second'>Second paragraph</p><p id='text2'>text</p>
)HTML");
Node* first_paragraph = GetDocument().getElementById("first")->firstChild();
Node* first_text = GetDocument().getElementById("text1")->firstChild();
const auto& selected_start = Position(first_paragraph, 6);
const auto& selected_end = Position(first_text, 4);
ASSERT_EQ("paragraph\n\ntext",
PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end,
"First-,paragraph,text,-Second");
}
// When using all the selected text for the range is not enough for unique
// match, context should be added, but only prefxi and no suffix is available.
TEST_F(TextFragmentSelectorGeneratorTest,
RangeSelector_RangeNotUnique_NoSuffix) {
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</p><p id='text1'>text</p>
<p id='second'>Second paragraph</p><p id='text2'>text</p>
)HTML");
Node* second_paragraph = GetDocument().getElementById("second")->firstChild();
Node* second_text = GetDocument().getElementById("text2")->firstChild();
const auto& selected_start = Position(second_paragraph, 7);
const auto& selected_end = Position(second_text, 4);
ASSERT_EQ("paragraph\n\ntext",
PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end,
"Second-,paragraph,text");
}
// When no range end is available it should return empty selector.
// There is no range end available because there is no word break in the second
// half of the selection.
TEST_F(TextFragmentSelectorGeneratorTest, RangeSelector_NoRangeEnd) {
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 text text text text text text
text text text text text text text text text text text text text
text text text text text text text text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_text_and_last_text</p>
)HTML");
Node* first_paragraph = GetDocument().getElementById("first")->firstChild();
const auto& selected_start = Position(first_paragraph, 0);
const auto& selected_end = Position(first_paragraph, 312);
ASSERT_EQ(
"First paragraph text text text text text text text \
text text text text text text text text text text text text text \
text text text text text text text text_text_text_text_text_text_\
text_text_text_text_text_text_text_text_text_text_text_text_text_\
text_text_text_text_text_text_text_text_text_and_last_text",
PlainText(EphemeralRange(selected_start, selected_end)));
GenerateAndVerifySelector(selected_start, selected_end, ""); GenerateAndVerifySelector(selected_start, selected_end, "");
} }
// Basic test case for |GetAvailableSuffixAsText|. // Basic test case for |GetNextTextBlock|.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText) { TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -462,11 +563,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText) { ...@@ -462,11 +563,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText) {
EXPECT_EQ("First paragraph", GetDocument() EXPECT_EQ("First paragraph", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix contains collapsible space. // Check the case when available prefix contains collapsible space.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ExtraSpace) { TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_ExtraSpace) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -483,12 +584,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ExtraSpace) { ...@@ -483,12 +584,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ExtraSpace) {
EXPECT_EQ("First paragraph", GetDocument() EXPECT_EQ("First paragraph", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix complete text content of the previous // Check the case when available prefix complete text content of the previous
// block. // block.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_PrevNode) { TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_PrevNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -505,13 +606,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_PrevNode) { ...@@ -505,13 +606,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_PrevNode) {
GetDocument() GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when there is a commented block between selection and the // Check the case when there is a commented block between selection and the
// available prefix. // available prefix.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailablePrefixAsText_PrevNode_WithComment) { GetPreviousTextBlock_PrevNode_WithComment) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -531,13 +632,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -531,13 +632,12 @@ TEST_F(TextFragmentSelectorGeneratorTest,
GetDocument() GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix is a text node outside of selection // Check the case when available prefix is a text node outside of selection
// block. // block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_PrevTextNode) {
GetAvailablePrefixAsText_PrevTextNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -553,12 +653,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -553,12 +653,12 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("text", GetDocument() EXPECT_EQ("text", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix is a parent node text content outside of // Check the case when available prefix is a parent node text content outside of
// selection block. // selection block.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ParentNode) { TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_ParentNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -574,12 +674,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ParentNode) { ...@@ -574,12 +674,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailablePrefixAsText_ParentNode) {
EXPECT_EQ("nested", GetDocument() EXPECT_EQ("nested", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix contains non-block tag(e.g. <b>). // Check the case when available prefix contains non-block tag(e.g. <b>).
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_NestedTextNode) {
GetAvailablePrefixAsText_NestedTextNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -595,12 +694,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -595,12 +694,11 @@ TEST_F(TextFragmentSelectorGeneratorTest,
GetDocument() GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix is collected until nested block. // Check the case when available prefix is collected until nested block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetPreviousTextBlock_NestedBlock) {
GetAvailablePrefixAsText_NestedBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -615,13 +713,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -615,13 +713,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("paragraph", GetDocument() EXPECT_EQ("paragraph", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix includes non-block element but stops at // Check the case when available prefix includes non-block element but stops at
// nested block. // nested block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailablePrefixAsText_NestedBlockInNestedText) { GetPreviousTextBlock_NestedBlockInNestedText) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -636,12 +734,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -636,12 +734,12 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("bold paragraph", GetDocument() EXPECT_EQ("bold paragraph", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when available prefix includes invisible block. // Check the case when available prefix includes invisible block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailablePrefixAsText_NestedInvisibleBlock) { GetPreviousTextBlock_NestedInvisibleBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -656,13 +754,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -656,13 +754,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("First", GetDocument() EXPECT_EQ("First", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when previous node is used for available prefix when selection // Check the case when previous node is used for available prefix when selection
// is not at index=0 but there is only space before it. // is not at index=0 but there is only space before it.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailablePrefixAsText_SpacesBeforeSelection) { GetPreviousTextBlock_SpacesBeforeSelection) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -681,13 +779,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -681,13 +779,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
GetDocument() GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Check the case when previous node is used for available prefix when selection // Check the case when previous node is used for available prefix when selection
// is not at index=0 but there is only invisible block. // is not at index=0 but there is only invisible block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailablePrefixAsText_InvisibleBeforeSelection) { GetPreviousTextBlock_InvisibleBeforeSelection) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -710,13 +808,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -710,13 +808,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
GetDocument() GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailablePrefixAsTextForTesting(start)); ->GetPreviousTextBlockForTesting(start));
} }
// Similar test for suffix. // Similar test for suffix.
// Basic test case for |GetAvailableSuffixAsText|. // Basic test case for |GetNextTextBlock|.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText) { TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -731,11 +829,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText) { ...@@ -731,11 +829,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText) {
EXPECT_EQ("paragraph text", GetDocument() EXPECT_EQ("paragraph text", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix contains collapsible space. // Check the case when available suffix contains collapsible space.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ExtraSpace) { TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_ExtraSpace) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -753,12 +851,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ExtraSpace) { ...@@ -753,12 +851,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ExtraSpace) {
EXPECT_EQ("paragraph text", GetDocument() EXPECT_EQ("paragraph text", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix is complete text content of the next // Check the case when available suffix is complete text content of the next
// block. // block.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_NextNode) { TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_NextNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -771,17 +869,16 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_NextNode) { ...@@ -771,17 +869,16 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_NextNode) {
const auto& end = Position(first_paragraph, 20); const auto& end = Position(first_paragraph, 20);
ASSERT_EQ("First paragraph text", PlainText(EphemeralRange(start, end))); ASSERT_EQ("First paragraph text", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("Second paragraph text", EXPECT_EQ("Second paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
// Check the case when there is a commented block between selection and the // Check the case when there is a commented block between selection and the
// available suffix. // available suffix.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_NextNode_WithComment) { GetNextTextBlock_NextNode_WithComment) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -797,17 +894,15 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -797,17 +894,15 @@ TEST_F(TextFragmentSelectorGeneratorTest,
const auto& end = Position(first_paragraph, 20); const auto& end = Position(first_paragraph, 20);
ASSERT_EQ("First paragraph text", PlainText(EphemeralRange(start, end))); ASSERT_EQ("First paragraph text", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("Second paragraph text", EXPECT_EQ("Second paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
// Check the case when available suffix is a text node outside of selection // Check the case when available suffix is a text node outside of selection
// block. // block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_NextTextNode) {
GetAvailableSuffixAsText_NextTextNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -823,12 +918,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -823,12 +918,12 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("text", GetDocument() EXPECT_EQ("text", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix is a parent node text content outside of // Check the case when available suffix is a parent node text content outside of
// selection block. // selection block.
TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ParentNode) { TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_ParentNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -843,12 +938,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ParentNode) { ...@@ -843,12 +938,11 @@ TEST_F(TextFragmentSelectorGeneratorTest, GetAvailableSuffixAsText_ParentNode) {
EXPECT_EQ("nested", GetDocument() EXPECT_EQ("nested", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix contains non-block tag(e.g. <b>). // Check the case when available suffix contains non-block tag(e.g. <b>).
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_NestedTextNode) {
GetAvailableSuffixAsText_NestedTextNode) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -860,16 +954,14 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -860,16 +954,14 @@ TEST_F(TextFragmentSelectorGeneratorTest,
const auto& end = Position(first_paragraph, 5); const auto& end = Position(first_paragraph, 5);
ASSERT_EQ("First", PlainText(EphemeralRange(start, end))); ASSERT_EQ("First", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("bold text paragraph text", EXPECT_EQ("bold text paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
// Check the case when available suffix is collected until nested block. // Check the case when available suffix is collected until nested block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest, GetNextTextBlock_NestedBlock) {
GetAvailableSuffixAsText_NestedBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -884,13 +976,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -884,13 +976,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("paragraph", GetDocument() EXPECT_EQ("paragraph", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix includes non-block element but stops at // Check the case when available suffix includes non-block element but stops at
// nested block. // nested block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_NestedBlockInNestedText) { GetNextTextBlock_NestedBlockInNestedText) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -905,12 +997,12 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -905,12 +997,12 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("bold", GetDocument() EXPECT_EQ("bold", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when available suffix includes invisible block. // Check the case when available suffix includes invisible block.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_NestedInvisibleBlock) { GetNextTextBlock_NestedInvisibleBlock) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -925,13 +1017,13 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -925,13 +1017,13 @@ TEST_F(TextFragmentSelectorGeneratorTest,
EXPECT_EQ("paragraph text", GetDocument() EXPECT_EQ("paragraph text", GetDocument()
.GetFrame() .GetFrame()
->GetTextFragmentSelectorGenerator() ->GetTextFragmentSelectorGenerator()
->GetAvailableSuffixAsTextForTesting(end)); ->GetNextTextBlockForTesting(end));
} }
// Check the case when next node is used for available suffix when selection is // Check the case when next node is used for available suffix when selection is
// not at last index but there is only space after it. // not at last index but there is only space after it.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_SpacesAfterSelection) { GetNextTextBlock_SpacesAfterSelection) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -948,17 +1040,16 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -948,17 +1040,16 @@ TEST_F(TextFragmentSelectorGeneratorTest,
const auto& end = Position(first_paragraph, 27); const auto& end = Position(first_paragraph, 27);
ASSERT_EQ("text", PlainText(EphemeralRange(start, end))); ASSERT_EQ("text", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("Second paragraph text", EXPECT_EQ("Second paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
// Check the case when next node is used for available suffix when selection is // Check the case when next node is used for available suffix when selection is
// not at last index but there is only invisible block after it. // not at last index but there is only invisible block after it.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_InvisibleAfterSelection) { GetNextTextBlock_InvisibleAfterSelection) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -978,18 +1069,17 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -978,18 +1069,17 @@ TEST_F(TextFragmentSelectorGeneratorTest,
const auto& end = Position(first_paragraph, 27); const auto& end = Position(first_paragraph, 27);
ASSERT_EQ("text", PlainText(EphemeralRange(start, end))); ASSERT_EQ("text", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("Second paragraph text", EXPECT_EQ("Second paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
// Check the case when previous node is used for available prefix when selection // Check the case when previous node is used for available prefix when selection
// is not at last index but there is only invisible block. Invisible block // is not at last index but there is only invisible block. Invisible block
// contains another block which also should be invisible. // contains another block which also should be invisible.
TEST_F(TextFragmentSelectorGeneratorTest, TEST_F(TextFragmentSelectorGeneratorTest,
GetAvailableSuffixAsText_InvisibleAfterSelection_WithNestedInvisible) { GetNextTextBlock_InvisibleAfterSelection_WithNestedInvisible) {
SimRequest request("https://example.com/test.html", "text/html"); SimRequest request("https://example.com/test.html", "text/html");
LoadURL("https://example.com/test.html"); LoadURL("https://example.com/test.html");
request.Complete(R"HTML( request.Complete(R"HTML(
...@@ -1019,11 +1109,10 @@ TEST_F(TextFragmentSelectorGeneratorTest, ...@@ -1019,11 +1109,10 @@ TEST_F(TextFragmentSelectorGeneratorTest,
const auto& end = Position(first_paragraph, 27); const auto& end = Position(first_paragraph, 27);
ASSERT_EQ("text", PlainText(EphemeralRange(start, end))); ASSERT_EQ("text", PlainText(EphemeralRange(start, end)));
EXPECT_EQ("Second paragraph text", EXPECT_EQ("Second paragraph text", GetDocument()
GetDocument() .GetFrame()
.GetFrame() ->GetTextFragmentSelectorGenerator()
->GetTextFragmentSelectorGenerator() ->GetNextTextBlockForTesting(end));
->GetAvailableSuffixAsTextForTesting(end));
} }
} // namespace blink } // namespace blink
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