Firefox/URL Bar Algorithm: Difference between revisions

(Created page with "This page documents the steps Firefox takes as a user types some text into the URL bar, and when they press "Enter" or "Go". ==While The User Is Typing== From the very first ch...")
 
 
(8 intermediate revisions by 3 users not shown)
Line 9: Line 9:
* open tabs titles and URLs
* open tabs titles and URLs


with search results limited to some subset of those sources if requested, according to [http://support.mozilla.com/en-US/kb/Location%20bar%20autocomplete#w_changing-results-on-the-fly this algorithm].
The results are sorted in the following order:
 
* Keywords: If the first search string matches a bookmark keyword, the first result will be the keyword search URL. Any subsequent search strings will be added to the URL in the result. For example, a Google keyword search will show a result for "google" of "http://www.google.com/search?q=", with the result for "google foo" being "http://www.google.com/search?q=foo".
* Adaptive matching: The next set of results will be those for which the typed string matches a previous selection from the results for that same typed string. For example, if I often type "goo" and select http://www.google.com from the results, then that URL will quickly become the top matching result when I type "goo".
* Frecency: The rest of the results are sorted according to the [https://developer.mozilla.org/en/The_Places_frecency_algorithm Places Frecency algorithm].
 
Search results can be limited to some subset of the possible sources, according to [http://support.mozilla.com/en-US/kb/Location%20bar%20autocomplete#w_changing-results-on-the-fly this algorithm]. A slightly older description of this process is [http://stackoverflow.com/questions/540725/how-does-firefoxs-awesome-bar-match-strings/1208458#1208458 also available].


==When Enter Is Pressed==
==When Enter Is Pressed==
(Adding a Ctrl or Shift modifier changes the behaviour; the below is the unmodified algorithm.)


==Before Network==
==Before Network==
* The command is handled by urlbarBindings.xml's "urlbar" binding's handleCommand method
** It first calls _canonizeURL (urlbarBindings.xml), which handles Ctrl/Shift/Alt modifier suffix addition (e.g. Ctrl+Enter to add "www." and ".com")
** _canonizeURL also calls getShortcutOrURI (browser.js)
*** If the string contains a space, split on the first space and examine the first token.
**** If it's a search engine keyword, expand using the relevant search URL.
**** If it's a user-defined keyword, expand that.
** With the URL filtered through _canonizeURL, and the right tab/window obtained via handleCommand's modifier checking code, call nsDocShell::LoadURI with LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP


Function [http://mxr.mozilla.org/mozilla-central/source/browser/base/content/browser.js#2297 getShortCutOrURI in browser.js].
..


* If the string contains a space, split on the first space and examine the first token.
* If it looks like a search according to [http://mxr.mozilla.org/mozilla-central/source/docshell/base/nsDefaultURIFixup.cpp#804 this algorithm], insert it into the <tt>keyword.URL</tt> preference value to produce a search URL, since the <code>keyword.enabled</code> preference defaults to true in Firefox.
* If it's a search engine keyword, expand using the relevant search URL.
** Note: "insert it into the <tt>keyword.URL</tt> preference" now means "perform a search using the default search engine", when keyword.URL's value is empty (the default in Firefox)
* If it's a user-defined keyword, expand that.
** The algorithm says it's a search if it contains a space or a quote before the first dot, colon or question mark, or if it starts with a question mark.


...
...


* If it looks like a search according to [http://mxr.mozilla.org/mozilla-central/source/docshell/base/nsDefaultURIFixup.cpp#804 this algorithm], insert it into the <tt>keyword.URL</tt> preference value to produce a search URL.
* If we're looking at something that does not have a scheme specified, prepend either "ftp://" or "http://" depending on whether <code>nsDefaultURIFixup::IsLikelyFTP</code> returns true.
* The algorithm says it's a search if it contains a space or a quote before the first dot, colon or question mark, or if it starts with a question mark.
* Try to construct an <code>nsIURI</code>. If that fails, fall back on producing a search url from <tt>keyword.URL</tt>, since again <code>keyword.enabled</code> defaults to true.
* Start the network load.


==Network==
==Network==


...
# Since the <code>keyword.enabled</code> preference is true, if DNS lookup for the hostname failed and the URI scheme contains the string "http" and the hostname does not contain a '.' then plug the hostname (possibly after some decoding from punycode to UTF-8) into <tt>keyword.URL</tt>.
# If the above step did not produce a <code>nsIURI</code> and either DNS lookup failed or the network access failed with <code>NS_ERROR_NET_RESET</code> (typically means the server did not respond or dropped the connection immediately) and the protocol is "http" and the url does not contain a username or password then because the <code>browser.fixup.alternate.enabled</code> preference defaults to true we try to construct an alternate hostname.  The steps for this are in <code>nsDefaultURIFixup::MakeAlternateURI</code> and are as follows:
## Get the prefix to use from the <code>browser.fixup.alternate.prefix</code> preference; default is "www.".  Some locales set this preference.
## Get the suffix to use from the <code>browser.fixup.alternate.suffix</code> preference; default is ".com".  Some locales set this preference.
## If the hostname includes no '.' characters, prepend the prefix and append the suffix.  Else if it has a single '.' and the part ending on the '.' is equal to our prefix, append the suffix.  Else if it has a single '.' and tour suffix is nonempty prepend the prefix.  For the default pref values, this means the <tt>"foo"</tt> will become <tt>"www.foo.com"</tt>, as will <tt>"foo.com"</tt> and <tt>"www.foo"</tt>.  Similarly, <tt>"foo.org"</tt> will become <tt>"www.foo.org"</tt>.  On the other hand, <tt>"web.foo"</tt> and <tt>"x.y.foo"</tt> won't be changed.
# If either of the two steps above produced a URI different from the URI whose load failed, load that new URI.
Account confirmers, Anti-spam team, Confirmed users, Bureaucrats and Sysops emeriti
4,925

edits