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