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 <memory> | 7 #include <memory> |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/dom/Element.h" | 9 #include "core/dom/Element.h" |
| 10 #include "core/dom/Range.h" | 10 #include "core/dom/Range.h" |
| (...skipping 1409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1420 | 1420 |
| 1421 Vector<CompositionUnderline> underlines; | 1421 Vector<CompositionUnderline> underlines; |
| 1422 underlines.push_back(CompositionUnderline(0, 3, Color(255, 0, 0), false, 0)); | 1422 underlines.push_back(CompositionUnderline(0, 3, Color(255, 0, 0), false, 0)); |
| 1423 controller().setComposition(String("def"), underlines, 0, 3); | 1423 controller().setComposition(String("def"), underlines, 0, 3); |
| 1424 controller().setComposition(String(""), underlines, 0, 3); | 1424 controller().setComposition(String(""), underlines, 0, 3); |
| 1425 controller().commitText(String("def"), underlines, 0); | 1425 controller().commitText(String("def"), underlines, 0); |
| 1426 | 1426 |
| 1427 EXPECT_STREQ("abc\ndef", textarea->value().utf8().data()); | 1427 EXPECT_STREQ("abc\ndef", textarea->value().utf8().data()); |
| 1428 } | 1428 } |
| 1429 | 1429 |
| 1430 TEST_F(InputMethodControllerTest, WhitespaceFixup) { | |
| 1431 Element* div = insertHTMLElement( | |
| 1432 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1433 | |
| 1434 // Delete "Initial" | |
| 1435 Vector<CompositionUnderline> emptyUnderlines; | |
| 1436 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1437 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1438 | |
| 1439 // The space at the beginning of the string should have been converted to an | |
| 1440 // nbsp | |
| 1441 EXPECT_STREQ(" text blah", div->innerHTML().utf8().data()); | |
| 1442 | |
| 1443 // Delete "blah" | |
| 1444 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1445 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1446 | |
| 1447 // The space at the end of the string should have been converted to an nbsp | |
| 1448 EXPECT_STREQ(" text ", div->innerHTML().utf8().data()); | |
| 1449 } | |
| 1450 | |
| 1451 static String getMarkedText(DocumentMarkerController& documentMarkerController, | |
| 1452 Node* node, | |
| 1453 int markerIndex) { | |
| 1454 DocumentMarker* marker = documentMarkerController.markers()[markerIndex]; | |
| 1455 return node->textContent().substring( | |
| 1456 marker->startOffset(), marker->endOffset() - marker->startOffset()); | |
| 1457 } | |
| 1458 | |
| 1459 TEST_F(InputMethodControllerTest, | |
| 1460 WhitespaceFixupAroundMarkerNotContainingSpace) { | |
| 1461 Element* div = insertHTMLElement( | |
| 1462 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1463 | |
| 1464 // Add marker under "text" (use TextMatch since Composition markers don't | |
| 1465 // persist across editing operations) | |
| 1466 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1467 document().markers().addMarker(markerRange.startPosition(), | |
| 1468 markerRange.endPosition(), | |
| 1469 DocumentMarker::TextMatch); | |
| 1470 // Delete "Initial" | |
| 1471 Vector<CompositionUnderline> emptyUnderlines; | |
| 1472 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1473 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1474 | |
| 1475 // Delete "blah" | |
| 1476 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1477 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1478 | |
|
yabinh
2017/03/02 06:47:05
How about adding one line:
EXPECT_STREQ(" tex
Xiaocheng
2017/03/02 20:24:31
I suggested not checking precondition in PS1.
yabinh
2017/03/03 01:39:14
Sorry I didn't notice that. But now I still prefer
| |
| 1479 // Check that the marker is still attached to "text" and doesn't include | |
| 1480 // either space around it | |
| 1481 EXPECT_EQ(1u, document().markers().markersFor(div->firstChild()).size()); | |
| 1482 EXPECT_STREQ( | |
| 1483 "text", | |
| 1484 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1485 } | |
| 1486 | |
| 1487 TEST_F(InputMethodControllerTest, | |
| 1488 WhitespaceFixupAroundMarkerBeginningWithSpace) { | |
| 1489 Element* div = insertHTMLElement( | |
| 1490 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1491 | |
| 1492 // Add marker under " text" (use TextMatch since Composition markers don't | |
| 1493 // persist across editing operations) | |
| 1494 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); | |
| 1495 document().markers().addMarker(markerRange.startPosition(), | |
| 1496 markerRange.endPosition(), | |
| 1497 DocumentMarker::TextMatch); | |
| 1498 // Delete "Initial" | |
| 1499 Vector<CompositionUnderline> emptyUnderlines; | |
| 1500 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1501 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1502 | |
| 1503 // Delete "blah" | |
| 1504 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1505 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1506 | |
| 1507 // Check that the marker was split when the space at the beginning was | |
| 1508 // converted to an nbsp | |
| 1509 EXPECT_EQ(2u, document().markers().markers().size()); | |
| 1510 EXPECT_STREQ( | |
| 1511 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1512 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1513 EXPECT_STREQ( | |
| 1514 "text", | |
| 1515 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1516 } | |
| 1517 | |
| 1518 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarkerEndingWithSpace) { | |
| 1519 Element* div = insertHTMLElement( | |
| 1520 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1521 | |
| 1522 // Add marker under "text " (use TextMatch since Composition markers don't | |
| 1523 // persist across editing operations) | |
| 1524 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); | |
| 1525 document().markers().addMarker(markerRange.startPosition(), | |
| 1526 markerRange.endPosition(), | |
| 1527 DocumentMarker::TextMatch); | |
| 1528 // Delete "Initial" | |
| 1529 Vector<CompositionUnderline> emptyUnderlines; | |
| 1530 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1531 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1532 | |
| 1533 // Delete "blah" | |
| 1534 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1535 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1536 | |
| 1537 // Check that the marker was split when the space at the end was | |
| 1538 // converted to an nbsp | |
| 1539 EXPECT_EQ(2u, document().markers().markers().size()); | |
| 1540 EXPECT_STREQ( | |
| 1541 "text", | |
| 1542 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1543 EXPECT_STREQ( | |
| 1544 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1545 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1546 } | |
| 1547 | |
| 1548 TEST_F(InputMethodControllerTest, | |
| 1549 WhitespaceFixupAroundMarkerBeginningAndEndingWithSpaces) { | |
| 1550 Element* div = insertHTMLElement( | |
| 1551 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1552 | |
| 1553 // Add marker under " text " (use TextMatch since Composition markers don't | |
| 1554 // persist across editing operations) | |
| 1555 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); | |
| 1556 document().markers().addMarker(markerRange.startPosition(), | |
| 1557 markerRange.endPosition(), | |
| 1558 DocumentMarker::TextMatch); | |
| 1559 | |
| 1560 // Delete "Initial" | |
| 1561 Vector<CompositionUnderline> emptyUnderlines; | |
| 1562 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1563 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1564 | |
| 1565 // Delete "blah" | |
| 1566 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1567 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1568 | |
| 1569 // Check that the marker was split into three pieces when the two spaces were | |
| 1570 // converted to nbsps | |
| 1571 EXPECT_EQ(3u, document().markers().markers().size()); | |
| 1572 EXPECT_STREQ( | |
| 1573 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1574 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1575 EXPECT_STREQ( | |
| 1576 "text", | |
| 1577 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1578 EXPECT_STREQ( | |
| 1579 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1580 getMarkedText(document().markers(), div->firstChild(), 2).utf8().data()); | |
| 1581 } | |
| 1582 | |
| 1583 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { | |
| 1584 Element* div = insertHTMLElement( | |
| 1585 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1586 | |
| 1587 // Add marker under "Initial text" | |
| 1588 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1589 document().markers().addMarker(markerRange.startPosition(), | |
| 1590 markerRange.endPosition(), | |
| 1591 DocumentMarker::TextMatch); | |
| 1592 | |
| 1593 // Replace "Initial" with "Original" | |
| 1594 Vector<CompositionUnderline> emptyUnderlines; | |
| 1595 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1596 controller().commitText(String("Original"), emptyUnderlines, 0); | |
| 1597 | |
| 1598 // Verify marker is under "al text" | |
| 1599 // ("Initial" and "Original" have "al" as a common suffix) | |
| 1600 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1601 EXPECT_STREQ( | |
| 1602 "al text", | |
| 1603 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1604 } | |
| 1605 | |
| 1606 TEST_F(InputMethodControllerTest, ReplaceTextContainsStartOfMarker) { | |
| 1607 Element* div = insertHTMLElement( | |
| 1608 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1609 "sample"); | |
| 1610 | |
| 1611 // Add marker under "initial text" | |
| 1612 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); | |
| 1613 document().markers().addMarker(markerRange.startPosition(), | |
| 1614 markerRange.endPosition(), | |
| 1615 DocumentMarker::TextMatch); | |
| 1616 | |
| 1617 // Replace "some initial" with "boring" | |
| 1618 Vector<CompositionUnderline> emptyUnderlines; | |
| 1619 controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); | |
| 1620 controller().commitText(String("boring"), emptyUnderlines, 0); | |
| 1621 | |
| 1622 // Verify marker is under " text" | |
| 1623 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1624 EXPECT_STREQ( | |
| 1625 " text", | |
| 1626 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1627 } | |
| 1628 | |
| 1629 TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { | |
| 1630 Element* div = insertHTMLElement( | |
| 1631 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1632 | |
| 1633 // Add marker under "Initial text" | |
| 1634 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1635 document().markers().addMarker(markerRange.startPosition(), | |
| 1636 markerRange.endPosition(), | |
| 1637 DocumentMarker::TextMatch); | |
| 1638 | |
| 1639 // Replace "text" with "string" | |
| 1640 Vector<CompositionUnderline> emptyUnderlines; | |
| 1641 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1642 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1643 | |
| 1644 // Verify marker is under "Initial " | |
| 1645 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1646 EXPECT_STREQ( | |
| 1647 "Initial ", | |
| 1648 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1649 } | |
| 1650 | |
| 1651 TEST_F(InputMethodControllerTest, ReplaceTextContainsEndOfMarker) { | |
| 1652 Element* div = insertHTMLElement( | |
| 1653 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1654 "sample"); | |
| 1655 | |
| 1656 // Add marker under "some initial" | |
| 1657 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); | |
| 1658 document().markers().addMarker(markerRange.startPosition(), | |
| 1659 markerRange.endPosition(), | |
| 1660 DocumentMarker::TextMatch); | |
| 1661 | |
| 1662 // Replace "initial text" with "content" | |
| 1663 Vector<CompositionUnderline> emptyUnderlines; | |
| 1664 controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); | |
| 1665 controller().commitText(String("content"), emptyUnderlines, 0); | |
| 1666 | |
| 1667 EXPECT_STREQ("This is some content", div->innerHTML().utf8().data()); | |
| 1668 | |
| 1669 // Verify marker is under "some " | |
| 1670 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1671 EXPECT_STREQ( | |
| 1672 "some ", | |
| 1673 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1674 } | |
| 1675 | |
| 1676 TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { | |
| 1677 Element* div = insertHTMLElement( | |
| 1678 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1679 | |
| 1680 // Add marker under "text" | |
| 1681 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1682 document().markers().addMarker(markerRange.startPosition(), | |
| 1683 markerRange.endPosition(), | |
| 1684 DocumentMarker::TextMatch); | |
| 1685 | |
| 1686 // Replace "text" with "string" | |
| 1687 Vector<CompositionUnderline> emptyUnderlines; | |
| 1688 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1689 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1690 | |
| 1691 // Verify marker was removed | |
| 1692 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1693 } | |
| 1694 | |
| 1695 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { | |
| 1696 Element* div = insertHTMLElement( | |
| 1697 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1698 | |
| 1699 // Add marker under "Initial" | |
| 1700 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); | |
| 1701 document().markers().addMarker(markerRange.startPosition(), | |
| 1702 markerRange.endPosition(), | |
| 1703 DocumentMarker::TextMatch); | |
| 1704 | |
| 1705 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1706 | |
| 1707 // Replace "Initial text" with "New string" | |
| 1708 Vector<CompositionUnderline> emptyUnderlines; | |
| 1709 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1710 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1711 | |
| 1712 // Verify marker was removed | |
| 1713 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1714 } | |
| 1715 | |
| 1716 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { | |
| 1717 Element* div = insertHTMLElement( | |
| 1718 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1719 | |
| 1720 // Add marker under "text" | |
| 1721 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1722 document().markers().addMarker(markerRange.startPosition(), | |
| 1723 markerRange.endPosition(), | |
| 1724 DocumentMarker::TextMatch); | |
| 1725 | |
| 1726 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1727 | |
| 1728 // Replace "Initial text" with "New string" | |
| 1729 Vector<CompositionUnderline> emptyUnderlines; | |
| 1730 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1731 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1732 | |
| 1733 // Verify marker was removed | |
| 1734 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1735 } | |
| 1736 | |
| 1430 } // namespace blink | 1737 } // namespace blink |
| OLD | NEW |