summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/page/scrolling/text_fragment_finder.cc
blob: 864c63fcffcc4752360e319677fe6c968787e950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/page/scrolling/text_fragment_finder.h"

#include <memory>

#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/range.h"
#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/finder/find_buffer.h"
#include "third_party/blink/renderer/core/editing/iterators/character_iterator.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/page/scrolling/text_fragment_selector.h"

namespace blink {

namespace {

const char kNoContext[] = "";

EphemeralRangeInFlatTree FindMatchInRange(String search_text,
                                          PositionInFlatTree search_start,
                                          PositionInFlatTree search_end) {
  const FindOptions find_options = kCaseInsensitive;
  const EphemeralRangeInFlatTree search_range(search_start, search_end);
  return FindBuffer::FindMatchInRange(search_range, search_text, find_options);
}

PositionInFlatTree NextTextPosition(PositionInFlatTree position,
                                    PositionInFlatTree end_position) {
  const TextIteratorBehavior options =
      TextIteratorBehavior::Builder().SetEmitsSpaceForNbsp(true).Build();
  CharacterIteratorInFlatTree char_it(position, end_position, options);

  for (; char_it.length(); char_it.Advance(1)) {
    if (!IsSpaceOrNewline(char_it.CharacterAt(0)))
      return char_it.StartPosition();
  }

  return end_position;
}

// Find and return the range of |search_text| if the first text in the search
// range is |search_text|, skipping over whitespace and element boundaries.
EphemeralRangeInFlatTree FindImmediateMatch(String search_text,
                                            PositionInFlatTree search_start,
                                            PositionInFlatTree search_end) {
  if (search_text.IsEmpty())
    return EphemeralRangeInFlatTree();

  search_start = NextTextPosition(search_start, search_end);
  if (search_start == search_end)
    return EphemeralRangeInFlatTree();

  FindBuffer buffer(EphemeralRangeInFlatTree(search_start, search_end));
  const FindOptions find_options = kCaseInsensitive;

  std::unique_ptr<FindBuffer::Results> match_results =
      buffer.FindMatches(search_text, find_options);

  if (!match_results->IsEmpty() && match_results->front().start == 0u) {
    FindBuffer::BufferMatchResult match = match_results->front();
    return buffer.RangeFromBufferIndex(match.start, match.start + match.length);
  }

  return EphemeralRangeInFlatTree();
}

EphemeralRangeInFlatTree FindMatchInRangeWithContext(
    const String& search_text,
    const String& prefix,
    const String& suffix,
    PositionInFlatTree search_start,
    PositionInFlatTree search_end) {
  while (search_start != search_end) {
    EphemeralRangeInFlatTree potential_match;

    if (!prefix.IsEmpty()) {
      EphemeralRangeInFlatTree prefix_match =
          FindMatchInRange(prefix, search_start, search_end);

      // No prefix_match in remaining range
      if (prefix_match.IsNull())
        return EphemeralRangeInFlatTree();

      search_start = prefix_match.EndPosition();
      potential_match =
          FindImmediateMatch(search_text, search_start, search_end);

      // No search_text match after current prefix_match
      if (potential_match.IsNull())
        continue;
    } else {
      potential_match = FindMatchInRange(search_text, search_start, search_end);

      // No search_text match in remaining range
      if (potential_match.IsNull())
        return EphemeralRangeInFlatTree();
    }

    DCHECK(potential_match.IsNotNull());
    search_start = potential_match.EndPosition();
    if (!suffix.IsEmpty()) {
      EphemeralRangeInFlatTree suffix_match =
          FindImmediateMatch(suffix, search_start, search_end);

      // No suffix match after current potential_match
      if (suffix_match.IsNull())
        continue;
    }

    // If we reach here without a return or continue, we have a full match.
    return potential_match;
  }

  return EphemeralRangeInFlatTree();
}

}  // namespace

TextFragmentFinder::TextFragmentFinder(Client& client,
                                       const TextFragmentSelector& selector)
    : client_(client), selector_(selector) {
  DCHECK(!selector_.Start().IsEmpty());
}

void TextFragmentFinder::FindMatch(Document& document) {
  PositionInFlatTree search_start =
      PositionInFlatTree::FirstPositionInNode(document);

  EphemeralRangeInFlatTree match =
      FindMatchFromPosition(document, search_start);

  if (match.IsNotNull()) {
    client_.DidFindMatch(match);

    // Continue searching to see if we have an ambiguous selector.
    // TODO(crbug.com/919204): This is temporary and only for measuring
    // ambiguous matching during prototyping.
    EphemeralRangeInFlatTree ambiguous_match =
        FindMatchFromPosition(document, match.EndPosition());
    if (ambiguous_match.IsNotNull())
      client_.DidFindAmbiguousMatch();
  }
}

EphemeralRangeInFlatTree TextFragmentFinder::FindMatchFromPosition(
    Document& document,
    PositionInFlatTree search_start) {
  PositionInFlatTree search_end;
  if (document.documentElement() && document.documentElement()->lastChild()) {
    search_end =
        PositionInFlatTree::AfterNode(*document.documentElement()->lastChild());
  } else {
    search_end = PositionInFlatTree::LastPositionInNode(document);
  }

  // TODO(crbug.com/930156): Make FindMatch work asynchronously.
  EphemeralRangeInFlatTree match;
  if (selector_.Type() == TextFragmentSelector::kExact) {
    match = FindMatchInRangeWithContext(selector_.Start(), selector_.Prefix(),
                                        selector_.Suffix(), search_start,
                                        search_end);
  } else {
    EphemeralRangeInFlatTree start_match =
        FindMatchInRangeWithContext(selector_.Start(), selector_.Prefix(),
                                    kNoContext, search_start, search_end);
    if (start_match.IsNull())
      return start_match;

    // TODO(crbug.com/924964): Determine what we should do if the start text and
    // end text are the same (and there are no context terms). This
    // implementation continues searching for the next instance of the text,
    // from the end of the first instance.
    search_start = start_match.EndPosition();
    EphemeralRangeInFlatTree end_match = FindMatchInRangeWithContext(
        selector_.End(), kNoContext, selector_.Suffix(), search_start,
        search_end);
    if (end_match.IsNotNull()) {
      match = EphemeralRangeInFlatTree(start_match.StartPosition(),
                                       end_match.EndPosition());
    }
  }

  return match;
}

}  // namespace blink