DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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
<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!DOCTYPE bindings [
<!ENTITY % findBarDTD SYSTEM "chrome://global/locale/findbar.dtd" >
%findBarDTD;
]>

<bindings id="findbarBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
   xmlns:xbl="http://www.mozilla.org/xbl">

  <!-- Private binding -->
  <binding id="findbar-textbox"
           extends="chrome://global/content/bindings/textbox.xml#textbox">
    <implementation>

      <field name="_findbar">null</field>
      <property name="findbar" readonly="true">
        <getter>
          return this._findbar ?
                 this._findbar : this._findbar = document.getBindingParent(this);
        </getter>
      </property>

      <method name="_handleEnter">
        <parameter name="aEvent"/>
        <body><![CDATA[
          if (this.findbar._findMode == this.findbar.FIND_NORMAL) {
            var findString = this.findbar._findField;
            if (!findString.value)
              return;
#ifdef XP_MACOSX
            if (aEvent.metaKey) {
#else
            if (aEvent.ctrlKey) {
#endif
              this.findbar.getElement("highlight").click();
              return;
            }

            this.findbar.onFindAgainCommand(aEvent.shiftKey);
          }
          else {
            // We need to keep a reference to _foundLink because
            // _finishFAYT resets it to null.
            var tmpLink = this._findbar._foundLink;
            if (tmpLink && this.findbar._finishFAYT(aEvent))
              this.findbar._dispatchKeypressEvent(tmpLink, aEvent);
          }
        ]]></body>
      </method>

      <method name="_handleTab">
        <parameter name="aEvent"/>
        <body><![CDATA[
          var shouldHandle = !aEvent.altKey && !aEvent.ctrlKey &&
                             !aEvent.metaKey;
          if (shouldHandle &&
              this.findbar._findMode != this.findbar.FIND_NORMAL &&
              this.findbar._finishFAYT(aEvent)) {
            if (aEvent.shiftKey)
              document.commandDispatcher.rewindFocus();
            else
              document.commandDispatcher.advanceFocus();
          }
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="input"><![CDATA[
        // We should do nothing during composition.  E.g., composing string
        // before converting may matches a forward word of expected word.
        // After that, even if user converts the composition string to the
        // expected word, it may find second or later searching word in the
        // document.
        if (this.findbar._isIMEComposing) {
          return;
        }
        this.findbar._find(this.value);
      ]]></handler>

      <handler event="keypress"><![CDATA[
        var win = this.findbar._currentWindow ||
                  this.findbar.browser.contentWindow;
        var controller = this.findbar._getSelectionController(win);

        var shouldHandle = !event.altKey && !event.ctrlKey &&
                           !event.metaKey && !event.shiftKey;

        switch (event.keyCode) {
          case KeyEvent.DOM_VK_RETURN:
            this._handleEnter(event);
            break;
          case KeyEvent.DOM_VK_TAB:
            this._handleTab(event);
            break;
          case KeyEvent.DOM_VK_PAGE_UP:
            if (shouldHandle) {
              controller.scrollPage(false);
              event.preventDefault();
            }
            break;
          case KeyEvent.DOM_VK_PAGE_DOWN:
            if (shouldHandle) {
              controller.scrollPage(true);
              event.preventDefault();
            }
            break;
          case KeyEvent.DOM_VK_UP:
            controller.scrollLine(false);
            event.preventDefault();
            break;
          case KeyEvent.DOM_VK_DOWN:
            controller.scrollLine(true);
            event.preventDefault();
            break;
        }
      ]]></handler>

      <handler event="blur"><![CDATA[
        var findbar = this.findbar;
        var fastFind = findbar.browser.fastFind;
        if (findbar._foundEditable)
          fastFind.collapseSelection();
        else {
          fastFind.setSelectionModeAndRepaint
            (findbar.nsISelectionController.SELECTION_ON);
        }
        findbar._setFoundLink(null);
        findbar._foundEditable = null;
        findbar._currentWindow = null;
      ]]></handler>

      <handler event="compositionstart"><![CDATA[
        // Don't close the find toolbar while IME is composing.
        var findbar = this.findbar;
        findbar._isIMEComposing = true;
        if (findbar._quickFindTimeout) {
          clearTimeout(findbar._quickFindTimeout);
          findbar._quickFindTimeout = null;
        }
      ]]></handler>

      <handler event="compositionend"><![CDATA[
        var findbar = this.findbar;
        findbar._isIMEComposing = false;
        if (findbar._findMode != findbar.FIND_NORMAL &&
            !findbar.hidden)
          findbar._setFindCloseTimeout();
      ]]></handler>

      <handler event="dragover"><![CDATA[
        if (event.dataTransfer.types.contains("text/plain"))
          event.preventDefault();
      ]]></handler>

      <handler event="drop"><![CDATA[
        var value = event.dataTransfer.getData("text/plain");
        this.value = value;
        this.findbar._find(value);
        event.stopPropagation();
        event.preventDefault();
      ]]></handler>
    </handlers>
  </binding>

  <binding id="findbar"
           extends="chrome://global/content/bindings/toolbar.xml#toolbar">
    <resources>
      <stylesheet src="chrome://global/skin/findBar.css"/>
    </resources>

    <content hidden="true">
    <xul:hbox anonid="findbar-container" class="findbar-container" flex="1" align="center">
      <xul:toolbarbutton anonid="find-closebutton"
                         class="findbar-closebutton"
                         tooltiptext="&findCloseButton.tooltip;"
                         oncommand="close();"/>
      <xul:label anonid="find-label" class="findbar-find-fast" control="findbar-textbox"/>
      <xul:textbox class="findbar-textbox findbar-find-fast" anonid="findbar-textbox"
                   xbl:inherits="flash"/>
#ifdef MOZ_WIDGET_GTK
      <xul:toolbarbutton anonid="find-previous"
                         class="findbar-find-previous tabbable"
                         label="&previous.label;"
                         accesskey="&previous.accesskey;"
                         tooltiptext="&previous.tooltip;"
                         oncommand="onFindAgainCommand(true);"
                         disabled="true"
                         xbl:inherits="accesskey=findpreviousaccesskey"/>
#endif
      <xul:toolbarbutton anonid="find-next"
                         class="findbar-find-next tabbable"
                         label="&next.label;"
                         accesskey="&next.accesskey;"
                         tooltiptext="&next.tooltip;"
                         oncommand="onFindAgainCommand(false);"
                         disabled="true"
                         xbl:inherits="accesskey=findnextaccesskey"/>
#ifndef MOZ_WIDGET_GTK
      <xul:toolbarbutton anonid="find-previous"
                         class="findbar-find-previous tabbable"
                         label="&previous.label;"
                         accesskey="&previous.accesskey;"
                         tooltiptext="&previous.tooltip;"
                         oncommand="onFindAgainCommand(true);"
                         disabled="true"
                         xbl:inherits="accesskey=findpreviousaccesskey"/>
#endif
      <xul:toolbarbutton anonid="highlight"
                         class="findbar-highlight tabbable"
                         label="&highlight.label;"
                         accesskey="&highlight.accesskey;"
                         tooltiptext="&highlight.tooltiptext;"
                         oncommand="toggleHighlight(this.checked);"
                         type="checkbox"
                         disabled="true"
                         xbl:inherits="accesskey=highlightaccesskey"/>
      <xul:checkbox anonid="find-case-sensitive"
                    oncommand="_setCaseSensitivity(this.checked);"
                    label="&caseSensitiveCheckbox.label;"
                    accesskey="&caseSensitiveCheckbox.accesskey;"
                    xbl:inherits="accesskey=matchcaseaccesskey"/>
      <xul:label anonid="match-case-status" class="findbar-find-fast"/>
      <xul:image anonid="find-status-icon" class="findbar-find-fast find-status-icon"/>
      <xul:description anonid="find-status" class="findbar-find-fast findbar-find-status"
                       control="findbar-textbox">
      <!-- Do not use value, first child is used because it provides a11y with text change events -->
      </xul:description>
    </xul:hbox>
    </content>

    <implementation implements="nsIDOMEventListener, nsIEditActionListener">
      <field name="FIND_NORMAL">0</field>
      <field name="FIND_TYPEAHEAD">1</field>
      <field name="FIND_LINKS">2</field>

      <field name="_findMode">0</field>
      <field name="_tmpOutline">null</field>
      <field name="_tmpOutlineOffset">"0"</field>
      <field name="_drawOutline">false</field>
      <field name="_editors">null</field>
      <field name="_stateListeners">null</field>

      <field name="_flashFindBar">0</field>
      <field name="_initialFlashFindBarCount">6</field>

      <property name="_foundLink"
                onget="return this._foundLinkRef.get();"
                onset="this._foundLinkRef = Components.utils.getWeakReference(val); return val;"/>
      <property name="_foundEditable"
                onget="return this._foundEditableRef.get();"
                onset="this._foundEditableRef = Components.utils.getWeakReference(val); return val;"/>
      <property name="_currentWindow"
                onget="return this._currentWindowRef.get();"
                onset="this._currentWindowRef = Components.utils.getWeakReference(val); return val;"/>

      <property name="prefillWithSelection"
                onget="return this.getAttribute('prefillwithselection') != 'false'"
                onset="this.setAttribute('prefillwithselection', val); return val;"/>
      <field name="_selectionMaxLen">150</field>

      <method name="getElement">
        <parameter name="aAnonymousID"/>
        <body><![CDATA[
          return document.getAnonymousElementByAttribute(this,
                                                         "anonid",
                                                         aAnonymousID)
        ]]></body>
      </method>

      <property name="findMode"
                readonly="true"
                onget="return this._findMode;"/>

      <property name="canClear" readonly="true">
        <getter><![CDATA[
          if (this._findField.value)
            return true;

          // Watch out for lazy editor init
          if (this._findField.editor) {
            let tm = this._findField.editor.transactionManager;
            return !!(tm.numberOfUndoItems || tm.numberOfRedoItems);
          }
          return false;
        ]]></getter>
      </property>

      <field name="_browser">null</field>
      <property name="browser">
        <getter><![CDATA[
          if (!this._browser) {
            this._browser =
              document.getElementById(this.getAttribute("browserid"));
          }
          return this._browser;
        ]]></getter>
        <setter><![CDATA[
          if (this._browser) {
            this._browser.removeEventListener("keypress", this, false);
            this._browser.removeEventListener("mouseup", this, false);
            this._foundLink = null;
            this._foundEditable = null;
            this._currentWindow = null;
          }

          this._browser = val;
          if (this._browser) {
            this._browser.addEventListener("keypress", this, false);
            this._browser.addEventListener("mouseup", this, false);
            this._findField.value = this._browser.fastFind.searchString;
          }
          return val;
        ]]></setter>
      </property>

      <field name="_observer"><![CDATA[({
        _self: this,

        QueryInterface: function(aIID) {
          if (aIID.equals(Components.interfaces.nsIObserver) ||
              aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
              aIID.equals(Components.interfaces.nsISupports))
            return this;

          throw Components.results.NS_ERROR_NO_INTERFACE;
        },

        observe: function(aSubject, aTopic, aPrefName) {
          if (aTopic != "nsPref:changed")
            return;

          var prefsvc =
            aSubject.QueryInterface(Components.interfaces.nsIPrefBranch);

          switch (aPrefName) {
            case "accessibility.typeaheadfind":
              this._self._useTypeAheadFind = prefsvc.getBoolPref(aPrefName);
              break;
            case "accessibility.typeaheadfind.linksonly":
              this._self._typeAheadLinksOnly = prefsvc.getBoolPref(aPrefName);
              break;
            case "accessibility.typeaheadfind.casesensitive":
              this._self._typeAheadCaseSensitive = prefsvc.getIntPref(aPrefName);
              this._self._updateCaseSensitivity();
              if (this._self.getElement("highlight").checked)
                this._self._setHighlightTimeout();
              break;
          }
        }
      })]]></field>

      <field name="_destroyed">false</field>

      <constructor><![CDATA[
        // These elements are accessed frequently and are therefore cached
        this._findField = this.getElement("findbar-textbox");
        this._findStatusIcon = this.getElement("find-status-icon");
        this._findStatusDesc = this.getElement("find-status");

        this._foundLink = null;
        this._foundEditable = null;
        this._currentWindow = null;

        var prefsvc =
          Components.classes["@mozilla.org/preferences-service;1"]
                    .getService(Components.interfaces.nsIPrefBranch);

        this._quickFindTimeoutLength =
          prefsvc.getIntPref("accessibility.typeaheadfind.timeout");
        this._flashFindBar =
          prefsvc.getIntPref("accessibility.typeaheadfind.flashBar");

        prefsvc.addObserver("accessibility.typeaheadfind",
                            this._observer, false);
        prefsvc.addObserver("accessibility.typeaheadfind.linksonly",
                            this._observer, false);
        prefsvc.addObserver("accessibility.typeaheadfind.casesensitive",
                            this._observer, false);

        this._useTypeAheadFind =
          prefsvc.getBoolPref("accessibility.typeaheadfind");
        this._typeAheadLinksOnly =
          prefsvc.getBoolPref("accessibility.typeaheadfind.linksonly");
        this._typeAheadCaseSensitive =
          prefsvc.getIntPref("accessibility.typeaheadfind.casesensitive");

        // Convenience
        this.nsITypeAheadFind = Components.interfaces.nsITypeAheadFind;
        this.nsISelectionController = Components.interfaces.nsISelectionController;
        this._findSelection = this.nsISelectionController.SELECTION_FIND;

        this._findResetTimeout = -1;

        // Make sure the FAYT keypress listener is attached by initializing the
        // browser property
        if (this.getAttribute("browserid"))
          setTimeout(function(aSelf) { aSelf.browser = aSelf.browser; }, 0, this);
      ]]></constructor>

      <destructor><![CDATA[
        this.destroy();
      ]]></destructor>

      <!-- This is necessary because the destructor isn't called when
           we are removed from a document that is not destroyed. This
           needs to be explicitly called in this case -->
      <method name="destroy">
        <body><![CDATA[
          if (this._destroyed)
            return;
          this._destroyed = true;

          // It is possible that the findbar may be destroyed before any
          // documents it is listening to (see nsIEditActionListener code below).
          // Thus, to avoid leaking, if we are listening to any editors, unhook
          // ourselves now, and remove our cached copies
          if (this._editors) {
            for (var x = this._editors.length - 1; x >= 0; --x)
              this._unhookListenersAtIndex(x);
          }

          this.browser = null;

          var prefsvc =
            Components.classes["@mozilla.org/preferences-service;1"]
                      .getService(Components.interfaces.nsIPrefBranch);
          prefsvc.removeObserver("accessibility.typeaheadfind",
                                 this._observer);
          prefsvc.removeObserver("accessibility.typeaheadfind.linksonly",
                                 this._observer);
          prefsvc.removeObserver("accessibility.typeaheadfind.casesensitive",
                                 this._observer);
        ]]></body>
      </method>

      <method name="_setFindCloseTimeout">
        <body><![CDATA[
          if (this._quickFindTimeout)
            clearTimeout(this._quickFindTimeout);

          // Don't close the find toolbar while IME is composing.
          if (this._isIMEComposing) {
            this._quickFindTimeout = null;
            return;
          }

          this._quickFindTimeout =
            setTimeout(function(aSelf) {
                         if (aSelf._findMode != aSelf.FIND_NORMAL)
                           aSelf.close();
                       }, this._quickFindTimeoutLength, this);
        ]]></body>
      </method>

      <!--
        - Gets the selection controller for the current browser
        -->
      <method name="_getSelectionController">
        <parameter name="aWindow"/>
        <body><![CDATA[
          // display: none iframes don't have a selection controller, see bug 493658
          if (!aWindow.innerWidth || !aWindow.innerHeight)
            return null;

          // Yuck. See bug 138068.
          var Ci = Components.interfaces;
          var docShell = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
                                .getInterface(Ci.nsIWebNavigation)
                                .QueryInterface(Ci.nsIDocShell);

          var controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                                   .getInterface(Ci.nsISelectionDisplay)
                                   .QueryInterface(Ci.nsISelectionController);
          return controller;
        ]]></body>
      </method>

      <!--
        - For a given node, walk up it's parent chain, to try and find an
        - editable node.
        -
        - @param aNode the node we want to check
        - @returns the first node in the parent chain that is editable,
        -          null if there is no such node
        -->
      <method name="_getEditableNode">
        <parameter name="aNode"/>
        <body><![CDATA[
          while (aNode) {
            if (aNode instanceof Components.interfaces.nsIDOMNSEditableElement) {
              return aNode.editor ? aNode : null;
            }
            aNode = aNode.parentNode;
          }
          return null;
        ]]></body>
      </method>

      <!--
        - Helper method to unhook listeners, remove cached editors
        - and keep the relevant arrays in sync
        -
        - @param aIndex the index into the array of editors/state listeners
        -        we wish to remove
        -->
      <method name="_unhookListenersAtIndex">
        <parameter name="aIndex"/>
        <body><![CDATA[
          this._editors[aIndex].removeEditActionListener(this);
          this._editors[aIndex]
              .removeDocumentStateListener(this._stateListeners[aIndex]);
          this._editors.splice(aIndex, 1);
          this._stateListeners.splice(aIndex, 1);
          if (this._editors.length == 0) {
            delete this._editors;
            delete this._stateListeners;
          }
        ]]></body>
      </method>

      <!--
        - Remove ourselves as an nsIEditActionListener and
        - nsIDocumentStateListener from a given cached editor
        -
        - @param aEditor the editor we no longer wish to listen to
        -->
      <method name="_removeEditorListeners">
        <parameter name="aEditor"/>
        <body><![CDATA[
          // aEditor is an editor that we listen to, so therefore must be
          // cached. Find the index of this editor
          var idx = 0;
          while (this._editors[idx] != aEditor)
            idx++;

          // Now unhook ourselves, and remove our cached copy
          this._unhookListenersAtIndex(idx);
        ]]></body>
      </method>

      <!--
        - nsIEditActionListener logic follows
        -
        - We implement this interface to allow us to catch the case where
        - the findbar found a match in a HTML <input> or <textarea>. If the
        - user adjusts the text in some way, it will no longer match, so we
        - want to remove the highlight, rather than have it expand/contract
        - when letters are added or removed.
        -->

      <!--
        - Helper method used to check whether a selection intersects with
        - some highlighting
        -
        - @param aSelectionRange the range from the selection to check
        - @param aFindRange the highlighted range to check against
        - @returns true if they intersect, false otherwise
        -->
      <method name="_checkOverlap">
        <parameter name="aSelectionRange"/>
        <parameter name="aFindRange"/>
        <body><![CDATA[
          // The ranges overlap if one of the following is true:
          // 1) At least one of the endpoints of the deleted selection
          //    is in the find selection
          // 2) At least one of the endpoints of the find selection
          //    is in the deleted selection
          if (aFindRange.isPointInRange(aSelectionRange.startContainer,
                                        aSelectionRange.startOffset))
            return true;
          if (aFindRange.isPointInRange(aSelectionRange.endContainer,
                                        aSelectionRange.endOffset))
            return true;
          if (aSelectionRange.isPointInRange(aFindRange.startContainer,
                                             aFindRange.startOffset))
            return true;
          if (aSelectionRange.isPointInRange(aFindRange.endContainer,
                                             aFindRange.endOffset))
            return true;

          return false;
        ]]></body>
      </method>

      <!--
        - Helper method to determine if an edit occurred within a highlight
        -
        - @param aSelection the selection we wish to check
        - @param aNode the node we want to check is contained in aSelection
        - @param aOffset the offset into aNode that we want to check
        - @returns the range containing (aNode, aOffset) or null if no ranges
        -          in the selection contain it
        -->
      <method name="_findRange">
        <parameter name="aSelection"/>
        <parameter name="aNode"/>
        <parameter name="aOffset"/>
        <body><![CDATA[
          var rangeCount = aSelection.rangeCount;
          var rangeidx = 0;
          var foundContainingRange = false;
          var range = null;

          // Check to see if this node is inside one of the selection's ranges
          while (!foundContainingRange && rangeidx < rangeCount) {
            range = aSelection.getRangeAt(rangeidx);
            if (range.isPointInRange(aNode, aOffset)) {
              foundContainingRange = true;
              break;
            }
            rangeidx++;
          }

          if (foundContainingRange)
            return range;

          return null;
        ]]></body>
      </method>

      <!-- Start of nsIEditActionListener implementations -->

      <method name="WillDeleteText">
        <parameter name="aTextNode"/>
        <parameter name="aOffset"/>
        <parameter name="aLength"/>
        <body><![CDATA[
          var editor = this._getEditableNode(aTextNode).editor;
          var controller = editor.selectionController;
          var fSelection = controller.getSelection(this._findSelection);
          var range = this._findRange(fSelection, aTextNode, aOffset);

          if (range) {
            // Don't remove the highlighting if the deleted text is at the
            // end of the range
            if (aTextNode != range.endContainer ||
                aOffset != range.endOffset) {
              // Text within the highlight is being removed - the text can
              // no longer be a match, so remove the highlighting
              fSelection.removeRange(range);
              if (fSelection.rangeCount == 0)
                this._removeEditorListeners(editor);
            }
          }
        ]]></body>
      </method>

      <method name="DidInsertText">
        <parameter name="aTextNode"/>
        <parameter name="aOffset"/>
        <parameter name="aString"/>
        <body><![CDATA[
          var editor = this._getEditableNode(aTextNode).editor;
          var controller = editor.selectionController;
          var fSelection = controller.getSelection(this._findSelection);
          var range = this._findRange(fSelection, aTextNode, aOffset);

          if (range) {
            // If the text was inserted before the highlight
            // adjust the highlight's bounds accordingly
            if (aTextNode == range.startContainer &&
                       aOffset == range.startOffset)
              range.setStart(range.startContainer,
                             range.startOffset+aString.length);
            else if (aTextNode != range.endContainer ||
                     aOffset != range.endOffset) {
              // The edit occurred within the highlight - any addition of text
              // will result in the text no longer being a match,
              // so remove the highlighting
              fSelection.removeRange(range);
              if (fSelection.rangeCount == 0)
                this._removeEditorListeners(editor);
            }
          }
        ]]></body>
      </method>

      <method name="WillDeleteSelection">
        <parameter name="aSelection"/>
        <body><![CDATA[
          var editor = this._getEditableNode(aSelection.getRangeAt(0)
                                                       .startContainer).editor;
          var controller = editor.selectionController;
          var fSelection = controller.getSelection(this._findSelection);

          var selectionIndex = 0;
          var findSelectionIndex = 0;
          var shouldDelete = {};
          var numberOfDeletedSelections = 0;
          var numberOfMatches = fSelection.rangeCount;

          // We need to test if any ranges in the deleted selection (aSelection)
          // are in any of the ranges of the find selection
          // Usually both selections will only contain one range, however
          // either may contain more than one.

          for (var fIndex = 0; fIndex < numberOfMatches; fIndex++) {
            shouldDelete[fIndex] = false;
            var fRange = fSelection.getRangeAt(fIndex);

            for (var index = 0; index < aSelection.rangeCount; index++) {
              if (!shouldDelete[fIndex]) {
                var selRange = aSelection.getRangeAt(index);
                var doesOverlap = this._checkOverlap(selRange, fRange);
                if (doesOverlap) {
                  shouldDelete[fIndex] = true;
                  numberOfDeletedSelections++;
                }
              }
            }
          }

          // OK, so now we know what matches (if any) are in the selection
          // that is being deleted. Time to remove them.
          if (numberOfDeletedSelections == 0)
            return;

          for (var i = numberOfMatches - 1; i >= 0; i--) {
            if (shouldDelete[i]) {
              var r = fSelection.getRangeAt(i);
              fSelection.removeRange(r);
            }
          }

          // Remove listeners if no more highlights left
          if (fSelection.rangeCount == 0)
            this._removeEditorListeners(editor);
        ]]></body>
      </method>

      <method name="WillInsertText">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidCreateNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidDeleteNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidDeleteSelection">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidDeleteText">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidInsertNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidJoinNodes">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="DidSplitNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="WillCreateNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="WillDeleteNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="WillInsertNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="WillJoinNodes">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <method name="WillSplitNode">
        <body><![CDATA[
          // Unimplemented
        ]]></body>
      </method>

      <!-- End of nsIEditActionListener implementations -->

      <!--
        - nsIDocumentStateListener logic follows
        -
        - When attaching nsIEditActionListeners, there are no guarantees
        - as to whether the findbar or the documents in the browser will get
        - destructed first. This leads to the potential to either leak, or to
        - hold on to a reference an editable element's editor for too long,
        - preventing it from being destructed.
        -
        - However, when an editor's owning node is being destroyed, the editor
        - sends out a DocumentWillBeDestroyed notification. We can use this to
        - clean up our references to the object, to allow it to be destroyed in a
        - timely fashion.
        -->

      <!--
        - Unhook ourselves when one of our state listeners has been called.
        - This can happen in 4 cases:
        -  1) The document the editor belongs to is navigated away from, and
        -     the document is not being cached
        -
        -  2) The document the editor belongs to is expired from the cache
        -
        -  3) The tab containing the owning document is closed
        -
        -  4) The <input> or <textarea> that owns the editor is explicitly
        -     removed from the DOM
        -
        - @param the listener that was invoked
        -->
      <method name="_onEditorDestruction">
        <parameter name="aListener"/>
        <body><![CDATA[
          // First find the index of the editor the given listener listens to.
          // The listeners and editors arrays must always be in sync.
          // The listener will be in our array of cached listeners, as this
          // method could not have been called otherwise.
          var idx = 0;
          while (this._stateListeners[idx] != aListener)
            idx++;

          // Unhook both listeners
          this._unhookListenersAtIndex(idx);
        ]]></body>
      </method>

      <!--
        - Creates a unique document state listener for an editor.
        -
        - It is not possible to simply have the findbar implement the
        - listener interface itself, as it wouldn't have sufficient information
        - to work out which editor was being destroyed. Therefore, we create new
        - listeners on the fly, and cache them in sync with the editors they
        - listen to.
        -->
      <method name="_createStateListener">
        <body><![CDATA[
          return ({
            findbar: this,

            QueryInterface: function(aIID) {
              if (aIID.equals(Components.interfaces.nsIDocumentStateListener) ||
                  aIID.equals(Components.interfaces.nsISupports))
                return this;

              throw Components.results.NS_ERROR_NO_INTERFACE;
            },

            NotifyDocumentWillBeDestroyed: function() {
              this.findbar._onEditorDestruction(this);
            },

            // Unimplemented
            notifyDocumentCreated: function() {},
            notifyDocumentStateChanged: function(aDirty) {}
          });
        ]]></body>
      </method>

      <!--
        - Turns highlight on or off.
        - @param aHighlight (boolean)
        -        Whether to turn the highlight on or off
        -->
      <method name="toggleHighlight">
        <parameter name="aHighlight"/>
        <body><![CDATA[
          if (!this._dispatchFindEvent("highlightallchange"))
            return;

          var word = this._findField.value;

          // Bug 429723. Don't attempt to highlight ""
          if (aHighlight && !word)
            return;

          // We have to update the status because we might still have the status
          // of another tab
          if (this._highlightDoc(aHighlight, word))
            this._updateStatusUI(this.nsITypeAheadFind.FIND_FOUND);
          else
            this._updateStatusUI(this.nsITypeAheadFind.FIND_NOTFOUND);
        ]]></body>
      </method>

      <!--
        - (Un)highlights each instance of the searched word in the passed
        - window's content.
        - @param aHighlight (boolean)
        -        Whether to turn on highlight
        - @param aWord
        -        the word to search for
        - @param aWindow
        -        the window to search in. Passing undefined will search the
        -        current content window.
        - @returns true if aWord was found
        -->
      <method name="_highlightDoc">
        <parameter name="aHighlight"/>
        <parameter name="aWord"/>
        <parameter name="aWindow"/>
        <body><![CDATA[
          var win = aWindow || this.browser.contentWindow;

          var textFound = false;

          for (var i = 0; win.frames && i < win.frames.length; i++) {
            if (this._highlightDoc(aHighlight, aWord, win.frames[i]))
              textFound = true;
          }

          var controller = this._getSelectionController(win);
          if (!controller) {
            // Without the selection controller,
            // we are unable to (un)highlight any matches
            return textFound;
          }

          var doc = win.document;
          if (!doc || !doc.documentElement)
           return textFound;

          var body = (doc instanceof HTMLDocument && doc.body) ?
                     doc.body : doc.documentElement;

          if (aHighlight) {
            var searchRange = doc.createRange();
             searchRange.selectNodeContents(body);

            var startPt = searchRange.cloneRange();
            startPt.collapse(true);

            var endPt = searchRange.cloneRange();
            endPt.collapse(false);

            var retRange = null;
            var finder = Components.classes["@mozilla.org/embedcomp/rangefind;1"]
                                   .createInstance()
                                   .QueryInterface(Components.interfaces.nsIFind);

            finder.caseSensitive = this._shouldBeCaseSensitive(aWord);

            while ((retRange = finder.Find(aWord, searchRange,
                                           startPt, endPt))) {
              this._highlight(retRange, controller);
              startPt = retRange.cloneRange();
              startPt.collapse(false);

              textFound = true;
            }
          } else {
            // First, attempt to remove highlighting from main document
            var sel = controller.getSelection(this._findSelection);
            sel.removeAllRanges();

            // Next, check our editor cache, for editors belonging to this
            // document
            if (this._editors) {
              for (var x = this._editors.length - 1; x >= 0; --x) {
                if (this._editors[x].document == doc) {
                  sel = this._editors[x].selectionController
                                        .getSelection(this._findSelection);
                  sel.removeAllRanges();
                  // We don't need to listen to this editor any more
                  this._unhookListenersAtIndex(x);
                }
              }
            }
            return true;
          }

          return textFound;
        ]]></body>
      </method>

     <!--
       - Highlights the word in the passed range.
       -
       - @param aRange
       -        the range that contains the word to highlight
       - @param aController
       -        the current document's selection controller
       -->
      <method name="_highlight">
        <parameter name="aRange"/>
        <parameter name="aController"/>
        <body><![CDATA[
          var node = aRange.startContainer;
          var controller = aController;
          var editableNode = this._getEditableNode(node);
          if (editableNode)
            controller = editableNode.editor.selectionController;

          var findSelection = controller.getSelection(this._findSelection);
          findSelection.addRange(aRange);

          if (editableNode) {
            // Highlighting added, so cache this editor, and hook up listeners
            // to ensure we deal properly with edits within the highlighting
            if (!this._editors) {
              this._editors = [];
              this._stateListeners = [];
            }

            var existingIndex = this._editors.indexOf(editableNode.editor);
            if (existingIndex == -1) {
              var x = this._editors.length;
              this._editors[x] = editableNode.editor;
              this._stateListeners[x] = this._createStateListener();
              this._editors[x].addEditActionListener(this);
              this._editors[x].addDocumentStateListener(this._stateListeners[x]);
            }
          }
        ]]></body>
      </method>

      <!--
        - Updates the case-sensitivity mode of the findbar and its UI.
        - @param [optional] aString
        -        The string for which case sensitivity might be turned on.
        -        This only used when case-sensitivity is in auto mode,
        -        @see _shouldBeCaseSensitive. The default value for this
        -        parameter is the find-field value.
        -->
      <method name="_updateCaseSensitivity">
        <parameter name="aString"/>
        <body><![CDATA[
          var val = aString || this._findField.value;

          var caseSensitive = this._shouldBeCaseSensitive(val);
          var checkbox = this.getElement("find-case-sensitive");
          var statusLabel = this.getElement("match-case-status");
          checkbox.checked = caseSensitive;

          statusLabel.value = caseSensitive ? this._caseSensitiveStr : "";

          // Show the checkbox on the full Find bar in non-auto mode.
          // Show the label in all other cases.
          var hideCheckbox = this._findMode != this.FIND_NORMAL ||
            (this._typeAheadCaseSensitive != 0 &&
             this._typeAheadCaseSensitive != 1);
          checkbox.hidden = hideCheckbox;
          statusLabel.hidden = !hideCheckbox;

          var fastFind = this.browser.fastFind;
          fastFind.caseSensitive = caseSensitive;
        ]]></body>
      </method>

      <!--
        - Sets the findbar case-sensitivity mode
        - @param aCaseSensitive (boolean)
        -        Whether or not case-sensitivity should be turned on.
        -->
      <method name="_setCaseSensitivity">
        <parameter name="aCaseSensitive"/>
        <body><![CDATA[
          var prefsvc =
            Components.classes["@mozilla.org/preferences-service;1"]
                      .getService(Components.interfaces.nsIPrefBranch);

          // Just set the pref; our observer will change the find bar behavior
          prefsvc.setIntPref("accessibility.typeaheadfind.casesensitive",
                             aCaseSensitive ? 1 : 0);

          this._dispatchFindEvent("casesensitivitychange");
        ]]></body>
      </method>

      <!--
        - Opens and displays the find bar.
        -
        - @param aMode
        -        the find mode to be used, which is either FIND_NORMAL,
        -        FIND_TYPEAHEAD or FIND_LINKS. If not passed, the last
        -        find mode if any or FIND_NORMAL.
        - @returns true if the find bar wasn't previously open, false otherwise.
        -->
      <method name="open">
        <parameter name="aMode"/>
        <body><![CDATA[
          if (aMode != undefined)
            this._findMode = aMode;

          if (!this._notFoundStr) {
            var stringsBundle =
              Components.classes["@mozilla.org/intl/stringbundle;1"]
                        .getService(Components.interfaces.nsIStringBundleService)
                        .createBundle("chrome://global/locale/findbar.properties");
            this._notFoundStr = stringsBundle.GetStringFromName("NotFound");
            this._wrappedToTopStr =
              stringsBundle.GetStringFromName("WrappedToTop");
            this._wrappedToBottomStr =
              stringsBundle.GetStringFromName("WrappedToBottom");
            this._normalFindStr =
              stringsBundle.GetStringFromName("NormalFindLabel");
            this._fastFindStr =
              stringsBundle.GetStringFromName("FastFindLabel");
            this._fastFindLinksStr =
              stringsBundle.GetStringFromName("FastFindLinksLabel");
            this._caseSensitiveStr =
              stringsBundle.GetStringFromName("CaseSensitive");
          }

          this._findFailedString = null;

          this._updateFindUI();
          if (this.hidden) {
            this.hidden = false;

            this._updateStatusUI(this.nsITypeAheadFind.FIND_FOUND);

            let event = document.createEvent("Events");
            event.initEvent("findbaropen", true, false);
            this.dispatchEvent(event);

            return true;
          }
          return false;
        ]]></body>
      </method>

      <!--
        - Closes the findbar.
        -->
      <method name="close">
        <body><![CDATA[
          if (this.hidden)
            return;

          var fm =
            Components.classes["@mozilla.org/focus-manager;1"]
                      .getService(Components.interfaces.nsIFocusManager);
          if (window == fm.focusedWindow) {
            var focusedElement = fm.focusedElement;
            if (focusedElement) {
              var bindingParent = document.getBindingParent(focusedElement);
              if (bindingParent == this || bindingParent == this._findField) {
                // block scrolling on focus since find already scrolls, further
                // scrolling is due to user action, so don't override this
                var element = this._foundLink || this._foundEditable;
                if (element)
                  fm.setFocus(element, fm.FLAG_NOSCROLL);
                else if (this._currentWindow)
                    this._currentWindow.focus();
                else
                  this.browser.contentWindow.focus();
              }
            }
          }

          this.hidden = true;
          var fastFind = this.browser.fastFind;
          fastFind.setSelectionModeAndRepaint
            (this.nsISelectionController.SELECTION_ON);
          this._setFoundLink(null);
          this._foundEditable = null;
          this._currentWindow = null;
          if (this._quickFindTimeout) {
            clearTimeout(this._quickFindTimeout);
            this._quickFindTimeout = null;
          }

          this._findFailedString = null;
        ]]></body>
      </method>

      <method name="clear">
        <body><![CDATA[
          this.browser.fastFind.collapseSelection();
          this._findField.reset();
          this.toggleHighlight(false);
          this._updateStatusUI();
          this._enableFindButtons(false);
          this._setFoundLink(null);
          this._foundEditable = null;
          this._currentWindow = null;
        ]]></body>
      </method>

      <method name="_dispatchKeypressEvent">
        <parameter name="aTarget"/>
        <parameter name="aEvent"/>
        <body><![CDATA[
          if (!aTarget)
            return;

          var event = document.createEvent("KeyEvents");
          event.initKeyEvent(aEvent.type, aEvent.bubbles, aEvent.cancelable,
                             aEvent.view, aEvent.ctrlKey, aEvent.altKey,
                             aEvent.shiftKey, aEvent.metaKey, aEvent.keyCode,
                             aEvent.charCode);
          aTarget.dispatchEvent(event);
        ]]></body>
      </method>

      <field name="_xulBrowserWindow">null</field>
      <method name="_updateStatusUIBar">
        <body><![CDATA[
          if (!this._xulBrowserWindow) {
            try {
              this._xulBrowserWindow =
                window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                      .getInterface(Components.interfaces.nsIWebNavigation)
                      .QueryInterface(Components.interfaces.nsIDocShellTreeItem)
                      .treeOwner
                      .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                      .getInterface(Components.interfaces.nsIXULWindow)
                      .XULBrowserWindow;
            }
            catch(ex) { }
            if (!this._xulBrowserWindow)
              return false;
          }

          if (!this._foundLink || !this._foundLink.href ||
              this._foundLink.href == "") {
            this._xulBrowserWindow.setOverLink("", null);
            return true;
          }

          var docCharset = "";
          var ownerDoc = this._foundLink.ownerDocument;
          if (ownerDoc)
            docCharset = ownerDoc.characterSet;

          if (!this._textToSubURIService) {
            this._textToSubURIService =
              Components.classes["@mozilla.org/intl/texttosuburi;1"]
                        .getService(Components.interfaces.nsITextToSubURI);
          }
          var url =
            this._textToSubURIService.unEscapeURIForUI(docCharset,
                                                       this._foundLink.href);
          this._xulBrowserWindow.setOverLink(url, null);

          return true;
        ]]></body>
      </method>

      <method name="_setFoundLink">
        <parameter name="aFoundLink"/>
        <body><![CDATA[
          if (this._foundLink == aFoundLink)
            return;

          if (this._foundLink && this._drawOutline) {
            // restore original outline
            this._foundLink.style.outline = this._tmpOutline;
            this._foundLink.style.outlineOffset = this._tmpOutlineOffset;
          }
          this._drawOutline = (aFoundLink && this._findMode != this.FIND_NORMAL);
          if (this._drawOutline) {
            // backup original outline
            this._tmpOutline = aFoundLink.style.outline;
            this._tmpOutlineOffset = aFoundLink.style.outlineOffset;

            // draw pseudo focus rect
            // XXX Should we change the following style for FAYT pseudo focus?
            // XXX Shouldn't we change default design if outline is visible
            //     already?
            // Don't set the outline-color, we should always use initial value.
            aFoundLink.style.outline = "1px dotted";
            aFoundLink.style.outlineOffset = "0";
          }

          this._foundLink = aFoundLink;

          // If the mouse cursor is on the document, the status bar text is
          // changed by a mouse event which is dispatched by a scroll event.
          // Thus we should change it only after the mouse event is dispatched.
          if (this._findMode != this.FIND_NORMAL)
            setTimeout(function(aSelf) { aSelf._updateStatusUIBar(); }, 0, this);
        ]]></body>
      </method>

      <method name="_finishFAYT">
        <parameter name="aKeypressEvent"/>
        <body><![CDATA[
          try {
            if (this._foundLink)
              this._foundLink.focus();
            else if (this._foundEditable) {
              this._foundEditable.focus();
              var fastFind = this.browser.fastFind;
              fastFind.collapseSelection();
            }
            else if (this._currentWindow)
              this._currentWindow.focus();
            else
              return false;
          }
          catch(e) {
            return false;
          }

          if (aKeypressEvent)
            aKeypressEvent.preventDefault();

          this.close();
          return true;
        ]]></body>
      </method>

      <!--
        - Returns true if |aMimeType| is text-based, or false otherwise.
        -
        - @param aMimeType
        -        The MIME type to check.
        -
        - if adding types to this function, please see the similar function
        - in browser/base/content/browser.js
        -->
      <method name="_mimeTypeIsTextBased">
        <parameter name="aMimeType"/>
        <body><![CDATA[
          return /^text\/|\+xml$/.test(aMimeType) ||
                 aMimeType == "application/x-javascript" ||
                 aMimeType == "application/javascript" ||
                 aMimeType == "application/json" ||
                 aMimeType == "application/xml";
        ]]></body>
      </method>

      <!--
        - Returns whether FAYT can be used for the given event in
        - the current content state.
        -->
      <method name="_shouldFastFind">
        <parameter name="aEvent"/>
        <body><![CDATA[
          if (aEvent.ctrlKey || aEvent.altKey || aEvent.metaKey ||
              aEvent.defaultPrevented)
            return false;

          var elt = document.commandDispatcher.focusedElement;
          if (elt) {
            if (elt instanceof HTMLInputElement && elt.mozIsTextField(false))
              return false;

            if (elt instanceof HTMLTextAreaElement ||
                elt instanceof HTMLSelectElement ||
                elt instanceof HTMLObjectElement ||
                elt instanceof HTMLEmbedElement)
              return false;
          }

          var win = document.commandDispatcher.focusedWindow;
          if (win && !this._mimeTypeIsTextBased(win.document.contentType))
              return false;

          // disable FAYT in about:blank to prevent FAYT opening unexpectedly.
          var url = this.browser.currentURI;
          if (url.spec == "about:blank")
            return false;

          // disable FAYT in documents that ask for it to be disabled.
          if ((url.schemeIs("about") || url.schemeIs("chrome")) &&
              (win.document.documentElement &&
               win.document.documentElement.getAttribute("disablefastfind") == "true"))
            return false;

          if (win) {
            try {
              var editingSession = win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                   .getInterface(Components.interfaces.nsIWebNavigation)
                                   .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
                                   .getInterface(Components.interfaces.nsIEditingSession);
              if (editingSession.windowIsEditable(win))
                return false;
            }
            catch (e) {
              // If someone built with composer disabled, we can't get an editing session.
            }
         }

          return true;
        ]]></body>
      </method>

      <method name="_shouldBeCaseSensitive">
        <parameter name="aString"/>
        <body><![CDATA[
          if (this._typeAheadCaseSensitive == 0)
            return false;
          if (this._typeAheadCaseSensitive == 1)
            return true;

          return aString != aString.toLowerCase();
        ]]></body>
      </method>

      <method name="_onBrowserKeypress">
        <parameter name="aEvent"/>
        <body><![CDATA[
          const TAF_LINKS_KEY = "'";
          const TAF_TEXT_KEY = "/";

          if (!this._shouldFastFind(aEvent))
            return;

          if (this._findMode != this.FIND_NORMAL && this._quickFindTimeout) {
            if (!aEvent.charCode)
              return;

            this._findField.select();
            this._findField.focus();
            this._dispatchKeypressEvent(this._findField.inputField, aEvent);
            aEvent.preventDefault();
            return;
          }

          var key = aEvent.charCode ? String.fromCharCode(aEvent.charCode) : null;
          var manualstartFAYT = (key == TAF_LINKS_KEY || key == TAF_TEXT_KEY);
          var autostartFAYT = !manualstartFAYT && this._useTypeAheadFind &&
                              key && key != " ";
          if (manualstartFAYT || autostartFAYT) {
            var mode = (key == TAF_LINKS_KEY ||
                        (autostartFAYT && this._typeAheadLinksOnly)) ?
              this.FIND_LINKS : this.FIND_TYPEAHEAD;

            // Clear bar first, so that when openFindBar() calls setCaseSensitivity()
            // it doesn't get confused by a lingering value
            this._findField.value = "";

            this.open(mode);
            this._setFindCloseTimeout();
            this._findField.select();
            this._findField.focus();

            if (autostartFAYT)
              this._dispatchKeypressEvent(this._findField.inputField, aEvent);
            else
              this._updateStatusUI(this.nsITypeAheadFind.FIND_FOUND);

            aEvent.preventDefault();
          }
        ]]></body>
      </method>

      <!-- See nsIDOMEventListener -->
      <method name="handleEvent">
        <parameter name="aEvent"/>
        <body><![CDATA[
          switch (aEvent.type) {
            case "mouseup":
              if (!this.hidden && this._findMode != this.FIND_NORMAL)
                this.close();

              break;
            case "keypress":
              this._onBrowserKeypress(aEvent);
              break;
          }
        ]]></body>
      </method>

      <method name="_enableFindButtons">
        <parameter name="aEnable"/>
        <body><![CDATA[
          this.getElement("find-next").disabled =
            this.getElement("find-previous").disabled =
            this.getElement("highlight").disabled = !aEnable;
        ]]></body>
      </method>

      <!--
        - Determines whether minimalist or general-purpose search UI is to be
        - displayed when the find bar is activated.
        -->
      <method name="_updateFindUI">
        <body><![CDATA[
          var showMinimalUI = this._findMode != this.FIND_NORMAL;

          var nodes = this.getElement("findbar-container").childNodes;
          for (var i = 0; i < nodes.length; i++) {
            if (nodes[i].className.indexOf("findbar-find-fast") != -1)
               continue;

            nodes[i].hidden = showMinimalUI;
          }
          this._updateCaseSensitivity();

          if (this._findMode == this.FIND_TYPEAHEAD)
            this.getElement("find-label").value = this._fastFindStr;
          else if (this._findMode == this.FIND_LINKS)
            this.getElement("find-label").value = this._fastFindLinksStr;
          else
            this.getElement("find-label").value = this._normalFindStr;
        ]]></body>
      </method>

      <method name="_updateFoundLink">
        <parameter name="res"/>
        <body><![CDATA[
          var val = this._findField.value;
          if (res == this.nsITypeAheadFind.FIND_NOTFOUND || !val) {
            this._setFoundLink(null);
            this._foundEditable = null;
            this._currentWindow = null;
          }
          else {
            this._setFoundLink(this.browser.fastFind.foundLink);
            this._foundEditable = this.browser.fastFind.foundEditable;
            this._currentWindow = this.browser.fastFind.currentWindow;
          }
        ]]></body>
      </method>

      <method name="_find">
        <parameter name="aValue"/>
        <body><![CDATA[
          if (!this._dispatchFindEvent(""))
            return this.nsITypeAheadFind.FIND_PENDING;

          var val = aValue || this._findField.value;
          var res = this.nsITypeAheadFind.FIND_NOTFOUND;

          // Only search on input if we don't have a last-failed string,
          // or if the current search string doesn't start with it.
          if (this._findFailedString == null ||
              val.indexOf(this._findFailedString) != 0)
          {
            this._enableFindButtons(val);
            if (this.getElement("highlight").checked)
              this._setHighlightTimeout();

            this._updateCaseSensitivity(val);

            var fastFind = this.browser.fastFind;
            res = fastFind.find(val, this._findMode == this.FIND_LINKS);

            this._updateFoundLink(res);
            this._updateStatusUI(res, false);

            if (res == this.nsITypeAheadFind.FIND_NOTFOUND)
              this._findFailedString = val;
            else
              this._findFailedString = null;
          }

          if (this._findMode != this.FIND_NORMAL)
            this._setFindCloseTimeout();

          if (this._findResetTimeout != -1)
            clearTimeout(this._findResetTimeout);

          // allow a search to happen on input again after a second has
          // expired since the previous input, to allow for dynamic
          // content and/or page loading
          this._findResetTimeout = setTimeout(function(self) {
						  self._findFailedString = null;
						  self._findResetTimeout = -1; },
					      1000, this);

          return res;
        ]]></body>
      </method>

      <method name="_flash">
        <body><![CDATA[
          if (this._flashFindBarCount === undefined)
            this._flashFindBarCount = this._initialFlashFindBarCount;

          if (this._flashFindBarCount-- == 0) {
            clearInterval(this._flashFindBarTimeout);
            this.removeAttribute("flash");
            this._flashFindBarCount = 6;
            return;
          }

          this.setAttribute("flash",
                            (this._flashFindBarCount % 2 == 0) ?
                            "false" : "true");
        ]]></body>
      </method>

      <method name="_setHighlightTimeout">
        <body><![CDATA[
          if (this._highlightTimeout)
            clearTimeout(this._highlightTimeout);
          this._highlightTimeout =
            setTimeout(function(aSelf) {
                         aSelf.toggleHighlight(false);
                         aSelf.toggleHighlight(true);
                       }, 500, this);
        ]]></body>
      </method>

      <method name="_findAgain">
        <parameter name="aFindPrevious"/>
        <body><![CDATA[
          var fastFind = this.browser.fastFind;
          var res = fastFind.findAgain(aFindPrevious,
                                       this._findMode == this.FIND_LINKS);
          this._updateFoundLink(res);
          this._updateStatusUI(res, aFindPrevious);

          if (this._findMode != this.FIND_NORMAL && !this.hidden)
            this._setFindCloseTimeout();

          return res;
        ]]></body>
      </method>

      <method name="_updateStatusUI">
        <parameter name="res"/>
        <parameter name="aFindPrevious"/>
        <body><![CDATA[
          switch (res) {
            case this.nsITypeAheadFind.FIND_WRAPPED:
              this._findStatusIcon.setAttribute("status", "wrapped");
              this._findStatusDesc.textContent =
                aFindPrevious ? this._wrappedToBottomStr : this._wrappedToTopStr;
              this._findField.removeAttribute("status");
              break;
            case this.nsITypeAheadFind.FIND_NOTFOUND:
              this._findStatusIcon.setAttribute("status", "notfound");
              this._findStatusDesc.textContent = this._notFoundStr;
              this._findField.setAttribute("status", "notfound");
              break;
            case this.nsITypeAheadFind.FIND_PENDING:
              this._findStatusIcon.setAttribute("status", "pending");
              this._findStatusDesc.textContent = "";
              this._findField.removeAttribute("status");
              break;
            case this.nsITypeAheadFind.FIND_FOUND:
            default:
              this._findStatusIcon.removeAttribute("status");
              this._findStatusDesc.textContent = "";
              this._findField.removeAttribute("status");
              break;
          }
        ]]></body>
      </method>

      <method name="updateControlState">
        <parameter name="aResult"/>
        <parameter name="aFindPrevious"/>
        <body><![CDATA[
          this._updateStatusUI(aResult, aFindPrevious);
          this._enableFindButtons(aResult !== this.nsITypeAheadFind.FIND_NOTFOUND);
        ]]></body>
      </method>


      <method name="_getInitialSelection">
        <body><![CDATA[
          var focusedElement = document.commandDispatcher.focusedElement;
          var selText;

          if (focusedElement instanceof Components.interfaces.nsIDOMNSEditableElement &&
              focusedElement.editor &&
              focusedElement.ownerDocument.defaultView.top == this._browser.contentWindow)
          {
            // The user may have a selection in an input or textarea
            selText = focusedElement.editor.selectionController
              .getSelection(Components.interfaces.nsISelectionController.SELECTION_NORMAL)
              .toString();
          }
          else {
            // Look for any selected text on the actual page
            var focusedWindow = document.commandDispatcher.focusedWindow;
            if (focusedWindow.top == this._browser.contentWindow)
              selText = focusedWindow.getSelection().toString();
          }

          if (!selText)
            return "";

          // Process our text to get rid of unwanted characters
          if (selText.length > this._selectionMaxLen) {
            var pattern = new RegExp("^(?:\\s*.){0," + this._selectionMaxLen + "}");
            pattern.test(selText);
            selText = RegExp.lastMatch;
          }
          return selText.replace(/^\s+/, "")
                        .replace(/\s+$/, "")
                        .replace(/\s+/g, " ")
                        .substr(0, this._selectionMaxLen);
        ]]></body>
      </method>

      <method name="_dispatchFindEvent">
        <parameter name="aType"/>
        <parameter name="aFindPrevious"/>
        <body><![CDATA[
          let event = document.createEvent("CustomEvent");
          event.initCustomEvent("find" + aType, true, true, {
            query: this._findField.value,
            caseSensitive: !!this._typeAheadCaseSensitive,
            highlightAll: this.getElement("highlight").checked,
            findPrevious: aFindPrevious
          });
          return this.dispatchEvent(event);
        ]]></body>
      </method>


      <!--
        - Opens the findbar, focuses the findfield and selects its contents.
        - Also flashes the findbar the first time it's used.
        - @param aMode
        -        the find mode to be used, which is either FIND_NORMAL,
        -        FIND_TYPEAHEAD or FIND_LINKS. If not passed, the last
        -        find mode if any or FIND_NORMAL.
        -->
      <method name="startFind">
        <parameter name="aMode"/>
        <body><![CDATA[
          var prefsvc =
            Components.classes["@mozilla.org/preferences-service;1"]
                      .getService(Components.interfaces.nsIPrefBranch);
          var userWantsPrefill = true;
          this.open(aMode);

          if (this._flashFindBar) {
            this._flashFindBarTimeout =
              setInterval(function(aSelf) { aSelf._flash(); }, 500, this);
            prefsvc.setIntPref("accessibility.typeaheadfind.flashBar",
                               --this._flashFindBar);
          }

          if (this.prefillWithSelection)
            userWantsPrefill =
              prefsvc.getBoolPref("accessibility.typeaheadfind.prefillwithselection");

          var initialString = (this.prefillWithSelection && userWantsPrefill) ?
                              this._getInitialSelection() : null;
          if (initialString) {
            this._findField.value = initialString;
            this._enableFindButtons(true);
          }
          else if (!this._findField.value)
            this._enableFindButtons(false);

          this._findField.select();
          this._findField.focus();
        ]]></body>
      </method>

      <!--
        - Convenient alias to startFind(gFindBar.FIND_NORMAL);
        -
        - You should generally map the window's find command to this method.
        -   e.g. <command name="cmd_find" oncommand="gFindBar.onFindCommand();"/>
        -->
      <method name="onFindCommand">
        <body><![CDATA[
          this.startFind(this.FIND_NORMAL);
        ]]></body>
      </method>

      <!--
        - Stub for find-next and find-previous commands
        - @param aFindPrevious
        -        true for find-previous, false otherwise.
        -->
      <method name="onFindAgainCommand">
        <parameter name="aFindPrevious"/>
        <body><![CDATA[
          var findString = this._browser.fastFind.searchString || this._findField.value;
          if (!findString) {
            this.startFind();
            return;
          }

          // We dispatch the findAgain event here instead of in _findAgain since
          // if there is a find event handler that prevents the default then
          // fastFind.searchString will never get updated which in turn means
          // there would never be findAgain events because of the logic below.
          if (!this._dispatchFindEvent("again", aFindPrevious))
            return;

          // user explicitly requested another search, so do it even if we think it'll fail
          this._findFailedString = null;

          var res;
          // Ensure the stored SearchString is in sync with what we want to find
          if (this._findField.value != this._browser.fastFind.searchString)
            res = this._find(this._findField.value);
          else
            res = this._findAgain(aFindPrevious);

          if (res == this.nsITypeAheadFind.FIND_NOTFOUND) {
            if (this.open()) {
              if (this._findMode != this.FIND_NORMAL)
                this._setFindCloseTimeout();
              this._findField.focus();
              this._findField.focus();
              this._updateStatusUI(res, aFindPrevious);
            }
          }
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <!--
        - We have to guard against `this.close` being |null| due to an unknown
        - issue, which is tracked in bug 957999.
        -->
      <handler event="keypress" keycode="VK_ESCAPE" phase="capturing"
               action="if (this.close) this.close();" preventdefault="true"/>
    </handlers>
  </binding>
</bindings>