Chromium Code Reviews| 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 static String getMarkedText(DocumentMarkerController& documentMarkerController, | |
|
yosin_UTC9
2017/02/28 01:29:37
Could you move IMCTest changes into separate CL to
| |
| 1294 Node* node, | |
| 1295 int markerIndex) { | |
| 1296 DocumentMarker* marker = documentMarkerController.markers()[markerIndex]; | |
| 1297 return node->textContent().substring( | |
| 1298 marker->startOffset(), marker->endOffset() - marker->startOffset()); | |
| 1299 } | |
| 1300 | |
| 1301 // TODO(crbug.com/696652): setCompositionFromExistingText() is counting the | |
|
yosin_UTC9
2017/02/28 01:29:37
I would like to discuss this issue in crbug.com/69
| |
| 1302 // offsets funny in the next few tests because the string starts with a space. | |
| 1303 // The 5, 9 offsets will need to be changed to 6, 10 once this is fixed | |
| 1304 | |
| 1305 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker) { | |
| 1306 Element* div = insertHTMLElement( | |
| 1307 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1308 | |
| 1309 // Add marker under "text" (use TextMatch since Composition markers don't | |
| 1310 // persist across editing operations) | |
| 1311 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1312 document().markers().addMarker(markerRange.startPosition(), | |
| 1313 markerRange.endPosition(), | |
| 1314 DocumentMarker::TextMatch); | |
| 1315 // Delete "Initial" | |
| 1316 Vector<CompositionUnderline> emptyUnderlines; | |
| 1317 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1318 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1319 | |
| 1320 // Delete "blah" | |
| 1321 controller().setCompositionFromExistingText(emptyUnderlines, 5, 9); | |
| 1322 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1323 | |
| 1324 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | |
| 1325 | |
| 1326 // Check that the marker is still attached to "text" and doesn't include | |
| 1327 // either space around it | |
| 1328 EXPECT_EQ(1u, document().markers().markersFor(div->firstChild()).size()); | |
| 1329 ASSERT_STREQ( | |
| 1330 "text", | |
| 1331 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1332 } | |
| 1333 | |
| 1334 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker2) { | |
| 1335 Element* div = insertHTMLElement( | |
| 1336 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1337 | |
| 1338 // Add marker under " text" (use TextMatch since Composition markers don't | |
| 1339 // persist across editing operations) | |
| 1340 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); | |
| 1341 document().markers().addMarker(markerRange.startPosition(), | |
| 1342 markerRange.endPosition(), | |
| 1343 DocumentMarker::TextMatch); | |
| 1344 // Delete "Initial" | |
| 1345 Vector<CompositionUnderline> emptyUnderlines; | |
| 1346 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1347 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1348 | |
| 1349 // Delete "blah" | |
| 1350 controller().setCompositionFromExistingText(emptyUnderlines, 5, 9); | |
| 1351 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1352 | |
| 1353 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | |
| 1354 | |
| 1355 // before "text" but not the space after | |
| 1356 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1357 ASSERT_STREQ( | |
| 1358 " text", | |
| 1359 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1360 } | |
| 1361 | |
| 1362 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { | |
| 1363 Element* div = insertHTMLElement( | |
| 1364 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1365 | |
| 1366 // Add marker under "text " (use TextMatch since Composition markers don't | |
| 1367 // persist across editing operations) | |
| 1368 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); | |
| 1369 document().markers().addMarker(markerRange.startPosition(), | |
| 1370 markerRange.endPosition(), | |
| 1371 DocumentMarker::TextMatch); | |
| 1372 // Delete "Initial" | |
| 1373 Vector<CompositionUnderline> emptyUnderlines; | |
| 1374 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1375 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1376 | |
| 1377 // Delete "blah" | |
| 1378 controller().setCompositionFromExistingText(emptyUnderlines, 5, 9); | |
| 1379 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1380 | |
| 1381 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | |
| 1382 | |
| 1383 // Check that the marker is still attached to "text " and includes the space | |
| 1384 // after "text" but not the space before | |
| 1385 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1386 ASSERT_STREQ( | |
| 1387 "text ", | |
| 1388 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1389 } | |
| 1390 | |
| 1391 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { | |
| 1392 Element* div = insertHTMLElement( | |
| 1393 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1394 | |
| 1395 // Add marker under " text " (use TextMatch since Composition markers don't | |
| 1396 // persist across editing operations) | |
| 1397 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); | |
| 1398 document().markers().addMarker(markerRange.startPosition(), | |
| 1399 markerRange.endPosition(), | |
| 1400 DocumentMarker::TextMatch); | |
| 1401 | |
| 1402 // Delete "Initial" | |
| 1403 Vector<CompositionUnderline> emptyUnderlines; | |
| 1404 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1405 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1406 | |
| 1407 // Delete "blah" | |
| 1408 controller().setCompositionFromExistingText(emptyUnderlines, 5, 9); | |
| 1409 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1410 | |
| 1411 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | |
| 1412 | |
| 1413 // Check that the marker is still attached to " text " and includes both the | |
| 1414 // space before "text" and the space after | |
| 1415 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1416 ASSERT_STREQ( | |
| 1417 " text ", | |
| 1418 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1419 } | |
| 1420 | |
| 1421 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { | |
| 1422 Element* div = insertHTMLElement( | |
| 1423 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1424 | |
| 1425 // Add marker under "Initial text" | |
| 1426 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1427 document().markers().addMarker(markerRange.startPosition(), | |
| 1428 markerRange.endPosition(), | |
| 1429 DocumentMarker::TextMatch); | |
| 1430 | |
| 1431 // Replace "Initial" with "Original" | |
| 1432 Vector<CompositionUnderline> emptyUnderlines; | |
| 1433 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1434 controller().commitText(String("Original"), emptyUnderlines, 0); | |
| 1435 | |
| 1436 ASSERT_STREQ("Original text", div->innerHTML().utf8().data()); | |
| 1437 | |
| 1438 // Verify marker is under "Original text" | |
| 1439 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1440 ASSERT_STREQ( | |
| 1441 "Original text", | |
| 1442 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1443 } | |
| 1444 | |
| 1445 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterStartOfMarker) { | |
| 1446 Element* div = insertHTMLElement( | |
| 1447 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1448 "sample"); | |
| 1449 | |
| 1450 // Add marker under "initial text" | |
| 1451 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); | |
| 1452 document().markers().addMarker(markerRange.startPosition(), | |
| 1453 markerRange.endPosition(), | |
| 1454 DocumentMarker::TextMatch); | |
| 1455 | |
| 1456 // Replace "some initial" with "boring" | |
| 1457 Vector<CompositionUnderline> emptyUnderlines; | |
| 1458 controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); | |
| 1459 controller().commitText(String("boring"), emptyUnderlines, 0); | |
| 1460 | |
| 1461 ASSERT_STREQ("This is boring text", div->innerHTML().utf8().data()); | |
| 1462 | |
| 1463 // Verify marker is under " text" | |
| 1464 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1465 ASSERT_STREQ( | |
| 1466 " text", | |
| 1467 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1468 } | |
| 1469 | |
| 1470 TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { | |
| 1471 Element* div = insertHTMLElement( | |
| 1472 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1473 | |
| 1474 // Add marker under "Initial text" | |
| 1475 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1476 document().markers().addMarker(markerRange.startPosition(), | |
| 1477 markerRange.endPosition(), | |
| 1478 DocumentMarker::TextMatch); | |
| 1479 | |
| 1480 // Replace "text" with "string" | |
| 1481 Vector<CompositionUnderline> emptyUnderlines; | |
| 1482 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1483 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1484 | |
| 1485 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); | |
| 1486 | |
| 1487 // Verify marker is under "Initial string" | |
| 1488 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1489 ASSERT_STREQ( | |
| 1490 "Initial string", | |
| 1491 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1492 } | |
| 1493 | |
| 1494 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterEndOfMarker) { | |
| 1495 Element* div = insertHTMLElement( | |
| 1496 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1497 "sample"); | |
| 1498 | |
| 1499 // Add marker under "some initial" | |
| 1500 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); | |
| 1501 document().markers().addMarker(markerRange.startPosition(), | |
| 1502 markerRange.endPosition(), | |
| 1503 DocumentMarker::TextMatch); | |
| 1504 | |
| 1505 // Replace "initial text" with "content" | |
| 1506 Vector<CompositionUnderline> emptyUnderlines; | |
| 1507 controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); | |
| 1508 controller().commitText(String("content"), emptyUnderlines, 0); | |
| 1509 | |
| 1510 ASSERT_STREQ("This is some content", div->innerHTML().utf8().data()); | |
| 1511 | |
| 1512 // Verify marker is under "some " | |
| 1513 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1514 ASSERT_STREQ( | |
| 1515 "some ", | |
| 1516 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1517 } | |
| 1518 | |
| 1519 TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { | |
| 1520 Element* div = insertHTMLElement( | |
| 1521 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1522 | |
| 1523 // Add marker under "text" | |
| 1524 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1525 document().markers().addMarker(markerRange.startPosition(), | |
| 1526 markerRange.endPosition(), | |
| 1527 DocumentMarker::TextMatch); | |
| 1528 | |
| 1529 // Replace "text" with "string" | |
| 1530 Vector<CompositionUnderline> emptyUnderlines; | |
| 1531 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1532 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1533 | |
| 1534 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); | |
| 1535 | |
| 1536 // Verify marker is under "string" | |
| 1537 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1538 ASSERT_STREQ( | |
| 1539 "string", | |
| 1540 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1541 } | |
| 1542 | |
| 1543 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { | |
| 1544 Element* div = insertHTMLElement( | |
| 1545 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1546 | |
| 1547 // Add marker under "Initial" | |
| 1548 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); | |
| 1549 document().markers().addMarker(markerRange.startPosition(), | |
| 1550 markerRange.endPosition(), | |
| 1551 DocumentMarker::TextMatch); | |
| 1552 | |
| 1553 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1554 | |
| 1555 // Replace "Initial text" with "New string" | |
| 1556 Vector<CompositionUnderline> emptyUnderlines; | |
| 1557 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1558 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1559 | |
| 1560 ASSERT_STREQ("New string", div->innerHTML().utf8().data()); | |
| 1561 | |
| 1562 // Verify marker was removed | |
| 1563 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1564 } | |
| 1565 | |
| 1566 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { | |
| 1567 Element* div = insertHTMLElement( | |
| 1568 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1569 | |
| 1570 // Add marker under "text" | |
| 1571 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1572 document().markers().addMarker(markerRange.startPosition(), | |
| 1573 markerRange.endPosition(), | |
| 1574 DocumentMarker::TextMatch); | |
| 1575 | |
| 1576 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1577 | |
| 1578 // Replace "Initial text" with "New string" | |
| 1579 Vector<CompositionUnderline> emptyUnderlines; | |
| 1580 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1581 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1582 | |
| 1583 ASSERT_STREQ("New string", div->innerHTML().utf8().data()); | |
| 1584 | |
| 1585 // Verify marker was removed | |
| 1586 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1587 } | |
| 1588 | |
| 1293 } // namespace blink | 1589 } // namespace blink |
| OLD | NEW |