From 0933676efd955bee08d64e2f2f9450e705b114e2 Mon Sep 17 00:00:00 2001 From: "gavin%gavinsharp.com" Date: Fri, 17 Mar 2006 07:16:00 +0000 Subject: [PATCH] Bug 317107: new search service, r=mconnor, sr=ben --- allmakefiles.sh | 1 + browser/app/profile/firefox.js | 10 +- browser/base/content/browser-context.inc | 3 +- browser/base/content/browser-menubar.inc | 2 +- browser/base/content/browser-sets.inc | 2 +- browser/base/content/browser.css | 2 +- browser/base/content/browser.js | 290 ++- browser/base/content/browser.xul | 4 +- browser/base/content/search.xml | 0 browser/base/content/searchDialog.css | 0 browser/base/content/searchDialog.js | 0 browser/base/content/searchDialog.xul | 0 browser/base/content/searchbarBindings.css | 0 browser/base/jar.mn | 5 - browser/components/Makefile.in | 1 + .../bookmarks/content/bookmarksTree.xml | 6 +- browser/components/search/Makefile.in | 53 + browser/components/search/content/search.xml | 606 +++-- .../search/content/searchDialog.css | 0 .../components/search/content/searchDialog.js | 0 .../search/content/searchDialog.xul | 0 .../search/content/searchbarBindings.css | 5 +- browser/components/search/jar.mn | 3 + .../search/nsIBrowserSearchService.idl | 288 +++ browser/components/search/nsSearchService.js | 2071 +++++++++++++++++ browser/components/sidebar/src/nsSidebar.js | 16 +- browser/installer/removed-files.in | 12 + browser/installer/unix/packages-static | 2 + browser/installer/windows/packages-static | 2 + browser/locales/Makefile.in | 4 +- .../chrome/browser-region/region.properties | 4 +- .../en-US/chrome/browser/browser.properties | 2 +- .../en-US/chrome/browser/search.properties | 15 + .../en-US/chrome/browser/searchDialog.dtd | 0 .../en-US/chrome/browser/searchbar.properties | 0 .../chrome/browser/sidebar/sidebar.properties | 2 +- .../en-US/searchplugins/amazondotcom.png | 0 .../en-US/searchplugins/amazondotcom.src | 0 .../en-US/searchplugins/amazondotcom.xml | 12 + .../locales/en-US/searchplugins/answers.png | 0 .../locales/en-US/searchplugins/answers.src | 0 .../locales/en-US/searchplugins/answers.xml | 10 + .../en-US/searchplugins/creativecommons.png | 0 .../en-US/searchplugins/creativecommons.src | 0 .../en-US/searchplugins/creativecommons.xml | 10 + .../en-US/searchplugins/dictionary.src | 0 browser/locales/en-US/searchplugins/eBay.gif | 0 browser/locales/en-US/searchplugins/eBay.src | 0 browser/locales/en-US/searchplugins/eBay.xml | 16 + .../locales/en-US/searchplugins/google.gif | 0 .../locales/en-US/searchplugins/google.src | 0 .../locales/en-US/searchplugins/google.xml | 15 + browser/locales/en-US/searchplugins/list.txt | 8 +- browser/locales/en-US/searchplugins/yahoo.gif | 0 browser/locales/en-US/searchplugins/yahoo.src | 0 browser/locales/en-US/searchplugins/yahoo.xml | 11 + browser/locales/jar.mn | 3 +- 57 files changed, 2969 insertions(+), 527 deletions(-) delete mode 100644 browser/base/content/search.xml delete mode 100644 browser/base/content/searchDialog.css delete mode 100644 browser/base/content/searchDialog.js delete mode 100644 browser/base/content/searchDialog.xul delete mode 100644 browser/base/content/searchbarBindings.css create mode 100644 browser/components/search/Makefile.in delete mode 100644 browser/components/search/content/searchDialog.css delete mode 100644 browser/components/search/content/searchDialog.js delete mode 100644 browser/components/search/content/searchDialog.xul create mode 100755 browser/components/search/jar.mn create mode 100755 browser/components/search/nsIBrowserSearchService.idl create mode 100755 browser/components/search/nsSearchService.js create mode 100755 browser/locales/en-US/chrome/browser/search.properties delete mode 100644 browser/locales/en-US/chrome/browser/searchDialog.dtd delete mode 100644 browser/locales/en-US/chrome/browser/searchbar.properties delete mode 100755 browser/locales/en-US/searchplugins/amazondotcom.png delete mode 100755 browser/locales/en-US/searchplugins/amazondotcom.src create mode 100755 browser/locales/en-US/searchplugins/amazondotcom.xml delete mode 100755 browser/locales/en-US/searchplugins/answers.png delete mode 100644 browser/locales/en-US/searchplugins/answers.src create mode 100755 browser/locales/en-US/searchplugins/answers.xml delete mode 100644 browser/locales/en-US/searchplugins/creativecommons.png delete mode 100644 browser/locales/en-US/searchplugins/creativecommons.src create mode 100755 browser/locales/en-US/searchplugins/creativecommons.xml delete mode 100755 browser/locales/en-US/searchplugins/dictionary.src delete mode 100755 browser/locales/en-US/searchplugins/eBay.gif delete mode 100755 browser/locales/en-US/searchplugins/eBay.src create mode 100755 browser/locales/en-US/searchplugins/eBay.xml delete mode 100644 browser/locales/en-US/searchplugins/google.gif delete mode 100644 browser/locales/en-US/searchplugins/google.src create mode 100755 browser/locales/en-US/searchplugins/google.xml delete mode 100755 browser/locales/en-US/searchplugins/yahoo.gif delete mode 100755 browser/locales/en-US/searchplugins/yahoo.src create mode 100755 browser/locales/en-US/searchplugins/yahoo.xml diff --git a/allmakefiles.sh b/allmakefiles.sh index 4a3eb740ee7..b334b7eef10 100755 --- a/allmakefiles.sh +++ b/allmakefiles.sh @@ -916,6 +916,7 @@ browser/components/migration/Makefile browser/components/migration/public/Makefile browser/components/migration/src/Makefile browser/components/preferences/Makefile +browser/components/search/Makefile browser/components/sidebar/Makefile browser/components/sidebar/src/Makefile browser/components/shell/Makefile diff --git a/browser/app/profile/firefox.js b/browser/app/profile/firefox.js index 2860e8305ca..df0aded107e 100644 --- a/browser/app/profile/firefox.js +++ b/browser/app/profile/firefox.js @@ -205,8 +205,10 @@ pref("browser.download.manager.flashCount", 2); // pointer to the default engine name pref("browser.search.defaultenginename", "chrome://browser-region/locale/region.properties"); -// pointer to the Web Search url (content area context menu) -pref("browser.search.defaulturl", "chrome://browser-region/locale/region.properties"); + +// disable logging for the search service by default +pref("browser.search.log", false); + // Ordering of Search Engines in the Engine list. pref("browser.search.order.1", "chrome://browser-region/locale/region.properties"); pref("browser.search.order.2", "chrome://browser-region/locale/region.properties"); @@ -217,10 +219,6 @@ pref("browser.search.order.Yahoo.1", "chrome://branding/content/searchc pref("browser.search.order.Yahoo.2", "chrome://branding/content/searchconfig.properties"); pref("browser.search.order.Yahoo", "chrome://branding/content/searchconfig.properties"); -// basic search popup constraint: minimum sherlock plugin version displayed -// (note: must be a string representation of a float or it'll default to 0.0) -pref("browser.search.basic.min_ver", "0.0"); - // search bar results always open in a new tab pref("browser.search.openintab", false); diff --git a/browser/base/content/browser-context.inc b/browser/base/content/browser-context.inc index ff700787e49..74b8f18b7b0 100644 --- a/browser/base/content/browser-context.inc +++ b/browser/base/content/browser-context.inc @@ -198,7 +198,7 @@ oncommand="AddKeywordForSearchField();"/> + oncommand="BrowserSearch.loadSearch(getBrowserSelection(), true);"/> @@ -262,4 +262,3 @@ accesskey="&bidiSwitchPageDirectionItem.accesskey;" oncommand="SwitchDocumentDirection(window.content);"/> - diff --git a/browser/base/content/browser-menubar.inc b/browser/base/content/browser-menubar.inc index 89359cd31a5..acae0007ce6 100644 --- a/browser/base/content/browser-menubar.inc +++ b/browser/base/content/browser-menubar.inc @@ -406,7 +406,7 @@ #ifndef MOZ_PLACES - + diff --git a/browser/base/content/browser.css b/browser/base/content/browser.css index d83358275a7..a3285e520d9 100644 --- a/browser/base/content/browser.css +++ b/browser/base/content/browser.css @@ -1,5 +1,5 @@ searchbar { - -moz-binding: url("chrome://browser/content/search.xml#searchbar"); + -moz-binding: url("chrome://browser/content/search/search.xml#searchbar"); } /* ::::: print preview toolbar ::::: */ diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index ed35a67d1ae..a73533a1d6f 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -1806,27 +1806,6 @@ function BrowserLoadURL(aTriggeringEvent, aPostData) content.focus(); } -function delayedSearchLoadURL(aURL, aInNewTab) { - setTimeout(SearchLoadURL, 0, aURL, aInNewTab); -} - -function SearchLoadURL(aURL, aInNewTab) -{ - if (!aURL) - return; - - if (gBrowser.localName == "tabbrowser" && aInNewTab) { - content.focus(); - // Modal action, remember last viewed tab - gBrowser.loadOneTab(aURL, null, null, null, false); - if (gURLBar) - gURLBar.value = aURL; - } - else - loadURI(aURL, null, null); - content.focus(); -} - function getShortcutOrURI(aURL, aPostDataRef) { // rjc: added support for URL shortcuts (3/30/1999) @@ -2788,107 +2767,83 @@ var DownloadsButtonDNDObserver = { } } -function WebSearch() -{ +const BrowserSearch = { + /** + * Gives focus to the search bar, if it is present on the toolbar, or loads + * the default engine's search form otherwise. For Mac, opens a new window + * or focuses an existing window, if necessary. + */ + webSearch: function BrowserSearch_webSearch() { #ifdef XP_MACOSX - if (window.location.href != getBrowserURL()) { - var win = getTopWin(); - if (win) { - // If there's an open browser window, it should handle this command - win.focus() - win.WebSearch(); - } - else { - // If there are no open browser windows, open a new one - win = window.openDialog("chrome://browser/content/", "_blank", - "chrome,all,dialog=no", "about:blank"); - win.addEventListener("load", WebSearchCallback, false); - } - return; - } -#endif - var searchBar = document.getElementById("searchbar"); - if (searchBar && !searchBar.parentNode.parentNode.collapsed && - !(window.getComputedStyle(searchBar.parentNode, null).display == "none")) { - searchBar.select(); - searchBar.focus(); - } else { - openDialog("chrome://browser/content/searchDialog.xul", "", - "chrome,dialog,resizable,modal"); - } -} - -function WebSearchCallback() { - // make sure the DOM is ready - setTimeout(function() { this.WebSearch(); }, 0); -} - -function OpenSearch(tabName, searchStr, newTabFlag) -{ - //This function needs to be split up someday. - //XXXnoririty I don't want any prefs switching open by tabs to window - //XXXpch: this routine needs to be cleaned up. - - var defaultSearchURL = null; - var navigatorRegionBundle = document.getElementById("bundle_browser_region"); - var fallbackDefaultSearchURL = navigatorRegionBundle.getString("fallbackDefaultSearchURL"); - - //Check to see if search string contains "://" or "ftp." or white space. - //If it does treat as url and match for pattern - - var urlmatch= /(:\/\/|^ftp\.)[^ \S]+$/ - var forceAsURL = urlmatch.test(searchStr); - - try { - defaultSearchURL = gPrefService.getComplexValue("browser.search.defaulturl", - Components.interfaces.nsIPrefLocalizedString).data; - } catch (ex) { - } - - // Fallback to a default url (one that we can get sidebar search results for) - if (!defaultSearchURL) - defaultSearchURL = fallbackDefaultSearchURL; - - if (!searchStr) { - BrowserSearchInternet(); - } else { - - //Check to see if location bar field is a url - //If it is a url go to URL. A Url is "://" or "." as commented above - //Otherwise search on entry - if (forceAsURL) { - BrowserLoadURL(null, null) - } else { - if (searchStr) { - var escapedSearchStr = encodeURIComponent(searchStr); - defaultSearchURL += escapedSearchStr; - var searchDS = Components.classes["@mozilla.org/rdf/datasource;1?name=internetsearch"] - .getService(Components.interfaces.nsIInternetSearchService); - - searchDS.RememberLastSearchText(escapedSearchStr); - try { - var searchEngineURI = gPrefService.getCharPref("browser.search.defaultengine"); - if (searchEngineURI) { - var searchURL = getSearchUrl("actionButton"); - if (searchURL) { - defaultSearchURL = searchURL + escapedSearchStr; - } else { - searchURL = searchDS.GetInternetSearchURL(searchEngineURI, escapedSearchStr, 0, 0, {value:0}); - if (searchURL) - defaultSearchURL = searchURL; - } - } - } catch (ex) { - } - - if (!newTabFlag) { - loadURI(defaultSearchURL, null, null); - } - else { - getBrowser().loadOneTab(defaultSearchURL); - } + if (window.location.href != getBrowserURL()) { + var win = getTopWin(); + if (win) { + // If there's an open browser window, it should handle this command + win.focus() + win.BrowserSearch.webSearch(); + } else { + // If there are no open browser windows, open a new one + win = window.openDialog("chrome://browser/content/", "_blank", + "chrome,all,dialog=no", "about:blank"); + win.addEventListener("load", BrowserSearch.WebSearch, false); } + return; } +#endif + var searchBar = this.getSearchBar(); + if (searchBar) { + searchBar.select(); + searchBar.focus(); + } else { + var ss = Cc["@mozilla.org/browser/search-service;1"]. + getService(Ci.nsIBrowserSearchService); + var searchForm = ss.defaultEngine.searchForm; + loadURI(searchForm, null, null); + } + }, + + /** + * Loads a search results page, given a set of search terms. Uses the current + * engine if the search bar is visible, or the default engine otherwise. + * + * @param searchText + * The search terms to use for the search. + * + * @param useNewTab + * Boolean indicating whether or not the search should load in a new + * tab. + */ + loadSearch: function BrowserSearch_search(searchText, useNewTab) { + var ss = Cc["@mozilla.org/browser/search-service;1"]. + getService(Ci.nsIBrowserSearchService); + var engine; + + // If the search bar is visible, use the current engine, otherwise, fall back + // to the default engine. + if (this.getSearchBar()) + engine = ss.currentEngine; + else + engine = ss.defaultEngine; + + var submission = engine.getSubmission(searchText); + + if (useNewTab) { + getBrowser().loadOneTab(submission.uri.spec, null, null, + submission.postData, false); + } else + loadURI(submission.uri.spec, null, submission.postData); + }, + + /** + * Returns the search bar element if it is present in the toolbar and not + * hidden, null otherwise. + */ + getSearchBar: function BrowserSearch_getSearchBar() { + var searchBar = document.getElementById("searchbar"); + if (searchBar && !searchBar.parentNode.parentNode.collapsed && + !(window.getComputedStyle(searchBar.parentNode, null).display == "none")) + return searchBar; + return null; } } @@ -4903,24 +4858,35 @@ nsContextMenu.prototype = { return text; }, - // Get selected object and convert it to a string to get - // selected text. Only display the first 15 chars. + // Get selected text. Only display the first 15 chars. isTextSelection : function() { - var result = false; - var selection = this.searchSelected(16); + // Get 16 characters, so that we can trim the selection if it's greater + // than 15 chars + var selectedText = getBrowserSelection(16); - var searchSelectText; - if (selection) { - searchSelectText = selection.toString(); - if (searchSelectText.length > 15) - searchSelectText = searchSelectText.substr(0,15) + "..."; - result = true; + if (!selectedText) + return false; - // format "Search for " string to show in menu - searchSelectText = gNavigatorBundle.getFormattedString("searchText", [searchSelectText]); - this.setItemAttr("context-searchselect", "label", searchSelectText); - } - return result; + if (selectedText.length > 15) + selectedText = selectedText.substr(0,15) + "..."; + + // Use the current engine if the search bar is visible, the default + // engine otherwise. + var engineName = ""; + var ss = Cc["@mozilla.org/browser/search-service;1"]. + getService(Ci.nsIBrowserSearchService); + if (BrowserSearch.getSearchBar()) + engineName = ss.currentEngine.name; + else + engineName = ss.defaultEngine.name; + + // format "Search for " string to show in menu + var menuLabel = gNavigatorBundle.getFormattedString("contextMenuSearchText", + [engineName, + selectedText]); + this.setItemAttr("context-searchselect", "label", menuLabel); + + return true; }, // Returns true if anything is selected. @@ -4928,25 +4894,6 @@ nsContextMenu.prototype = { return !document.commandDispatcher.focusedWindow.getSelection().isCollapsed; }, - searchSelected : function( charlen ) { - var focusedWindow = document.commandDispatcher.focusedWindow; - var searchStr = focusedWindow.getSelection(); - searchStr = searchStr.toString(); - // searching for more than 150 chars makes no sense - if (!charlen) - charlen = 150; - if (charlen < searchStr.length) { - // only use the first charlen important chars. see bug 221361 - var pattern = new RegExp("^(?:\\s*.){0," + charlen + "}"); - pattern.test(searchStr); - searchStr = RegExp.lastMatch; - } - searchStr = searchStr.replace(/^\s+/, ""); - searchStr = searchStr.replace(/\s+$/, ""); - searchStr = searchStr.replace(/\s+/g, " "); - return searchStr; - }, - toString : function () { return "contextMenu.target = " + this.target + "\n" + "contextMenu.onImage = " + this.onImage + "\n" + @@ -4955,6 +4902,7 @@ nsContextMenu.prototype = { "contextMenu.inFrame = " + this.inFrame + "\n" + "contextMenu.hasBGImage = " + this.hasBGImage + "\n"; }, + isTargetATextBox : function ( node ) { if (node instanceof HTMLInputElement) @@ -5001,6 +4949,41 @@ nsContextMenu.prototype = { } } +/** + * Gets the selected text in the active browser. Leading and trailing + * whitespace is removed, and consecutive whitespace is replaced by a single + * space. A maximum of 150 characters will be returned, regardless of the value + * of aCharLen. + * + * @param aCharLen + * The maximum number of characters to return. + */ +function getBrowserSelection(aCharLen) { + // selections of more than 150 characters aren't useful + const kMaxSelectionLen = 150; + const charLen = Math.min(aCharLen || kMaxSelectionLen, kMaxSelectionLen); + + var focusedWindow = document.commandDispatcher.focusedWindow; + var selection = focusedWindow.getSelection().toString(); + + if (selection) { + if (selection.length > charLen) { + // only use the first charLen important chars. see bug 221361 + var pattern = new RegExp("^(?:\\s*.){0," + charLen + "}"); + pattern.test(selection); + selection = RegExp.lastMatch; + } + + selection = selection.replace(/^\s+/, "") + .replace(/\s+$/, "") + .replace(/\s+/g, " "); + + if (selection.length > charLen) + selection = selection.substr(0, charLen); + } + return selection; +} + var gWebPanelURI; function openWebPanel(aTitle, aURI) { @@ -6557,3 +6540,4 @@ var BookmarksEventHandler = { #include ../../../toolkit/content/debug.js #endif + diff --git a/browser/base/content/browser.xul b/browser/base/content/browser.xul index 57ade544379..a9628a67423 100644 --- a/browser/base/content/browser.xul +++ b/browser/base/content/browser.xul @@ -301,7 +301,7 @@ onpopuphidden="BookmarksToolbar.unsetOpenedMenu(event);">