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 Marker_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 |
| 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 // TODO(rlanday): The behavior tested in the following DocumentMarker tests is |
| 1488 // going to be changed so markers are not split when text they contain is |
| 1489 // deleted |
| 1490 |
| 1491 TEST_F(InputMethodControllerTest, |
| 1492 Marker_WhitespaceFixupAroundMarkerBeginningWithSpace) { |
| 1493 Element* div = insertHTMLElement( |
| 1494 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1495 |
| 1496 // Add marker under " text" (use TextMatch since Composition markers don't |
| 1497 // persist across editing operations) |
| 1498 EphemeralRange markerRange = PlainTextRange(7, 12).createRange(*div); |
| 1499 document().markers().addMarker(markerRange.startPosition(), |
| 1500 markerRange.endPosition(), |
| 1501 DocumentMarker::TextMatch); |
| 1502 // Delete "Initial" |
| 1503 Vector<CompositionUnderline> emptyUnderlines; |
| 1504 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1505 controller().commitText(String(""), emptyUnderlines, 0); |
| 1506 |
| 1507 // Delete "blah" |
| 1508 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1509 controller().commitText(String(""), emptyUnderlines, 0); |
| 1510 |
| 1511 // Check that the marker was split when the space at the beginning was |
| 1512 // converted to an nbsp |
| 1513 EXPECT_EQ(2u, document().markers().markers().size()); |
| 1514 EXPECT_STREQ( |
| 1515 "\xC2\xA0", // UTF-8 for an nbsp |
| 1516 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1517 EXPECT_STREQ( |
| 1518 "text", |
| 1519 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
| 1520 } |
| 1521 |
| 1522 TEST_F(InputMethodControllerTest, |
| 1523 Marker_WhitespaceFixupAroundMarkerEndingWithSpace) { |
| 1524 Element* div = insertHTMLElement( |
| 1525 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1526 |
| 1527 // Add marker under "text " (use TextMatch since Composition markers don't |
| 1528 // persist across editing operations) |
| 1529 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); |
| 1530 document().markers().addMarker(markerRange.startPosition(), |
| 1531 markerRange.endPosition(), |
| 1532 DocumentMarker::TextMatch); |
| 1533 // Delete "Initial" |
| 1534 Vector<CompositionUnderline> emptyUnderlines; |
| 1535 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1536 controller().commitText(String(""), emptyUnderlines, 0); |
| 1537 |
| 1538 // Delete "blah" |
| 1539 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1540 controller().commitText(String(""), emptyUnderlines, 0); |
| 1541 |
| 1542 // Check that the marker was split when the space at the end was |
| 1543 // converted to an nbsp |
| 1544 EXPECT_EQ(2u, document().markers().markers().size()); |
| 1545 EXPECT_STREQ( |
| 1546 "text", |
| 1547 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1548 EXPECT_STREQ( |
| 1549 "\xC2\xA0", // UTF-8 for an nbsp |
| 1550 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
| 1551 } |
| 1552 |
| 1553 TEST_F(InputMethodControllerTest, |
| 1554 Marker_WhitespaceFixupAroundMarkerBeginningAndEndingWithSpaces) { |
| 1555 Element* div = insertHTMLElement( |
| 1556 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1557 |
| 1558 // Add marker under " text " (use TextMatch since Composition markers don't |
| 1559 // persist across editing operations) |
| 1560 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); |
| 1561 document().markers().addMarker(markerRange.startPosition(), |
| 1562 markerRange.endPosition(), |
| 1563 DocumentMarker::TextMatch); |
| 1564 |
| 1565 // Delete "Initial" |
| 1566 Vector<CompositionUnderline> emptyUnderlines; |
| 1567 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1568 controller().commitText(String(""), emptyUnderlines, 0); |
| 1569 |
| 1570 // Delete "blah" |
| 1571 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1572 controller().commitText(String(""), emptyUnderlines, 0); |
| 1573 |
| 1574 // Check that the marker was split into three pieces when the two spaces were |
| 1575 // converted to nbsps |
| 1576 EXPECT_EQ(3u, document().markers().markers().size()); |
| 1577 EXPECT_STREQ( |
| 1578 "\xC2\xA0", // UTF-8 for an nbsp |
| 1579 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1580 EXPECT_STREQ( |
| 1581 "text", |
| 1582 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); |
| 1583 EXPECT_STREQ( |
| 1584 "\xC2\xA0", // UTF-8 for an nbsp |
| 1585 getMarkedText(document().markers(), div->firstChild(), 2).utf8().data()); |
| 1586 } |
| 1587 |
| 1588 TEST_F(InputMethodControllerTest, Marker_ReplaceStartOfMarker) { |
| 1589 Element* div = insertHTMLElement( |
| 1590 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1591 |
| 1592 // Add marker under "Initial text" |
| 1593 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1594 document().markers().addMarker(markerRange.startPosition(), |
| 1595 markerRange.endPosition(), |
| 1596 DocumentMarker::TextMatch); |
| 1597 |
| 1598 // Replace "Initial" with "Original" |
| 1599 Vector<CompositionUnderline> emptyUnderlines; |
| 1600 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1601 controller().commitText(String("Original"), emptyUnderlines, 0); |
| 1602 |
| 1603 // Verify marker is under "al text" |
| 1604 // ("Initial" and "Original" have "al" as a common suffix) |
| 1605 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1606 EXPECT_STREQ( |
| 1607 "al text", |
| 1608 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1609 } |
| 1610 |
| 1611 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsStartOfMarker) { |
| 1612 Element* div = insertHTMLElement( |
| 1613 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1614 "sample"); |
| 1615 |
| 1616 // Add marker under "initial text" |
| 1617 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); |
| 1618 document().markers().addMarker(markerRange.startPosition(), |
| 1619 markerRange.endPosition(), |
| 1620 DocumentMarker::TextMatch); |
| 1621 |
| 1622 // Replace "some initial" with "boring" |
| 1623 Vector<CompositionUnderline> emptyUnderlines; |
| 1624 controller().setCompositionFromExistingText(emptyUnderlines, 8, 20); |
| 1625 controller().commitText(String("boring"), emptyUnderlines, 0); |
| 1626 |
| 1627 // Verify marker is under " text" |
| 1628 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1629 EXPECT_STREQ( |
| 1630 " text", |
| 1631 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1632 } |
| 1633 |
| 1634 TEST_F(InputMethodControllerTest, Marker_ReplaceEndOfMarker) { |
| 1635 Element* div = insertHTMLElement( |
| 1636 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1637 |
| 1638 // Add marker under "Initial text" |
| 1639 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1640 document().markers().addMarker(markerRange.startPosition(), |
| 1641 markerRange.endPosition(), |
| 1642 DocumentMarker::TextMatch); |
| 1643 |
| 1644 // Replace "text" with "string" |
| 1645 Vector<CompositionUnderline> emptyUnderlines; |
| 1646 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1647 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1648 |
| 1649 // Verify marker is under "Initial " |
| 1650 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1651 EXPECT_STREQ( |
| 1652 "Initial ", |
| 1653 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1654 } |
| 1655 |
| 1656 TEST_F(InputMethodControllerTest, Marker_ReplaceTextContainsEndOfMarker) { |
| 1657 Element* div = insertHTMLElement( |
| 1658 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1659 "sample"); |
| 1660 |
| 1661 // Add marker under "some initial" |
| 1662 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); |
| 1663 document().markers().addMarker(markerRange.startPosition(), |
| 1664 markerRange.endPosition(), |
| 1665 DocumentMarker::TextMatch); |
| 1666 |
| 1667 // Replace "initial text" with "content" |
| 1668 Vector<CompositionUnderline> emptyUnderlines; |
| 1669 controller().setCompositionFromExistingText(emptyUnderlines, 13, 25); |
| 1670 controller().commitText(String("content"), emptyUnderlines, 0); |
| 1671 |
| 1672 EXPECT_STREQ("This is some content", div->innerHTML().utf8().data()); |
| 1673 |
| 1674 // Verify marker is under "some " |
| 1675 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1676 EXPECT_STREQ( |
| 1677 "some ", |
| 1678 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1679 } |
| 1680 |
| 1681 TEST_F(InputMethodControllerTest, Marker_ReplaceEntireMarker) { |
| 1682 Element* div = insertHTMLElement( |
| 1683 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1684 |
| 1685 // Add marker under "text" |
| 1686 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1687 document().markers().addMarker(markerRange.startPosition(), |
| 1688 markerRange.endPosition(), |
| 1689 DocumentMarker::TextMatch); |
| 1690 |
| 1691 // Replace "text" with "string" |
| 1692 Vector<CompositionUnderline> emptyUnderlines; |
| 1693 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1694 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1695 |
| 1696 // Verify marker was removed |
| 1697 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1698 } |
| 1699 |
| 1700 TEST_F(InputMethodControllerTest, Marker_ReplaceTextWithMarkerAtBeginning) { |
| 1701 Element* div = insertHTMLElement( |
| 1702 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1703 |
| 1704 // Add marker under "Initial" |
| 1705 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); |
| 1706 document().markers().addMarker(markerRange.startPosition(), |
| 1707 markerRange.endPosition(), |
| 1708 DocumentMarker::TextMatch); |
| 1709 |
| 1710 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1711 |
| 1712 // Replace "Initial text" with "New string" |
| 1713 Vector<CompositionUnderline> emptyUnderlines; |
| 1714 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1715 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1716 |
| 1717 // Verify marker was removed |
| 1718 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1719 } |
| 1720 |
| 1721 TEST_F(InputMethodControllerTest, Marker_ReplaceTextWithMarkerAtEnd) { |
| 1722 Element* div = insertHTMLElement( |
| 1723 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1724 |
| 1725 // Add marker under "text" |
| 1726 EphemeralRange markerRange = PlainTextRange(8, 12).createRange(*div); |
| 1727 document().markers().addMarker(markerRange.startPosition(), |
| 1728 markerRange.endPosition(), |
| 1729 DocumentMarker::TextMatch); |
| 1730 |
| 1731 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1732 |
| 1733 // Replace "Initial text" with "New string" |
| 1734 Vector<CompositionUnderline> emptyUnderlines; |
| 1735 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1736 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1737 |
| 1738 // Verify marker was removed |
| 1739 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1740 } |
| 1741 |
1430 } // namespace blink | 1742 } // namespace blink |
OLD | NEW |