summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/iterators/simplified_backwards_text_iterator_test.cc
blob: cf442f1828ef40a52cd12630279b3d031f25dfe5 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2015 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/editing/iterators/simplified_backwards_text_iterator.h"

#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/selection_template.h"
#include "third_party/blink/renderer/core/editing/testing/editing_test_base.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {
namespace simplified_backwards_text_iterator_test {

TextIteratorBehavior EmitsSmallXForTextSecurityBehavior() {
  return TextIteratorBehavior::Builder()
      .SetEmitsSmallXForTextSecurity(true)
      .Build();
}

class SimplifiedBackwardsTextIteratorTest : public EditingTestBase {
 protected:
  std::string ExtractStringInRange(
      const std::string selection_text,
      const TextIteratorBehavior& behavior = TextIteratorBehavior()) {
    const SelectionInDOMTree selection = SetSelectionTextToBody(selection_text);
    StringBuilder builder;
    bool is_first = true;
    for (SimplifiedBackwardsTextIterator iterator(selection.ComputeRange(),
                                                  behavior);
         !iterator.AtEnd(); iterator.Advance()) {
      if (!is_first)
        builder.Append(", ", 2);
      is_first = false;
      builder.Append(iterator.GetTextState().GetTextForTesting());
    }
    return builder.ToString().Utf8();
  }
};

template <typename Strategy>
static String ExtractString(const Element& element) {
  const EphemeralRangeTemplate<Strategy> range =
      EphemeralRangeTemplate<Strategy>::RangeOfContents(element);
  String result;
  for (SimplifiedBackwardsTextIteratorAlgorithm<Strategy> it(range);
       !it.AtEnd(); it.Advance()) {
    result = it.GetTextState().GetTextForTesting() + result;
  }
  return result;
}

TEST_F(SimplifiedBackwardsTextIteratorTest, IterateWithFirstLetterPart) {
  InsertStyleElement("p::first-letter {font-size: 200%}");
  // TODO(editing-dev): |SimplifiedBackwardsTextIterator| should not account
  // collapsed whitespace (http://crbug.com/760428)

  // Simulate PreviousBoundary()
  EXPECT_EQ(" , \n", ExtractStringInRange("^<p> |[(3)]678</p>"));
  EXPECT_EQ(" [, \n", ExtractStringInRange("^<p> [|(3)]678</p>"));
  EXPECT_EQ(" [(, \n", ExtractStringInRange("^<p> [(|3)]678</p>"));
  EXPECT_EQ(" [(3, \n", ExtractStringInRange("^<p> [(3|)]678</p>"));
  EXPECT_EQ(" [(3), \n", ExtractStringInRange("^<p> [(3)|]678</p>"));
  EXPECT_EQ(" [(3)], \n", ExtractStringInRange("^<p> [(3)]|678</p>"));

  EXPECT_EQ("6,  [(3)], \n, ab", ExtractStringInRange("^ab<p> [(3)]6|78</p>"))
      << "From remaining part to outside";

  EXPECT_EQ("(3)", ExtractStringInRange("<p> [^(3)|]678</p>"))
      << "Iterate in first-letter part";

  EXPECT_EQ("67, (3)]", ExtractStringInRange("<p> [^(3)]67|8</p>"))
      << "From remaining part to first-letter part";

  EXPECT_EQ("789", ExtractStringInRange("<p> [(3)]6^789|a</p>"))
      << "Iterate in remaining part";

  EXPECT_EQ("9, \n, 78", ExtractStringInRange("<p> [(3)]6^78</p>9|a"))
      << "Enter into remaining part and stop in remaining part";

  EXPECT_EQ("9, \n, 678, (3)]", ExtractStringInRange("<p> [^(3)]678</p>9|a"))
      << "Enter into remaining part and stop in first-letter part";
}

TEST_F(SimplifiedBackwardsTextIteratorTest, Basic) {
  SetBodyContent("<p> [(3)]678</p>");
  const Element* const sample = GetDocument().QuerySelector("p");
  SimplifiedBackwardsTextIterator iterator(EphemeralRange(
      Position(sample->firstChild(), 0), Position(sample->firstChild(), 9)));
  // TODO(editing-dev): |SimplifiedBackwardsTextIterator| should not account
  // collapsed whitespace (http://crbug.com/760428)
  EXPECT_EQ(9, iterator.length())
      << "We should have 8 as ignoring collapsed whitespace.";
  EXPECT_EQ(Position(sample->firstChild(), 0), iterator.StartPosition());
  EXPECT_EQ(Position(sample->firstChild(), 9), iterator.EndPosition());
  EXPECT_EQ(sample->firstChild(), iterator.StartContainer());
  EXPECT_EQ(9, iterator.EndOffset());
  EXPECT_EQ(sample->firstChild(), iterator.GetNode());
  EXPECT_EQ('8', iterator.CharacterAt(0));
  EXPECT_EQ('7', iterator.CharacterAt(1));
  EXPECT_EQ('6', iterator.CharacterAt(2));
  EXPECT_EQ(']', iterator.CharacterAt(3));
  EXPECT_EQ(')', iterator.CharacterAt(4));
  EXPECT_EQ('3', iterator.CharacterAt(5));
  EXPECT_EQ('(', iterator.CharacterAt(6));
  EXPECT_EQ('[', iterator.CharacterAt(7));
  EXPECT_EQ(' ', iterator.CharacterAt(8));

  EXPECT_FALSE(iterator.AtEnd());
  iterator.Advance();
  EXPECT_TRUE(iterator.AtEnd());
}

TEST_F(SimplifiedBackwardsTextIteratorTest, FirstLetter) {
  SetBodyContent(
      "<style>p::first-letter {font-size: 200%}</style>"
      "<p> [(3)]678</p>");
  const Element* const sample = GetDocument().QuerySelector("p");
  SimplifiedBackwardsTextIterator iterator(EphemeralRange(
      Position(sample->firstChild(), 0), Position(sample->firstChild(), 9)));
  EXPECT_EQ(3, iterator.length());
  EXPECT_EQ(Position(sample->firstChild(), 6), iterator.StartPosition());
  EXPECT_EQ(Position(sample->firstChild(), 9), iterator.EndPosition());
  EXPECT_EQ(sample->firstChild(), iterator.StartContainer());
  EXPECT_EQ(9, iterator.EndOffset());
  EXPECT_EQ(sample->firstChild(), iterator.GetNode());
  EXPECT_EQ('8', iterator.CharacterAt(0));
  EXPECT_EQ('7', iterator.CharacterAt(1));
  EXPECT_EQ('6', iterator.CharacterAt(2));

  iterator.Advance();
  // TODO(editing-dev): |SimplifiedBackwardsTextIterator| should not account
  // collapsed whitespace (http://crbug.com/760428)
  EXPECT_EQ(6, iterator.length())
      << "We should have 5 as ignoring collapsed whitespace.";
  EXPECT_EQ(Position(sample->firstChild(), 0), iterator.StartPosition());
  EXPECT_EQ(Position(sample->firstChild(), 6), iterator.EndPosition());
  EXPECT_EQ(sample->firstChild(), iterator.StartContainer());
  EXPECT_EQ(6, iterator.EndOffset());
  EXPECT_EQ(sample->firstChild(), iterator.GetNode());
  EXPECT_EQ(']', iterator.CharacterAt(0));
  EXPECT_EQ(')', iterator.CharacterAt(1));
  EXPECT_EQ('3', iterator.CharacterAt(2));
  EXPECT_EQ('(', iterator.CharacterAt(3));
  EXPECT_EQ('[', iterator.CharacterAt(4));
  EXPECT_EQ(' ', iterator.CharacterAt(5));

  EXPECT_FALSE(iterator.AtEnd());
  iterator.Advance();
  EXPECT_TRUE(iterator.AtEnd());
}

TEST_F(SimplifiedBackwardsTextIteratorTest, SubrangeWithReplacedElements) {
  static const char* body_content =
      "<a id=host><b id=one>one</b> not appeared <b id=two>two</b></a>";
  const char* shadow_content =
      "three <content select=#two></content> <content select=#one></content> "
      "zero";
  SetBodyContent(body_content);
  SetShadowContent(shadow_content, "host");

  Element* host = GetDocument().getElementById("host");

  // We should not apply DOM tree version to containing shadow tree in
  // general. To record current behavior, we have this test. even if it
  // isn't intuitive.
  EXPECT_EQ("onetwo", ExtractString<EditingStrategy>(*host));
  EXPECT_EQ("three two one zero",
            ExtractString<EditingInFlatTreeStrategy>(*host));
}

TEST_F(SimplifiedBackwardsTextIteratorTest, characterAt) {
  const char* body_content =
      "<a id=host><b id=one>one</b> not appeared <b id=two>two</b></a>";
  const char* shadow_content =
      "three <content select=#two></content> <content select=#one></content> "
      "zero";
  SetBodyContent(body_content);
  SetShadowContent(shadow_content, "host");

  Element* host = GetDocument().getElementById("host");

  EphemeralRangeTemplate<EditingStrategy> range1(
      EphemeralRangeTemplate<EditingStrategy>::RangeOfContents(*host));
  SimplifiedBackwardsTextIteratorAlgorithm<EditingStrategy> back_iter1(range1);
  const char* message1 =
      "|backIter1| should emit 'one' and 'two' in reverse order.";
  EXPECT_EQ('o', back_iter1.CharacterAt(0)) << message1;
  EXPECT_EQ('w', back_iter1.CharacterAt(1)) << message1;
  EXPECT_EQ('t', back_iter1.CharacterAt(2)) << message1;
  back_iter1.Advance();
  EXPECT_EQ('e', back_iter1.CharacterAt(0)) << message1;
  EXPECT_EQ('n', back_iter1.CharacterAt(1)) << message1;
  EXPECT_EQ('o', back_iter1.CharacterAt(2)) << message1;

  EphemeralRangeTemplate<EditingInFlatTreeStrategy> range2(
      EphemeralRangeTemplate<EditingInFlatTreeStrategy>::RangeOfContents(
          *host));
  SimplifiedBackwardsTextIteratorAlgorithm<EditingInFlatTreeStrategy>
      back_iter2(range2);
  const char* message2 =
      "|backIter2| should emit 'three ', 'two', ' ', 'one' and ' zero' in "
      "reverse order.";
  EXPECT_EQ('o', back_iter2.CharacterAt(0)) << message2;
  EXPECT_EQ('r', back_iter2.CharacterAt(1)) << message2;
  EXPECT_EQ('e', back_iter2.CharacterAt(2)) << message2;
  EXPECT_EQ('z', back_iter2.CharacterAt(3)) << message2;
  EXPECT_EQ(' ', back_iter2.CharacterAt(4)) << message2;
  back_iter2.Advance();
  EXPECT_EQ('e', back_iter2.CharacterAt(0)) << message2;
  EXPECT_EQ('n', back_iter2.CharacterAt(1)) << message2;
  EXPECT_EQ('o', back_iter2.CharacterAt(2)) << message2;
  back_iter2.Advance();
  EXPECT_EQ(' ', back_iter2.CharacterAt(0)) << message2;
  back_iter2.Advance();
  EXPECT_EQ('o', back_iter2.CharacterAt(0)) << message2;
  EXPECT_EQ('w', back_iter2.CharacterAt(1)) << message2;
  EXPECT_EQ('t', back_iter2.CharacterAt(2)) << message2;
  back_iter2.Advance();
  EXPECT_EQ(' ', back_iter2.CharacterAt(0)) << message2;
  EXPECT_EQ('e', back_iter2.CharacterAt(1)) << message2;
  EXPECT_EQ('e', back_iter2.CharacterAt(2)) << message2;
  EXPECT_EQ('r', back_iter2.CharacterAt(3)) << message2;
  EXPECT_EQ('h', back_iter2.CharacterAt(4)) << message2;
  EXPECT_EQ('t', back_iter2.CharacterAt(5)) << message2;
}

TEST_F(SimplifiedBackwardsTextIteratorTest, TextSecurity) {
  InsertStyleElement("s {-webkit-text-security:disc;}");
  EXPECT_EQ("baz, xxx, abc",
            ExtractStringInRange("^abc<s>foo</s>baz|",
                                 EmitsSmallXForTextSecurityBehavior()));
  // E2 80 A2 is U+2022 BULLET
  EXPECT_EQ("baz, \xE2\x80\xA2\xE2\x80\xA2\xE2\x80\xA2, abc",
            ExtractStringInRange("^abc<s>foo</s>baz|", TextIteratorBehavior()));
}

}  // namespace simplified_backwards_text_iterator_test
}  // namespace blink