1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Netscape Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Daniel Glazman <glazman@netscape.com>
*
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the NPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the NPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsPlaintextEditor.h"
#include "nsICaret.h"
#include "nsTextEditUtils.h"
#include "nsTextEditRules.h"
#include "nsEditorEventListeners.h"
#include "nsIEditActionListener.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMDocument.h"
#include "nsIDocument.h"
#include "nsIDOMEventReceiver.h"
#include "nsIDOM3EventTarget.h"
#include "nsIDOMKeyEvent.h"
#include "nsIDOMMouseListener.h"
#include "nsISelection.h"
#include "nsISelectionPrivate.h"
#include "nsISelectionController.h"
#include "nsGUIEvent.h"
#include "nsIDOMEventGroup.h"
#include "nsCRT.h"
#include "nsIEnumerator.h"
#include "nsIContent.h"
#include "nsIContentIterator.h"
#include "nsIDOMRange.h"
#include "nsISupportsArray.h"
#include "nsIComponentManager.h"
#include "nsIServiceManager.h"
#include "nsIDocumentEncoder.h"
#include "nsIPresShell.h"
#include "nsISupportsPrimitives.h"
#include "nsReadableUtils.h"
// Misc
#include "nsEditorUtils.h" // nsAutoEditBatch, nsAutoRules
#include "nsIPrefBranch.h"
#include "nsIPrefService.h"
#include "nsUnicharUtils.h"
#include "nsAOLCiter.h"
#include "nsInternetCiter.h"
// Drag & Drop, Clipboard
#include "nsIClipboard.h"
#include "nsITransferable.h"
// prototype for rules creation shortcut
nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
nsPlaintextEditor::nsPlaintextEditor()
: nsEditor()
, mIgnoreSpuriousDragEvent(PR_FALSE)
, mRules(nsnull)
, mWrapToWindow(PR_FALSE)
, mWrapColumn(0)
, mMaxTextLength(-1)
, mInitTriggerCounter(0)
{
}
nsPlaintextEditor::~nsPlaintextEditor()
{
// remove the rules as an action listener. Else we get a bad ownership loop later on.
// it's ok if the rules aren't a listener; we ignore the error.
nsCOMPtr<nsIEditActionListener> mListener = do_QueryInterface(mRules);
RemoveEditActionListener(mListener);
// Remove event listeners. Note that if we had an HTML editor,
// it installed its own instead of these
nsCOMPtr<nsIDOMEventReceiver> erP;
nsresult result = GetDOMEventReceiver(getter_AddRefs(erP));
if (NS_SUCCEEDED(result) && erP)
{
nsCOMPtr<nsIDOM3EventTarget> dom3Targ(do_QueryInterface(erP));
nsCOMPtr<nsIDOMEventGroup> sysGroup;
if (NS_SUCCEEDED(erP->GetSystemEventGroup(getter_AddRefs(sysGroup)))) {
result = dom3Targ->RemoveGroupedEventListener(NS_LITERAL_STRING("keypress"), mKeyListenerP, PR_FALSE, sysGroup);
}
if (mMouseListenerP) {
erP->RemoveEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener));
}
if (mTextListenerP) {
erP->RemoveEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener));
}
if (mCompositionListenerP) {
erP->RemoveEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener));
}
if (mFocusListenerP) {
erP->RemoveEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener));
}
if (mDragListenerP) {
erP->RemoveEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener));
}
}
}
NS_IMPL_ADDREF_INHERITED(nsPlaintextEditor, nsEditor)
NS_IMPL_RELEASE_INHERITED(nsPlaintextEditor, nsEditor)
NS_INTERFACE_MAP_BEGIN(nsPlaintextEditor)
NS_INTERFACE_MAP_ENTRY(nsIPlaintextEditor)
NS_INTERFACE_MAP_ENTRY(nsIEditorMailSupport)
NS_INTERFACE_MAP_END_INHERITING(nsEditor)
NS_IMETHODIMP nsPlaintextEditor::Init(nsIDOMDocument *aDoc,
nsIPresShell *aPresShell, nsIContent *aRoot, nsISelectionController *aSelCon, PRUint32 aFlags)
{
NS_PRECONDITION(aDoc && aPresShell, "bad arg");
if (!aDoc || !aPresShell)
return NS_ERROR_NULL_POINTER;
nsresult res = NS_OK, rulesRes = NS_OK;
if (1)
{
// block to scope nsAutoEditInitRulesTrigger
nsAutoEditInitRulesTrigger rulesTrigger(this, rulesRes);
// Init the base editor
res = nsEditor::Init(aDoc, aPresShell, aRoot, aSelCon, aFlags);
}
if (NS_FAILED(rulesRes)) return rulesRes;
return res;
}
void
nsPlaintextEditor::BeginEditorInit()
{
mInitTriggerCounter++;
}
nsresult
nsPlaintextEditor::EndEditorInit()
{
nsresult res = NS_OK;
NS_PRECONDITION(mInitTriggerCounter > 0, "ended editor init before we began?");
mInitTriggerCounter--;
if (mInitTriggerCounter == 0)
{
res = InitRules();
if (NS_SUCCEEDED(res))
EnableUndo(PR_TRUE);
}
return res;
}
NS_IMETHODIMP
nsPlaintextEditor::SetDocumentCharacterSet(const nsACString & characterSet)
{
nsresult result;
result = nsEditor::SetDocumentCharacterSet(characterSet);
// update META charset tag
if (NS_SUCCEEDED(result)) {
nsCOMPtr<nsIDOMDocument>domdoc;
result = GetDocument(getter_AddRefs(domdoc));
if (NS_SUCCEEDED(result) && domdoc) {
nsCOMPtr<nsIDOMNodeList>metaList;
nsCOMPtr<nsIDOMElement>metaElement;
PRBool newMetaCharset = PR_TRUE;
// get a list of META tags
result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("meta"), getter_AddRefs(metaList));
if (NS_SUCCEEDED(result) && metaList) {
PRUint32 listLength = 0;
(void) metaList->GetLength(&listLength);
nsCOMPtr<nsIDOMNode>metaNode;
for (PRUint32 i = 0; i < listLength; i++) {
metaList->Item(i, getter_AddRefs(metaNode));
if (!metaNode) continue;
metaElement = do_QueryInterface(metaNode);
if (!metaElement) continue;
nsAutoString currentValue;
if (NS_FAILED(metaElement->GetAttribute(NS_LITERAL_STRING("http-equiv"), currentValue))) continue;
if (FindInReadable(NS_LITERAL_STRING("content-type"),
currentValue,
nsCaseInsensitiveStringComparator())) {
NS_NAMED_LITERAL_STRING(content, "content");
if (NS_FAILED(metaElement->GetAttribute(content, currentValue))) continue;
NS_NAMED_LITERAL_STRING(charsetEquals, "charset=");
nsAString::const_iterator originalStart, start, end;
originalStart = currentValue.BeginReading(start);
currentValue.EndReading(end);
if (FindInReadable(charsetEquals, start, end,
nsCaseInsensitiveStringComparator())) {
// set attribute to <original prefix> charset=text/html
result = nsEditor::SetAttribute(metaElement, content,
Substring(originalStart, start) +
charsetEquals + NS_ConvertASCIItoUCS2(characterSet));
if (NS_SUCCEEDED(result))
newMetaCharset = PR_FALSE;
break;
}
}
}
}
if (newMetaCharset) {
nsCOMPtr<nsIDOMNodeList>headList;
result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList));
if (NS_SUCCEEDED(result) && headList) {
nsCOMPtr<nsIDOMNode>headNode;
headList->Item(0, getter_AddRefs(headNode));
if (headNode) {
nsCOMPtr<nsIDOMNode>resultNode;
// Create a new meta charset tag
result = CreateNode(NS_LITERAL_STRING("meta"), headNode, 0, getter_AddRefs(resultNode));
if (NS_FAILED(result))
return NS_ERROR_FAILURE;
// Set attributes to the created element
if (resultNode && !characterSet.IsEmpty()) {
metaElement = do_QueryInterface(resultNode);
if (metaElement) {
// not undoable, undo should undo CreateNode
result = metaElement->SetAttribute(NS_LITERAL_STRING("http-equiv"), NS_LITERAL_STRING("Content-Type"));
if (NS_SUCCEEDED(result)) {
// not undoable, undo should undo CreateNode
result = metaElement->SetAttribute(NS_LITERAL_STRING("content"),
NS_LITERAL_STRING("text/html;charset=") + NS_ConvertASCIItoUCS2(characterSet));
}
}
}
}
}
}
}
}
return result;
}
NS_IMETHODIMP
nsPlaintextEditor::PostCreate()
{
nsresult result = InstallEventListeners();
if (NS_FAILED(result)) return result;
return nsEditor::PostCreate();
}
NS_IMETHODIMP
nsPlaintextEditor::InstallEventListeners()
{
NS_ASSERTION(mDocWeak, "no document set on this editor");
if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED;
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
nsresult result;
// get a key listener
result = NS_NewEditorKeyListener(getter_AddRefs(mKeyListenerP), this);
if (NS_FAILED(result)) {
HandleEventListenerError();
return result;
}
// get a mouse listener
result = NS_NewEditorMouseListener(getter_AddRefs(mMouseListenerP), this);
if (NS_FAILED(result)) {
HandleEventListenerError();
return result;
}
// get a text listener
result = NS_NewEditorTextListener(getter_AddRefs(mTextListenerP),this);
if (NS_FAILED(result)) {
#ifdef DEBUG_TAGUE
printf("nsTextEditor.cpp: failed to get TextEvent Listener\n");
#endif
HandleEventListenerError();
return result;
}
// get a composition listener
result = NS_NewEditorCompositionListener(getter_AddRefs(mCompositionListenerP),this);
if (NS_FAILED(result)) {
#ifdef DEBUG_TAGUE
printf("nsTextEditor.cpp: failed to get TextEvent Listener\n");
#endif
HandleEventListenerError();
return result;
}
// get a drag listener
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShellWeak);
result = NS_NewEditorDragListener(getter_AddRefs(mDragListenerP), presShell, this);
if (NS_FAILED(result)) {
HandleEventListenerError();
return result;
}
// get a focus listener
result = NS_NewEditorFocusListener(getter_AddRefs(mFocusListenerP), this);
if (NS_FAILED(result)) {
HandleEventListenerError();
return result;
}
nsCOMPtr<nsIDOMEventReceiver> erP;
result = GetDOMEventReceiver(getter_AddRefs(erP));
//end hack
if (NS_FAILED(result)) {
HandleEventListenerError();
return result;
}
// register the event listeners with the DOM event reveiver
nsCOMPtr<nsIDOM3EventTarget> dom3Targ(do_QueryInterface(erP));
nsCOMPtr<nsIDOMEventGroup> sysGroup;
if (NS_SUCCEEDED(erP->GetSystemEventGroup(getter_AddRefs(sysGroup)))) {
result = dom3Targ->AddGroupedEventListener(NS_LITERAL_STRING("keypress"), mKeyListenerP, PR_FALSE, sysGroup);
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register key listener in system group");
}
if (NS_SUCCEEDED(result))
{
result = erP->AddEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener));
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register mouse listener");
if (NS_SUCCEEDED(result))
{
result = erP->AddEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener));
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register focus listener");
if (NS_SUCCEEDED(result))
{
result = erP->AddEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener));
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register text listener");
if (NS_SUCCEEDED(result))
{
result = erP->AddEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener));
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register composition listener");
if (NS_SUCCEEDED(result))
{
result = erP->AddEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener));
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register drag listener");
}
}
}
}
}
if (NS_FAILED(result)) {
HandleEventListenerError();
}
return result;
}
NS_IMETHODIMP
nsPlaintextEditor::GetFlags(PRUint32 *aFlags)
{
if (!mRules || !aFlags) { return NS_ERROR_NULL_POINTER; }
return mRules->GetFlags(aFlags);
}
NS_IMETHODIMP
nsPlaintextEditor::SetFlags(PRUint32 aFlags)
{
if (!mRules) { return NS_ERROR_NULL_POINTER; }
return mRules->SetFlags(aFlags);
}
NS_IMETHODIMP nsPlaintextEditor::InitRules()
{
// instantiate the rules for this text editor
nsresult res = NS_NewTextEditRules(getter_AddRefs(mRules));
if (NS_FAILED(res)) return res;
if (!mRules) return NS_ERROR_UNEXPECTED;
return mRules->Init(this, mFlags);
}
NS_IMETHODIMP
nsPlaintextEditor::GetIsDocumentEditable(PRBool *aIsDocumentEditable)
{
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
nsCOMPtr<nsIDOMDocument> doc;
GetDocument(getter_AddRefs(doc));
*aIsDocumentEditable = doc ? IsModifiable() : PR_FALSE;
return NS_OK;
}
PRBool nsPlaintextEditor::IsModifiable()
{
PRUint32 flags;
if (NS_SUCCEEDED(GetFlags(&flags)))
return ((flags & eEditorReadonlyMask) == 0);
return PR_FALSE;
}
#ifdef XP_MAC
#pragma mark -
#pragma mark nsIHTMLEditor methods
#pragma mark -
#endif
NS_IMETHODIMP nsPlaintextEditor::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent)
{
PRUint32 keyCode, character;
PRBool ctrlKey, altKey, metaKey;
if (!aKeyEvent) return NS_ERROR_NULL_POINTER;
if (NS_SUCCEEDED(aKeyEvent->GetKeyCode(&keyCode)) &&
NS_SUCCEEDED(aKeyEvent->GetCtrlKey(&ctrlKey)) &&
NS_SUCCEEDED(aKeyEvent->GetAltKey(&altKey)) &&
NS_SUCCEEDED(aKeyEvent->GetMetaKey(&metaKey)))
{
aKeyEvent->GetCharCode(&character);
if (keyCode == nsIDOMKeyEvent::DOM_VK_RETURN
|| keyCode == nsIDOMKeyEvent::DOM_VK_ENTER)
{
nsString empty;
return TypedText(empty, eTypedBreak);
}
else if (keyCode == nsIDOMKeyEvent::DOM_VK_ESCAPE)
{
// pass escape keypresses through as empty strings: needed for ime support
nsString empty;
return TypedText(empty, eTypedText);
}
if (character && !altKey && !ctrlKey && !metaKey)
{
aKeyEvent->PreventDefault();
nsAutoString key(character);
return TypedText(key, eTypedText);
}
}
return NS_ERROR_FAILURE;
}
/* This routine is needed to provide a bottleneck for typing for logging
purposes. Can't use EditorKeyPress() (above) for that since it takes
a nsIDOMUIEvent* parameter. So instead we pass enough info through
to TypedText() to determine what action to take, but without passing
an event.
*/
NS_IMETHODIMP nsPlaintextEditor::TypedText(const nsAString& aString,
PRInt32 aAction)
{
nsAutoPlaceHolderBatch batch(this, gTypingTxnName);
switch (aAction)
{
case eTypedText:
{
return InsertText(aString);
}
case eTypedBreak:
{
return InsertLineBreak();
}
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsPlaintextEditor::CreateBRImpl(nsCOMPtr<nsIDOMNode> *aInOutParent, PRInt32 *aInOutOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
{
if (!aInOutParent || !*aInOutParent || !aInOutOffset || !outBRNode) return NS_ERROR_NULL_POINTER;
*outBRNode = nsnull;
nsresult res;
// we need to insert a br. unfortunately, we may have to split a text node to do it.
nsCOMPtr<nsIDOMNode> node = *aInOutParent;
PRInt32 theOffset = *aInOutOffset;
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(node);
NS_NAMED_LITERAL_STRING(brType, "br");
nsCOMPtr<nsIDOMNode> brNode;
if (nodeAsText)
{
nsCOMPtr<nsIDOMNode> tmp;
PRInt32 offset;
PRUint32 len;
nodeAsText->GetLength(&len);
GetNodeLocation(node, address_of(tmp), &offset);
if (!tmp) return NS_ERROR_FAILURE;
if (!theOffset)
{
// we are already set to go
}
else if (theOffset == (PRInt32)len)
{
// update offset to point AFTER the text node
offset++;
}
else
{
// split the text node
res = SplitNode(node, theOffset, getter_AddRefs(tmp));
if (NS_FAILED(res)) return res;
res = GetNodeLocation(node, address_of(tmp), &offset);
if (NS_FAILED(res)) return res;
}
// create br
res = CreateNode(brType, tmp, offset, getter_AddRefs(brNode));
if (NS_FAILED(res)) return res;
*aInOutParent = tmp;
*aInOutOffset = offset+1;
}
else
{
res = CreateNode(brType, node, theOffset, getter_AddRefs(brNode));
if (NS_FAILED(res)) return res;
(*aInOutOffset)++;
}
*outBRNode = brNode;
if (*outBRNode && (aSelect != eNone))
{
nsCOMPtr<nsIDOMNode> parent;
PRInt32 offset;
res = GetNodeLocation(*outBRNode, address_of(parent), &offset);
if (NS_FAILED(res)) return res;
nsCOMPtr<nsISelection> selection;
res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res)) return res;
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
if (aSelect == eNext)
{
// position selection after br
selPriv->SetInterlinePosition(PR_TRUE);
res = selection->Collapse(parent, offset+1);
}
else if (aSelect == ePrevious)
{
// position selection before br
selPriv->SetInterlinePosition(PR_TRUE);
res = selection->Collapse(parent, offset);
}
}
return NS_OK;
}
NS_IMETHODIMP nsPlaintextEditor::CreateBR(nsIDOMNode *aNode, PRInt32 aOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
{
nsCOMPtr<nsIDOMNode> parent = aNode;
PRInt32 offset = aOffset;
return CreateBRImpl(address_of(parent), &offset, outBRNode, aSelect);
}
NS_IMETHODIMP nsPlaintextEditor::InsertBR(nsCOMPtr<nsIDOMNode> *outBRNode)
{
if (!outBRNode) return NS_ERROR_NULL_POINTER;
*outBRNode = nsnull;
// calling it text insertion to trigger moz br treatment by rules
nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext);
nsCOMPtr<nsISelection> selection;
nsresult res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res)) return res;
PRBool bCollapsed;
res = selection->GetIsCollapsed(&bCollapsed);
if (NS_FAILED(res)) return res;
if (!bCollapsed)
{
res = DeleteSelection(nsIEditor::eNone);
if (NS_FAILED(res)) return res;
}
nsCOMPtr<nsIDOMNode> selNode;
PRInt32 selOffset;
res = GetStartNodeAndOffset(selection, address_of(selNode), &selOffset);
if (NS_FAILED(res)) return res;
res = CreateBR(selNode, selOffset, outBRNode);
if (NS_FAILED(res)) return res;
// position selection after br
res = GetNodeLocation(*outBRNode, address_of(selNode), &selOffset);
if (NS_FAILED(res)) return res;
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
selPriv->SetInterlinePosition(PR_TRUE);
return selection->Collapse(selNode, selOffset+1);
}
nsresult nsPlaintextEditor::GetTextSelectionOffsets(nsISelection *aSelection,
PRInt32 &aOutStartOffset,
PRInt32 &aOutEndOffset)
{
if(!aSelection) { return NS_ERROR_NULL_POINTER; }
// initialize out params
aOutStartOffset = 0; // default to first char in selection
aOutEndOffset = -1; // default to total length of text in selection
nsCOMPtr<nsIDOMNode> startNode, endNode, parentNode;
PRInt32 startOffset, endOffset;
aSelection->GetAnchorNode(getter_AddRefs(startNode));
aSelection->GetAnchorOffset(&startOffset);
aSelection->GetFocusNode(getter_AddRefs(endNode));
aSelection->GetFocusOffset(&endOffset);
nsCOMPtr<nsIEnumerator> enumerator;
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(aSelection));
nsresult result = selPriv->GetEnumerator(getter_AddRefs(enumerator));
if (NS_FAILED(result)) return result;
if (!enumerator) return NS_ERROR_NULL_POINTER;
// don't use "result" in this block
enumerator->First();
nsCOMPtr<nsISupports> currentItem;
nsresult findParentResult = enumerator->CurrentItem(getter_AddRefs(currentItem));
if ((NS_SUCCEEDED(findParentResult)) && (currentItem))
{
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
range->GetCommonAncestorContainer(getter_AddRefs(parentNode));
}
else
{
parentNode = do_QueryInterface(startNode);
}
return GetAbsoluteOffsetsForPoints(startNode, startOffset,
endNode, endOffset,
parentNode,
aOutStartOffset, aOutEndOffset);
}
nsresult
nsPlaintextEditor::GetAbsoluteOffsetsForPoints(nsIDOMNode *aInStartNode,
PRInt32 aInStartOffset,
nsIDOMNode *aInEndNode,
PRInt32 aInEndOffset,
nsIDOMNode *aInCommonParentNode,
PRInt32 &aOutStartOffset,
PRInt32 &aOutEndOffset)
{
if(!aInStartNode || !aInEndNode || !aInCommonParentNode)
return NS_ERROR_NULL_POINTER;
// initialize out params
aOutStartOffset = 0; // default to first char in selection
aOutEndOffset = -1; // default to total length of text in selection
nsresult result;
nsCOMPtr<nsIContentIterator> iter = do_CreateInstance(
"@mozilla.org/content/post-content-iterator;1", &result);
if (NS_FAILED(result)) return result;
if (!iter) return NS_ERROR_NULL_POINTER;
PRUint32 totalLength=0;
nsCOMPtr<nsIDOMCharacterData>textNode;
nsCOMPtr<nsIContent>blockParentContent = do_QueryInterface(aInCommonParentNode);
iter->Init(blockParentContent);
// loop through the content iterator for each content node
while (!iter->IsDone())
{
textNode = do_QueryInterface(iter->GetCurrentNode());
if (textNode)
{
nsCOMPtr<nsIDOMNode>currentNode = do_QueryInterface(textNode);
if (!currentNode) {return NS_ERROR_NO_INTERFACE;}
if (IsEditable(currentNode))
{
if (currentNode.get() == aInStartNode)
{
aOutStartOffset = totalLength + aInStartOffset;
}
if (currentNode.get() == aInEndNode)
{
aOutEndOffset = totalLength + aInEndOffset;
break;
}
PRUint32 length;
textNode->GetLength(&length);
totalLength += length;
}
}
iter->Next();
}
if (-1==aOutEndOffset) {
aOutEndOffset = totalLength;
}
// guarantee that aOutStartOffset <= aOutEndOffset
if (aOutEndOffset<aOutStartOffset)
{
PRInt32 temp = aOutStartOffset;
aOutStartOffset= aOutEndOffset;
aOutEndOffset = temp;
}
NS_POSTCONDITION(aOutStartOffset <= aOutEndOffset, "start > end");
return result;
}
nsresult
nsPlaintextEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver)
{
if (!aEventReceiver)
return NS_ERROR_NULL_POINTER;
*aEventReceiver = 0;
nsCOMPtr<nsIDOMElement> rootElement;
nsresult result = GetRootElement(getter_AddRefs(rootElement));
if (NS_FAILED(result))
return result;
if (!rootElement)
return NS_ERROR_FAILURE;
// Now hack to make sure we are not anonymous content.
// If we are grab the parent of root element for our observer.
nsCOMPtr<nsIContent> content = do_QueryInterface(rootElement);
if (content)
{
nsCOMPtr<nsIContent> parent = content->GetParent();
if (parent)
{
if (parent->IndexOf(content) < 0 )
{
rootElement = do_QueryInterface(parent); //this will put listener on the form element basically
result = CallQueryInterface(rootElement, aEventReceiver);
}
else
rootElement = 0; // Let the event receiver work on the document instead of the root element
}
}
else
rootElement = 0;
if (!rootElement && mDocWeak)
{
// Don't use getDocument here, because we have no way of knowing if
// Init() was ever called. So we need to get the document ourselves,
// if it exists.
nsCOMPtr<nsIDOMDocument> domdoc = do_QueryReferent(mDocWeak);
if (!domdoc)
return NS_ERROR_FAILURE;
result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver);
}
return result;
}
NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction)
{
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
nsresult result;
// delete placeholder txns merge.
nsAutoPlaceHolderBatch batch(this, gDeleteTxnName);
nsAutoRules beginRulesSniffing(this, kOpDeleteSelection, aAction);
// If it's one of these modes,
// we have to extend the selection first.
// This needs to happen inside selection batching,
// otherwise the deleted text is autocopied to the clipboard.
if (aAction == eNextWord || aAction == ePreviousWord
|| aAction == eToBeginningOfLine || aAction == eToEndOfLine)
{
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
if (!ps) return NS_ERROR_NOT_INITIALIZED;
PRUint8 caretBidiLevel;
result = ps->GetCaretBidiLevel(&caretBidiLevel);
if (NS_FAILED(result)) return result;
nsCOMPtr<nsISelectionController> selCont (do_QueryReferent(mSelConWeak));
if (!selCont)
return NS_ERROR_NO_INTERFACE;
switch (aAction)
{
// if caret has odd Bidi level, i.e. text is right-to-left,
// reverse the effect of ePreviousWord and eNextWord
case eNextWord:
result = (caretBidiLevel & 1) ?
selCont->WordMove(PR_FALSE, PR_TRUE) :
selCont->WordMove(PR_TRUE, PR_TRUE);
// DeleteSelectionImpl doesn't handle these actions
// because it's inside batching, so don't confuse it:
aAction = eNone;
break;
case ePreviousWord:
result = (caretBidiLevel & 1) ?
selCont->WordMove(PR_TRUE, PR_TRUE) :
selCont->WordMove(PR_FALSE, PR_TRUE);
aAction = eNone;
break;
case eToBeginningOfLine:
selCont->IntraLineMove(PR_TRUE, PR_FALSE); // try to move to end
result = selCont->IntraLineMove(PR_FALSE, PR_TRUE); // select to beginning
aAction = eNone;
break;
case eToEndOfLine:
result = selCont->IntraLineMove(PR_TRUE, PR_TRUE);
aAction = eNext;
break;
default: // avoid several compiler warnings
result = NS_OK;
break;
}
if (NS_FAILED(result))
{
#ifdef DEBUG
printf("Selection controller interface didn't work!\n");
#endif
return result;
}
}
// pre-process
nsCOMPtr<nsISelection> selection;
result = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
nsTextRulesInfo ruleInfo(nsTextEditRules::kDeleteSelection);
ruleInfo.collapsedAction = aAction;
PRBool cancel, handled;
result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(result)) return result;
if (!cancel && !handled)
{
result = DeleteSelectionImpl(aAction);
}
if (!cancel)
{
// post-process
result = mRules->DidDoAction(selection, &ruleInfo, result);
}
return result;
}
NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert)
{
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
PRInt32 theAction = nsTextEditRules::kInsertText;
PRInt32 opID = kOpInsertText;
if (mInIMEMode)
{
theAction = nsTextEditRules::kInsertTextIME;
opID = kOpInsertIMEText;
}
nsAutoPlaceHolderBatch batch(this, nsnull);
nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext);
// pre-process
nsCOMPtr<nsISelection> selection;
nsresult result = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
if (!selection) return NS_ERROR_NULL_POINTER;
nsAutoString resultString;
// XXX can we trust instring to outlive ruleInfo,
// XXX and ruleInfo not to refer to instring in its dtor?
//nsAutoString instring(aStringToInsert);
nsTextRulesInfo ruleInfo(theAction);
ruleInfo.inString = &aStringToInsert;
ruleInfo.outString = &resultString;
ruleInfo.maxLength = mMaxTextLength;
PRBool cancel, handled;
result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(result)) return result;
if (!cancel && !handled)
{
// we rely on rules code for now - no default implementation
}
if (!cancel)
{
// post-process
result = mRules->DidDoAction(selection, &ruleInfo, result);
}
return result;
}
NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak()
{
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
nsAutoEditBatch beginBatching(this);
nsAutoRules beginRulesSniffing(this, kOpInsertBreak, nsIEditor::eNext);
// pre-process
nsCOMPtr<nsISelection> selection;
nsresult res;
res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res)) return res;
if (!selection) return NS_ERROR_NULL_POINTER;
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertBreak);
PRBool cancel, handled;
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(res)) return res;
if (!cancel && !handled)
{
// create the new BR node
nsCOMPtr<nsIDOMNode> newNode;
res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("br"), getter_AddRefs(newNode));
if (!newNode) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called
if (NS_SUCCEEDED(res))
{
// set the selection to the new node
nsCOMPtr<nsIDOMNode>parent;
res = newNode->GetParentNode(getter_AddRefs(parent));
if (!parent) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called
if (NS_SUCCEEDED(res))
{
PRInt32 offsetInParent=-1; // we use the -1 as a marker to see if we need to compute this or not
nsCOMPtr<nsIDOMNode>nextNode;
newNode->GetNextSibling(getter_AddRefs(nextNode));
if (nextNode)
{
nsCOMPtr<nsIDOMCharacterData>nextTextNode = do_QueryInterface(nextNode);
if (!nextTextNode) {
nextNode = do_QueryInterface(newNode); // is this QI needed?
}
else {
offsetInParent=0;
}
}
else {
nextNode = do_QueryInterface(newNode); // is this QI needed?
}
if (-1==offsetInParent)
{
nextNode->GetParentNode(getter_AddRefs(parent));
res = GetChildOffset(nextNode, parent, offsetInParent);
if (NS_SUCCEEDED(res)) {
// SetInterlinePosition(PR_TRUE) means we want the caret to stick to the content on the "right".
// We want the caret to stick to whatever is past the break. This is
// because the break is on the same line we were on, but the next content
// will be on the following line.
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
selPriv->SetInterlinePosition(PR_TRUE);
res = selection->Collapse(parent, offsetInParent+1); // +1 to insert just after the break
}
}
else
{
res = selection->Collapse(nextNode, offsetInParent);
}
}
}
}
if (!cancel)
{
// post-process, always called if WillInsertBreak didn't return cancel==PR_TRUE
res = mRules->DidDoAction(selection, &ruleInfo, res);
}
return res;
}
NS_IMETHODIMP
nsPlaintextEditor::BeginComposition(nsTextEventReply* aReply)
{
if(mFlags & nsIPlaintextEditor::eEditorPasswordMask) {
if (mRules) {
nsIEditRules *p = mRules.get();
nsTextEditRules *textEditRules = NS_STATIC_CAST(nsTextEditRules *, p);
textEditRules->ResetIMETextPWBuf();
}
else {
return NS_ERROR_NULL_POINTER;
}
}
return nsEditor::BeginComposition(aReply);
}
NS_IMETHODIMP
nsPlaintextEditor::GetDocumentIsEmpty(PRBool *aDocumentIsEmpty)
{
if (!aDocumentIsEmpty)
return NS_ERROR_NULL_POINTER;
if (!mRules)
return NS_ERROR_NOT_INITIALIZED;
return mRules->DocumentIsEmpty(aDocumentIsEmpty);
}
NS_IMETHODIMP
nsPlaintextEditor::GetTextLength(PRInt32 *aCount)
{
if (!aCount) { return NS_ERROR_NULL_POINTER; }
// initialize out params
*aCount = 0;
// special-case for empty document, to account for the bogus text node
PRBool docEmpty;
nsresult result = GetDocumentIsEmpty(&docEmpty);
if (NS_FAILED(result)) return result;
if (docEmpty)
return NS_OK;
// get the body node
nsCOMPtr<nsIDOMElement> bodyElement;
result = GetRootElement(getter_AddRefs(bodyElement));
if (NS_FAILED(result)) { return result; }
if (!bodyElement) { return NS_ERROR_NULL_POINTER; }
// get the offsets of the first and last children of the body node
nsCOMPtr<nsIDOMNode>lastChild;
result = bodyElement->GetLastChild(getter_AddRefs(lastChild));
if (NS_FAILED(result)) { return result; }
if (!lastChild) { return NS_ERROR_NULL_POINTER; }
PRInt32 numBodyChildren = 0;
result = GetChildOffset(lastChild, bodyElement, numBodyChildren);
if (NS_FAILED(result)) { return result; }
// count
PRInt32 start, end;
result = GetAbsoluteOffsetsForPoints(bodyElement, 0,
bodyElement, numBodyChildren,
bodyElement, start, end);
if (NS_SUCCEEDED(result))
{
NS_ASSERTION(0==start, "GetAbsoluteOffsetsForPoints failed to set start correctly.");
NS_ASSERTION(0<=end, "GetAbsoluteOffsetsForPoints failed to set end correctly.");
if (0<=end) {
*aCount = end;
}
}
return result;
}
NS_IMETHODIMP
nsPlaintextEditor::SetMaxTextLength(PRInt32 aMaxTextLength)
{
mMaxTextLength = aMaxTextLength;
return NS_OK;
}
NS_IMETHODIMP
nsPlaintextEditor::GetMaxTextLength(PRInt32* aMaxTextLength)
{
if (!aMaxTextLength)
return NS_ERROR_INVALID_POINTER;
*aMaxTextLength = mMaxTextLength;
return NS_OK;
}
//
// Get the wrap width
//
NS_IMETHODIMP
nsPlaintextEditor::GetWrapWidth(PRInt32 *aWrapColumn)
{
if (! aWrapColumn)
return NS_ERROR_NULL_POINTER;
*aWrapColumn = mWrapColumn;
return NS_OK;
}
//
// See if the style value includes this attribute, and if it does,
// cut out everything from the attribute to the next semicolon.
//
static void CutStyle(const char* stylename, nsString& styleValue)
{
// Find the current wrapping type:
PRInt32 styleStart = styleValue.Find(stylename, PR_TRUE);
if (styleStart >= 0)
{
PRInt32 styleEnd = styleValue.Find(";", PR_FALSE, styleStart);
if (styleEnd > styleStart)
styleValue.Cut(styleStart, styleEnd - styleStart + 1);
else
styleValue.Cut(styleStart, styleValue.Length() - styleStart);
}
}
//
// Change the wrap width on the root of this document.
//
NS_IMETHODIMP
nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn)
{
mWrapColumn = aWrapColumn;
// Make sure we're a plaintext editor, otherwise we shouldn't
// do the rest of this.
PRUint32 flags = 0;
GetFlags(&flags);
if (!(flags & eEditorPlaintextMask))
return NS_OK;
// Ought to set a style sheet here ...
// Probably should keep around an mPlaintextStyleSheet for this purpose.
nsCOMPtr<nsIDOMElement> bodyElement;
nsresult res = GetRootElement(getter_AddRefs(bodyElement));
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_NULL_POINTER;
// Get the current style for this body element:
NS_NAMED_LITERAL_STRING(styleName, "style");
nsAutoString styleValue;
res = bodyElement->GetAttribute(styleName, styleValue);
if (NS_FAILED(res)) return res;
// We'll replace styles for these values:
CutStyle("white-space", styleValue);
CutStyle("width", styleValue);
CutStyle("font-family", styleValue);
// If we have other style left, trim off any existing semicolons
// or whitespace, then add a known semicolon-space:
if (!styleValue.IsEmpty())
{
styleValue.Trim("; \t", PR_FALSE, PR_TRUE);
styleValue.Append(NS_LITERAL_STRING("; "));
}
// Make sure we have fixed-width font. This should be done for us,
// but it isn't, see bug 22502, so we have to add "font: -moz-fixed;".
// Only do this if we're wrapping.
if ((flags & eEditorEnableWrapHackMask) && aWrapColumn >= 0)
styleValue.Append(NS_LITERAL_STRING("font-family: -moz-fixed; "));
// If "mail.compose.wrap_to_window_width" is set, and we're a mail editor,
// then remember our wrap width (for output purposes) but set the visual
// wrapping to window width.
// We may reset mWrapToWindow here, based on the pref's current value.
if (flags & eEditorMailMask)
{
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv))
prefBranch->GetBoolPref("mail.compose.wrap_to_window_width",
&mWrapToWindow);
}
// and now we're ready to set the new whitespace/wrapping style.
if (aWrapColumn > 0 && !mWrapToWindow) // Wrap to a fixed column
{
styleValue.Append(NS_LITERAL_STRING("white-space: -moz-pre-wrap; width: "));
styleValue.AppendInt(aWrapColumn);
styleValue.Append(NS_LITERAL_STRING("ch;"));
}
else if (mWrapToWindow || aWrapColumn == 0)
styleValue.Append(NS_LITERAL_STRING("white-space: -moz-pre-wrap;"));
else
styleValue.Append(NS_LITERAL_STRING("white-space: pre;"));
return bodyElement->SetAttribute(styleName, styleValue);
}
#ifdef XP_MAC
#pragma mark -
#pragma mark nsIEditor overrides
#pragma mark -
#endif
NS_IMETHODIMP
nsPlaintextEditor::Undo(PRUint32 aCount)
{
nsAutoUpdateViewBatch beginViewBatching(this);
ForceCompositionEnd();
nsAutoRules beginRulesSniffing(this, kOpUndo, nsIEditor::eNone);
nsTextRulesInfo ruleInfo(nsTextEditRules::kUndo);
nsCOMPtr<nsISelection> selection;
GetSelection(getter_AddRefs(selection));
PRBool cancel, handled;
nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (!cancel && NS_SUCCEEDED(result))
{
result = nsEditor::Undo(aCount);
result = mRules->DidDoAction(selection, &ruleInfo, result);
}
return result;
}
NS_IMETHODIMP
nsPlaintextEditor::Redo(PRUint32 aCount)
{
nsAutoUpdateViewBatch beginViewBatching(this);
ForceCompositionEnd();
nsAutoRules beginRulesSniffing(this, kOpRedo, nsIEditor::eNone);
nsTextRulesInfo ruleInfo(nsTextEditRules::kRedo);
nsCOMPtr<nsISelection> selection;
GetSelection(getter_AddRefs(selection));
PRBool cancel, handled;
nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (!cancel && NS_SUCCEEDED(result))
{
result = nsEditor::Redo(aCount);
result = mRules->DidDoAction(selection, &ruleInfo, result);
}
return result;
}
NS_IMETHODIMP nsPlaintextEditor::Cut()
{
nsCOMPtr<nsISelection> selection;
nsresult res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res))
return res;
PRBool isCollapsed;
if (NS_SUCCEEDED(selection->GetIsCollapsed(&isCollapsed)) && isCollapsed)
return NS_OK; // just return ok so no JS error is thrown
res = Copy();
if (NS_SUCCEEDED(res))
res = DeleteSelection(eNone);
return res;
}
NS_IMETHODIMP nsPlaintextEditor::CanCut(PRBool *aCanCut)
{
nsresult res = CanCopy(aCanCut);
if (NS_FAILED(res)) return res;
*aCanCut = *aCanCut && IsModifiable();
return NS_OK;
}
NS_IMETHODIMP nsPlaintextEditor::Copy()
{
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
if (!ps) return NS_ERROR_NOT_INITIALIZED;
return ps->DoCopy();
}
NS_IMETHODIMP nsPlaintextEditor::CanCopy(PRBool *aCanCopy)
{
if (!aCanCopy)
return NS_ERROR_NULL_POINTER;
*aCanCopy = PR_FALSE;
nsCOMPtr<nsISelection> selection;
nsresult res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res)) return res;
PRBool isCollapsed;
res = selection->GetIsCollapsed(&isCollapsed);
if (NS_FAILED(res)) return res;
*aCanCopy = !isCollapsed;
return NS_OK;
}
// Shared between OutputToString and OutputToStream
NS_IMETHODIMP
nsPlaintextEditor::GetAndInitDocEncoder(const nsAString& aFormatType,
PRUint32 aFlags,
const nsACString& aCharset,
nsIDocumentEncoder** encoder)
{
nsCOMPtr<nsIPresShell> presShell;
nsresult rv = GetPresShell(getter_AddRefs(presShell));
if (NS_FAILED(rv)) return rv;
if (!presShell) return NS_ERROR_FAILURE;
nsCAutoString formatType(NS_DOC_ENCODER_CONTRACTID_BASE);
formatType.AppendWithConversion(aFormatType);
nsCOMPtr<nsIDocumentEncoder> docEncoder (do_CreateInstance(formatType.get(), &rv));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocument> doc;
rv = presShell->GetDocument(getter_AddRefs(doc));
NS_ENSURE_SUCCESS(rv, rv);
rv = docEncoder->Init(doc, aFormatType, aFlags);
NS_ENSURE_SUCCESS(rv, rv);
if (!aCharset.IsEmpty()
&& !(aCharset.Equals(NS_LITERAL_CSTRING("null"))))
docEncoder->SetCharset(aCharset);
PRInt32 wc;
(void) GetWrapWidth(&wc);
if (wc >= 0)
(void) docEncoder->SetWrapColumn(wc);
// Set the selection, if appropriate.
// We do this either if the OutputSelectionOnly flag is set,
// in which case we use our existing selection ...
if (aFlags & nsIDocumentEncoder::OutputSelectionOnly)
{
nsCOMPtr<nsISelection> selection;
rv = GetSelection(getter_AddRefs(selection));
if (NS_SUCCEEDED(rv) && selection)
rv = docEncoder->SetSelection(selection);
NS_ENSURE_SUCCESS(rv, rv);
}
// ... or if the root element is not a body,
// in which case we set the selection to encompass the root.
else
{
nsCOMPtr<nsIDOMElement> rootElement;
GetRootElement(getter_AddRefs(rootElement));
NS_ENSURE_TRUE(rootElement, NS_ERROR_FAILURE);
if (!nsTextEditUtils::IsBody(rootElement))
{
// XXX Why does this use range rather than selection collapse/extend?
nsCOMPtr<nsIDOMRange> range (do_CreateInstance("@mozilla.org/content/range;1", &rv));
if (NS_FAILED(rv)) return rv;
if (!range) return NS_ERROR_FAILURE;
nsCOMPtr<nsISelection> selection (do_CreateInstance(
"@mozilla.org/content/dom-selection;1", &rv));
if (NS_FAILED(rv)) return rv;
if (!selection) return NS_ERROR_FAILURE;
// get the independent selection interface
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
if (selPriv)
selPriv->SetPresShell(presShell);
nsCOMPtr<nsIContent> content(do_QueryInterface(rootElement));
if (content)
{
PRInt32 children = content->GetChildCount();
range->SetStart(rootElement, 0);
range->SetEnd(rootElement, children);
if (NS_FAILED(selection->AddRange(range)))
return NS_ERROR_FAILURE;
}
rv = docEncoder->SetSelection(selection);
NS_ENSURE_SUCCESS(rv, rv);
}
}
NS_ADDREF(*encoder = docEncoder);
return rv;
}
NS_IMETHODIMP
nsPlaintextEditor::OutputToString(const nsAString& aFormatType,
PRUint32 aFlags,
nsAString& aOutputString)
{
nsString resultString;
nsTextRulesInfo ruleInfo(nsTextEditRules::kOutputText);
ruleInfo.outString = &resultString;
// XXX Struct should store a nsAReadable*
nsAutoString str(aFormatType);
ruleInfo.outputFormat = &str;
PRBool cancel, handled;
nsresult rv = mRules->WillDoAction(nsnull, &ruleInfo, &cancel, &handled);
if (cancel || NS_FAILED(rv)) { return rv; }
if (handled)
{ // this case will get triggered by password fields
aOutputString.Assign(*(ruleInfo.outString));
return rv;
}
nsCAutoString charsetStr;
rv = GetDocumentCharacterSet(charsetStr);
if(NS_FAILED(rv) || charsetStr.IsEmpty())
charsetStr = NS_LITERAL_CSTRING("ISO-8859-1");
nsCOMPtr<nsIDocumentEncoder> encoder;
rv = GetAndInitDocEncoder(aFormatType, aFlags, charsetStr, getter_AddRefs(encoder));
if (NS_FAILED(rv))
return rv;
return encoder->EncodeToString(aOutputString);
}
NS_IMETHODIMP
nsPlaintextEditor::OutputToStream(nsIOutputStream* aOutputStream,
const nsAString& aFormatType,
const nsACString& aCharset,
PRUint32 aFlags)
{
nsresult rv;
// special-case for empty document when requesting plain text,
// to account for the bogus text node.
// XXX Should there be a similar test in OutputToString?
if (aFormatType == NS_LITERAL_STRING("text/plain"))
{
PRBool docEmpty;
rv = GetDocumentIsEmpty(&docEmpty);
if (NS_FAILED(rv)) return rv;
if (docEmpty)
return NS_OK; // output nothing
}
nsCOMPtr<nsIDocumentEncoder> encoder;
rv = GetAndInitDocEncoder(aFormatType, aFlags, aCharset,
getter_AddRefs(encoder));
if (NS_FAILED(rv))
return rv;
return encoder->EncodeToStream(aOutputStream);
}
#ifdef XP_MAC
#pragma mark -
#pragma mark nsIEditorMailSupport overrides
#pragma mark -
#endif
NS_IMETHODIMP
nsPlaintextEditor::InsertTextWithQuotations(const nsAString &aStringToInsert)
{
return InsertText(aStringToInsert);
}
NS_IMETHODIMP
nsPlaintextEditor::PasteAsQuotation(PRInt32 aSelectionType)
{
// Get Clipboard Service
nsresult rv;
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
if (NS_FAILED(rv)) return rv;
// Create generic Transferable for getting the data
nsCOMPtr<nsITransferable> trans = do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
if (NS_SUCCEEDED(rv) && trans)
{
// We only handle plaintext pastes here
trans->AddDataFlavor(kUnicodeMime);
// Get the Data from the clipboard
clipboard->GetData(trans, aSelectionType);
// Now we ask the transferable for the data
// it still owns the data, we just have a pointer to it.
// If it can't support a "text" output of the data the call will fail
nsCOMPtr<nsISupports> genericDataObj;
PRUint32 len;
char* flav = nsnull;
rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj),
&len);
if (NS_FAILED(rv) || !flav)
{
#ifdef DEBUG_akkana
printf("PasteAsPlaintextQuotation: GetAnyTransferData failed, %d\n", rv);
#endif
return rv;
}
#ifdef DEBUG_clipboard
printf("Got flavor [%s]\n", flav);
#endif
if (0 == nsCRT::strcmp(flav, kUnicodeMime))
{
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
if (textDataObj && len > 0)
{
nsAutoString stuffToPaste;
textDataObj->GetData ( stuffToPaste );
nsAutoEditBatch beginBatching(this);
rv = InsertAsQuotation(stuffToPaste, 0);
}
}
nsCRT::free(flav);
}
return rv;
}
// Utility routine to make a new citer. This addrefs, of course.
static nsICiter* MakeACiter()
{
// Make a citer of an appropriate type
nsICiter* citer = 0;
nsresult rv;
nsCOMPtr<nsIPrefBranch> prefBranch =
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
if (NS_FAILED(rv)) return 0;
char *citationType = 0;
rv = prefBranch->GetCharPref("mail.compose.citationType", &citationType);
if (NS_SUCCEEDED(rv) && citationType[0] && !strncmp(citationType, "aol", 3))
citer = new nsAOLCiter;
else
citer = new nsInternetCiter;
if (citationType)
PL_strfree(citationType);
if (citer)
NS_ADDREF(citer);
return citer;
}
NS_IMETHODIMP
nsPlaintextEditor::InsertAsQuotation(const nsAString& aQuotedText,
nsIDOMNode **aNodeInserted)
{
// We have the text. Cite it appropriately:
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
// Let the citer quote it for us:
nsString quotedStuff;
nsresult rv = citer->GetCiteString(aQuotedText, quotedStuff);
if (NS_FAILED(rv))
return rv;
// It's best to put a blank line after the quoted text so that mails
// written without thinking won't be so ugly.
if (!aQuotedText.IsEmpty() && (aQuotedText.Last() != PRUnichar('\n')))
quotedStuff.Append(PRUnichar('\n'));
// get selection
nsCOMPtr<nsISelection> selection;
rv = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(rv)) return rv;
if (!selection) return NS_ERROR_NULL_POINTER;
nsAutoEditBatch beginBatching(this);
nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext);
// give rules a chance to handle or cancel
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement);
PRBool cancel, handled;
rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
if (NS_FAILED(rv)) return rv;
if (cancel) return NS_OK; // rules canceled the operation
if (!handled)
{
rv = InsertText(quotedStuff);
// XXX Should set *aNodeInserted to the first node inserted
if (aNodeInserted && NS_SUCCEEDED(rv))
{
*aNodeInserted = 0;
//NS_IF_ADDREF(*aNodeInserted);
}
}
return rv;
}
NS_IMETHODIMP
nsPlaintextEditor::PasteAsCitedQuotation(const nsAString& aCitation,
PRInt32 aSelectionType)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
nsPlaintextEditor::InsertAsCitedQuotation(const nsAString& aQuotedText,
const nsAString& aCitation,
PRBool aInsertHTML,
nsIDOMNode **aNodeInserted)
{
return InsertAsQuotation(aQuotedText, aNodeInserted);
}
nsresult
nsPlaintextEditor::SharedOutputString(PRUint32 aFlags,
PRBool* aIsCollapsed,
nsAString& aResult)
{
nsCOMPtr<nsISelection> selection;
nsresult rv = GetSelection(getter_AddRefs(selection));
NS_ENSURE_SUCCESS(rv, rv);
if (!selection)
return NS_ERROR_NOT_INITIALIZED;
rv = selection->GetIsCollapsed(aIsCollapsed);
NS_ENSURE_SUCCESS(rv, rv);
if (!*aIsCollapsed)
aFlags |= nsIDocumentEncoder::OutputSelectionOnly;
// If the selection isn't collapsed, we'll use the whole document.
return OutputToString(NS_LITERAL_STRING("text/plain"), aFlags, aResult);
}
NS_IMETHODIMP
nsPlaintextEditor::Rewrap(PRBool aRespectNewlines)
{
PRInt32 wrapCol;
nsresult rv = GetWrapWidth(&wrapCol);
if (NS_FAILED(rv))
return NS_OK;
// Rewrap makes no sense if there's no wrap column; default to 72.
if (wrapCol <= 0)
wrapCol = 72;
#ifdef DEBUG_akkana
printf("nsPlaintextEditor::Rewrap to %ld columns\n", (long)wrapCol);
#endif
nsAutoString current;
PRBool isCollapsed;
rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted
| nsIDocumentEncoder::OutputLFLineBreak,
&isCollapsed, current);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
if (NS_FAILED(rv)) return rv;
if (!citer) return NS_ERROR_UNEXPECTED;
nsString wrapped;
PRUint32 firstLineOffset = 0; // XXX need to reset this if there is a selection
rv = citer->Rewrap(current, wrapCol, firstLineOffset, aRespectNewlines,
wrapped);
if (NS_FAILED(rv)) return rv;
if (isCollapsed) // rewrap the whole document
SelectAll();
return InsertTextWithQuotations(wrapped);
}
NS_IMETHODIMP
nsPlaintextEditor::StripCites()
{
#ifdef DEBUG_akkana
printf("nsPlaintextEditor::StripCites()\n");
#endif
nsAutoString current;
PRBool isCollapsed;
nsresult rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted,
&isCollapsed, current);
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
if (NS_FAILED(rv)) return rv;
if (!citer) return NS_ERROR_UNEXPECTED;
nsString stripped;
rv = citer->StripCites(current, stripped);
if (NS_FAILED(rv)) return rv;
if (isCollapsed) // rewrap the whole document
{
rv = SelectAll();
if (NS_FAILED(rv)) return rv;
}
return InsertText(stripped);
}
NS_IMETHODIMP
nsPlaintextEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
{
*aNodeList = 0;
return NS_OK;
}
#ifdef XP_MAC
#pragma mark -
#pragma mark nsIEditorIMESupport overrides
#pragma mark -
#endif
NS_IMETHODIMP
nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply)
{
NS_ASSERTION(aTextRangeList, "null ptr");
if (!aTextRangeList)
return NS_ERROR_NULL_POINTER;
// workaround for windows ime bug 23558: we get every ime event twice.
// for escape keypress, this causes an empty string to be passed
// twice, which freaks out the editor. This is to detect and avoid that
// situation:
if (aCompositionString.IsEmpty() && !mIMETextNode)
{
return NS_OK;
}
mIMETextRangeList = aTextRangeList;
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
if (!ps)
return NS_ERROR_NOT_INITIALIZED;
// XXX_kin: BEGIN HACK! HACK! HACK!
// XXX_kin:
// XXX_kin: This is lame! The IME stuff needs caret coordinates
// XXX_kin: synchronously, but the editor could be using async
// XXX_kin: updates (reflows and paints) for performance reasons.
// XXX_kin: In order to give IME what it needs, we have to temporarily
// XXX_kin: switch to sync updating during this call so that the
// XXX_kin: nsAutoPlaceHolderBatch can force sync reflows, paints,
// XXX_kin: and selection scrolling, so that we get back accurate
// XXX_kin: caret coordinates.
PRUint32 flags = 0;
PRBool restoreFlags = PR_FALSE;
if (NS_SUCCEEDED(GetFlags(&flags)) &&
(flags & nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))
{
if (NS_SUCCEEDED(SetFlags(flags & (~nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))))
restoreFlags = PR_TRUE;
}
// XXX_kin: END HACK! HACK! HACK!
nsCOMPtr<nsISelection> selection;
nsresult result = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(result)) return result;
nsCOMPtr<nsICaret> caretP;
// we need the nsAutoPlaceHolderBatch destructor called before hitting
// GetCaretCoordinates so the states in Frame system sync with content
// therefore, we put the nsAutoPlaceHolderBatch into a inner block
{
nsAutoPlaceHolderBatch batch(this, gIMETxnName);
SetIsIMEComposing(); // We set mIsIMEComposing properly.
result = InsertText(aCompositionString);
mIMEBufferLength = aCompositionString.Length();
ps->GetCaret(getter_AddRefs(caretP));
if (caretP)
caretP->SetCaretDOMSelection(selection);
// second part of 23558 fix:
if (aCompositionString.IsEmpty())
{
mIMETextNode = nsnull;
}
}
// XXX_kin: BEGIN HACK! HACK! HACK!
// XXX_kin:
// XXX_kin: Restore the previous set of flags!
if (restoreFlags)
SetFlags(flags);
// XXX_kin: END HACK! HACK! HACK!
if (caretP)
{
result = caretP->GetCaretCoordinates(nsICaret::eIMECoordinates, selection,
&(aReply->mCursorPosition), &(aReply->mCursorIsCollapsed), nsnull);
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get caret position");
}
return result;
}
NS_IMETHODIMP
nsPlaintextEditor::GetReconversionString(nsReconversionEventReply* aReply)
{
nsCOMPtr<nsISelection> selection;
nsresult res = GetSelection(getter_AddRefs(selection));
if (NS_FAILED(res)) return res;
if (!selection) return NS_ERROR_FAILURE;
// get the first range in the selection. Since it is
// unclear what to do if reconversion happens with a
// multirange selection, we will ignore any additional ranges.
nsCOMPtr<nsIDOMRange> range;
res = selection->GetRangeAt(0, getter_AddRefs(range));
if (NS_FAILED(res)) return res;
if (!range) return NS_ERROR_FAILURE;
nsAutoString textValue;
res = range->ToString(textValue);
if (NS_FAILED(res))
return res;
aReply->mReconversionString = (PRUnichar*) nsMemory::Clone(textValue.get(),
(textValue.Length() + 1) * sizeof(PRUnichar));
if (!aReply->mReconversionString)
return NS_ERROR_OUT_OF_MEMORY;
// delete the selection
return DeleteSelection(eNone);
}
#ifdef XP_MAC
#pragma mark -
#pragma mark nsEditor overrides
#pragma mark -
#endif
/** All editor operations which alter the doc should be prefaced
* with a call to StartOperation, naming the action and direction */
NS_IMETHODIMP
nsPlaintextEditor::StartOperation(PRInt32 opID, nsIEditor::EDirection aDirection)
{
nsEditor::StartOperation(opID, aDirection); // will set mAction, mDirection
if (mRules) return mRules->BeforeEdit(mAction, mDirection);
return NS_OK;
}
/** All editor operations which alter the doc should be followed
* with a call to EndOperation */
NS_IMETHODIMP
nsPlaintextEditor::EndOperation()
{
// post processing
nsresult res = NS_OK;
if (mRules) res = mRules->AfterEdit(mAction, mDirection);
nsEditor::EndOperation(); // will clear mAction, mDirection
return res;
}
NS_IMETHODIMP
nsPlaintextEditor::SelectEntireDocument(nsISelection *aSelection)
{
if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; }
// is doc empty?
PRBool bDocIsEmpty;
if (NS_SUCCEEDED(mRules->DocumentIsEmpty(&bDocIsEmpty)) && bDocIsEmpty)
{
// get body node
nsCOMPtr<nsIDOMElement>bodyElement;
nsresult res = GetRootElement(getter_AddRefs(bodyElement));
if (NS_FAILED(res)) return res;
if (!bodyElement) return NS_ERROR_FAILURE;
// if it's empty don't select entire doc - that would select the bogus node
return aSelection->Collapse(bodyElement, 0);
}
return nsEditor::SelectEntireDocument(aSelection);
}
#ifdef XP_MAC
#pragma mark -
#pragma mark Random methods
#pragma mark -
#endif
NS_IMETHODIMP nsPlaintextEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject)
{
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
if (!ps) return NS_ERROR_NOT_INITIALIZED;
nsresult result = NS_ERROR_NULL_POINTER;
if (aNode)
{ // get the content interface
nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aNode) );
if (nodeAsContent)
{ // get the frame from the content interface
//Note: frames are not ref counted, so don't use an nsCOMPtr
*aLayoutObject = nsnull;
result = ps->GetLayoutObjectFor(nodeAsContent, aLayoutObject);
}
}
return result;
}
#ifdef XP_MAC
#pragma mark -
#endif
void nsPlaintextEditor::HandleEventListenerError()
{
// null out the nsCOMPtrs
mKeyListenerP = nsnull;
mMouseListenerP = nsnull;
mTextListenerP = nsnull;
mDragListenerP = nsnull;
mCompositionListenerP = nsnull;
mFocusListenerP = nsnull;
}
#ifdef XP_MAC
#pragma mark -
#endif
nsresult
nsPlaintextEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
const nsAString & aAttribute,
const nsAString & aValue,
PRBool aSuppressTransaction)
{
return nsEditor::SetAttribute(aElement, aAttribute, aValue);
}
nsresult
nsPlaintextEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
const nsAString & aAttribute,
PRBool aSuppressTransaction)
{
return nsEditor::RemoveAttribute(aElement, aAttribute);
}
|