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 "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/Range.h" | 8 #include "core/dom/Range.h" |
9 #include "core/dom/Text.h" | 9 #include "core/dom/Text.h" |
10 #include "core/editing/Position.h" | 10 #include "core/editing/Position.h" |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); | 106 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
107 EXPECT_EQ(7u, surroundingText.endOffsetInContent()); | 107 EXPECT_EQ(7u, surroundingText.endOffsetInContent()); |
108 } | 108 } |
109 } | 109 } |
110 | 110 |
111 TEST_F(SurroundingTextTest, BasicRangeSelection) { | 111 TEST_F(SurroundingTextTest, BasicRangeSelection) { |
112 setHTML(String("<p id='selection'>Lorem ipsum dolor sit amet</p>")); | 112 setHTML(String("<p id='selection'>Lorem ipsum dolor sit amet</p>")); |
113 | 113 |
114 { | 114 { |
115 VisibleSelection selection = select(0, 5); | 115 VisibleSelection selection = select(0, 5); |
116 SurroundingText surroundingText(*firstRangeOf(selection), 1); | 116 SurroundingText surroundingText( |
| 117 *createRange(firstEphemeralRangeOf(selection)), 1); |
117 | 118 |
118 EXPECT_EQ("Lorem ", surroundingText.content()); | 119 EXPECT_EQ("Lorem ", surroundingText.content()); |
119 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); | 120 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
120 EXPECT_EQ(5u, surroundingText.endOffsetInContent()); | 121 EXPECT_EQ(5u, surroundingText.endOffsetInContent()); |
121 } | 122 } |
122 | 123 |
123 { | 124 { |
124 VisibleSelection selection = select(0, 5); | 125 VisibleSelection selection = select(0, 5); |
125 SurroundingText surroundingText(*firstRangeOf(selection), 5); | 126 SurroundingText surroundingText( |
| 127 *createRange(firstEphemeralRangeOf(selection)), 5); |
126 | 128 |
127 EXPECT_EQ("Lorem ip", surroundingText.content().simplifyWhiteSpace()); | 129 EXPECT_EQ("Lorem ip", surroundingText.content().simplifyWhiteSpace()); |
128 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); | 130 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
129 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); | 131 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
130 } | 132 } |
131 | 133 |
132 { | 134 { |
133 VisibleSelection selection = select(0, 5); | 135 VisibleSelection selection = select(0, 5); |
134 SurroundingText surroundingText(*firstRangeOf(selection), 42); | 136 SurroundingText surroundingText( |
| 137 *createRange(firstEphemeralRangeOf(selection)), 42); |
135 | 138 |
136 EXPECT_EQ("Lorem ipsum dolor sit amet", | 139 EXPECT_EQ("Lorem ipsum dolor sit amet", |
137 surroundingText.content().simplifyWhiteSpace()); | 140 surroundingText.content().simplifyWhiteSpace()); |
138 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); | 141 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
139 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); | 142 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
140 } | 143 } |
141 | 144 |
142 { | 145 { |
143 VisibleSelection selection = select(6, 11); | 146 VisibleSelection selection = select(6, 11); |
144 SurroundingText surroundingText(*firstRangeOf(selection), 2); | 147 SurroundingText surroundingText( |
| 148 *createRange(firstEphemeralRangeOf(selection)), 2); |
145 | 149 |
146 EXPECT_EQ(" ipsum ", surroundingText.content()); | 150 EXPECT_EQ(" ipsum ", surroundingText.content()); |
147 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); | 151 EXPECT_EQ(1u, surroundingText.startOffsetInContent()); |
148 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); | 152 EXPECT_EQ(6u, surroundingText.endOffsetInContent()); |
149 } | 153 } |
150 | 154 |
151 { | 155 { |
152 VisibleSelection selection = select(6, 11); | 156 VisibleSelection selection = select(6, 11); |
153 SurroundingText surroundingText(*firstRangeOf(selection), 42); | 157 SurroundingText surroundingText( |
| 158 *createRange(firstEphemeralRangeOf(selection)), 42); |
154 | 159 |
155 EXPECT_EQ("Lorem ipsum dolor sit amet", | 160 EXPECT_EQ("Lorem ipsum dolor sit amet", |
156 surroundingText.content().simplifyWhiteSpace()); | 161 surroundingText.content().simplifyWhiteSpace()); |
157 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); | 162 EXPECT_EQ(7u, surroundingText.startOffsetInContent()); |
158 EXPECT_EQ(12u, surroundingText.endOffsetInContent()); | 163 EXPECT_EQ(12u, surroundingText.endOffsetInContent()); |
159 } | 164 } |
160 } | 165 } |
161 | 166 |
162 TEST_F(SurroundingTextTest, TreeCaretSelection) { | 167 TEST_F(SurroundingTextTest, TreeCaretSelection) { |
163 setHTML( | 168 setHTML( |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 } | 217 } |
213 } | 218 } |
214 | 219 |
215 TEST_F(SurroundingTextTest, TreeRangeSelection) { | 220 TEST_F(SurroundingTextTest, TreeRangeSelection) { |
216 setHTML( | 221 setHTML( |
217 String("<div>This is outside of <p id='selection'>foo bar</p> the " | 222 String("<div>This is outside of <p id='selection'>foo bar</p> the " |
218 "selected node</div>")); | 223 "selected node</div>")); |
219 | 224 |
220 { | 225 { |
221 VisibleSelection selection = select(0, 1); | 226 VisibleSelection selection = select(0, 1); |
222 SurroundingText surroundingText(*firstRangeOf(selection), 1); | 227 SurroundingText surroundingText( |
| 228 *createRange(firstEphemeralRangeOf(selection)), 1); |
223 | 229 |
224 EXPECT_EQ("fo", surroundingText.content().simplifyWhiteSpace()); | 230 EXPECT_EQ("fo", surroundingText.content().simplifyWhiteSpace()); |
225 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); | 231 EXPECT_EQ(0u, surroundingText.startOffsetInContent()); |
226 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); | 232 EXPECT_EQ(1u, surroundingText.endOffsetInContent()); |
227 } | 233 } |
228 | 234 |
229 { | 235 { |
230 VisibleSelection selection = select(0, 3); | 236 VisibleSelection selection = select(0, 3); |
231 SurroundingText surroundingText(*firstRangeOf(selection), 12); | 237 SurroundingText surroundingText( |
| 238 *createRange(firstEphemeralRangeOf(selection)), 12); |
232 | 239 |
233 EXPECT_EQ("e of foo bar", surroundingText.content().simplifyWhiteSpace()); | 240 EXPECT_EQ("e of foo bar", surroundingText.content().simplifyWhiteSpace()); |
234 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); | 241 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
235 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); | 242 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
236 } | 243 } |
237 | 244 |
238 { | 245 { |
239 VisibleSelection selection = select(0, 3); | 246 VisibleSelection selection = select(0, 3); |
240 SurroundingText surroundingText(*firstRangeOf(selection), 1337); | 247 SurroundingText surroundingText( |
| 248 *createRange(firstEphemeralRangeOf(selection)), 1337); |
241 | 249 |
242 EXPECT_EQ("This is outside of foo bar the selected node", | 250 EXPECT_EQ("This is outside of foo bar the selected node", |
243 surroundingText.content().simplifyWhiteSpace()); | 251 surroundingText.content().simplifyWhiteSpace()); |
244 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); | 252 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
245 EXPECT_EQ(23u, surroundingText.endOffsetInContent()); | 253 EXPECT_EQ(23u, surroundingText.endOffsetInContent()); |
246 } | 254 } |
247 | 255 |
248 { | 256 { |
249 VisibleSelection selection = select(4, 7); | 257 VisibleSelection selection = select(4, 7); |
250 SurroundingText surroundingText(*firstRangeOf(selection), 12); | 258 SurroundingText surroundingText( |
| 259 *createRange(firstEphemeralRangeOf(selection)), 12); |
251 | 260 |
252 EXPECT_EQ("foo bar the se", surroundingText.content().simplifyWhiteSpace()); | 261 EXPECT_EQ("foo bar the se", surroundingText.content().simplifyWhiteSpace()); |
253 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); | 262 EXPECT_EQ(5u, surroundingText.startOffsetInContent()); |
254 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); | 263 EXPECT_EQ(8u, surroundingText.endOffsetInContent()); |
255 } | 264 } |
256 | 265 |
257 { | 266 { |
258 VisibleSelection selection = select(0, 7); | 267 VisibleSelection selection = select(0, 7); |
259 SurroundingText surroundingText(*firstRangeOf(selection), 1337); | 268 SurroundingText surroundingText( |
| 269 *createRange(firstEphemeralRangeOf(selection)), 1337); |
260 | 270 |
261 EXPECT_EQ("This is outside of foo bar the selected node", | 271 EXPECT_EQ("This is outside of foo bar the selected node", |
262 surroundingText.content().simplifyWhiteSpace()); | 272 surroundingText.content().simplifyWhiteSpace()); |
263 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); | 273 EXPECT_EQ(20u, surroundingText.startOffsetInContent()); |
264 EXPECT_EQ(27u, surroundingText.endOffsetInContent()); | 274 EXPECT_EQ(27u, surroundingText.endOffsetInContent()); |
265 } | 275 } |
266 } | 276 } |
267 | 277 |
268 } // namespace blink | 278 } // namespace blink |
OLD | NEW |