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, WhitespaceFixupAroundMarker) { | |
| 1460 Element* div = insertHTMLElement( | |
| 1461 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1462 | |
| 1463 // Add marker under "text" (use TextMatch since Composition markers don't | |
| 1464 // persist across editing operations) | |
| 1465 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1466 document().markers().addMarker(markerRange.startPosition(), | |
| 1467 markerRange.endPosition(), | |
| 1468 DocumentMarker::TextMatch); | |
| 1469 // Delete "Initial" | |
| 1470 Vector<CompositionUnderline> emptyUnderlines; | |
| 1471 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1472 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1473 | |
| 1474 // Delete "blah" | |
| 1475 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1476 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1477 | |
| 1478 // Check that the marker is still attached to "text" and doesn't include | |
| 1479 // either space around it | |
| 1480 EXPECT_EQ(1u, document().markers().markersFor(div->firstChild()).size()); | |
| 1481 EXPECT_STREQ( | |
| 1482 "text", | |
| 1483 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1484 } | |
| 1485 | |
| 1486 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker2) { | |
|
Xiaocheng
2017/03/01 21:32:10
Sorry that I forgot about it last time.
Please us
| |
| 1487 Element* div = insertHTMLElement( | |
| 1488 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1489 | |
| 1490 // Add marker under " text" (use TextMatch since Composition markers don't | |
| 1491 // persist across editing operations) | |
| 1492 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); | |
| 1493 document().markers().addMarker(markerRange.startPosition(), | |
| 1494 markerRange.endPosition(), | |
| 1495 DocumentMarker::TextMatch); | |
| 1496 // Delete "Initial" | |
| 1497 Vector<CompositionUnderline> emptyUnderlines; | |
| 1498 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1499 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1500 | |
| 1501 // Delete "blah" | |
| 1502 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1503 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1504 | |
| 1505 // Check that the marker was split when the space at the beginning was | |
| 1506 // converted to an nbsp | |
| 1507 EXPECT_EQ(2u, document().markers().markers().size()); | |
| 1508 EXPECT_STREQ( | |
| 1509 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1510 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1511 EXPECT_STREQ( | |
| 1512 "text", | |
| 1513 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1514 } | |
| 1515 | |
| 1516 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { | |
| 1517 Element* div = insertHTMLElement( | |
| 1518 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1519 | |
| 1520 // Add marker under "text " (use TextMatch since Composition markers don't | |
| 1521 // persist across editing operations) | |
| 1522 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); | |
| 1523 document().markers().addMarker(markerRange.startPosition(), | |
| 1524 markerRange.endPosition(), | |
| 1525 DocumentMarker::TextMatch); | |
| 1526 // Delete "Initial" | |
| 1527 Vector<CompositionUnderline> emptyUnderlines; | |
| 1528 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1529 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1530 | |
| 1531 // Delete "blah" | |
| 1532 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1533 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1534 | |
| 1535 // Check that the marker was split when the space at the end was | |
| 1536 // converted to an nbsp | |
| 1537 EXPECT_EQ(2u, document().markers().markers().size()); | |
| 1538 EXPECT_STREQ( | |
| 1539 "text", | |
| 1540 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1541 EXPECT_STREQ( | |
| 1542 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1543 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1544 } | |
| 1545 | |
| 1546 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { | |
| 1547 Element* div = insertHTMLElement( | |
| 1548 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | |
| 1549 | |
| 1550 // Add marker under " text " (use TextMatch since Composition markers don't | |
| 1551 // persist across editing operations) | |
| 1552 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); | |
| 1553 document().markers().addMarker(markerRange.startPosition(), | |
| 1554 markerRange.endPosition(), | |
| 1555 DocumentMarker::TextMatch); | |
| 1556 | |
| 1557 // Delete "Initial" | |
| 1558 Vector<CompositionUnderline> emptyUnderlines; | |
| 1559 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1560 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1561 | |
| 1562 // Delete "blah" | |
| 1563 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | |
| 1564 controller().commitText(String(""), emptyUnderlines, 0); | |
| 1565 | |
| 1566 // Check that the marker was split into three pieces when the two spaces were | |
| 1567 // converted to nbsps | |
| 1568 EXPECT_EQ(3u, document().markers().markers().size()); | |
| 1569 EXPECT_STREQ( | |
| 1570 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1571 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1572 EXPECT_STREQ( | |
| 1573 "text", | |
| 1574 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1575 EXPECT_STREQ( | |
| 1576 "\xC2\xA0", // UTF-8 for an nbsp | |
| 1577 getMarkedText(document().markers(), div->firstChild(), 2).utf8().data()); | |
| 1578 } | |
| 1579 | |
| 1580 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { | |
| 1581 Element* div = insertHTMLElement( | |
| 1582 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1583 | |
| 1584 // Add marker under "Initial text" | |
| 1585 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1586 document().markers().addMarker(markerRange.startPosition(), | |
| 1587 markerRange.endPosition(), | |
| 1588 DocumentMarker::TextMatch); | |
| 1589 | |
| 1590 // Replace "Initial" with "Original" | |
| 1591 Vector<CompositionUnderline> emptyUnderlines; | |
| 1592 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | |
| 1593 controller().commitText(String("Original"), emptyUnderlines, 0); | |
| 1594 | |
| 1595 // Verify marker is under "al text" | |
| 1596 // ("Initial" and "Original" have "al" as a common suffix) | |
| 1597 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1598 EXPECT_STREQ( | |
| 1599 "al text", | |
| 1600 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1601 } | |
| 1602 | |
| 1603 TEST_F(InputMethodControllerTest, ReplaceTextContainsStartOfMarker) { | |
| 1604 Element* div = insertHTMLElement( | |
| 1605 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1606 "sample"); | |
| 1607 | |
| 1608 // Add marker under "initial text" | |
| 1609 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); | |
| 1610 document().markers().addMarker(markerRange.startPosition(), | |
| 1611 markerRange.endPosition(), | |
| 1612 DocumentMarker::TextMatch); | |
| 1613 | |
| 1614 // Replace "some initial" with "boring" | |
| 1615 Vector<CompositionUnderline> emptyUnderlines; | |
| 1616 controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); | |
| 1617 controller().commitText(String("boring"), emptyUnderlines, 0); | |
| 1618 | |
| 1619 // Verify marker is under " text" | |
| 1620 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1621 EXPECT_STREQ( | |
| 1622 " text", | |
| 1623 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1624 } | |
| 1625 | |
| 1626 TEST_F(InputMethodControllerTest, ReplaceEndOfMarker) { | |
| 1627 Element* div = insertHTMLElement( | |
| 1628 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1629 | |
| 1630 // Add marker under "Initial text" | |
| 1631 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | |
| 1632 document().markers().addMarker(markerRange.startPosition(), | |
| 1633 markerRange.endPosition(), | |
| 1634 DocumentMarker::TextMatch); | |
| 1635 | |
| 1636 // Replace "text" with "string" | |
| 1637 Vector<CompositionUnderline> emptyUnderlines; | |
| 1638 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1639 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1640 | |
| 1641 // Verify marker is under "Initial " | |
| 1642 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1643 EXPECT_STREQ( | |
| 1644 "Initial ", | |
| 1645 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1646 } | |
| 1647 | |
| 1648 TEST_F(InputMethodControllerTest, ReplaceTextContainsEndOfMarker) { | |
| 1649 Element* div = insertHTMLElement( | |
| 1650 "<div id='sample' contenteditable>This is some initial text</div>", | |
| 1651 "sample"); | |
| 1652 | |
| 1653 // Add marker under "some initial" | |
| 1654 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); | |
| 1655 document().markers().addMarker(markerRange.startPosition(), | |
| 1656 markerRange.endPosition(), | |
| 1657 DocumentMarker::TextMatch); | |
| 1658 | |
| 1659 // Replace "initial text" with "content" | |
| 1660 Vector<CompositionUnderline> emptyUnderlines; | |
| 1661 controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); | |
| 1662 controller().commitText(String("content"), emptyUnderlines, 0); | |
| 1663 | |
| 1664 EXPECT_STREQ("This is some content", div->innerHTML().utf8().data()); | |
| 1665 | |
| 1666 // Verify marker is under "some " | |
| 1667 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1668 EXPECT_STREQ( | |
| 1669 "some ", | |
| 1670 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | |
| 1671 } | |
| 1672 | |
| 1673 TEST_F(InputMethodControllerTest, ReplaceEntireMarker) { | |
| 1674 Element* div = insertHTMLElement( | |
| 1675 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1676 | |
| 1677 // Add marker under "text" | |
| 1678 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1679 document().markers().addMarker(markerRange.startPosition(), | |
| 1680 markerRange.endPosition(), | |
| 1681 DocumentMarker::TextMatch); | |
| 1682 | |
| 1683 // Replace "text" with "string" | |
| 1684 Vector<CompositionUnderline> emptyUnderlines; | |
| 1685 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | |
| 1686 controller().commitText(String("string"), emptyUnderlines, 0); | |
| 1687 | |
| 1688 // Verify marker was removed | |
| 1689 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1690 } | |
| 1691 | |
| 1692 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { | |
| 1693 Element* div = insertHTMLElement( | |
| 1694 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1695 | |
| 1696 // Add marker under "Initial" | |
| 1697 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); | |
| 1698 document().markers().addMarker(markerRange.startPosition(), | |
| 1699 markerRange.endPosition(), | |
| 1700 DocumentMarker::TextMatch); | |
| 1701 | |
| 1702 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1703 | |
| 1704 // Replace "Initial text" with "New string" | |
| 1705 Vector<CompositionUnderline> emptyUnderlines; | |
| 1706 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1707 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1708 | |
| 1709 // Verify marker was removed | |
| 1710 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1711 } | |
| 1712 | |
| 1713 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtEnd) { | |
| 1714 Element* div = insertHTMLElement( | |
| 1715 "<div id='sample' contenteditable>Initial text</div>", "sample"); | |
| 1716 | |
| 1717 // Add marker under "text" | |
| 1718 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); | |
| 1719 document().markers().addMarker(markerRange.startPosition(), | |
| 1720 markerRange.endPosition(), | |
| 1721 DocumentMarker::TextMatch); | |
| 1722 | |
| 1723 EXPECT_EQ(1u, document().markers().markers().size()); | |
| 1724 | |
| 1725 // Replace "Initial text" with "New string" | |
| 1726 Vector<CompositionUnderline> emptyUnderlines; | |
| 1727 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | |
| 1728 controller().commitText(String("New string"), emptyUnderlines, 0); | |
| 1729 | |
| 1730 // Verify marker was removed | |
| 1731 EXPECT_EQ(0u, document().markers().markers().size()); | |
| 1732 } | |
| 1733 | |
| 1430 } // namespace blink | 1734 } // namespace blink |
| OLD | NEW |