| 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. |
| 83 VisibleSelection selection = Select(7); | 85 VisibleSelection selection = Select(7); |
| 84 SurroundingText surrounding_text(selection.Start(), 42); | 86 SurroundingText surrounding_text(selection.Start(), 42); |
| 85 | 87 |
| 86 EXPECT_EQ("foo bar", surrounding_text.Content().SimplifyWhiteSpace()); | 88 EXPECT_EQ(0u, surrounding_text.Content().length()); |
| 87 EXPECT_EQ(8u, surrounding_text.StartOffsetInContent()); | 89 EXPECT_EQ(0u, surrounding_text.StartOffsetInContent()); |
| 88 EXPECT_EQ(8u, surrounding_text.EndOffsetInContent()); | 90 EXPECT_EQ(0u, surrounding_text.EndOffsetInContent()); |
| 89 } | 91 } |
| 90 | 92 |
| 91 { | 93 { |
| 92 VisibleSelection selection = Select(6); | 94 VisibleSelection selection = Select(6); |
| 93 SurroundingText surrounding_text(selection.Start(), 2); | 95 SurroundingText surrounding_text(selection.Start(), 2); |
| 94 | 96 |
| 95 EXPECT_EQ("ar", surrounding_text.Content()); | 97 EXPECT_EQ("ar", surrounding_text.Content()); |
| 96 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); | 98 EXPECT_EQ(1u, surrounding_text.StartOffsetInContent()); |
| 97 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); | 99 EXPECT_EQ(1u, surrounding_text.EndOffsetInContent()); |
| 98 } | 100 } |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 { | 156 { |
| 155 VisibleSelection selection = Select(6, 11); | 157 VisibleSelection selection = Select(6, 11); |
| 156 SurroundingText surrounding_text( | 158 SurroundingText surrounding_text( |
| 157 *CreateRange(FirstEphemeralRangeOf(selection)), 42); | 159 *CreateRange(FirstEphemeralRangeOf(selection)), 42); |
| 158 | 160 |
| 159 EXPECT_EQ("Lorem ipsum dolor sit amet", | 161 EXPECT_EQ("Lorem ipsum dolor sit amet", |
| 160 surrounding_text.Content().SimplifyWhiteSpace()); | 162 surrounding_text.Content().SimplifyWhiteSpace()); |
| 161 EXPECT_EQ(7u, surrounding_text.StartOffsetInContent()); | 163 EXPECT_EQ(7u, surrounding_text.StartOffsetInContent()); |
| 162 EXPECT_EQ(12u, surrounding_text.EndOffsetInContent()); | 164 EXPECT_EQ(12u, surrounding_text.EndOffsetInContent()); |
| 163 } | 165 } |
| 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 } | |
| 175 } | 166 } |
| 176 | 167 |
| 177 TEST_F(SurroundingTextTest, TreeCaretSelection) { | 168 TEST_F(SurroundingTextTest, TreeCaretSelection) { |
| 178 SetHTML( | 169 SetHTML( |
| 179 String("<div>This is outside of <p id='selection'>foo bar</p> the " | 170 String("<div>This is outside of <p id='selection'>foo bar</p> the " |
| 180 "selected node</div>")); | 171 "selected node</div>")); |
| 181 | 172 |
| 182 { | 173 { |
| 183 VisibleSelection selection = Select(0); | 174 VisibleSelection selection = Select(0); |
| 184 SurroundingText surrounding_text(selection.Start(), 1); | 175 SurroundingText surrounding_text(selection.Start(), 1); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 | 291 |
| 301 SurroundingText surrounding_text( | 292 SurroundingText surrounding_text( |
| 302 *CreateRange(FirstEphemeralRangeOf(selection)), 20); | 293 *CreateRange(FirstEphemeralRangeOf(selection)), 20); |
| 303 | 294 |
| 304 EXPECT_EQ("abc def ghi", surrounding_text.Content().SimplifyWhiteSpace()); | 295 EXPECT_EQ("abc def ghi", surrounding_text.Content().SimplifyWhiteSpace()); |
| 305 EXPECT_EQ(4u, surrounding_text.StartOffsetInContent()); | 296 EXPECT_EQ(4u, surrounding_text.StartOffsetInContent()); |
| 306 EXPECT_EQ(7u, surrounding_text.EndOffsetInContent()); | 297 EXPECT_EQ(7u, surrounding_text.EndOffsetInContent()); |
| 307 } | 298 } |
| 308 | 299 |
| 309 } // namespace blink | 300 } // namespace blink |
| OLD | NEW |