diff --git a/chrome/browser/resources/local_ntp/local_ntp.js b/chrome/browser/resources/local_ntp/local_ntp.js index 5e3941a1ff1df6..b9c391a5a83802 100644 --- a/chrome/browser/resources/local_ntp/local_ntp.js +++ b/chrome/browser/resources/local_ntp/local_ntp.js @@ -1083,15 +1083,28 @@ function onDeleteAutocompleteMatch(result) { return matchEl.classList.contains(CLASSES.SELECTED); }); + const wasFocused = matchEls[selected].contains(document.activeElement); + populateAutocompleteMatches(result.matches); if (result.matches.length === 0) { + if (wasFocused) { + $(IDS.REALBOX).focus(); + } return; } const newMatchEls = Array.from($(IDS.REALBOX_MATCHES).children); const newSelected = Math.min(newMatchEls.length - 1, selected); - selectMatchEl(newMatchEls[newSelected]); + const newSelectedEl = newMatchEls[newSelected]; + + selectMatchEl(newSelectedEl); + + if (wasFocused) { + const removeIcon = newSelectedEl.querySelector(`.${CLASSES.REMOVE_ICON}`); + assert(removeIcon || newSelectedEl).focus(); + } + updateRealboxOutput({ moveCursorToEnd: true, inline: '', diff --git a/chrome/test/data/local_ntp/realbox_browsertest.js b/chrome/test/data/local_ntp/realbox_browsertest.js index c18732ec714adf..6d7586c0c5680c 100644 --- a/chrome/test/data/local_ntp/realbox_browsertest.js +++ b/chrome/test/data/local_ntp/realbox_browsertest.js @@ -564,10 +564,15 @@ test.realbox.testSupportedDeletion = function() { assertEquals(1, test.realbox.deletedLines.length); assertEquals(1, test.realbox.deletedLines[0]); + matchesEl.children[1].focus(); + assertEquals(matchesEl.children[1], document.activeElement); + chrome.embeddedSearch.searchBox.ondeleteautocompletematch( {success: true, matches: [test.realbox.getSearchMatch()]}); - assertEquals(1, $(test.realbox.IDS.REALBOX_MATCHES).children.length); + const newMatchesEl = $(test.realbox.IDS.REALBOX_MATCHES); + assertEquals(1, newMatchesEl.children.length); + assertEquals(newMatchesEl.children[0], document.activeElement); }; test.realbox.testRemoveIcon = function() {