search.xml 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. <?xml version="1.0"?>
  2. <!-- This Source Code Form is subject to the terms of the Mozilla Public
  3. License, v. 2.0. If a copy of the MPL was not distributed with this
  4. file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
  5. <!DOCTYPE bindings [
  6. <!ENTITY % searchBarDTD SYSTEM "chrome://browser/locale/searchbar.dtd" >
  7. %searchBarDTD;
  8. <!ENTITY % browserDTD SYSTEM "chrome://browser/locale/browser.dtd">
  9. %browserDTD;
  10. ]>
  11. <bindings id="SearchBindings"
  12. xmlns="http://www.mozilla.org/xbl"
  13. xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  14. xmlns:xbl="http://www.mozilla.org/xbl">
  15. <binding id="searchbar">
  16. <resources>
  17. <stylesheet src="chrome://browser/content/search/searchbarBindings.css"/>
  18. <stylesheet src="chrome://browser/skin/searchbar.css"/>
  19. </resources>
  20. <content>
  21. <xul:stringbundle src="chrome://browser/locale/search.properties"
  22. anonid="searchbar-stringbundle"/>
  23. <xul:textbox class="searchbar-textbox"
  24. anonid="searchbar-textbox"
  25. type="autocomplete"
  26. flex="1"
  27. autocompletepopup="PopupAutoComplete"
  28. autocompletesearch="search-autocomplete"
  29. autocompletesearchparam="searchbar-history"
  30. timeout="250"
  31. maxrows="10"
  32. completeselectedindex="true"
  33. showcommentcolumn="true"
  34. tabscrolling="true"
  35. xbl:inherits="disabled,disableautocomplete,searchengine,src,newlines">
  36. <xul:box>
  37. <xul:button class="searchbar-engine-button"
  38. type="menu"
  39. anonid="searchbar-engine-button">
  40. <xul:image class="searchbar-engine-image" xbl:inherits="src"/>
  41. <xul:image class="searchbar-dropmarker-image"/>
  42. <xul:menupopup class="searchbar-popup"
  43. anonid="searchbar-popup">
  44. <xul:menuseparator/>
  45. <xul:menuitem class="open-engine-manager"
  46. anonid="open-engine-manager"
  47. label="&cmd_engineManager.label;"
  48. oncommand="openManager(event);"/>
  49. </xul:menupopup>
  50. </xul:button>
  51. </xul:box>
  52. <xul:hbox class="search-go-container">
  53. <xul:image class="search-go-button"
  54. anonid="search-go-button"
  55. onclick="handleSearchCommand(event);"
  56. tooltiptext="&searchEndCap.label;"/>
  57. </xul:hbox>
  58. </xul:textbox>
  59. </content>
  60. <implementation implements="nsIObserver">
  61. <constructor><![CDATA[
  62. if (this.parentNode.parentNode.localName == "toolbarpaletteitem")
  63. return;
  64. // Make sure we rebuild the popup in onpopupshowing
  65. this._needToBuildPopup = true;
  66. var os =
  67. Components.classes["@mozilla.org/observer-service;1"]
  68. .getService(Components.interfaces.nsIObserverService);
  69. os.addObserver(this, "browser-search-engine-modified", false);
  70. this._initialized = true;
  71. this.searchService.init((function search_init_cb(aStatus) {
  72. // Bail out if the binding has been destroyed
  73. if (!this._initialized)
  74. return;
  75. if (Components.isSuccessCode(aStatus)) {
  76. // Refresh the display (updating icon, etc)
  77. this.updateDisplay();
  78. } else {
  79. Components.utils.reportError("Cannot initialize search service, bailing out: " + aStatus);
  80. }
  81. }).bind(this));
  82. ]]></constructor>
  83. <destructor><![CDATA[
  84. if (this._initialized) {
  85. this._initialized = false;
  86. var os = Components.classes["@mozilla.org/observer-service;1"]
  87. .getService(Components.interfaces.nsIObserverService);
  88. os.removeObserver(this, "browser-search-engine-modified");
  89. }
  90. // Make sure to break the cycle from _textbox to us. Otherwise we leak
  91. // the world. But make sure it's actually pointing to us.
  92. if (this._textbox.mController.input == this)
  93. this._textbox.mController.input = null;
  94. ]]></destructor>
  95. <field name="_stringBundle">document.getAnonymousElementByAttribute(this,
  96. "anonid", "searchbar-stringbundle");</field>
  97. <field name="_textbox">document.getAnonymousElementByAttribute(this,
  98. "anonid", "searchbar-textbox");</field>
  99. <field name="_popup">document.getAnonymousElementByAttribute(this,
  100. "anonid", "searchbar-popup");</field>
  101. <field name="_ss">null</field>
  102. <field name="_engines">null</field>
  103. <field name="FormHistory" readonly="true">
  104. (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory;
  105. </field>
  106. <property name="engines" readonly="true">
  107. <getter><![CDATA[
  108. if (!this._engines)
  109. this._engines = this.searchService.getVisibleEngines();
  110. return this._engines;
  111. ]]></getter>
  112. </property>
  113. <field name="searchButton">document.getAnonymousElementByAttribute(this,
  114. "anonid", "searchbar-engine-button");</field>
  115. <property name="currentEngine">
  116. <setter><![CDATA[
  117. let ss = this.searchService;
  118. ss.defaultEngine = ss.currentEngine = val;
  119. return val;
  120. ]]></setter>
  121. <getter><![CDATA[
  122. var currentEngine = this.searchService.currentEngine;
  123. // Return a dummy engine if there is no currentEngine
  124. return currentEngine || {name: "", uri: null};
  125. ]]></getter>
  126. </property>
  127. <!-- textbox is used by sanitize.js to clear the undo history when
  128. clearing form information. -->
  129. <property name="textbox" readonly="true"
  130. onget="return this._textbox;"/>
  131. <property name="searchService" readonly="true">
  132. <getter><![CDATA[
  133. if (!this._ss) {
  134. const nsIBSS = Components.interfaces.nsIBrowserSearchService;
  135. this._ss =
  136. Components.classes["@mozilla.org/browser/search-service;1"]
  137. .getService(nsIBSS);
  138. }
  139. return this._ss;
  140. ]]></getter>
  141. </property>
  142. <property name="value" onget="return this._textbox.value;"
  143. onset="return this._textbox.value = val;"/>
  144. <method name="focus">
  145. <body><![CDATA[
  146. this._textbox.focus();
  147. ]]></body>
  148. </method>
  149. <method name="select">
  150. <body><![CDATA[
  151. this._textbox.select();
  152. ]]></body>
  153. </method>
  154. <method name="observe">
  155. <parameter name="aEngine"/>
  156. <parameter name="aTopic"/>
  157. <parameter name="aVerb"/>
  158. <body><![CDATA[
  159. if (aTopic == "browser-search-engine-modified") {
  160. switch (aVerb) {
  161. case "engine-removed":
  162. this.offerNewEngine(aEngine);
  163. break;
  164. case "engine-added":
  165. this.hideNewEngine(aEngine);
  166. break;
  167. case "engine-current":
  168. // The current engine was changed. Rebuilding the menu appears to
  169. // confuse its idea of whether it should be open when it's just
  170. // been clicked, so we force it to close now.
  171. this._popup.hidePopup();
  172. break;
  173. case "engine-changed":
  174. // An engine was removed (or hidden) or added, or an icon was
  175. // changed. Do nothing special.
  176. }
  177. // Make sure the engine list is refetched next time it's needed
  178. this._engines = null;
  179. // Rebuild the popup and update the display after any modification.
  180. this.rebuildPopup();
  181. this.updateDisplay();
  182. }
  183. ]]></body>
  184. </method>
  185. <!-- There are two seaprate lists of search engines, whose uses intersect
  186. in this file. The search service (nsIBrowserSearchService and
  187. nsSearchService.js) maintains a list of Engine objects which is used to
  188. populate the searchbox list of available engines and to perform queries.
  189. That list is accessed here via this.SearchService, and it's that sort of
  190. Engine that is passed to this binding's observer as aEngine.
  191. In addition, browser.js fills two lists of autodetected search engines
  192. (browser.engines and browser.hiddenEngines) as properties of
  193. mCurrentBrowser. Those lists contain unnamed JS objects of the form
  194. { uri:, title:, icon: }, and that's what the searchbar uses to determine
  195. whether to show any "Add <EngineName>" menu items in the drop-down.
  196. The two types of engines are currently related by their identifying
  197. titles (the Engine object's 'name'), although that may change; see bug
  198. 335102. -->
  199. <!-- If the engine that was just removed from the searchbox list was
  200. autodetected on this page, move it to each browser's active list so it
  201. will be offered to be added again. -->
  202. <method name="offerNewEngine">
  203. <parameter name="aEngine"/>
  204. <body><![CDATA[
  205. var allbrowsers = getBrowser().mPanelContainer.childNodes;
  206. for (var tab = 0; tab < allbrowsers.length; tab++) {
  207. var browser = getBrowser().getBrowserAtIndex(tab);
  208. if (browser.hiddenEngines) {
  209. // XXX This will need to be changed when engines are identified by
  210. // URL rather than title; see bug 335102.
  211. var removeTitle = aEngine.wrappedJSObject.name;
  212. for (var i = 0; i < browser.hiddenEngines.length; i++) {
  213. if (browser.hiddenEngines[i].title == removeTitle) {
  214. if (!browser.engines)
  215. browser.engines = [];
  216. browser.engines.push(browser.hiddenEngines[i]);
  217. browser.hiddenEngines.splice(i, 1);
  218. break;
  219. }
  220. }
  221. }
  222. }
  223. ]]></body>
  224. </method>
  225. <!-- If the engine that was just added to the searchbox list was
  226. autodetected on this page, move it to each browser's hidden list so it is
  227. no longer offered to be added. -->
  228. <method name="hideNewEngine">
  229. <parameter name="aEngine"/>
  230. <body><![CDATA[
  231. var allbrowsers = getBrowser().mPanelContainer.childNodes;
  232. for (var tab = 0; tab < allbrowsers.length; tab++) {
  233. var browser = getBrowser().getBrowserAtIndex(tab);
  234. if (browser.engines) {
  235. // XXX This will need to be changed when engines are identified by
  236. // URL rather than title; see bug 335102.
  237. var removeTitle = aEngine.wrappedJSObject.name;
  238. for (var i = 0; i < browser.engines.length; i++) {
  239. if (browser.engines[i].title == removeTitle) {
  240. if (!browser.hiddenEngines)
  241. browser.hiddenEngines = [];
  242. browser.hiddenEngines.push(browser.engines[i]);
  243. browser.engines.splice(i, 1);
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. ]]></body>
  250. </method>
  251. <method name="setIcon">
  252. <parameter name="element"/>
  253. <parameter name="uri"/>
  254. <body><![CDATA[
  255. element.setAttribute("src", uri);
  256. ]]></body>
  257. </method>
  258. <method name="updateDisplay">
  259. <body><![CDATA[
  260. var uri = this.currentEngine.iconURI;
  261. this.setIcon(this, uri ? uri.spec : "");
  262. var name = this.currentEngine.name;
  263. var text = this._stringBundle.getFormattedString("searchtip", [name]);
  264. this._textbox.placeholder = name;
  265. this._textbox.label = text;
  266. this._textbox.tooltipText = text;
  267. ]]></body>
  268. </method>
  269. <!-- Rebuilds the dynamic portion of the popup menu (i.e., the menu items
  270. for new search engines that can be added to the available list). This
  271. is called each time the popup is shown.
  272. -->
  273. <method name="rebuildPopupDynamic">
  274. <body><![CDATA[
  275. // We might not have added the main popup items yet, do that first
  276. // if needed.
  277. if (this._needToBuildPopup)
  278. this.rebuildPopup();
  279. var popup = this._popup;
  280. // Clear any addengine menuitems, including addengine-item entries and
  281. // the addengine-separator. Work backward to avoid invalidating the
  282. // indexes as items are removed.
  283. var items = popup.childNodes;
  284. for (var i = items.length - 1; i >= 0; i--) {
  285. if (items[i].classList.contains("addengine-item") ||
  286. items[i].classList.contains("addengine-separator"))
  287. popup.removeChild(items[i]);
  288. }
  289. var addengines = getBrowser().mCurrentBrowser.engines;
  290. if (addengines && addengines.length > 0) {
  291. const kXULNS =
  292. "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  293. // Find the (first) separator in the remaining menu, or the first item
  294. // if no separators are present.
  295. var insertLocation = popup.firstChild;
  296. while (insertLocation.nextSibling &&
  297. insertLocation.localName != "menuseparator") {
  298. insertLocation = insertLocation.nextSibling;
  299. }
  300. if (insertLocation.localName != "menuseparator")
  301. insertLocation = popup.firstChild;
  302. var separator = document.createElementNS(kXULNS, "menuseparator");
  303. separator.setAttribute("class", "addengine-separator");
  304. popup.insertBefore(separator, insertLocation);
  305. // Insert the "add this engine" items.
  306. for (var i = 0; i < addengines.length; i++) {
  307. var menuitem = document.createElement("menuitem");
  308. var engineInfo = addengines[i];
  309. var labelStr =
  310. this._stringBundle.getFormattedString("cmd_addFoundEngine",
  311. [engineInfo.title]);
  312. menuitem = document.createElementNS(kXULNS, "menuitem");
  313. menuitem.setAttribute("class", "menuitem-iconic addengine-item");
  314. menuitem.setAttribute("label", labelStr);
  315. menuitem.setAttribute("tooltiptext", engineInfo.uri);
  316. menuitem.setAttribute("uri", engineInfo.uri);
  317. if (engineInfo.icon)
  318. this.setIcon(menuitem, engineInfo.icon);
  319. menuitem.setAttribute("title", engineInfo.title);
  320. popup.insertBefore(menuitem, insertLocation);
  321. }
  322. }
  323. ]]></body>
  324. </method>
  325. <!-- Rebuilds the list of visible search engines in the menu. Does not remove
  326. or update any dynamic entries (i.e., "Add this engine" items) nor the
  327. Manage Engines item. This is called by the observer when the list of
  328. visible engines, or the currently selected engine, has changed.
  329. -->
  330. <method name="rebuildPopup">
  331. <body><![CDATA[
  332. var popup = this._popup;
  333. // Clear the popup, down to the first separator
  334. while (popup.firstChild && popup.firstChild.localName != "menuseparator")
  335. popup.removeChild(popup.firstChild);
  336. const kXULNS =
  337. "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  338. var engines = this.engines;
  339. for (var i = engines.length - 1; i >= 0; --i) {
  340. var menuitem = document.createElementNS(kXULNS, "menuitem");
  341. var name = engines[i].name;
  342. menuitem.setAttribute("label", name);
  343. menuitem.setAttribute("id", name);
  344. menuitem.setAttribute("class", "menuitem-iconic searchbar-engine-menuitem menuitem-with-favicon");
  345. // Since this menu is rebuilt by the observer method whenever a new
  346. // engine is selected, the "selected" attribute does not need to be
  347. // explicitly cleared anywhere.
  348. if (engines[i] == this.currentEngine)
  349. menuitem.setAttribute("selected", "true");
  350. var tooltip = this._stringBundle.getFormattedString("searchtip", [name]);
  351. menuitem.setAttribute("tooltiptext", tooltip);
  352. if (engines[i].iconURI)
  353. this.setIcon(menuitem, engines[i].iconURI.spec);
  354. popup.insertBefore(menuitem, popup.firstChild);
  355. menuitem.engine = engines[i];
  356. }
  357. this._needToBuildPopup = false;
  358. ]]></body>
  359. </method>
  360. <method name="openManager">
  361. <parameter name="aEvent"/>
  362. <body><![CDATA[
  363. var wm =
  364. Components.classes["@mozilla.org/appshell/window-mediator;1"]
  365. .getService(Components.interfaces.nsIWindowMediator);
  366. var window = wm.getMostRecentWindow("Browser:SearchManager");
  367. if (window)
  368. window.focus()
  369. else {
  370. setTimeout(function () {
  371. openDialog("chrome://browser/content/search/engineManager.xul",
  372. "_blank", "chrome,dialog,modal,centerscreen,resizable");
  373. }, 0);
  374. }
  375. ]]></body>
  376. </method>
  377. <method name="selectEngine">
  378. <parameter name="aEvent"/>
  379. <parameter name="isNextEngine"/>
  380. <body><![CDATA[
  381. // Find the new index
  382. var newIndex = this.engines.indexOf(this.currentEngine);
  383. newIndex += isNextEngine ? 1 : -1;
  384. if (newIndex >= 0 && newIndex < this.engines.length) {
  385. this.currentEngine = this.engines[newIndex];
  386. }
  387. aEvent.preventDefault();
  388. aEvent.stopPropagation();
  389. ]]></body>
  390. </method>
  391. <method name="handleSearchCommand">
  392. <parameter name="aEvent"/>
  393. <body><![CDATA[
  394. var textBox = this._textbox;
  395. var textValue = textBox.value;
  396. var where = "current";
  397. if (aEvent && aEvent.originalTarget.getAttribute("anonid") == "search-go-button") {
  398. if (aEvent.button == 2)
  399. return;
  400. where = whereToOpenLink(aEvent, false, true);
  401. }
  402. else {
  403. var newTabPref = textBox._prefBranch.getBoolPref("browser.search.openintab");
  404. if ((aEvent && aEvent.altKey) ^ newTabPref)
  405. where = "tab";
  406. }
  407. this.doSearch(textValue, where);
  408. ]]></body>
  409. </method>
  410. <method name="doSearch">
  411. <parameter name="aData"/>
  412. <parameter name="aWhere"/>
  413. <body><![CDATA[
  414. var textBox = this._textbox;
  415. // Save the current value in the form history
  416. if (aData && !PrivateBrowsingUtils.isWindowPrivate(window)) {
  417. this.FormHistory.update(
  418. { op : "bump",
  419. fieldname : textBox.getAttribute("autocompletesearchparam"),
  420. value : aData },
  421. { handleError : function(aError) {
  422. Components.utils.reportError("Saving search to form history failed: " + aError.message);
  423. }});
  424. }
  425. // null parameter below specifies HTML response for search
  426. var submission = this.currentEngine.getSubmission(aData);
  427. openUILinkIn(submission.uri.spec, aWhere, null, submission.postData);
  428. ]]></body>
  429. </method>
  430. </implementation>
  431. <handlers>
  432. <handler event="command"><![CDATA[
  433. const target = event.originalTarget;
  434. if (target.engine) {
  435. this.currentEngine = target.engine;
  436. } else if (target.classList.contains("addengine-item")) {
  437. var searchService =
  438. Components.classes["@mozilla.org/browser/search-service;1"]
  439. .getService(Components.interfaces.nsIBrowserSearchService);
  440. // We only detect OpenSearch files
  441. var type = Components.interfaces.nsISearchEngine.DATA_XML;
  442. // Select the installed engine if the installation succeeds
  443. var installCallback = {
  444. onSuccess: engine => this.currentEngine = engine
  445. }
  446. searchService.addEngine(target.getAttribute("uri"), type,
  447. target.getAttribute("src"), false,
  448. installCallback);
  449. }
  450. else
  451. return;
  452. this.focus();
  453. this.select();
  454. ]]></handler>
  455. <handler event="popupshowing" action="this.rebuildPopupDynamic();"/>
  456. <handler event="DOMMouseScroll"
  457. phase="capturing"
  458. modifiers="accel"
  459. action="this.selectEngine(event, (event.detail > 0));"/>
  460. <handler event="focus">
  461. <![CDATA[
  462. // Speculatively connect to the current engine's search URI (and
  463. // suggest URI, if different) to reduce request latency
  464. const SUGGEST_TYPE = "application/x-suggestions+json";
  465. var engine = this.currentEngine;
  466. var connector =
  467. Services.io.QueryInterface(Components.interfaces.nsISpeculativeConnect);
  468. var searchURI = engine.getSubmission("dummy").uri;
  469. let callbacks = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
  470. .getInterface(Components.interfaces.nsIWebNavigation)
  471. .QueryInterface(Components.interfaces.nsILoadContext);
  472. connector.speculativeConnect(searchURI, callbacks);
  473. if (engine.supportsResponseType(SUGGEST_TYPE)) {
  474. var suggestURI = engine.getSubmission("dummy", SUGGEST_TYPE).uri;
  475. if (suggestURI.prePath != searchURI.prePath)
  476. connector.speculativeConnect(suggestURI, callbacks);
  477. }
  478. ]]></handler>
  479. </handlers>
  480. </binding>
  481. <binding id="searchbar-textbox"
  482. extends="chrome://global/content/bindings/autocomplete.xml#autocomplete">
  483. <implementation implements="nsIObserver">
  484. <constructor><![CDATA[
  485. const kXULNS =
  486. "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  487. if (document.getBindingParent(this).parentNode.parentNode.localName ==
  488. "toolbarpaletteitem")
  489. return;
  490. // Initialize fields
  491. this._stringBundle = document.getBindingParent(this)._stringBundle;
  492. this._prefBranch =
  493. Components.classes["@mozilla.org/preferences-service;1"]
  494. .getService(Components.interfaces.nsIPrefBranch);
  495. this._suggestEnabled =
  496. this._prefBranch.getBoolPref("browser.search.suggest.enabled");
  497. this._clickSelectsAll =
  498. this._prefBranch.getBoolPref("browser.urlbar.clickSelectsAll");
  499. this.setAttribute("clickSelectsAll", this._clickSelectsAll);
  500. // Add items to context menu and attach controller to handle them
  501. var textBox = document.getAnonymousElementByAttribute(this,
  502. "anonid", "textbox-input-box");
  503. var cxmenu = document.getAnonymousElementByAttribute(textBox,
  504. "anonid", "input-box-contextmenu");
  505. var pasteAndSearch;
  506. cxmenu.addEventListener("popupshowing", function() {
  507. if (!pasteAndSearch)
  508. return;
  509. var controller = document.commandDispatcher.getControllerForCommand("cmd_paste");
  510. var enabled = controller.isCommandEnabled("cmd_paste");
  511. if (enabled)
  512. pasteAndSearch.removeAttribute("disabled");
  513. else
  514. pasteAndSearch.setAttribute("disabled", "true");
  515. }, false);
  516. var element, label, akey;
  517. element = document.createElementNS(kXULNS, "menuseparator");
  518. cxmenu.appendChild(element);
  519. var insertLocation = cxmenu.firstChild;
  520. while (insertLocation.nextSibling &&
  521. insertLocation.getAttribute("cmd") != "cmd_paste")
  522. insertLocation = insertLocation.nextSibling;
  523. if (insertLocation) {
  524. element = document.createElementNS(kXULNS, "menuitem");
  525. label = this._stringBundle.getString("cmd_pasteAndSearch");
  526. element.setAttribute("label", label);
  527. element.setAttribute("anonid", "paste-and-search");
  528. element.setAttribute("oncommand",
  529. "BrowserSearch.searchBar.select(); goDoCommand('cmd_paste'); BrowserSearch.searchBar.handleSearchCommand();");
  530. cxmenu.insertBefore(element, insertLocation.nextSibling);
  531. pasteAndSearch = element;
  532. }
  533. element = document.createElementNS(kXULNS, "menuitem");
  534. label = this._stringBundle.getString("cmd_clearHistory");
  535. akey = this._stringBundle.getString("cmd_clearHistory_accesskey");
  536. element.setAttribute("label", label);
  537. element.setAttribute("accesskey", akey);
  538. element.setAttribute("cmd", "cmd_clearhistory");
  539. cxmenu.appendChild(element);
  540. element = document.createElementNS(kXULNS, "menuitem");
  541. label = this._stringBundle.getString("cmd_showSuggestions");
  542. akey = this._stringBundle.getString("cmd_showSuggestions_accesskey");
  543. element.setAttribute("anonid", "toggle-suggest-item");
  544. element.setAttribute("label", label);
  545. element.setAttribute("accesskey", akey);
  546. element.setAttribute("cmd", "cmd_togglesuggest");
  547. element.setAttribute("type", "checkbox");
  548. element.setAttribute("checked", this._suggestEnabled);
  549. element.setAttribute("autocheck", "false");
  550. this._suggestMenuItem = element;
  551. cxmenu.appendChild(element);
  552. this.controllers.appendController(this.searchbarController);
  553. // Add observer for suggest preference
  554. var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  555. .getService(Components.interfaces.nsIPrefBranch);
  556. prefs.addObserver("browser.search.suggest.enabled", this, false);
  557. prefs.addObserver("browser.urlbar.clickSelectsAll", this, false);
  558. ]]></constructor>
  559. <destructor><![CDATA[
  560. var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  561. .getService(Components.interfaces.nsIPrefBranch);
  562. prefs.removeObserver("browser.search.suggest.enabled", this);
  563. prefs.removeObserver("browser.urlbar.clickSelectsAll", this);
  564. // Because XBL and the customize toolbar code interacts poorly,
  565. // there may not be anything to remove here
  566. try {
  567. this.controllers.removeController(this.searchbarController);
  568. } catch (ex) { }
  569. ]]></destructor>
  570. <field name="_stringBundle"/>
  571. <field name="_prefBranch"/>
  572. <field name="_suggestMenuItem"/>
  573. <field name="_suggestEnabled"/>
  574. <field name="_clickSelectsAll"/>
  575. <!--
  576. This overrides the searchParam property in autocomplete.xml. We're
  577. hijacking this property as a vehicle for delivering the privacy
  578. information about the window into the guts of nsSearchSuggestions.
  579. Note that the setter is the same as the parent. We were not sure whether
  580. we can override just the getter. If that proves to be the case, the setter
  581. can be removed.
  582. -->
  583. <property name="searchParam"
  584. onget="return this.getAttribute('autocompletesearchparam') +
  585. (PrivateBrowsingUtils.isWindowPrivate(window) ? '|private' : '');"
  586. onset="this.setAttribute('autocompletesearchparam', val); return val;"/>
  587. <!--
  588. This method overrides the autocomplete binding's openPopup (essentially
  589. duplicating the logic from the autocomplete popup binding's
  590. openAutocompletePopup method), modifying it so that the popup is aligned with
  591. the inner textbox, but sized to not extend beyond the search bar border.
  592. -->
  593. <method name="openPopup">
  594. <body><![CDATA[
  595. var popup = this.popup;
  596. if (!popup.mPopupOpen) {
  597. // Initially the panel used for the searchbar (PopupAutoComplete
  598. // in browser.xul) is hidden to avoid impacting startup / new
  599. // window performance. The base binding's openPopup would normally
  600. // call the overriden openAutocompletePopup in urlbarBindings.xml's
  601. // browser-autocomplete-result-popup binding to unhide the popup,
  602. // but since we're overriding openPopup we need to unhide the panel
  603. // ourselves.
  604. popup.hidden = false;
  605. popup.mInput = this;
  606. popup.view = this.controller.QueryInterface(Components.interfaces.nsITreeView);
  607. popup.invalidate();
  608. popup.showCommentColumn = this.showCommentColumn;
  609. popup.showImageColumn = this.showImageColumn;
  610. document.popupNode = null;
  611. const isRTL = getComputedStyle(this, "").direction == "rtl";
  612. var outerRect = this.getBoundingClientRect();
  613. var innerRect = this.inputField.getBoundingClientRect();
  614. if (isRTL) {
  615. var width = innerRect.right - outerRect.left;
  616. } else {
  617. var width = outerRect.right - innerRect.left;
  618. }
  619. popup.setAttribute("width", width > 100 ? width : 100);
  620. var yOffset = outerRect.bottom - innerRect.bottom;
  621. popup.openPopup(this.inputField, "after_start", 0, yOffset, false, false);
  622. }
  623. ]]></body>
  624. </method>
  625. <method name="observe">
  626. <parameter name="aSubject"/>
  627. <parameter name="aTopic"/>
  628. <parameter name="aData"/>
  629. <body><![CDATA[
  630. if (aTopic == "nsPref:changed") {
  631. switch (aData) {
  632. case "browser.search.suggest.enabled":
  633. this._suggestEnabled = this._prefBranch.getBoolPref(aData);
  634. this._suggestMenuItem.setAttribute("checked", this._suggestEnabled);
  635. break;
  636. case "browser.urlbar.clickSelectsAll":
  637. this._clickSelectsAll = this._prefBranch.getBoolPref(aData);
  638. this.setAttribute("clickSelectsAll", this._clickSelectsAll);
  639. break;
  640. }
  641. }
  642. ]]></body>
  643. </method>
  644. <method name="openSearch">
  645. <body>
  646. <![CDATA[
  647. // Don't open search popup if history popup is open
  648. if (!this.popupOpen) {
  649. document.getBindingParent(this).searchButton.open = true;
  650. return false;
  651. }
  652. return true;
  653. ]]>
  654. </body>
  655. </method>
  656. <!-- override |onTextEntered| in autocomplete.xml -->
  657. <method name="onTextEntered">
  658. <parameter name="aEvent"/>
  659. <body><![CDATA[
  660. var evt = aEvent || this.mEnterEvent;
  661. document.getBindingParent(this).handleSearchCommand(evt);
  662. this.mEnterEvent = null;
  663. ]]></body>
  664. </method>
  665. <!-- nsIController -->
  666. <field name="searchbarController" readonly="true"><![CDATA[({
  667. _self: this,
  668. supportsCommand: function(aCommand) {
  669. return aCommand == "cmd_clearhistory" ||
  670. aCommand == "cmd_togglesuggest";
  671. },
  672. isCommandEnabled: function(aCommand) {
  673. return true;
  674. },
  675. doCommand: function (aCommand) {
  676. switch (aCommand) {
  677. case "cmd_clearhistory":
  678. var param = this._self.getAttribute("autocompletesearchparam");
  679. let searchBar = this._self.parentNode;
  680. BrowserSearch.searchBar.FormHistory.update({ op : "remove", fieldname : param }, null);
  681. this._self.value = "";
  682. break;
  683. case "cmd_togglesuggest":
  684. // The pref observer will update _suggestEnabled and the menu
  685. // checkmark.
  686. this._self._prefBranch.setBoolPref("browser.search.suggest.enabled",
  687. !this._self._suggestEnabled);
  688. break;
  689. default:
  690. // do nothing with unrecognized command
  691. }
  692. }
  693. })]]></field>
  694. </implementation>
  695. <handlers>
  696. <handler event="keypress" keycode="VK_UP" modifiers="accel"
  697. phase="capturing"
  698. action="document.getBindingParent(this).selectEngine(event, false);"/>
  699. <handler event="keypress" keycode="VK_DOWN" modifiers="accel"
  700. phase="capturing"
  701. action="document.getBindingParent(this).selectEngine(event, true);"/>
  702. <handler event="keypress" keycode="VK_DOWN" modifiers="alt"
  703. phase="capturing"
  704. action="return this.openSearch();"/>
  705. <handler event="keypress" keycode="VK_UP" modifiers="alt"
  706. phase="capturing"
  707. action="return this.openSearch();"/>
  708. <handler event="keypress" keycode="VK_F4"
  709. phase="capturing"
  710. action="return this.openSearch();"/>
  711. <handler event="dragover">
  712. <![CDATA[
  713. var types = event.dataTransfer.types;
  714. if (types.contains("text/plain") || types.contains("text/x-moz-text-internal"))
  715. event.preventDefault();
  716. ]]>
  717. </handler>
  718. <handler event="drop">
  719. <![CDATA[
  720. var dataTransfer = event.dataTransfer;
  721. var data = dataTransfer.getData("text/plain");
  722. if (!data)
  723. data = dataTransfer.getData("text/x-moz-text-internal");
  724. if (data) {
  725. event.preventDefault();
  726. this.value = data;
  727. this.onTextEntered(event);
  728. }
  729. ]]>
  730. </handler>
  731. </handlers>
  732. </binding>
  733. </bindings>