| 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 1467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1478 Vector<CompositionUnderline> emptyUnderlines; | 1478 Vector<CompositionUnderline> emptyUnderlines; |
| 1479 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | 1479 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1480 controller().commitText(String(""), emptyUnderlines, 0); | 1480 controller().commitText(String(""), emptyUnderlines, 0); |
| 1481 | 1481 |
| 1482 // Delete "blah" | 1482 // Delete "blah" |
| 1483 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | 1483 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1484 controller().commitText(String(""), emptyUnderlines, 0); | 1484 controller().commitText(String(""), emptyUnderlines, 0); |
| 1485 | 1485 |
| 1486 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | 1486 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
| 1487 | 1487 |
| 1488 // Check that the marker was split when the space at the beginning was | 1488 // Check that the marker is still attached to " text" and includes the space |
| 1489 // converted to an nbsp | 1489 // before "text" but not the space after |
| 1490 EXPECT_EQ(2u, document().markers().markers().size()); | 1490 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1491 ASSERT_STREQ( | 1491 ASSERT_STREQ( |
| 1492 "\xC2\xA0", // UTF-8 for an nbsp | 1492 "\xC2\xA0text", |
| 1493 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | 1493 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1494 ASSERT_STREQ( | |
| 1495 "text", | |
| 1496 getMarkedText(document().markers(), div->firstChild(), 1).utf8().data()); | |
| 1497 } | 1494 } |
| 1498 | 1495 |
| 1499 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { | 1496 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker3) { |
| 1500 Element* div = insertHTMLElement( | 1497 Element* div = insertHTMLElement( |
| 1501 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | 1498 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1502 | 1499 |
| 1503 // Add marker under "text " (use TextMatch since Composition markers don't | 1500 // Add marker under "text " (use TextMatch since Composition markers don't |
| 1504 // persist across editing operations) | 1501 // persist across editing operations) |
| 1505 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); | 1502 EphemeralRange markerRange = PlainTextRange(8, 13).createRange(*div); |
| 1506 document().markers().addMarker(markerRange.startPosition(), | 1503 document().markers().addMarker(markerRange.startPosition(), |
| 1507 markerRange.endPosition(), | 1504 markerRange.endPosition(), |
| 1508 DocumentMarker::TextMatch); | 1505 DocumentMarker::TextMatch); |
| 1509 // Delete "Initial" | 1506 // Delete "Initial" |
| 1510 Vector<CompositionUnderline> emptyUnderlines; | 1507 Vector<CompositionUnderline> emptyUnderlines; |
| 1511 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | 1508 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1512 controller().commitText(String(""), emptyUnderlines, 0); | 1509 controller().commitText(String(""), emptyUnderlines, 0); |
| 1513 | 1510 |
| 1514 // Delete "blah" | 1511 // Delete "blah" |
| 1515 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | 1512 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1516 controller().commitText(String(""), emptyUnderlines, 0); | 1513 controller().commitText(String(""), emptyUnderlines, 0); |
| 1517 | 1514 |
| 1518 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | 1515 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
| 1519 | 1516 |
| 1520 // Check that the marker was split when the space at the end was | 1517 // Check that the marker is still attached to "text " and includes the space |
| 1521 // converted to an nbsp | 1518 // after "text" but not the space before |
| 1522 EXPECT_EQ(2u, document().markers().markers().size()); | 1519 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1523 ASSERT_STREQ( | 1520 ASSERT_STREQ( |
| 1524 "text", | 1521 "text\xC2\xA0", |
| 1525 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | 1522 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 } | 1523 } |
| 1530 | 1524 |
| 1531 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { | 1525 TEST_F(InputMethodControllerTest, WhitespaceFixupAroundMarker4) { |
| 1532 Element* div = insertHTMLElement( | 1526 Element* div = insertHTMLElement( |
| 1533 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); | 1527 "<div id='sample' contenteditable>Initial text blah</div>", "sample"); |
| 1534 | 1528 |
| 1535 // Add marker under " text " (use TextMatch since Composition markers don't | 1529 // Add marker under " text " (use TextMatch since Composition markers don't |
| 1536 // persist across editing operations) | 1530 // persist across editing operations) |
| 1537 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); | 1531 EphemeralRange markerRange = PlainTextRange(7, 13).createRange(*div); |
| 1538 document().markers().addMarker(markerRange.startPosition(), | 1532 document().markers().addMarker(markerRange.startPosition(), |
| 1539 markerRange.endPosition(), | 1533 markerRange.endPosition(), |
| 1540 DocumentMarker::TextMatch); | 1534 DocumentMarker::TextMatch); |
| 1541 | 1535 |
| 1542 // Delete "Initial" | 1536 // Delete "Initial" |
| 1543 Vector<CompositionUnderline> emptyUnderlines; | 1537 Vector<CompositionUnderline> emptyUnderlines; |
| 1544 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | 1538 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1545 controller().commitText(String(""), emptyUnderlines, 0); | 1539 controller().commitText(String(""), emptyUnderlines, 0); |
| 1546 | 1540 |
| 1547 // Delete "blah" | 1541 // Delete "blah" |
| 1548 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); | 1542 controller().setCompositionFromExistingText(emptyUnderlines, 6, 10); |
| 1549 controller().commitText(String(""), emptyUnderlines, 0); | 1543 controller().commitText(String(""), emptyUnderlines, 0); |
| 1550 | 1544 |
| 1551 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); | 1545 ASSERT_STREQ(" text ", div->innerHTML().utf8().data()); |
| 1552 | 1546 |
| 1553 // Check that the marker was split into three pieces when the two spaces were | 1547 // Check that the marker is still attached to " text " and includes both the |
| 1554 // converted to nbsps | 1548 // space before "text" and the space after |
| 1555 EXPECT_EQ(3u, document().markers().markers().size()); | 1549 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1556 ASSERT_STREQ( | 1550 ASSERT_STREQ( |
| 1557 "\xC2\xA0", // UTF-8 for an nbsp | 1551 "\xC2\xA0text\xC2\xA0", |
| 1558 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | 1552 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 } | 1553 } |
| 1566 | 1554 |
| 1567 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { | 1555 TEST_F(InputMethodControllerTest, ReplaceStartOfMarker) { |
| 1568 Element* div = insertHTMLElement( | 1556 Element* div = insertHTMLElement( |
| 1569 "<div id='sample' contenteditable>Initial text</div>", "sample"); | 1557 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1570 | 1558 |
| 1571 // Add marker under "Initial text" | 1559 // Add marker under "Initial text" |
| 1572 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); | 1560 EphemeralRange markerRange = PlainTextRange(0, 12).createRange(*div); |
| 1573 document().markers().addMarker(markerRange.startPosition(), | 1561 document().markers().addMarker(markerRange.startPosition(), |
| 1574 markerRange.endPosition(), | 1562 markerRange.endPosition(), |
| 1575 DocumentMarker::TextMatch); | 1563 DocumentMarker::TextMatch); |
| 1576 | 1564 |
| 1577 // Replace "Initial" with "Original" | 1565 // Replace "Initial" with "Original" |
| 1578 Vector<CompositionUnderline> emptyUnderlines; | 1566 Vector<CompositionUnderline> emptyUnderlines; |
| 1579 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); | 1567 controller().setCompositionFromExistingText(emptyUnderlines, 0, 7); |
| 1580 controller().commitText(String("Original"), emptyUnderlines, 0); | 1568 controller().commitText(String("Original"), emptyUnderlines, 0); |
| 1581 | 1569 |
| 1582 ASSERT_STREQ("Original text", div->innerHTML().utf8().data()); | 1570 ASSERT_STREQ("Original text", div->innerHTML().utf8().data()); |
| 1583 | 1571 |
| 1584 // Verify marker is under "al text" | 1572 // Verify marker is under "Original text" |
| 1585 // ("Initial" and "Original" have "al" as a common suffix) | |
| 1586 EXPECT_EQ(1u, document().markers().markers().size()); | 1573 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1587 ASSERT_STREQ( | 1574 ASSERT_STREQ( |
| 1588 "al text", | 1575 "Original text", |
| 1589 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | 1576 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1590 } | 1577 } |
| 1591 | 1578 |
| 1592 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterStartOfMarker) { | 1579 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterStartOfMarker) { |
| 1593 Element* div = insertHTMLElement( | 1580 Element* div = insertHTMLElement( |
| 1594 "<div id='sample' contenteditable>This is some initial text</div>", | 1581 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1595 "sample"); | 1582 "sample"); |
| 1596 | 1583 |
| 1597 // Add marker under "initial text" | 1584 // Add marker under "initial text" |
| 1598 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); | 1585 EphemeralRange markerRange = PlainTextRange(13, 25).createRange(*div); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1624 markerRange.endPosition(), | 1611 markerRange.endPosition(), |
| 1625 DocumentMarker::TextMatch); | 1612 DocumentMarker::TextMatch); |
| 1626 | 1613 |
| 1627 // Replace "text" with "string" | 1614 // Replace "text" with "string" |
| 1628 Vector<CompositionUnderline> emptyUnderlines; | 1615 Vector<CompositionUnderline> emptyUnderlines; |
| 1629 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | 1616 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1630 controller().commitText(String("string"), emptyUnderlines, 0); | 1617 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1631 | 1618 |
| 1632 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); | 1619 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); |
| 1633 | 1620 |
| 1634 // Verify marker is under "Initial " | 1621 // Verify marker is under "Initial string" |
| 1635 EXPECT_EQ(1u, document().markers().markers().size()); | 1622 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1636 ASSERT_STREQ( | 1623 ASSERT_STREQ( |
| 1637 "Initial ", | 1624 "Initial string", |
| 1638 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); | 1625 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1639 } | 1626 } |
| 1640 | 1627 |
| 1641 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterEndOfMarker) { | 1628 TEST_F(InputMethodControllerTest, ReplaceBeforeAndAfterEndOfMarker) { |
| 1642 Element* div = insertHTMLElement( | 1629 Element* div = insertHTMLElement( |
| 1643 "<div id='sample' contenteditable>This is some initial text</div>", | 1630 "<div id='sample' contenteditable>This is some initial text</div>", |
| 1644 "sample"); | 1631 "sample"); |
| 1645 | 1632 |
| 1646 // Add marker under "some initial" | 1633 // Add marker under "some initial" |
| 1647 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); | 1634 EphemeralRange markerRange = PlainTextRange(8, 20).createRange(*div); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1673 markerRange.endPosition(), | 1660 markerRange.endPosition(), |
| 1674 DocumentMarker::TextMatch); | 1661 DocumentMarker::TextMatch); |
| 1675 | 1662 |
| 1676 // Replace "text" with "string" | 1663 // Replace "text" with "string" |
| 1677 Vector<CompositionUnderline> emptyUnderlines; | 1664 Vector<CompositionUnderline> emptyUnderlines; |
| 1678 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); | 1665 controller().setCompositionFromExistingText(emptyUnderlines, 8, 12); |
| 1679 controller().commitText(String("string"), emptyUnderlines, 0); | 1666 controller().commitText(String("string"), emptyUnderlines, 0); |
| 1680 | 1667 |
| 1681 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); | 1668 ASSERT_STREQ("Initial string", div->innerHTML().utf8().data()); |
| 1682 | 1669 |
| 1683 // Verify marker was removed | 1670 // Verify marker is under "string" |
| 1684 EXPECT_EQ(0u, document().markers().markers().size()); | 1671 EXPECT_EQ(1u, document().markers().markers().size()); |
| 1672 ASSERT_STREQ( |
| 1673 "string", |
| 1674 getMarkedText(document().markers(), div->firstChild(), 0).utf8().data()); |
| 1685 } | 1675 } |
| 1686 | 1676 |
| 1687 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { | 1677 TEST_F(InputMethodControllerTest, ReplaceTextWithMarkerAtBeginning) { |
| 1688 Element* div = insertHTMLElement( | 1678 Element* div = insertHTMLElement( |
| 1689 "<div id='sample' contenteditable>Initial text</div>", "sample"); | 1679 "<div id='sample' contenteditable>Initial text</div>", "sample"); |
| 1690 | 1680 |
| 1691 // Add marker under "Initial" | 1681 // Add marker under "Initial" |
| 1692 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); | 1682 EphemeralRange markerRange = PlainTextRange(0, 7).createRange(*div); |
| 1693 document().markers().addMarker(markerRange.startPosition(), | 1683 document().markers().addMarker(markerRange.startPosition(), |
| 1694 markerRange.endPosition(), | 1684 markerRange.endPosition(), |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1724 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); | 1714 controller().setCompositionFromExistingText(emptyUnderlines, 0, 12); |
| 1725 controller().commitText(String("New string"), emptyUnderlines, 0); | 1715 controller().commitText(String("New string"), emptyUnderlines, 0); |
| 1726 | 1716 |
| 1727 ASSERT_STREQ("New string", div->innerHTML().utf8().data()); | 1717 ASSERT_STREQ("New string", div->innerHTML().utf8().data()); |
| 1728 | 1718 |
| 1729 // Verify marker was removed | 1719 // Verify marker was removed |
| 1730 EXPECT_EQ(0u, document().markers().markers().size()); | 1720 EXPECT_EQ(0u, document().markers().markers().size()); |
| 1731 } | 1721 } |
| 1732 | 1722 |
| 1733 } // namespace blink | 1723 } // namespace blink |
| OLD | NEW |