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 1272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 | 1283 |
1284 // Make sure that caret is still at the end of the inserted text. | 1284 // Make sure that caret is still at the end of the inserted text. |
1285 EXPECT_FALSE(controller().hasComposition()); | 1285 EXPECT_FALSE(controller().hasComposition()); |
1286 EXPECT_EQ(7, frame() | 1286 EXPECT_EQ(7, frame() |
1287 .selection() | 1287 .selection() |
1288 .computeVisibleSelectionInDOMTreeDeprecated() | 1288 .computeVisibleSelectionInDOMTreeDeprecated() |
1289 .start() | 1289 .start() |
1290 .computeOffsetInContainerNode()); | 1290 .computeOffsetInContainerNode()); |
1291 } | 1291 } |
1292 | 1292 |
| 1293 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker) { |
| 1294 Element* div = insertHTMLElement( |
| 1295 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1296 |
| 1297 // Add marker under "text" (use TextMatch since Composition markers don't |
| 1298 // persist across editing operations) |
| 1299 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1300 document().markers().addMarker(markerRange.startPosition(), |
| 1301 markerRange.endPosition(), |
| 1302 DocumentMarker::TextMatch); |
| 1303 // Delete "Initial" |
| 1304 Vector<CompositionUnderline> emptyUnderlines; |
| 1305 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1306 controller().commitText(String(""), emptyUnderlines, 0); |
| 1307 |
| 1308 // Delete "blah" |
| 1309 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1310 controller().commitText(String(""), emptyUnderlines, 0); |
| 1311 |
| 1312 // Check that the marker is still attached to "text" and doesn't include |
| 1313 // either space around it |
| 1314 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1315 EXPECT_EQ(1u, document().markers().markers()[0]->startOffset()); |
| 1316 EXPECT_EQ(5u, document().markers().markers()[0]->endOffset()); |
| 1317 } |
| 1318 |
| 1319 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker2) { |
| 1320 Element* div = insertHTMLElement( |
| 1321 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1322 |
| 1323 // Add marker under " text" (use TextMatch since Composition markers don't |
| 1324 // persist across editing operations) |
| 1325 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); |
| 1326 document().markers().addMarker(markerRange.startPosition(), |
| 1327 markerRange.endPosition(), |
| 1328 DocumentMarker::TextMatch); |
| 1329 // Delete "Initial" |
| 1330 Vector<CompositionUnderline> emptyUnderlines; |
| 1331 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1332 controller().commitText(String(""), emptyUnderlines, 0); |
| 1333 |
| 1334 // Delete "blah" |
| 1335 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1336 controller().commitText(String(""), emptyUnderlines, 0); |
| 1337 |
| 1338 // before "text" but not the space after |
| 1339 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1340 |
| 1341 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1342 EXPECT_EQ(5u, document().markers().markers()[0]->endOffset()); |
| 1343 } |
| 1344 |
| 1345 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { |
| 1346 Element* div = insertHTMLElement( |
| 1347 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1348 |
| 1349 // Add marker under "text " (use TextMatch since Composition markers don't |
| 1350 // persist across editing operations) |
| 1351 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); |
| 1352 document().markers().addMarker(markerRange.startPosition(), |
| 1353 markerRange.endPosition(), |
| 1354 DocumentMarker::TextMatch); |
| 1355 // Delete "Initial" |
| 1356 Vector<CompositionUnderline> emptyUnderlines; |
| 1357 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1358 controller().commitText(String(""), emptyUnderlines, 0); |
| 1359 |
| 1360 // Delete "blah" |
| 1361 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1362 controller().commitText(String(""), emptyUnderlines, 0); |
| 1363 |
| 1364 // Check that the marker is still attached to "text " and includes the space |
| 1365 // after "text" but not the space before |
| 1366 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1367 EXPECT_EQ(1u, document().markers().markers()[0]->startOffset()); |
| 1368 EXPECT_EQ(6u, document().markers().markers()[0]->endOffset()); |
| 1369 } |
| 1370 |
| 1371 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { |
| 1372 Element* div = insertHTMLElement( |
| 1373 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1374 |
| 1375 // Add marker under " text " (use TextMatch since Composition markers don't |
| 1376 // persist across editing operations) |
| 1377 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); |
| 1378 document().markers().addMarker(markerRange.startPosition(), |
| 1379 markerRange.endPosition(), |
| 1380 DocumentMarker::TextMatch); |
| 1381 |
| 1382 // Delete "Initial" |
| 1383 Vector<CompositionUnderline> emptyUnderlines; |
| 1384 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1385 controller().commitText(String(""), emptyUnderlines, 0); |
| 1386 |
| 1387 // Delete "blah" |
| 1388 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1389 controller().commitText(String(""), emptyUnderlines, 0); |
| 1390 |
| 1391 // Check that the marker is still attached to " text " and includes both the |
| 1392 // space before "text" and the space after |
| 1393 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1394 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1395 EXPECT_EQ(6u, document().markers().markers()[0]->endOffset()); |
| 1396 } |
| 1397 |
| 1398 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { |
| 1399 Element* div = insertHTMLElement( |
| 1400 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1401 |
| 1402 // Add marker under "Initial text" |
| 1403 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1404 document().markers().addMarker(markerRange.startPosition(), |
| 1405 markerRange.endPosition(), |
| 1406 DocumentMarker::TextMatch); |
| 1407 |
| 1408 // Replace "Initial" with "Original" |
| 1409 Vector<CompositionUnderline> emptyUnderlines; |
| 1410 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1411 controller().commitText(String("Original"), emptyUnderlines, 0); |
| 1412 |
| 1413 // Verify marker is under "Original text" |
| 1414 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1415 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1416 EXPECT_EQ(13u, document().markers().markers()[0]->endOffset()); |
| 1417 } |
| 1418 |
| 1419 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterStartOfMarker) { |
| 1420 Element* div = insertHTMLElement( |
| 1421 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1422 "sample"); |
| 1423 |
| 1424 // Add marker under "initial text" |
| 1425 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); |
| 1426 document().markers().addMarker(markerRange.startPosition(), |
| 1427 markerRange.endPosition(), |
| 1428 DocumentMarker::TextMatch); |
| 1429 |
| 1430 // Replace "some initial" with "boring" |
| 1431 Vector<CompositionUnderline> emptyUnderlines; |
| 1432 controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); |
| 1433 controller().commitText(String("boring"), emptyUnderlines, 0); |
| 1434 |
| 1435 // Verify marker is under " text" |
| 1436 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1437 EXPECT_EQ(14u, document().markers().markers()[0]->startOffset()); |
| 1438 EXPECT_EQ(19u, document().markers().markers()[0]->endOffset()); |
| 1439 } |
| 1440 |
| 1441 TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { |
| 1442 Element* div = insertHTMLElement( |
| 1443 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1444 |
| 1445 // Add marker under "Initial text" |
| 1446 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1447 document().markers().addMarker(markerRange.startPosition(), |
| 1448 markerRange.endPosition(), |
| 1449 DocumentMarker::TextMatch); |
| 1450 |
| 1451 // Replace "text" with "string" |
| 1452 Vector<CompositionUnderline> emptyUnderlines; |
| 1453 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1454 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1455 |
| 1456 // Verify marker is under "Initial string" |
| 1457 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1458 EXPECT_EQ(0u, document().markers().markers()[0]->startOffset()); |
| 1459 EXPECT_EQ(14u, document().markers().markers()[0]->endOffset()); |
| 1460 } |
| 1461 |
| 1462 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterEndOfMarker) { |
| 1463 Element* div = insertHTMLElement( |
| 1464 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1465 "sample"); |
| 1466 |
| 1467 // Add marker under "some initial" |
| 1468 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); |
| 1469 document().markers().addMarker(markerRange.startPosition(), |
| 1470 markerRange.endPosition(), |
| 1471 DocumentMarker::TextMatch); |
| 1472 |
| 1473 // Replace "initial text" with "content" |
| 1474 Vector<CompositionUnderline> emptyUnderlines; |
| 1475 controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); |
| 1476 controller().commitText(String("content"), emptyUnderlines, 0); |
| 1477 |
| 1478 // Verify marker is under "some " |
| 1479 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1480 EXPECT_EQ(8u, document().markers().markers()[0]->startOffset()); |
| 1481 EXPECT_EQ(13u, document().markers().markers()[0]->endOffset()); |
| 1482 } |
| 1483 |
| 1484 TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { |
| 1485 Element* div = insertHTMLElement( |
| 1486 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1487 |
| 1488 // Add marker under "text" |
| 1489 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1490 document().markers().addMarker(markerRange.startPosition(), |
| 1491 markerRange.endPosition(), |
| 1492 DocumentMarker::TextMatch); |
| 1493 |
| 1494 // Replace "text" with "string" |
| 1495 Vector<CompositionUnderline> emptyUnderlines; |
| 1496 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1497 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1498 |
| 1499 // Verify marker is under "string" |
| 1500 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1501 EXPECT_EQ(8u, document().markers().markers()[0]->startOffset()); |
| 1502 EXPECT_EQ(14u, document().markers().markers()[0]->endOffset()); |
| 1503 } |
| 1504 |
| 1505 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { |
| 1506 Element* div = insertHTMLElement( |
| 1507 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1508 |
| 1509 // Add marker under "Initial" |
| 1510 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); |
| 1511 document().markers().addMarker(markerRange.startPosition(), |
| 1512 markerRange.endPosition(), |
| 1513 DocumentMarker::TextMatch); |
| 1514 |
| 1515 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1516 |
| 1517 // Replace "Initial text" with "New string" |
| 1518 Vector<CompositionUnderline> emptyUnderlines; |
| 1519 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1520 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1521 |
| 1522 // Verify marker was removed |
| 1523 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1524 } |
| 1525 |
| 1526 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { |
| 1527 Element* div = insertHTMLElement( |
| 1528 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1529 |
| 1530 // Add marker under "text" |
| 1531 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1532 document().markers().addMarker(markerRange.startPosition(), |
| 1533 markerRange.endPosition(), |
| 1534 DocumentMarker::TextMatch); |
| 1535 |
| 1536 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1537 |
| 1538 // Replace "Initial text" with "New string" |
| 1539 Vector<CompositionUnderline> emptyUnderlines; |
| 1540 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1541 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1542 |
| 1543 // Verify marker was removed |
| 1544 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1545 } |
| 1546 |
1293 } // namespace blink | 1547 } // namespace blink |
OLD | NEW |