Bug 890690 - Support POST searches in about:home. r=gavin

--HG--
extra : transplant_source : %A5%23%F7%EC%87%DC%ED%D3V%00%C1%D7K%90%D4%009Pp%24
This commit is contained in:
Mike de Boer 2013-07-25 02:43:19 +02:00
parent d3a98caab4
commit bb48df9ad1
3 changed files with 41 additions and 12 deletions

View File

@ -294,13 +294,6 @@ function onSearchSubmit(aEvent)
let searchURL = document.documentElement.getAttribute("searchEngineURL");
if (searchURL && searchTerms.length > 0) {
const SEARCH_TOKENS = {
"_searchTerms_": encodeURIComponent(searchTerms)
}
for (let key in SEARCH_TOKENS) {
searchURL = searchURL.replace(key, SEARCH_TOKENS[key]);
}
// Send an event that a search was performed. This was originally
// added so Firefox Health Report could record that a search from
// about:home had occurred.
@ -308,7 +301,42 @@ function onSearchSubmit(aEvent)
let event = new CustomEvent("AboutHomeSearchEvent", {detail: engineName});
document.dispatchEvent(event);
window.location.href = searchURL;
const SEARCH_TOKEN = "_searchTerms_";
let searchPostData = document.documentElement.getAttribute("searchEnginePostData");
if (searchPostData) {
// Check if a post form already exists. If so, remove it.
const POST_FORM_NAME = "searchFormPost";
let form = document.forms[POST_FORM_NAME];
if (form) {
form.parentNode.removeChild(form);
}
// Create a new post form.
form = document.body.appendChild(document.createElement("form"));
form.setAttribute("name", POST_FORM_NAME);
// Set the URL to submit the form to.
form.setAttribute("action", searchURL.replace(SEARCH_TOKEN, searchTerms));
form.setAttribute("method", "post");
// Create new <input type=hidden> elements for search param.
searchPostData = searchPostData.split("&");
for (let postVar of searchPostData) {
let [name, value] = postVar.split("=");
if (value == SEARCH_TOKEN) {
value = searchTerms;
}
let input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("name", name);
input.setAttribute("value", value);
form.appendChild(input);
}
// Submit the form.
form.submit();
} else {
searchURL = searchURL.replace(SEARCH_TOKEN, encodeURIComponent(searchTerms));
window.location.href = searchURL;
}
}
aEvent.preventDefault();

View File

@ -2314,6 +2314,9 @@ function BrowserOnAboutPageLoad(doc) {
let updateSearchEngine = function() {
let engine = AboutHomeUtils.defaultSearchEngine;
docElt.setAttribute("searchEngineName", engine.name);
docElt.setAttribute("searchEnginePostData", engine.postDataString || "");
// Again, keep the searchEngineURL as the last attribute, because the
// mutation observer in aboutHome.js is counting on that.
docElt.setAttribute("searchEngineURL", engine.searchURL);
};
updateSearchEngine();

View File

@ -25,13 +25,11 @@ this.AboutHomeUtils = {
get defaultSearchEngine() {
let defaultEngine = Services.search.defaultEngine;
let submission = defaultEngine.getSubmission("_searchTerms_", null, "homepage");
if (submission.postData) {
throw new Error("Home page does not support POST search engines.");
}
return Object.freeze({
name: defaultEngine.name,
searchURL: submission.uri.spec
searchURL: submission.uri.spec,
postDataString: submission.postDataString
});
},