OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/editing/SurroundingText.h" | 5 #include "core/editing/SurroundingText.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
9 #include "core/dom/Range.h" | 9 #include "core/dom/Range.h" |
10 #include "core/dom/Text.h" | 10 #include "core/dom/Text.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 { | 73 { |
74 VisibleSelection selection = Select(0); | 74 VisibleSelection selection = Select(0); |
75 SurroundingText surrounding_text(selection.Start(), 42); | 75 SurroundingText surrounding_text(selection.Start(), 42); |
76 | 76 |
77 EXPECT_EQ("foo bar", surrounding_text.Content().SimplifyWhiteSpace()); | 77 EXPECT_EQ("foo bar", surrounding_text.Content().SimplifyWhiteSpace()); |
78 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); | 78 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); |
79 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); | 79 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); |
80 } | 80 } |
81 | 81 |
82 { | 82 { |
83 // FIXME: if the selection is at the end of the text, SurroundingText | |
84 // will return nothing. | |
85 VisibleSelection selection = Select(7); | 83 VisibleSelection selection = Select(7); |
86 SurroundingText surrounding_text(selection.Start(), 42); | 84 SurroundingText surrounding_text(selection.Start(), 42); |
87 | 85 |
88 EXPECT_EQ(0u, surrounding_text.Content().length()); | 86 EXPECT_EQ("foo bar", surrounding_text.Content().SimplifyWhiteSpace()); |
89 EXPECT_EQ(0u, surrounding_text.StartOffsetInContent()); | 87 EXPECT_EQ(8u, surrounding_text.StartOffsetInContent()); |
90 EXPECT_EQ(0u, surrounding_text.EndOffsetInContent()); | 88 EXPECT_EQ(8u, surrounding_text.EndOffsetInContent()); |
91 } | 89 } |
92 | 90 |
93 { | 91 { |
94 VisibleSelection selection = Select(6); | 92 VisibleSelection selection = Select(6); |
95 SurroundingText surrounding_text(selection.Start(), 2); | 93 SurroundingText surrounding_text(selection.Start(), 2); |
96 | 94 |
97 EXPECT_EQ("ar", surrounding_text.Content()); | 95 EXPECT_EQ("ar", surrounding_text.Content()); |
98 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); | 96 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); |
99 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); | 97 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); |
100 } | 98 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 { | 154 { |
157 VisibleSelection selection = Select(6, 11); | 155 VisibleSelection selection = Select(6, 11); |
158 SurroundingText surrounding_text( | 156 SurroundingText surrounding_text( |
159 *CreateRange(FirstEphemeralRangeOf(selection)), 42); | 157 *CreateRange(FirstEphemeralRangeOf(selection)), 42); |
160 | 158 |
161 EXPECT_EQ("Lorem ipsum dolor sit amet", | 159 EXPECT_EQ("Lorem ipsum dolor sit amet", |
162 surrounding_text.Content().SimplifyWhiteSpace()); | 160 surrounding_text.Content().SimplifyWhiteSpace()); |
163 EXPECT_EQ(7u, surrounding_text.StartOffsetInContent()); | 161 EXPECT_EQ(7u, surrounding_text.StartOffsetInContent()); |
164 EXPECT_EQ(12u, surrounding_text.EndOffsetInContent()); | 162 EXPECT_EQ(12u, surrounding_text.EndOffsetInContent()); |
165 } | 163 } |
| 164 |
| 165 { |
| 166 // Last word. |
| 167 VisibleSelection selection = Select(22, 26); |
| 168 SurroundingText surrounding_text( |
| 169 *CreateRange(FirstEphemeralRangeOf(selection)), 8); |
| 170 |
| 171 EXPECT_EQ("sit amet", surrounding_text.Content()); |
| 172 EXPECT_EQ(4u, surrounding_text.StartOffsetInContent()); |
| 173 EXPECT_EQ(8u, surrounding_text.EndOffsetInContent()); |
| 174 } |
166 } | 175 } |
167 | 176 |
168 TEST_F(SurroundingTextTest, TreeCaretSelection) { | 177 TEST_F(SurroundingTextTest, TreeCaretSelection) { |
169 SetHTML( | 178 SetHTML( |
170 String("<div>This is outside of <p id='selection'>foo bar</p> the " | 179 String("<div>This is outside of <p id='selection'>foo bar</p> the " |
171 "selected node</div>")); | 180 "selected node</div>")); |
172 | 181 |
173 { | 182 { |
174 VisibleSelection selection = Select(0); | 183 VisibleSelection selection = Select(0); |
175 SurroundingText surrounding_text(selection.Start(), 1); | 184 SurroundingText surrounding_text(selection.Start(), 1); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 300 |
292 SurroundingText surrounding_text( | 301 SurroundingText surrounding_text( |
293 *CreateRange(FirstEphemeralRangeOf(selection)), 20); | 302 *CreateRange(FirstEphemeralRangeOf(selection)), 20); |
294 | 303 |
295 EXPECT_EQ("abc def ghi", surrounding_text.Content().SimplifyWhiteSpace()); | 304 EXPECT_EQ("abc def ghi", surrounding_text.Content().SimplifyWhiteSpace()); |
296 EXPECT_EQ(4u, surrounding_text.StartOffsetInContent()); | 305 EXPECT_EQ(4u, surrounding_text.StartOffsetInContent()); |
297 EXPECT_EQ(7u, surrounding_text.EndOffsetInContent()); | 306 EXPECT_EQ(7u, surrounding_text.EndOffsetInContent()); |
298 } | 307 } |
299 | 308 |
300 } // namespace blink | 309 } // namespace blink |
OLD | NEW |