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/InputMethodController.h" | 5 #include "core/editing/InputMethodController.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/Element.h" | 8 #include "core/dom/Element.h" |
9 #include "core/dom/Range.h" | 9 #include "core/dom/Range.h" |
10 #include "core/editing/Editor.h" | 10 #include "core/editing/Editor.h" |
(...skipping 1186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1197 underlines.push_back(CompositionUnderline(1, 11, Color(255, 0, 0), false, 0)); | 1197 underlines.push_back(CompositionUnderline(1, 11, Color(255, 0, 0), false, 0)); |
1198 | 1198 |
1199 controller().commitText(String("string"), underlines, 0); | 1199 controller().commitText(String("string"), underlines, 0); |
1200 | 1200 |
1201 ASSERT_EQ(1u, document().markers().markers().size()); | 1201 ASSERT_EQ(1u, document().markers().markers().size()); |
1202 | 1202 |
1203 EXPECT_EQ(9u, document().markers().markers()[0]->startOffset()); | 1203 EXPECT_EQ(9u, document().markers().markers()[0]->startOffset()); |
1204 EXPECT_EQ(15u, document().markers().markers()[0]->endOffset()); | 1204 EXPECT_EQ(15u, document().markers().markers()[0]->endOffset()); |
1205 } | 1205 } |
1206 | 1206 |
| 1207 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker) { |
| 1208 Element* div = insertHTMLElement( |
| 1209 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1210 |
| 1211 // Add marker under "text" (use TextMatch since Composition markers don't |
| 1212 // persist across editing operations) |
| 1213 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1214 document().markers().addMarker(markerRange.startPosition(), |
| 1215 markerRange.endPosition(), |
| 1216 DocumentMarker::TextMatch); |
| 1217 // Delete "Initial" |
| 1218 Vector<CompositionUnderline> emptyUnderlines; |
| 1219 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1220 controller().commitText(String(""), emptyUnderlines, 0); |
| 1221 |
| 1222 // Delete "blah" |
| 1223 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1224 controller().commitText(String(""), emptyUnderlines, 0); |
| 1225 |
| 1226 // Check that the marker is still attached to "text" and doesn't include |
| 1227 // either space around it |
| 1228 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1229 EXPECT_EQ(1u, document().markers().markers()[0]->startOffset()); |
| 1230 EXPECT_EQ(5u, document().markers().markers()[0]->endOffset()); |
| 1231 } |
| 1232 |
| 1233 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker2) { |
| 1234 Element* div = insertHTMLElement( |
| 1235 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1236 |
| 1237 // Add marker under " text" (use TextMatch since Composition markers don't |
| 1238 // persist across editing operations) |
| 1239 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); |
| 1240 document().markers().addMarker(markerRange.startPosition(), |
| 1241 markerRange.endPosition(), |
| 1242 DocumentMarker::TextMatch); |
| 1243 // Delete "Initial" |
| 1244 Vector<CompositionUnderline> emptyUnderlines; |
| 1245 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1246 controller().commitText(String(""), emptyUnderlines, 0); |
| 1247 |
| 1248 // Delete "blah" |
| 1249 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1250 controller().commitText(String(""), emptyUnderlines, 0); |
| 1251 |
| 1252 // before "text" but not the space after |
| 1253 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1254 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1255 EXPECT_EQ(5u, document().markers().markers()[0]->endOffset()); |
| 1256 } |
| 1257 |
| 1258 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { |
| 1259 Element* div = insertHTMLElement( |
| 1260 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1261 |
| 1262 // Add marker under "text " (use TextMatch since Composition markers don't |
| 1263 // persist across editing operations) |
| 1264 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); |
| 1265 document().markers().addMarker(markerRange.startPosition(), |
| 1266 markerRange.endPosition(), |
| 1267 DocumentMarker::TextMatch); |
| 1268 // Delete "Initial" |
| 1269 Vector<CompositionUnderline> emptyUnderlines; |
| 1270 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1271 controller().commitText(String(""), emptyUnderlines, 0); |
| 1272 |
| 1273 // Delete "blah" |
| 1274 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1275 controller().commitText(String(""), emptyUnderlines, 0); |
| 1276 |
| 1277 // Check that the marker is still attached to "text " and includes the space |
| 1278 // after "text" but not the space before |
| 1279 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1280 EXPECT_EQ(1u, document().markers().markers()[0]->startOffset()); |
| 1281 EXPECT_EQ(6u, document().markers().markers()[0]->endOffset()); |
| 1282 } |
| 1283 |
| 1284 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { |
| 1285 Element* div = insertHTMLElement( |
| 1286 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1287 |
| 1288 // Add marker under " text " (use TextMatch since Composition markers don't |
| 1289 // persist across editing operations) |
| 1290 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); |
| 1291 document().markers().addMarker(markerRange.startPosition(), |
| 1292 markerRange.endPosition(), |
| 1293 DocumentMarker::TextMatch); |
| 1294 |
| 1295 // Delete "Initial" |
| 1296 Vector<CompositionUnderline> emptyUnderlines; |
| 1297 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1298 controller().commitText(String(""), emptyUnderlines, 0); |
| 1299 |
| 1300 // Delete "blah" |
| 1301 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1302 controller().commitText(String(""), emptyUnderlines, 0); |
| 1303 |
| 1304 // Check that the marker is still attached to " text " and includes both the |
| 1305 // space before "text" and the space after |
| 1306 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1307 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1308 EXPECT_EQ(6u, document().markers().markers()[0]->endOffset()); |
| 1309 } |
| 1310 |
| 1311 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { |
| 1312 Element* div = insertHTMLElement( |
| 1313 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1314 |
| 1315 // Add marker under "Initial text" |
| 1316 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1317 document().markers().addMarker(markerRange.startPosition(), |
| 1318 markerRange.endPosition(), |
| 1319 DocumentMarker::TextMatch); |
| 1320 |
| 1321 // Replace "Initial" with "Original" |
| 1322 Vector<CompositionUnderline> emptyUnderlines; |
| 1323 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1324 controller().commitText(String("Original"), emptyUnderlines, 0); |
| 1325 |
| 1326 // Verify marker is under "Original text" |
| 1327 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1328 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1329 EXPECT_EQ(13u, document().markers().markers()[0]->endOffset()); |
| 1330 } |
| 1331 |
| 1332 TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { |
| 1333 Element* div = insertHTMLElement( |
| 1334 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1335 |
| 1336 // Add marker under "Initial text" |
| 1337 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1338 document().markers().addMarker(markerRange.startPosition(), |
| 1339 markerRange.endPosition(), |
| 1340 DocumentMarker::TextMatch); |
| 1341 |
| 1342 // Replace "text" with "string" |
| 1343 Vector<CompositionUnderline> emptyUnderlines; |
| 1344 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1345 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1346 |
| 1347 // Verify marker is under "Initial string" |
| 1348 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1349 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1350 EXPECT_EQ(14u, document().markers().markers()[0]->endOffset()); |
| 1351 } |
| 1352 |
| 1353 TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { |
| 1354 Element* div = insertHTMLElement( |
| 1355 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1356 |
| 1357 // Add marker under "text" |
| 1358 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1359 document().markers().addMarker(markerRange.startPosition(), |
| 1360 markerRange.endPosition(), |
| 1361 DocumentMarker::TextMatch); |
| 1362 |
| 1363 // Replace "text" with "string" |
| 1364 Vector<CompositionUnderline> emptyUnderlines; |
| 1365 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1366 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1367 |
| 1368 // Verify marker is under "string" |
| 1369 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1370 EXPECT_EQ(8u, document().markers().markers()[0]->startOffset()); |
| 1371 EXPECT_EQ(14u, document().markers().markers()[0]->endOffset()); |
| 1372 } |
| 1373 |
| 1374 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { |
| 1375 Element* div = insertHTMLElement( |
| 1376 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1377 |
| 1378 // Add marker under "Initial" |
| 1379 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); |
| 1380 document().markers().addMarker(markerRange.startPosition(), |
| 1381 markerRange.endPosition(), |
| 1382 DocumentMarker::TextMatch); |
| 1383 |
| 1384 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1385 |
| 1386 // Replace "Initial text" with "New string" |
| 1387 Vector<CompositionUnderline> emptyUnderlines; |
| 1388 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1389 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1390 |
| 1391 // Verify marker was removed |
| 1392 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1393 } |
| 1394 |
| 1395 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { |
| 1396 Element* div = insertHTMLElement( |
| 1397 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1398 |
| 1399 // Add marker under "text" |
| 1400 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1401 document().markers().addMarker(markerRange.startPosition(), |
| 1402 markerRange.endPosition(), |
| 1403 DocumentMarker::TextMatch); |
| 1404 |
| 1405 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1406 |
| 1407 // Replace "Initial text" with "New string" |
| 1408 Vector<CompositionUnderline> emptyUnderlines; |
| 1409 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1410 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1411 |
| 1412 // Verify marker was removed |
| 1413 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1414 } |
| 1415 |
1207 } // namespace blink | 1416 } // namespace blink |
OLD | NEW |