Bug 1495614 - Don't highlight the search engine alias when the urlbar input doesn't match the first heuristic result r=mak

Differential Revision: https://phabricator.services.mozilla.com/D8479

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Drew Willcoxon 2018-10-12 13:45:37 +00:00
Родитель 56a06bbe68
Коммит 3ecf1f3476
2 изменённых файлов: 75 добавлений и 13 удалений

Просмотреть файл

@ -64,6 +64,50 @@ add_task(async function aliasCase() {
await promisePopupHidden(gURLBar.popup);
});
add_task(async function inputDoesntMatchHeuristicResult() {
// Do a search using the alias.
let searchString = `${ALIAS} aaa`;
gURLBar.search(searchString);
await promiseSearchComplete();
await waitForAutocompleteResultAt(0);
assertAlias(true);
// Hide the popup.
EventUtils.synthesizeKey("KEY_Escape");
await promisePopupHidden(gURLBar.popup);
// Manually set the urlbar value to a string that contains the alias at the
// beginning but is not the same as the search string.
let value = `${ALIAS} xxx`;
gURLBar.value = `${ALIAS} xxx`;
// The alias substring should not be highlighted.
Assert.equal(gURLBar.value, value);
Assert.ok(gURLBar.value.includes(ALIAS));
assertHighlighted(false, ALIAS);
// Do another search using the alias.
searchString = `${ALIAS} bbb`;
gURLBar.search(searchString);
await promiseSearchComplete();
await waitForAutocompleteResultAt(0);
assertAlias(true);
// Hide the popup.
EventUtils.synthesizeKey("KEY_Escape");
await promisePopupHidden(gURLBar.popup);
// Manually set the urlbar value to a string that contains the alias, but not
// at the beginning and is not the same as the search string.
value = `bbb ${ALIAS}`;
gURLBar.value = `bbb ${ALIAS}`;
// The alias substring should not be highlighted.
Assert.equal(gURLBar.value, value);
Assert.ok(gURLBar.value.includes(ALIAS));
assertHighlighted(false, ALIAS);
});
async function doTest(revertBetweenSteps) {
// "@tes" -- not an alias, no highlight

Просмотреть файл

@ -272,25 +272,34 @@ class UrlbarValueFormatter {
return false;
}
// There can only be an alias to highlight if the heuristic result is
// an alias searchengine result and it's either currently selected or
// was selected when the popup was closed. We also need to check
// whether a one-off search button is selected because in that case
// there won't be a selection but the alias should not be highlighted.
let popup = this.urlbarInput.popup;
if (!popup) {
// TODO: make this work with UrlbarView
return false;
}
// There can only be an alias to highlight if the heuristic result is
// an alias searchengine result and it's either currently selected or
// was selected when the popup was closed. We also need to check
// whether a one-off search button is selected because in that case
// there won't be a selection but the alias should not be highlighted.
if ((popup.selectedIndex < 0 &&
popup._previousSelectedIndex != 0) ||
popup.selectedIndex > 0 ||
popup.oneOffSearchButtons.selectedButton) {
return false;
}
let heuristicItem = popup.richlistbox.children[0] || null;
let alias =
(popup.selectedIndex == 0 ||
(popup.selectedIndex < 0 &&
popup._previousSelectedIndex == 0)) &&
!popup.oneOffSearchButtons.selectedButton &&
heuristicItem &&
heuristicItem.getAttribute("actiontype") == "searchengine" &&
this.urlbarInput._parseActionUrl(heuristicItem.getAttribute("url")).params.alias;
if (!heuristicItem ||
heuristicItem.getAttribute("actiontype") != "searchengine") {
return false;
}
let url = heuristicItem.getAttribute("url");
let action = this.urlbarInput._parseActionUrl(url);
if (!action) {
return false;
}
let alias = action.params.alias || null;
if (!alias) {
return false;
}
@ -299,6 +308,15 @@ class UrlbarValueFormatter {
let textNode = editor.rootElement.firstChild;
let value = textNode.textContent;
// Make sure the heuristic result's input matches the current urlbar input
// because the urlbar input can change without the popup results changing.
// Most notably that happens when the user performs a search using an alias:
// The popup closes, the search results page is loaded, and the urlbar value
// is set to the URL of the page.
if (decodeURIComponent(action.params.input) != value) {
return false;
}
let index = value.indexOf(alias);
if (index < 0) {
return false;