summaryrefslogtreecommitdiffstats
path: root/browser/components/search/content/search.xml
blob: 5c67bc649b1c72f2717949c43a492785ac69125b (plain)
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
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
<?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 % searchBarDTD SYSTEM "chrome://browser/locale/searchbar.dtd" >
%searchBarDTD;
<!ENTITY % browserDTD SYSTEM "chrome://browser/locale/browser.dtd">
%browserDTD;
]>

<bindings id="SearchBindings"
          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">

  <binding id="searchbar">
    <resources>
      <stylesheet src="chrome://browser/content/search/searchbarBindings.css"/>
      <stylesheet src="chrome://browser/skin/searchbar.css"/>
    </resources>
    <content>
      <xul:stringbundle src="chrome://browser/locale/search.properties"
                        anonid="searchbar-stringbundle"/>
      <!--
      There is a dependency between "maxrows" attribute and
      "SuggestAutoComplete._historyLimit" (nsSearchSuggestions.js). Changing
      one of them requires changing the other one.
      -->
      <xul:textbox class="searchbar-textbox"
                   anonid="searchbar-textbox"
                   type="autocomplete"
                   inputtype="search"
                   flex="1"
                   autocompletepopup="PopupSearchAutoComplete"
                   autocompletesearch="search-autocomplete"
                   autocompletesearchparam="searchbar-history"
                   maxrows="10"
                   completeselectedindex="true"
                   minresultsforpopup="0"
                   xbl:inherits="disabled,disableautocomplete,searchengine,src,newlines">
        <!--
        Empty <box> to properly position the icon within the autocomplete
        binding's anonymous children (the autocomplete binding positions <box>
        children differently)
        -->
        <xul:box>
          <xul:hbox class="searchbar-search-button-container">
            <xul:image class="searchbar-search-button"
                       anonid="searchbar-search-button"
                       xbl:inherits="addengines"
                       tooltiptext="&searchEndCap.label;"/>
          </xul:hbox>
        </xul:box>
        <xul:hbox class="search-go-container">
          <xul:image class="search-go-button" hidden="true"
                     anonid="search-go-button"
                     onclick="handleSearchCommand(event);"
                     tooltiptext="&searchEndCap.label;"/>
        </xul:hbox>
      </xul:textbox>
    </content>

    <implementation implements="nsIObserver">
      <constructor><![CDATA[
        if (this.parentNode.parentNode.localName == "toolbarpaletteitem")
          return;
        // Make sure we rebuild the popup in onpopupshowing
        this._needToBuildPopup = true;

        Services.obs.addObserver(this, "browser-search-engine-modified", false);

        this._initialized = true;

        Services.search.init((function search_init_cb(aStatus) {
          // Bail out if the binding's been destroyed
          if (!this._initialized)
            return;

          if (Components.isSuccessCode(aStatus)) {
            // Refresh the display (updating icon, etc)
            this.updateDisplay();
            BrowserSearch.updateOpenSearchBadge();
          } else {
            Components.utils.reportError("Cannot initialize search service, bailing out: " + aStatus);
          }
        }).bind(this));
      ]]></constructor>

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

      <method name="destroy">
        <body><![CDATA[
        if (this._initialized) {
          this._initialized = false;

          Services.obs.removeObserver(this, "browser-search-engine-modified");
        }

        // Make sure to break the cycle from _textbox to us. Otherwise we leak
        // the world. But make sure it's actually pointing to us.
        // Also make sure the textbox has ever been constructed, otherwise the
        // _textbox getter will cause the textbox constructor to run, add an
        // observer, and leak the world too.
        if (this._textboxInitialized && this._textbox.mController.input == this)
          this._textbox.mController.input = null;
        ]]></body>
      </method>

      <field name="_ignoreFocus">false</field>
      <field name="_clickClosedPopup">false</field>
      <field name="_stringBundle">document.getAnonymousElementByAttribute(this,
          "anonid", "searchbar-stringbundle");</field>
      <field name="_textboxInitialized">false</field>
      <field name="_textbox">document.getAnonymousElementByAttribute(this,
          "anonid", "searchbar-textbox");</field>
      <field name="_engines">null</field>
      <field name="FormHistory" readonly="true">
        (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory;
      </field>

      <property name="engines" readonly="true">
        <getter><![CDATA[
          if (!this._engines)
            this._engines = Services.search.getVisibleEngines();
          return this._engines;
        ]]></getter>
      </property>

      <property name="currentEngine">
        <setter><![CDATA[
          Services.search.currentEngine = val;
          return val;
        ]]></setter>
        <getter><![CDATA[
          var currentEngine = Services.search.currentEngine;
          // Return a dummy engine if there is no currentEngine
          return currentEngine || {name: "", uri: null};
        ]]></getter>
      </property>

      <!-- textbox is used by sanitize.js to clear the undo history when
           clearing form information. -->
      <property name="textbox" readonly="true"
                onget="return this._textbox;"/>

      <property name="value" onget="return this._textbox.value;"
                             onset="return this._textbox.value = val;"/>

      <method name="focus">
        <body><![CDATA[
          this._textbox.focus();
        ]]></body>
      </method>

      <method name="select">
        <body><![CDATA[
          this._textbox.select();
        ]]></body>
      </method>

      <method name="observe">
        <parameter name="aEngine"/>
        <parameter name="aTopic"/>
        <parameter name="aVerb"/>
        <body><![CDATA[
          if (aTopic == "browser-search-engine-modified") {
            switch (aVerb) {
            case "engine-removed":
              this.offerNewEngine(aEngine);
              break;
            case "engine-added":
              this.hideNewEngine(aEngine);
              break;
            case "engine-changed":
              // An engine was removed (or hidden) or added, or an icon was
              // changed.  Do nothing special.
            }

            // Make sure the engine list is refetched next time it's needed
            this._engines = null;

            // Update the popup header and update the display after any modification.
            this._textbox.popup.updateHeader();
            this.updateDisplay();
          }
        ]]></body>
      </method>

      <!-- There are two seaprate lists of search engines, whose uses intersect
      in this file.  The search service (nsIBrowserSearchService and
      nsSearchService.js) maintains a list of Engine objects which is used to
      populate the searchbox list of available engines and to perform queries.
      That list is accessed here via this.SearchService, and it's that sort of
      Engine that is passed to this binding's observer as aEngine.

      In addition, browser.js fills two lists of autodetected search engines
      (browser.engines and browser.hiddenEngines) as properties of
      mCurrentBrowser.  Those lists contain unnamed JS objects of the form
      { uri:, title:, icon: }, and that's what the searchbar uses to determine
      whether to show any "Add <EngineName>" menu items in the drop-down.

      The two types of engines are currently related by their identifying
      titles (the Engine object's 'name'), although that may change; see bug
      335102.  -->

      <!-- If the engine that was just removed from the searchbox list was
      autodetected on this page, move it to each browser's active list so it
      will be offered to be added again. -->
      <method name="offerNewEngine">
        <parameter name="aEngine"/>
        <body><![CDATA[
          for (let browser of gBrowser.browsers) {
            if (browser.hiddenEngines) {
              // XXX This will need to be changed when engines are identified by
              // URL rather than title; see bug 335102.
              var removeTitle = aEngine.wrappedJSObject.name;
              for (var i = 0; i < browser.hiddenEngines.length; i++) {
                if (browser.hiddenEngines[i].title == removeTitle) {
                  if (!browser.engines)
                    browser.engines = [];
                  browser.engines.push(browser.hiddenEngines[i]);
                  browser.hiddenEngines.splice(i, 1);
                  break;
                }
              }
            }
          }
          BrowserSearch.updateOpenSearchBadge();
        ]]></body>
      </method>

      <!-- If the engine that was just added to the searchbox list was
      autodetected on this page, move it to each browser's hidden list so it is
      no longer offered to be added. -->
      <method name="hideNewEngine">
        <parameter name="aEngine"/>
        <body><![CDATA[
          for (let browser of gBrowser.browsers) {
            if (browser.engines) {
              // XXX This will need to be changed when engines are identified by
              // URL rather than title; see bug 335102.
              var removeTitle = aEngine.wrappedJSObject.name;
              for (var i = 0; i < browser.engines.length; i++) {
                if (browser.engines[i].title == removeTitle) {
                  if (!browser.hiddenEngines)
                    browser.hiddenEngines = [];
                  browser.hiddenEngines.push(browser.engines[i]);
                  browser.engines.splice(i, 1);
                  break;
                }
              }
            }
          }
          BrowserSearch.updateOpenSearchBadge();
        ]]></body>
      </method>

      <method name="setIcon">
        <parameter name="element"/>
        <parameter name="uri"/>
        <body><![CDATA[
          element.setAttribute("src", uri);
        ]]></body>
      </method>

      <method name="updateDisplay">
        <body><![CDATA[
          var uri = this.currentEngine.iconURI;
          this.setIcon(this, uri ? uri.spec : "");

          var name = this.currentEngine.name;
          var text = this._stringBundle.getFormattedString("searchtip", [name]);

          this._textbox.placeholder = this._stringBundle.getString("searchPlaceholder");
          this._textbox.label = text;
          this._textbox.tooltipText = text;
        ]]></body>
      </method>

      <method name="updateGoButtonVisibility">
        <body><![CDATA[
          document.getAnonymousElementByAttribute(this, "anonid",
                                                  "search-go-button")
                  .hidden = !this._textbox.value;
        ]]></body>
      </method>

      <method name="openSuggestionsPanel">
        <parameter name="aShowOnlySettingsIfEmpty"/>
        <body><![CDATA[
          if (this._textbox.open)
            return;

          this._textbox.showHistoryPopup();

          if (this._textbox.value) {
            // showHistoryPopup does a startSearch("") call, ensure the
            // controller handles the text from the input box instead:
            this._textbox.mController.handleText();
          }
          else if (aShowOnlySettingsIfEmpty) {
            this.setAttribute("showonlysettings", "true");
          }
        ]]></body>
      </method>

      <method name="selectEngine">
        <parameter name="aEvent"/>
        <parameter name="isNextEngine"/>
        <body><![CDATA[
          // Find the new index
          var newIndex = this.engines.indexOf(this.currentEngine);
          newIndex += isNextEngine ? 1 : -1;

          if (newIndex >= 0 && newIndex < this.engines.length) {
            this.currentEngine = this.engines[newIndex];
          }

          aEvent.preventDefault();
          aEvent.stopPropagation();

          this.openSuggestionsPanel();
        ]]></body>
      </method>

      <method name="handleSearchCommand">
        <parameter name="aEvent"/>
        <parameter name="aEngine"/>
        <parameter name="aForceNewTab"/>
        <body><![CDATA[
          var where = "current";
          let params;

          // Open ctrl/cmd clicks on one-off buttons in a new background tab.
          if (aEvent && aEvent.originalTarget.getAttribute("anonid") == "search-go-button") {
            if (aEvent.button == 2)
              return;
            where = whereToOpenLink(aEvent, false, true);
          }
          else if (aForceNewTab) {
            where = "tab";
            if (Services.prefs.getBoolPref("browser.tabs.loadInBackground"))
              where += "-background";
          }
          else {
            var newTabPref = Services.prefs.getBoolPref("browser.search.openintab");
            if (((aEvent instanceof KeyboardEvent) && aEvent.altKey) ^ newTabPref)
              where = "tab";
            if ((aEvent instanceof MouseEvent) &&
                (aEvent.button == 1 || aEvent.getModifierState("Accel"))) {
              where = "tab";
              params = {
                inBackground: true,
              };
            }
          }

          this.handleSearchCommandWhere(aEvent, aEngine, where, params);
        ]]></body>
      </method>

      <method name="handleSearchCommandWhere">
        <parameter name="aEvent"/>
        <parameter name="aEngine"/>
        <parameter name="aWhere"/>
        <parameter name="aParams"/>
        <body><![CDATA[
          var textBox = this._textbox;
          var textValue = textBox.value;

          let selection = this.telemetrySearchDetails;
          let oneOffRecorded = false;

          if (!selection || (selection.index == -1)) {
            oneOffRecorded = this.textbox.popup.oneOffButtons
                                 .maybeRecordTelemetry(aEvent, aWhere, aParams);
            if (!oneOffRecorded) {
              let source = "unknown";
              let type = "unknown";
              let target = aEvent.originalTarget;
              if (aEvent instanceof KeyboardEvent) {
                type = "key";
              } else if (aEvent instanceof MouseEvent) {
                type = "mouse";
                if (target.classList.contains("search-panel-header") ||
                    target.parentNode.classList.contains("search-panel-header")) {
                  source = "header";
                }
              } else if (aEvent instanceof XULCommandEvent) {
                if (target.getAttribute("anonid") == "paste-and-search") {
                  source = "paste";
                }
              }
              if (!aEngine) {
                aEngine = this.currentEngine;
              }
              BrowserSearch.recordOneoffSearchInTelemetry(aEngine, source, type,
                                                          aWhere);
            }
          }

          // This is a one-off search only if oneOffRecorded is true.
          this.doSearch(textValue, aWhere, aEngine, aParams, oneOffRecorded);

          if (aWhere == "tab" && aParams && aParams.inBackground)
            this.focus();
        ]]></body>
      </method>

      <method name="doSearch">
        <parameter name="aData"/>
        <parameter name="aWhere"/>
        <parameter name="aEngine"/>
        <parameter name="aParams"/>
        <parameter name="aOneOff"/>
        <body><![CDATA[
          var textBox = this._textbox;

          // Save the current value in the form history
          if (aData && !PrivateBrowsingUtils.isWindowPrivate(window) && this.FormHistory.enabled) {
            this.FormHistory.update(
              { op : "bump",
                fieldname : textBox.getAttribute("autocompletesearchparam"),
                value : aData },
              { handleError : function(aError) {
                  Components.utils.reportError("Saving search to form history failed: " + aError.message);
              }});
          }

          let engine = aEngine || this.currentEngine;
          var submission = engine.getSubmission(aData, null, "searchbar");
          let telemetrySearchDetails = this.telemetrySearchDetails;
          this.telemetrySearchDetails = null;
          if (telemetrySearchDetails && telemetrySearchDetails.index == -1) {
            telemetrySearchDetails = null;
          }
          // If we hit here, we come either from a one-off, a plain search or a suggestion.
          const details = {
            isOneOff: aOneOff,
            isSuggestion: (!aOneOff && telemetrySearchDetails),
            selection: telemetrySearchDetails
          };
          BrowserSearch.recordSearchInTelemetry(engine, "searchbar", details);
          // null parameter below specifies HTML response for search
          let params = {
            postData: submission.postData,
          };
          if (aParams) {
            for (let key in aParams) {
              params[key] = aParams[key];
            }
          }
          openUILinkIn(submission.uri.spec, aWhere, params);
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="command"><![CDATA[
        const target = event.originalTarget;
        if (target.engine) {
          this.currentEngine = target.engine;
        } else if (target.classList.contains("addengine-item")) {
          // Select the installed engine if the installation succeeds
          var installCallback = {
            onSuccess: engine => this.currentEngine = engine
          }
          Services.search.addEngine(target.getAttribute("uri"), null,
                                    target.getAttribute("src"), false,
                                    installCallback);
        }
        else
          return;

        this.focus();
        this.select();
      ]]></handler>

      <handler event="DOMMouseScroll"
               phase="capturing"
               modifiers="accel"
               action="this.selectEngine(event, (event.detail > 0));"/>

      <handler event="input" action="this.updateGoButtonVisibility();"/>
      <handler event="drop" action="this.updateGoButtonVisibility();"/>

      <handler event="blur">
      <![CDATA[
        // If the input field is still focused then a different window has
        // received focus, ignore the next focus event.
        this._ignoreFocus = (document.activeElement == this._textbox.inputField);
      ]]></handler>

      <handler event="focus">
      <![CDATA[
        // Speculatively connect to the current engine's search URI (and
        // suggest URI, if different) to reduce request latency
        this.currentEngine.speculativeConnect({window: window});

        if (this._ignoreFocus) {
          // This window has been re-focused, don't show the suggestions
          this._ignoreFocus = false;
          return;
        }

        // Don't open the suggestions if there is no text in the textbox.
        if (!this._textbox.value)
          return;

        // Don't open the suggestions if the mouse was used to focus the
        // textbox, that will be taken care of in the click handler.
        if (Services.focus.getLastFocusMethod(window) & Services.focus.FLAG_BYMOUSE)
          return;

        this.openSuggestionsPanel();
      ]]></handler>

      <handler event="mousedown" phase="capturing">
      <![CDATA[
        if (event.originalTarget.getAttribute("anonid") == "searchbar-search-button") {
          this._clickClosedPopup = this._textbox.popup._isHiding;
        }
      ]]></handler>

      <handler event="click" button="0">
      <![CDATA[
        // Ignore clicks on the search go button.
        if (event.originalTarget.getAttribute("anonid") == "search-go-button") {
          return;
        }

        let isIconClick = event.originalTarget.getAttribute("anonid") == "searchbar-search-button";

        // Ignore clicks on the icon if they were made to close the popup
        if (isIconClick && this._clickClosedPopup) {
          return;
        }

        // Open the suggestions whenever clicking on the search icon or if there
        // is text in the textbox.
        if (isIconClick || this._textbox.value) {
          this.openSuggestionsPanel(true);
        }
      ]]></handler>

    </handlers>
  </binding>

  <binding id="searchbar-textbox"
      extends="chrome://global/content/bindings/autocomplete.xml#autocomplete">
    <implementation implements="nsIObserver">
      <constructor><![CDATA[
        const kXULNS =
          "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

        if (document.getBindingParent(this).parentNode.parentNode.localName ==
            "toolbarpaletteitem")
          return;

        // Initialize fields
        this._stringBundle = document.getBindingParent(this)._stringBundle;
        this._suggestEnabled =
          Services.prefs.getBoolPref("browser.search.suggest.enabled");

        if (Services.prefs.getBoolPref("browser.urlbar.clickSelectsAll"))
          this.setAttribute("clickSelectsAll", true);

        // Add items to context menu and attach controller to handle them
        var textBox = document.getAnonymousElementByAttribute(this,
                                              "anonid", "textbox-input-box");
        var cxmenu = document.getAnonymousElementByAttribute(textBox,
                                          "anonid", "input-box-contextmenu");
        var pasteAndSearch;
        cxmenu.addEventListener("popupshowing", function() {
          BrowserSearch.searchBar._textbox.closePopup();
          if (!pasteAndSearch)
            return;
          var controller = document.commandDispatcher.getControllerForCommand("cmd_paste");
          var enabled = controller.isCommandEnabled("cmd_paste");
          if (enabled)
            pasteAndSearch.removeAttribute("disabled");
          else
            pasteAndSearch.setAttribute("disabled", "true");
        }, false);

        var element, label, akey;

        element = document.createElementNS(kXULNS, "menuseparator");
        cxmenu.appendChild(element);

        this.setAttribute("aria-owns", this.popup.id);

        var insertLocation = cxmenu.firstChild;
        while (insertLocation.nextSibling &&
               insertLocation.getAttribute("cmd") != "cmd_paste")
          insertLocation = insertLocation.nextSibling;
        if (insertLocation) {
          element = document.createElementNS(kXULNS, "menuitem");
          label = this._stringBundle.getString("cmd_pasteAndSearch");
          element.setAttribute("label", label);
          element.setAttribute("anonid", "paste-and-search");
          element.setAttribute("oncommand", "BrowserSearch.pasteAndSearch(event)");
          cxmenu.insertBefore(element, insertLocation.nextSibling);
          pasteAndSearch = element;
        }

        element = document.createElementNS(kXULNS, "menuitem");
        label = this._stringBundle.getString("cmd_clearHistory");
        akey = this._stringBundle.getString("cmd_clearHistory_accesskey");
        element.setAttribute("label", label);
        element.setAttribute("accesskey", akey);
        element.setAttribute("cmd", "cmd_clearhistory");
        cxmenu.appendChild(element);

        element = document.createElementNS(kXULNS, "menuitem");
        label = this._stringBundle.getString("cmd_showSuggestions");
        akey = this._stringBundle.getString("cmd_showSuggestions_accesskey");
        element.setAttribute("anonid", "toggle-suggest-item");
        element.setAttribute("label", label);
        element.setAttribute("accesskey", akey);
        element.setAttribute("cmd", "cmd_togglesuggest");
        element.setAttribute("type", "checkbox");
        element.setAttribute("checked", this._suggestEnabled);
        element.setAttribute("autocheck", "false");
        this._suggestMenuItem = element;
        cxmenu.appendChild(element);

        this.addEventListener("keypress", aEvent => {
          if (navigator.platform.startsWith("Mac") && aEvent.keyCode == KeyEvent.VK_F4)
            this.openSearch()
        }, true);

        this.controllers.appendController(this.searchbarController);
        document.getBindingParent(this)._textboxInitialized = true;

        // Add observer for suggest preference
        Services.prefs.addObserver("browser.search.suggest.enabled", this, false);
      ]]></constructor>

      <destructor><![CDATA[
        Services.prefs.removeObserver("browser.search.suggest.enabled", this);

        // Because XBL and the customize toolbar code interacts poorly,
        // there may not be anything to remove here
        try {
          this.controllers.removeController(this.searchbarController);
        } catch (ex) { }
      ]]></destructor>

      <field name="_stringBundle"/>
      <field name="_suggestMenuItem"/>
      <field name="_suggestEnabled"/>

      <!--
        This overrides the searchParam property in autocomplete.xml.  We're
        hijacking this property as a vehicle for delivering the privacy
        information about the window into the guts of nsSearchSuggestions.

        Note that the setter is the same as the parent.  We were not sure whether
        we can override just the getter.  If that proves to be the case, the setter
        can be removed.
      -->
      <property name="searchParam"
                onget="return this.getAttribute('autocompletesearchparam') +
                       (PrivateBrowsingUtils.isWindowPrivate(window) ? '|private' : '');"
                onset="this.setAttribute('autocompletesearchparam', val); return val;"/>

      <!-- This is implemented so that when textbox.value is set directly (e.g.,
           by tests), the one-off query is updated. -->
      <method name="onBeforeValueSet">
        <parameter name="aValue"/>
        <body><![CDATA[
          this.popup.oneOffButtons.query = aValue;
          return aValue;
        ]]></body>
      </method>

      <!--
        This method overrides the autocomplete binding's openPopup (essentially
        duplicating the logic from the autocomplete popup binding's
        openAutocompletePopup method), modifying it so that the popup is aligned with
        the inner textbox, but sized to not extend beyond the search bar border.
      -->
      <method name="openPopup">
        <body><![CDATA[
          var popup = this.popup;
          if (!popup.mPopupOpen) {
            // Initially the panel used for the searchbar (PopupSearchAutoComplete
            // in browser.xul) is hidden to avoid impacting startup / new
            // window performance. The base binding's openPopup would normally
            // call the overriden openAutocompletePopup in urlbarBindings.xml's
            // browser-autocomplete-result-popup binding to unhide the popup,
            // but since we're overriding openPopup we need to unhide the panel
            // ourselves.
            popup.hidden = false;

            // Don't roll up on mouse click in the anchor for the search UI.
            if (popup.id == "PopupSearchAutoComplete") {
              popup.setAttribute("norolluponanchor", "true");
            }

            popup.mInput = this;
            popup.view = this.controller.QueryInterface(Ci.nsITreeView);
            popup.invalidate();

            popup.showCommentColumn = this.showCommentColumn;
            popup.showImageColumn = this.showImageColumn;

            document.popupNode = null;

            const isRTL = getComputedStyle(this, "").direction == "rtl";

            var outerRect = this.getBoundingClientRect();
            var innerRect = this.inputField.getBoundingClientRect();
            let width = isRTL ?
                        innerRect.right - outerRect.left :
                        outerRect.right - innerRect.left;
            popup.setAttribute("width", width > 100 ? width : 100);

            var yOffset = outerRect.bottom - innerRect.bottom;
            popup.openPopup(this.inputField, "after_start", 0, yOffset, false, false);
          }
        ]]></body>
      </method>

      <method name="observe">
        <parameter name="aSubject"/>
        <parameter name="aTopic"/>
        <parameter name="aData"/>
        <body><![CDATA[
          if (aTopic == "nsPref:changed") {
            this._suggestEnabled =
              Services.prefs.getBoolPref("browser.search.suggest.enabled");
            this._suggestMenuItem.setAttribute("checked", this._suggestEnabled);
          }
        ]]></body>
      </method>

      <method name="openSearch">
        <body>
          <![CDATA[
            if (!this.popupOpen) {
              document.getBindingParent(this).openSuggestionsPanel();
              return false;
            }
            return true;
          ]]>
        </body>
      </method>

      <!-- override |onTextEntered| in autocomplete.xml -->
      <method name="onTextEntered">
        <parameter name="aEvent"/>
        <body><![CDATA[
          let engine;
          let oneOff = this.selectedButton;
          if (oneOff) {
            if (!oneOff.engine) {
              oneOff.doCommand();
              return;
            }
            engine = oneOff.engine;
          }
          if (this._selectionDetails &&
              this._selectionDetails.currentIndex != -1) {
            BrowserSearch.searchBar.telemetrySearchDetails = this._selectionDetails;
            this._selectionDetails = null;
          }
          document.getBindingParent(this).handleSearchCommand(aEvent, engine);
        ]]></body>
      </method>

      <property name="selectedButton">
        <getter><![CDATA[
          return this.popup.oneOffButtons.selectedButton;
        ]]></getter>
        <setter><![CDATA[
          return this.popup.oneOffButtons.selectedButton = val;
        ]]></setter>
      </property>

      <method name="handleKeyboardNavigation">
        <parameter name="aEvent"/>
        <body><![CDATA[
          let popup = this.popup;
          if (!popup.popupOpen)
            return;

          // accel + up/down changes the default engine and shouldn't affect
          // the selection on the one-off buttons.
          if (aEvent.getModifierState("Accel"))
            return;

          let suggestions =
            document.getAnonymousElementByAttribute(popup, "anonid", "tree");
          let suggestionsHidden =
            suggestions.getAttribute("collapsed") == "true";
          let numItems = suggestionsHidden ? 0 : this.popup.view.rowCount;
          this.popup.oneOffButtons.handleKeyPress(aEvent, numItems, true);
        ]]></body>
      </method>

      <!-- nsIController -->
      <field name="searchbarController" readonly="true"><![CDATA[({
        _self: this,
        supportsCommand: function(aCommand) {
          return aCommand == "cmd_clearhistory" ||
                 aCommand == "cmd_togglesuggest";
        },

        isCommandEnabled: function(aCommand) {
          return true;
        },

        doCommand: function (aCommand) {
          switch (aCommand) {
            case "cmd_clearhistory":
              var param = this._self.getAttribute("autocompletesearchparam");

              BrowserSearch.searchBar.FormHistory.update({ op : "remove", fieldname : param }, null);
              this._self.value = "";
              break;
            case "cmd_togglesuggest":
              // The pref observer will update _suggestEnabled and the menu
              // checkmark.
              Services.prefs.setBoolPref("browser.search.suggest.enabled",
                                         !this._self._suggestEnabled);
              break;
            default:
              // do nothing with unrecognized command
          }
        }
      })]]></field>
    </implementation>

    <handlers>
      <handler event="input"><![CDATA[
        this.popup.removeAttribute("showonlysettings");
      ]]></handler>

      <handler event="keypress" phase="capturing"
               action="return this.handleKeyboardNavigation(event);"/>

      <handler event="keypress" keycode="VK_UP" modifiers="accel"
               phase="capturing"
               action="document.getBindingParent(this).selectEngine(event, false);"/>

      <handler event="keypress" keycode="VK_DOWN" modifiers="accel"
               phase="capturing"
               action="document.getBindingParent(this).selectEngine(event, true);"/>

      <handler event="keypress" keycode="VK_DOWN" modifiers="alt"
               phase="capturing"
               action="return this.openSearch();"/>

      <handler event="keypress" keycode="VK_UP" modifiers="alt"
               phase="capturing"
               action="return this.openSearch();"/>

      <handler event="dragover">
      <![CDATA[
        var types = event.dataTransfer.types;
        if (types.includes("text/plain") || types.includes("text/x-moz-text-internal"))
          event.preventDefault();
      ]]>
      </handler>

      <handler event="drop">
      <![CDATA[
        var dataTransfer = event.dataTransfer;
        var data = dataTransfer.getData("text/plain");
        if (!data)
          data = dataTransfer.getData("text/x-moz-text-internal");
        if (data) {
          event.preventDefault();
          this.value = data;
          document.getBindingParent(this).openSuggestionsPanel();
        }
      ]]>
      </handler>

    </handlers>
  </binding>

  <binding id="browser-search-autocomplete-result-popup" extends="chrome://browser/content/urlbarBindings.xml#browser-autocomplete-result-popup">
    <resources>
      <stylesheet src="chrome://browser/content/search/searchbarBindings.css"/>
      <stylesheet src="chrome://browser/skin/searchbar.css"/>
    </resources>
    <content ignorekeys="true" level="top" consumeoutsideclicks="never">
      <xul:hbox anonid="searchbar-engine" xbl:inherits="showonlysettings"
                class="search-panel-header search-panel-current-engine">
        <xul:image class="searchbar-engine-image" xbl:inherits="src"/>
        <xul:label anonid="searchbar-engine-name" flex="1" crop="end"
                   role="presentation"/>
      </xul:hbox>
      <xul:tree anonid="tree" flex="1"
                class="autocomplete-tree plain search-panel-tree"
                hidecolumnpicker="true" seltype="single">
        <xul:treecols anonid="treecols">
          <xul:treecol id="treecolAutoCompleteValue" class="autocomplete-treecol" flex="1" overflow="true"/>
        </xul:treecols>
        <xul:treechildren class="autocomplete-treebody"/>
      </xul:tree>
      <xul:vbox anonid="search-one-off-buttons" class="search-one-offs"/>
    </content>
    <implementation>
      <!-- Popup rollup is triggered by native events before the mousedown event
           reaches the DOM. The will be set to true by the popuphiding event and
           false after the mousedown event has been triggered to detect what
           caused rollup. -->
      <field name="_isHiding">false</field>
      <field name="_bundle">null</field>
      <property name="bundle" readonly="true">
        <getter>
          <![CDATA[
            if (!this._bundle) {
              const kBundleURI = "chrome://browser/locale/search.properties";
              this._bundle = Services.strings.createBundle(kBundleURI);
            }
            return this._bundle;
          ]]>
        </getter>
      </property>

      <field name="oneOffButtons" readonly="true">
        document.getAnonymousElementByAttribute(this, "anonid",
                                                "search-one-off-buttons");
      </field>

      <method name="updateHeader">
        <body><![CDATA[
          let currentEngine = Services.search.currentEngine;
          let uri = currentEngine.iconURI;
          if (uri) {
            this.setAttribute("src", uri.spec);
          }
          else {
            // If the default has just been changed to a provider without icon,
            // avoid showing the icon of the previous default provider.
            this.removeAttribute("src");
          }

          let headerText = this.bundle.formatStringFromName("searchHeader",
                                                            [currentEngine.name], 1);
          document.getAnonymousElementByAttribute(this, "anonid", "searchbar-engine-name")
                  .setAttribute("value", headerText);
          document.getAnonymousElementByAttribute(this, "anonid", "searchbar-engine")
                  .engine = currentEngine;
        ]]></body>
      </method>

      <!-- This is called when a one-off is clicked and when "search in new tab"
           is selected from a one-off context menu. -->
      <method name="handleOneOffSearch">
        <parameter name="event"/>
        <parameter name="engine"/>
        <parameter name="where"/>
        <parameter name="params"/>
        <body><![CDATA[
          let searchbar = document.getElementById("searchbar");
          searchbar.handleSearchCommandWhere(event, engine, where, params);
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="popupshowing"><![CDATA[
        if (!this.oneOffButtons.popup) {
          // The panel width only spans to the textbox size, but we also want it
          // to include the magnifier icon's width.
          let ltr = getComputedStyle(this).direction == "ltr";
          let magnifierWidth = parseInt(getComputedStyle(this)[
                                 ltr ? "marginLeft" : "marginRight"
                               ]) * -1;
          // Ensure the panel is wide enough to fit at least 3 engines.
          let minWidth = Math.max(
            parseInt(this.width) + magnifierWidth,
            this.oneOffButtons.buttonWidth * 3
          );
          this.style.minWidth = minWidth + "px";

          // Set the origin before assigning the popup, as the assignment does
          // a rebuild and would miss the origin.
          this.oneOffButtons.telemetryOrigin = "searchbar";
          // Set popup after setting the minWidth since it builds the buttons.
          this.oneOffButtons.popup = this;
          this.oneOffButtons.textbox = this.input;
        }

        // First handle deciding if we are showing the reduced version of the
        // popup containing only the preferences button. We do this if the
        // glass icon has been clicked if the text field is empty.
        let searchbar = document.getElementById("searchbar");
        let tree = document.getAnonymousElementByAttribute(this, "anonid",
                                                           "tree")
        if (searchbar.hasAttribute("showonlysettings")) {
          searchbar.removeAttribute("showonlysettings");
          this.setAttribute("showonlysettings", "true");

          // Setting this with an xbl-inherited attribute gets overridden the
          // second time the user clicks the glass icon for some reason...
          tree.collapsed = true;
        }
        else {
          this.removeAttribute("showonlysettings");
          // Uncollapse as long as we have a tree with a view which has >= 1 row.
          // The autocomplete binding itself will take care of uncollapsing later,
          // if we currently have no rows but end up having some in the future
          // when the search string changes
          tree.collapsed = !tree.view || !tree.view.rowCount;
        }

        // Show the current default engine in the top header of the panel.
        this.updateHeader();
      ]]></handler>

      <handler event="popuphiding"><![CDATA[
        this._isHiding = true;
        setTimeout(() => {
          this._isHiding = false;
        }, 0);
      ]]></handler>

      <!-- This handles clicks on the topmost "Foo Search" header in the
           popup (hbox[anonid="searchbar-engine"]). -->
      <handler event="click"><![CDATA[
        if (event.button == 2) {
          // Ignore right clicks.
          return;
        }
        let button = event.originalTarget;
        let engine = button.parentNode.engine;
        if (!engine) {
          return;
        }
        this.oneOffButtons.handleSearchCommand(event, engine);
      ]]></handler>
    </handlers>

  </binding>

  <!-- Used for additional open search providers in the search panel. -->
  <binding id="addengine-icon" extends="xul:box">
    <content>
      <xul:image class="addengine-icon" xbl:inherits="src"/>
      <xul:image class="addengine-badge"/>
    </content>
  </binding>

  <binding id="search-one-offs">
    <content context="_child">
      <xul:deck anonid="search-panel-one-offs-header"
                selectedIndex="0"
                class="search-panel-header search-panel-current-input">
        <xul:label anonid="searchbar-oneoffheader-search"
                   value="&searchWithHeader.label;"/>
        <xul:hbox anonid="search-panel-searchforwith"
                  class="search-panel-current-input">
          <xul:label anonid="searchbar-oneoffheader-before"
                     value="&searchFor.label;"/>
          <xul:label anonid="searchbar-oneoffheader-searchtext"
                     class="search-panel-input-value"
                     flex="1"
                     crop="end"/>
          <xul:label anonid="searchbar-oneoffheader-after"
                     flex="10000"
                     value="&searchWith.label;"/>
        </xul:hbox>
        <xul:hbox anonid="search-panel-searchonengine"
                  class="search-panel-current-input">
          <xul:label anonid="searchbar-oneoffheader-beforeengine"
                     value="&search.label;"/>
          <xul:label anonid="searchbar-oneoffheader-engine"
                     class="search-panel-input-value"
                     flex="1"
                     crop="end"/>
          <xul:label anonid="searchbar-oneoffheader-afterengine"
                     flex="10000"
                     value="&searchAfter.label;"/>
        </xul:hbox>
      </xul:deck>
      <xul:description anonid="search-panel-one-offs"
                       role="group"
                       class="search-panel-one-offs"
                       xbl:inherits="compact">
        <xul:button anonid="search-settings-compact"
                    oncommand="showSettings();"
                    class="searchbar-engine-one-off-item search-setting-button-compact"
                    tooltiptext="&changeSearchSettings.tooltip;"
                    xbl:inherits="compact"/>
      </xul:description>
      <xul:vbox anonid="add-engines"/>
      <xul:button anonid="search-settings"
                  oncommand="showSettings();"
                  class="search-setting-button search-panel-header"
                  label="&changeSearchSettings.button;"
                  xbl:inherits="compact"/>
      <xul:menupopup anonid="search-one-offs-context-menu">
        <xul:menuitem anonid="search-one-offs-context-open-in-new-tab"
                      label="&searchInNewTab.label;"
                      accesskey="&searchInNewTab.accesskey;"/>
        <xul:menuitem anonid="search-one-offs-context-set-default"
                      label="&searchSetAsDefault.label;"
                      accesskey="&searchSetAsDefault.accesskey;"/>
      </xul:menupopup>
    </content>

    <implementation implements="nsIDOMEventListener">

      <!-- Width in pixels of the one-off buttons.  49px is the min-width of
           each search engine button, adapt this const when changing the css.
           It's actually 48px + 1px of right border. -->
      <property name="buttonWidth" readonly="true" onget="return 49;"/>

      <field name="_popup">null</field>

      <!-- The popup that contains the one-offs.  This is required, so it should
           never be null or undefined, except possibly before the one-offs are
           used. -->
      <property name="popup">
        <getter><![CDATA[
          return this._popup;
        ]]></getter>
        <setter><![CDATA[
          if (this._popup == val) {
            return val;
          }

          let events = [
            "popupshowing",
            "popuphidden",
          ];
          if (this._popup) {
            for (let event of events) {
              this._popup.removeEventListener(event, this);
            }
          }
          if (val) {
            for (let event of events) {
              val.addEventListener(event, this);
            }
          }
          this._popup = val;

          // If the popup is already open, rebuild the one-offs now.  The
          // popup may be opening, so check that the state is not closed
          // instead of checking popupOpen.
          if (val && val.state != "closed") {
            this._rebuild();
          }
          return val;
        ]]></setter>
      </property>

      <field name="_textbox">null</field>

      <!-- The textbox associated with the one-offs.  Set this to a textbox to
           automatically keep the related one-offs UI up to date.  Otherwise you
           can leave it null/undefined, and in that case you should update the
           query property manually. -->
      <property name="textbox">
        <getter><![CDATA[
          return this._textbox;
        ]]></getter>
        <setter><![CDATA[
          if (this._textbox == val) {
            return val;
          }
          if (this._textbox) {
            this._textbox.removeEventListener("input", this);
          }
          if (val) {
            val.addEventListener("input", this);
          }
          return this._textbox = val;
        ]]></setter>
      </property>

      <!-- Set this to a string that identifies your one-offs consumer.  It'll
           be appended to telemetry recorded with maybeRecordTelemetry(). -->
      <field name="telemetryOrigin">""</field>

      <field name="_query">""</field>

      <!-- The query string currently shown in the one-offs.  If the textbox
           property is non-null, then this is automatically updated on
           input. -->
      <property name="query">
        <getter><![CDATA[
          return this._query;
        ]]></getter>
        <setter><![CDATA[
          this._query = val;
          if (this.popup && this.popup.popupOpen) {
            this._updateAfterQueryChanged();
          }
          return val;
        ]]></setter>
      </property>

      <field name="_selectedButton">null</field>

      <!-- The selected one-off, a xul:button, including the add-engine button
           and the search-settings button.  Null if no one-off is selected. -->
      <property name="selectedButton">
        <getter><![CDATA[
          return this._selectedButton;
        ]]></getter>
        <setter><![CDATA[
          this._changeVisuallySelectedButton(val, true);
          return val;
        ]]></setter>
      </property>

      <!-- The index of the selected one-off, including the add-engine button
           and the search-settings button.  -1 if no one-off is selected. -->
      <property name="selectedButtonIndex">
        <getter><![CDATA[
          let buttons = this.getSelectableButtons(true);
          for (let i = 0; i < buttons.length; i++) {
            if (buttons[i] == this._selectedButton) {
              return i;
            }
          }
          return -1;
        ]]></getter>
        <setter><![CDATA[
          let buttons = this.getSelectableButtons(true);
          this.selectedButton = buttons[val];
          return val;
        ]]></setter>
      </property>

      <!-- The visually selected one-off is the same as the selected one-off
           unless a one-off is moused over.  In that case, the visually selected
           one-off is the moused-over one-off, which may be different from the
           selected one-off.  The visually selected one-off is always the one
           that is visually highlighted.  Includes the add-engine button and the
           search-settings button.  A xul:button. -->
      <property name="visuallySelectedButton" readonly="true">
        <getter><![CDATA[
          return this.getSelectableButtons(true).find(button => {
            return button.getAttribute("selected") == "true";
          });
        ]]></getter>
      </property>

      <property name="compact" readonly="true">
        <getter><![CDATA[
          return this.getAttribute("compact") == "true";
        ]]></getter>
      </property>

      <property name="settingsButton" readonly="true">
        <getter><![CDATA[
          let id = this.compact ? "search-settings-compact" : "search-settings";
          return document.getAnonymousElementByAttribute(this, "anonid", id);
        ]]></getter>
      </property>

      <field name="_bundle">null</field>

      <property name="bundle" readonly="true">
        <getter><![CDATA[
          if (!this._bundle) {
            const kBundleURI = "chrome://browser/locale/search.properties";
            this._bundle = Services.strings.createBundle(kBundleURI);
          }
          return this._bundle;
        ]]></getter>
      </property>

      <!-- When a context menu is opened on a one-off button, this is set to the
           engine of that button for use with the context menu actions. -->
      <field name="_contextEngine">null</field>

      <constructor><![CDATA[
        // Prevent popup events from the context menu from reaching the autocomplete
        // binding (or other listeners).
        let menu = document.getAnonymousElementByAttribute(this, "anonid", "search-one-offs-context-menu");
        let listener = aEvent => aEvent.stopPropagation();
        menu.addEventListener("popupshowing", listener);
        menu.addEventListener("popuphiding", listener);
        menu.addEventListener("popupshown", aEvent => {
          this._ignoreMouseEvents = true;
          aEvent.stopPropagation();
        });
        menu.addEventListener("popuphidden", aEvent => {
          this._ignoreMouseEvents = false;
          aEvent.stopPropagation();
        });
      ]]></constructor>

      <!-- This handles events outside the one-off buttons, like on the popup
           and textbox. -->
      <method name="handleEvent">
        <parameter name="event"/>
        <body><![CDATA[
          switch (event.type) {
            case "input":
              // Allow the consumer's input to override its value property with
              // a oneOffSearchQuery property.  That way if the value is not
              // actually what the user typed (e.g., it's autofilled, or it's a
              // mozaction URI), the consumer has some way of providing it.
              this.query = event.target.oneOffSearchQuery || event.target.value;
              break;
            case "popupshowing":
              this._rebuild();
              break;
            case "popuphidden":
              Services.tm.mainThread.dispatch(() => {
                this.selectedButton = null;
                this._contextEngine = null;
              }, Ci.nsIThread.DISPATCH_NORMAL);
              break;
          }
        ]]></body>
      </method>

      <method name="showSettings">
        <body><![CDATA[
          BrowserUITelemetry.countSearchSettingsEvent(this.telemetryOrigin);
          openPreferences("paneSearch");
          // If the preference tab was already selected, the panel doesn't
          // close itself automatically.
          this.popup.hidePopup();
        ]]></body>
      </method>

      <!-- Updates the parts of the UI that show the query string. -->
      <method name="_updateAfterQueryChanged">
        <body><![CDATA[
          let headerSearchText =
            document.getAnonymousElementByAttribute(this, "anonid",
                                                    "searchbar-oneoffheader-searchtext");
          let headerPanel =
            document.getAnonymousElementByAttribute(this, "anonid",
                                                    "search-panel-one-offs-header");
          let list = document.getAnonymousElementByAttribute(this, "anonid",
                                                             "search-panel-one-offs");
          headerSearchText.setAttribute("value", this.query);
          let groupText;
          let isOneOffSelected =
            this.selectedButton &&
            this.selectedButton.classList.contains("searchbar-engine-one-off-item");
          // Typing de-selects the settings or opensearch buttons at the bottom
          // of the search panel, as typing shows the user intends to search.
          if (this.selectedButton && !isOneOffSelected)
            this.selectedButton = null;
          if (this.query) {
            groupText = headerSearchText.previousSibling.value +
                        '"' + headerSearchText.value + '"' +
                        headerSearchText.nextSibling.value;
            if (!isOneOffSelected)
              headerPanel.selectedIndex = 1;
          }
          else {
            let noSearchHeader =
              document.getAnonymousElementByAttribute(this, "anonid",
                                                      "searchbar-oneoffheader-search");
            groupText = noSearchHeader.value;
            if (!isOneOffSelected)
              headerPanel.selectedIndex = 0;
          }
          list.setAttribute("aria-label", groupText);
        ]]></body>
      </method>

      <!-- Builds all the UI. -->
      <method name="_rebuild">
        <body><![CDATA[
          // Update the 'Search for <keywords> with:" header.
          this._updateAfterQueryChanged();

          let list = document.getAnonymousElementByAttribute(this, "anonid",
                                                             "search-panel-one-offs");

          // Handle opensearch items. This needs to be done before building the
          // list of one off providers, as that code will return early if all the
          // alternative engines are hidden.
          let addEngineList =
            document.getAnonymousElementByAttribute(this, "anonid", "add-engines");
          while (addEngineList.firstChild)
            addEngineList.firstChild.remove();

          const kXULNS =
            "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

          // Add a button for each engine that the page in the selected browser
          // offers.  But not when the one-offs are compact.  Compact one-offs
          // are shown in the urlbar, and the add-engine buttons span the width
          // of the popup, so if we added all the engines that a site offers, it
          // could effectively break the urlbar popup by offering a ton of
          // engines.  We should probably make a smaller version of the buttons
          // for compact one-offs.
          if (!this.compact) {
            for (let engine of gBrowser.selectedBrowser.engines || []) {
              let button = document.createElementNS(kXULNS, "button");
              let label = this.bundle.formatStringFromName("cmd_addFoundEngine",
                                                           [engine.title], 1);
              button.id = this.telemetryOrigin + "-add-engine-" +
                          engine.title.replace(/ /g, '-');
              button.setAttribute("class", "addengine-item");
              button.setAttribute("label", label);
              button.setAttribute("pack", "start");

              button.setAttribute("crop", "end");
              button.setAttribute("tooltiptext", engine.uri);
              button.setAttribute("uri", engine.uri);
              if (engine.icon) {
                button.setAttribute("image", engine.icon);
              }
              button.setAttribute("title", engine.title);
              addEngineList.appendChild(button);
            }
          }

          let settingsButton =
            document.getAnonymousElementByAttribute(this, "anonid",
                                                    "search-settings-compact");
          // Finally, build the list of one-off buttons.
          while (list.firstChild != settingsButton)
            list.firstChild.remove();
          // Remove the trailing empty text node introduced by the binding's
          // content markup above.
          if (settingsButton.nextSibling)
            settingsButton.nextSibling.remove();

          let Preferences =
            Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences;
          let pref = Preferences.get("browser.search.hiddenOneOffs");
          let hiddenList = pref ? pref.split(",") : [];

          let currentEngineName = Services.search.currentEngine.name;
          let includeCurrentEngine = this.getAttribute("includecurrentengine");
          let engines = Services.search.getVisibleEngines().filter(e => {
            return (includeCurrentEngine || e.name != currentEngineName) &&
                   !hiddenList.includes(e.name);
          });

          let header = document.getAnonymousElementByAttribute(this, "anonid",
                                                               "search-panel-one-offs-header")
          // header is a xul:deck so collapsed doesn't work on it, see bug 589569.
          header.hidden = list.collapsed = !engines.length;

          if (!engines.length)
            return;

          let panelWidth = parseInt(this.popup.clientWidth);
          // The + 1 is because the last button doesn't have a right border.
          let enginesPerRow = Math.floor((panelWidth + 1) / this.buttonWidth);
          let buttonWidth = Math.floor(panelWidth / enginesPerRow);
          // There will be an emtpy area of:
          //   panelWidth - enginesPerRow * buttonWidth  px
          // at the end of each row.

          // If the <description> tag with the list of search engines doesn't have
          // a fixed height, the panel will be sized incorrectly, causing the bottom
          // of the suggestion <tree> to be hidden.
          let oneOffCount = engines.length;
          if (this.compact)
            ++oneOffCount;
          let rowCount = Math.ceil(oneOffCount / enginesPerRow);
          let height = rowCount * 33; // 32px per row, 1px border.
          list.setAttribute("height", height + "px");

          // Ensure we can refer to the settings buttons by ID:
          let settingsEl = document.getAnonymousElementByAttribute(this, "anonid", "search-settings");
          settingsEl.id = this.telemetryOrigin + "-anon-search-settings";
          let compactSettingsEl = document.getAnonymousElementByAttribute(this, "anonid", "search-settings-compact");
          compactSettingsEl.id = this.telemetryOrigin +
                                 "-anon-search-settings-compact";

          let dummyItems = enginesPerRow - (oneOffCount % enginesPerRow || enginesPerRow);
          for (let i = 0; i < engines.length; ++i) {
            let engine = engines[i];
            let button = document.createElementNS(kXULNS, "button");
            button.id = this._buttonIDForEngine(engine);
            let uri = "chrome://browser/skin/search-engine-placeholder.png";
            if (engine.iconURI) {
              uri = engine.iconURI.spec;
            }
            button.setAttribute("image", uri);
            button.setAttribute("class", "searchbar-engine-one-off-item");
            button.setAttribute("tooltiptext", engine.name);
            button.setAttribute("width", buttonWidth);
            button.engine = engine;

            if ((i + 1) % enginesPerRow == 0)
              button.classList.add("last-of-row");

            if (i + 1 == engines.length)
              button.classList.add("last-engine");

            if (i >= oneOffCount + dummyItems - enginesPerRow)
              button.classList.add("last-row");

            list.insertBefore(button, settingsButton);
          }

          let hasDummyItems = !!dummyItems;
          while (dummyItems) {
            let button = document.createElementNS(kXULNS, "button");
            button.setAttribute("class", "searchbar-engine-one-off-item dummy last-row");
            button.setAttribute("width", buttonWidth);

            if (!--dummyItems)
              button.classList.add("last-of-row");

            list.insertBefore(button, settingsButton);
          }

          if (this.compact) {
            this.settingsButton.setAttribute("width", buttonWidth);
            if (rowCount == 1 && hasDummyItems) {
              // When there's only one row, make the compact settings button
              // hug the right edge of the panel.  It may not due to the panel's
              // width not being an integral multiple of the button width.  (See
              // the "There will be an emtpy area" comment above.)  Increase the
              // width of the last dummy item by the remainder.
              //
              // There's one weird thing to guard against.  When layout pixels
              // aren't an integral multiple of device pixels, the calculated
              // remainder can end up being ~1px too big, at least on Windows,
              // which pushes the settings button to a new row.  The remainder
              // is integral, not a fraction, so that's not the problem.  To
              // work around that, unscale the remainder, floor it, scale it
              // back, and then floor that.
              let scale = window.QueryInterface(Ci.nsIInterfaceRequestor)
                                .getInterface(Ci.nsIDOMWindowUtils)
                                .screenPixelsPerCSSPixel;
              let remainder = panelWidth - (enginesPerRow * buttonWidth);
              remainder = Math.floor(Math.floor(remainder * scale) / scale);
              let width = remainder + buttonWidth;
              let lastDummyItem = this.settingsButton.previousSibling;
              lastDummyItem.setAttribute("width", width);
            }
          }
        ]]></body>
      </method>

      <method name="_buttonIDForEngine">
        <parameter name="engine"/>
        <body><![CDATA[
          return this.telemetryOrigin + "-engine-one-off-item-" +
                 engine.name.replace(/ /g, '-');
        ]]></body>
      </method>

      <method name="_buttonForEngine">
        <parameter name="engine"/>
        <body><![CDATA[
          return document.getElementById(this._buttonIDForEngine(engine));
        ]]></body>
      </method>

      <method name="_changeVisuallySelectedButton">
        <parameter name="val"/>
        <parameter name="aUpdateLogicallySelectedButton"/>
        <body><![CDATA[
          let visuallySelectedButton = this.visuallySelectedButton;
          if (visuallySelectedButton)
            visuallySelectedButton.removeAttribute("selected");

          let header =
            document.getAnonymousElementByAttribute(this, "anonid",
                                                    "search-panel-one-offs-header");
          // Avoid selecting dummy buttons.
          if (val && !val.classList.contains("dummy")) {
            val.setAttribute("selected", "true");
            if (val.classList.contains("searchbar-engine-one-off-item") &&
                val.engine) {
              let headerEngineText =
                document.getAnonymousElementByAttribute(this, "anonid",
                                                        "searchbar-oneoffheader-engine");
              header.selectedIndex = 2;
              headerEngineText.value = val.engine.name;
            }
            else {
              header.selectedIndex = this.query ? 1 : 0;
            }
            if (this.textbox) {
              this.textbox.setAttribute("aria-activedescendant", val.id);
            }
          } else {
            val = null;
            header.selectedIndex = this.query ? 1 : 0;
            if (this.textbox) {
              this.textbox.removeAttribute("aria-activedescendant");
            }
          }

          if (aUpdateLogicallySelectedButton) {
            this._selectedButton = val;
            if (val && !val.engine) {
              // If the button doesn't have an engine, then clear the popup's
              // selection to indicate that pressing Return while the button is
              // selected will do the button's command, not search.
              this.popup.selectedIndex = -1;
            }
            let event = document.createEvent("Events");
            event.initEvent("SelectedOneOffButtonChanged", true, false);
            this.dispatchEvent(event);
          }
        ]]></body>
      </method>

      <method name="getSelectableButtons">
        <parameter name="aIncludeNonEngineButtons"/>
        <body><![CDATA[
          let buttons = [];
          let oneOff = document.getAnonymousElementByAttribute(this, "anonid",
                                                               "search-panel-one-offs");
          for (oneOff = oneOff.firstChild; oneOff; oneOff = oneOff.nextSibling) {
            // oneOff may be a text node since the list xul:description contains
            // whitespace and the compact settings button.  See the markup
            // above.  _rebuild removes text nodes, but it may not have been
            // called yet (because e.g. the popup hasn't been opened yet).
            if (oneOff.nodeType == Node.ELEMENT_NODE) {
              if (oneOff.classList.contains("dummy") ||
                  oneOff.classList.contains("search-setting-button-compact"))
                break;
              buttons.push(oneOff);
            }
          }

          if (!aIncludeNonEngineButtons)
            return buttons;

          let addEngine =
            document.getAnonymousElementByAttribute(this, "anonid", "add-engines");
          for (addEngine = addEngine.firstChild; addEngine; addEngine = addEngine.nextSibling)
            buttons.push(addEngine);

          buttons.push(this.settingsButton);
          return buttons;
        ]]></body>
      </method>

      <method name="handleSearchCommand">
        <parameter name="aEvent"/>
        <parameter name="aEngine"/>
        <parameter name="aForceNewTab"/>
        <body><![CDATA[
          let where = "current";
          let params;

          // Open ctrl/cmd clicks on one-off buttons in a new background tab.
          if (aForceNewTab) {
            where = "tab";
            if (Services.prefs.getBoolPref("browser.tabs.loadInBackground")) {
              params = {
                inBackground: true,
              };
            }
          }
          else {
            var newTabPref = Services.prefs.getBoolPref("browser.search.openintab");
            if (((aEvent instanceof KeyboardEvent) && aEvent.altKey) ^ newTabPref)
              where = "tab";
            if ((aEvent instanceof MouseEvent) &&
                (aEvent.button == 1 || aEvent.getModifierState("Accel"))) {
              where = "tab";
              params = {
                inBackground: true,
              };
            }
          }

          this.popup.handleOneOffSearch(aEvent, aEngine, where, params);
        ]]></body>
      </method>

      <!--
        Increments or decrements the index of the currently selected one-off.

        @param aForward
               If true, the index is incremented, and if false, the index is
               decremented.
        @param aWrapAround
               This has a couple of effects, depending on whether there is
               currently a selection.
               (1) If true and the last one-off is currently selected,
               incrementing the index will cause the selection to be cleared and
               this method to return true.  Calling advanceSelection again after
               that (again with aForward=true) will select the first one-off.
               Likewise if decrementing the index when the first one-off is
               selected, except in the opposite direction of course.
               (2) If true and there currently is no selection, decrementing the
               index will cause the last one-off to become selected and this
               method to return true.  Only the aForward=false case is affected
               because it is always the case that if aForward=true and there
               currently is no selection, the first one-off becomes selected and
               this method returns true.
        @param aCycleEngines
               If true, only engine buttons are included.
        @return True if the selection can continue to advance after this method
                returns and false if not.
      -->
      <method name="advanceSelection">
        <parameter name="aForward"/>
        <parameter name="aWrapAround"/>
        <parameter name="aCycleEngines"/>
        <body><![CDATA[
          let selectedButton = this.selectedButton;
          let buttons = this.getSelectableButtons(aCycleEngines);

          if (selectedButton) {
            // cycle through one-off buttons.
            let index = buttons.indexOf(selectedButton);
            if (aForward)
              ++index;
            else
              --index;

            if (index >= 0 && index < buttons.length)
              this.selectedButton = buttons[index];
            else
              this.selectedButton = null;

            if (this.selectedButton || aWrapAround)
              return true;

            return false;
          }

          // If no selection, select the first button or ...
          if (aForward) {
            this.selectedButton = buttons[0];
            return true;
          }

          if (!aForward && aWrapAround) {
            // the last button.
            this.selectedButton = buttons[buttons.length - 1];
            return true;
          }

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

      <!--
        This handles key presses specific to the one-off buttons like Tab and
        Alt-Up/Down, and Up/Down keys within the buttons.  Since one-off buttons
        are always used in conjunction with a list of some sort (in this.popup),
        it also handles Up/Down keys that cross the boundaries between list
        items and the one-off buttons.

        @param event
               The key event.
        @param numListItems
               The number of items in the list.  The reason that this is a
               parameter at all is that the list may contain items at the end
               that should be ignored, depending on the consumer.  That's true
               for the urlbar for example.
        @param allowEmptySelection
               Pass true if it's OK that neither the list nor the one-off
               buttons contains a selection.  Pass false if either the list or
               the one-off buttons (or both) should always contain a selection.
        @param textboxUserValue
               When the last list item is selected and the user presses Down,
               the first one-off becomes selected and the textbox value is
               restored to the value that the user typed.  Pass that value here.
               However, if you pass true for allowEmptySelection, you don't need
               to pass anything for this parameter.  (Pass undefined or null.)
        @return True if this method handled the keypress and false if not.  If
                false, then you should let the autocomplete controller handle
                the keypress.  The value of event.defaultPrevented will be the
                same as this return value.
      -->
      <method name="handleKeyPress">
        <parameter name="event"/>
        <parameter name="numListItems"/>
        <parameter name="allowEmptySelection"/>
        <parameter name="textboxUserValue"/>
        <body><![CDATA[
          if (!this.popup) {
            return false;
          }

          let stopEvent = false;

          // Tab cycles through the one-offs and moves the focus out at the end.
          // But only if non-Shift modifiers aren't also pressed, to avoid
          // clobbering other shortcuts.
          if (event.keyCode == KeyEvent.DOM_VK_TAB &&
              !event.altKey &&
              !event.ctrlKey &&
              !event.metaKey &&
              this.getAttribute("disabletab") != "true") {
            stopEvent = this.advanceSelection(!event.shiftKey, false, true);
          }

          // Alt + up/down is very similar to (shift +) tab but differs in that
          // it loops through the list, whereas tab will move the focus out.
          else if (event.altKey &&
                   (event.keyCode == KeyEvent.DOM_VK_DOWN ||
                    event.keyCode == KeyEvent.DOM_VK_UP)) {
            stopEvent =
              this.advanceSelection(event.keyCode == KeyEvent.DOM_VK_DOWN,
                                    true, false);
          }

          else if (event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_UP) {
            if (numListItems > 0) {
              if (this.popup.selectedIndex > 0) {
                // The autocomplete controller should handle this case.
              } else if (this.popup.selectedIndex == 0) {
                if (!allowEmptySelection) {
                  // Wrap around the selection to the last one-off.
                  this.selectedButton = null;
                  this.popup.selectedIndex = -1;
                  // Call advanceSelection after setting selectedIndex so that
                  // screen readers see the newly selected one-off. Both trigger
                  // accessibility events.
                  this.advanceSelection(false, true, true);
                  stopEvent = true;
                }
              } else {
                let firstButtonSelected =
                  this.selectedButton &&
                  this.selectedButton == this.getSelectableButtons(true)[0];
                if (firstButtonSelected) {
                  this.selectedButton = null;
                } else {
                  stopEvent = this.advanceSelection(false, true, true);
                }
              }
            } else {
              stopEvent = this.advanceSelection(false, true, true);
            }
          }

          else if (event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_DOWN) {
            if (numListItems > 0) {
              if (this.popup.selectedIndex >= 0 &&
                  this.popup.selectedIndex < numListItems - 1) {
                // The autocomplete controller should handle this case.
              } else if (this.popup.selectedIndex == numListItems - 1) {
                this.selectedButton = null;
                if (!allowEmptySelection) {
                  this.popup.selectedIndex = -1;
                  stopEvent = true;
                }
                if (this.textbox && typeof(textboxUserValue) == "string") {
                  this.textbox.value = textboxUserValue;
                }
                // Call advanceSelection after setting selectedIndex so that
                // screen readers see the newly selected one-off. Both trigger
                // accessibility events.
                this.advanceSelection(true, true, true);
              } else {
                let buttons = this.getSelectableButtons(true);
                let lastButtonSelected =
                  this.selectedButton &&
                  this.selectedButton == buttons[buttons.length - 1];
                if (lastButtonSelected) {
                  this.selectedButton = null;
                  stopEvent = allowEmptySelection;
                } else if (this.selectedButton) {
                  stopEvent = this.advanceSelection(true, true, true);
                } else {
                  // The autocomplete controller should handle this case.
                }
              }
            } else {
              stopEvent = this.advanceSelection(true, true, true);
            }
          }

          if (stopEvent) {
            event.preventDefault();
            event.stopPropagation();
            return true;
          }
          return false;
        ]]></body>
      </method>

      <!--
        If the given event is related to the one-offs, this method records
        one-off telemetry for it.  this.telemetryOrigin will be appended to the
        computed source, so make sure you set that first.

        @param aEvent
               An event, like a click on a one-off button.
        @param aOpenUILinkWhere
               The "where" passed to openUILink.
        @param aOpenUILinkParams
               The "params" passed to openUILink.
        @return True if telemetry was recorded and false if not.
      -->
      <method name="maybeRecordTelemetry">
        <parameter name="aEvent"/>
        <parameter name="aOpenUILinkWhere"/>
        <parameter name="aOpenUILinkParams"/>
        <body><![CDATA[
          if (!aEvent) {
            return false;
          }

          let source = null;
          let type = "unknown";
          let engine = null;
          let target = aEvent.originalTarget;

          if (aEvent instanceof KeyboardEvent) {
            type = "key";
            if (this.selectedButton) {
              source = "oneoff";
              engine = this.selectedButton.engine;
            }
          } else if (aEvent instanceof MouseEvent) {
            type = "mouse";
            if (target.classList.contains("searchbar-engine-one-off-item")) {
              source = "oneoff";
              engine = target.engine;
            }
          } else if ((aEvent instanceof XULCommandEvent) &&
                     target.getAttribute("anonid") ==
                       "search-one-offs-context-open-in-new-tab") {
            source = "oneoff-context";
            engine = this._contextEngine;
          }

          if (!source) {
            return false;
          }

          if (this.telemetryOrigin) {
            source += "-" + this.telemetryOrigin;
          }

          let tabBackground = aOpenUILinkWhere == "tab" &&
                              aOpenUILinkParams &&
                              aOpenUILinkParams.inBackground;
          let where = tabBackground ? "tab-background" : aOpenUILinkWhere;
          BrowserSearch.recordOneoffSearchInTelemetry(engine, source, type,
                                                      where);
          return true;
        ]]></body>
      </method>

    </implementation>

    <handlers>

      <handler event="mousedown"><![CDATA[
        // Required to receive click events from the buttons on Linux.
        event.preventDefault();
      ]]></handler>

      <handler event="mousemove"><![CDATA[
        let target = event.originalTarget;
        if (target.localName != "button")
          return;

        // Ignore mouse events when the context menu is open.
         if (this._ignoreMouseEvents)
           return;

        if ((target.classList.contains("searchbar-engine-one-off-item") &&
             !target.classList.contains("dummy")) ||
            target.classList.contains("addengine-item") ||
            target.classList.contains("search-setting-button")) {
          this._changeVisuallySelectedButton(target);
        }
      ]]></handler>

      <handler event="mouseout"><![CDATA[
        let target = event.originalTarget;
        if (target.localName != "button") {
          return;
        }

        // Don't deselect the current button if the context menu is open.
        if (this._ignoreMouseEvents)
          return;

        // Unfortunately this will fire before mouseover hits another item.
        // If this button is selected, we replace that selection only if
        // we're not moving to a different one-off item:
        if (target.getAttribute("selected") == "true" &&
            (!event.relatedTarget ||
             !event.relatedTarget.classList.contains("searchbar-engine-one-off-item") ||
             event.relatedTarget.classList.contains("dummy"))) {
          this._changeVisuallySelectedButton(this.selectedButton);
        }
      ]]></handler>

      <handler event="click"><![CDATA[
        if (event.button == 2)
          return; // ignore right clicks.

        let button = event.originalTarget;
        let engine = button.engine;

        if (!engine)
          return;

        // Select the clicked button so that consumers can easily tell which
        // button was acted on.
        this.selectedButton = button;
        this.handleSearchCommand(event, engine);
      ]]></handler>

      <handler event="command"><![CDATA[
        let target = event.originalTarget;
        if (target.classList.contains("addengine-item")) {
          // On success, hide the panel and tell event listeners to reshow it to
          // show the new engine.
          let installCallback = {
            onSuccess: engine => {
              this._rebuild();
            },
            onError: function(errorCode) {
              if (errorCode != Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE) {
                // Download error is shown by the search service
                return;
              }
              const kSearchBundleURI = "chrome://global/locale/search/search.properties";
              let searchBundle = Services.strings.createBundle(kSearchBundleURI);
              let brandBundle = document.getElementById("bundle_brand");
              let brandName = brandBundle.getString("brandShortName");
              let title = searchBundle.GetStringFromName("error_invalid_engine_title");
              let text = searchBundle.formatStringFromName("error_duplicate_engine_msg",
                                                           [brandName, target.getAttribute("uri")], 2);
              Services.prompt.QueryInterface(Ci.nsIPromptFactory);
              let prompt = Services.prompt.getPrompt(gBrowser.contentWindow, Ci.nsIPrompt);
              prompt.QueryInterface(Ci.nsIWritablePropertyBag2);
              prompt.setPropertyAsBool("allowTabModal", true);
              prompt.alert(title, text);
            }
          }
          Services.search.addEngine(target.getAttribute("uri"), null,
                                    target.getAttribute("image"), false,
                                    installCallback);
        }
        let anonid = target.getAttribute("anonid");
        if (anonid == "search-one-offs-context-open-in-new-tab") {
          // Select the context-clicked button so that consumers can easily
          // tell which button was acted on.
          this.selectedButton = this._buttonForEngine(this._contextEngine);
          this.handleSearchCommand(event, this._contextEngine, true);
        }
        if (anonid == "search-one-offs-context-set-default") {
          let currentEngine = Services.search.currentEngine;

          if (!this.getAttribute("includecurrentengine")) {
            // Make the target button of the context menu reflect the current
            // search engine first. Doing this as opposed to rebuilding all the
            // one-off buttons avoids flicker.
            let button = this._buttonForEngine(this._contextEngine);
            button.id = this._buttonIDForEngine(currentEngine);
            let uri = "chrome://browser/skin/search-engine-placeholder.png";
            if (currentEngine.iconURI)
              uri = currentEngine.iconURI.spec;
            button.setAttribute("image", uri);
            button.setAttribute("tooltiptext", currentEngine.name);
            button.engine = currentEngine;
          }

          Services.search.currentEngine = this._contextEngine;
        }
      ]]></handler>

      <handler event="contextmenu"><![CDATA[
        let target = event.originalTarget;
        // Prevent the context menu from appearing except on the one off buttons.
        if (!target.classList.contains("searchbar-engine-one-off-item") ||
            target.classList.contains("dummy")) {
          event.preventDefault();
          return;
        }
        document.getAnonymousElementByAttribute(this, "anonid", "search-one-offs-context-set-default")
                .setAttribute("disabled", target.engine == Services.search.currentEngine);

        this._contextEngine = target.engine;
      ]]></handler>
    </handlers>

  </binding>

</bindings>