tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit cf20d85b1ed67592a37ef2732da89ca8d65c931e
parent 21800a1aa98c0cebfb60d053a6a39ec2620827bc
Author: Daisuke Akatsuka <daisuke@birchill.co.jp>
Date:   Thu, 11 Dec 2025 01:12:04 +0000

Bug 1995227: Introduce ALL highlight type r=adw

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

Diffstat:
Mbrowser/components/urlbar/UrlbarUtils.sys.mjs | 10++++++++++
Mbrowser/components/urlbar/private/ImportantDatesSuggestions.sys.mjs | 8+++-----
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js | 20++++++++++++++++----
Mbrowser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js | 27++++++++++++++++++++++++++-
Mbrowser/components/urlbar/tests/unit/test_UrlbarUtils_getTokenMatches.js | 61++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 115 insertions(+), 11 deletions(-)

diff --git a/browser/components/urlbar/UrlbarUtils.sys.mjs b/browser/components/urlbar/UrlbarUtils.sys.mjs @@ -186,6 +186,7 @@ export var UrlbarUtils = { NONE: 0, TYPED: 1, SUGGESTED: 2, + ALL: 3, }), // UrlbarProviderPlaces's autocomplete results store their titles and tags @@ -407,6 +408,7 @@ export var UrlbarUtils = { * TYPED: match ranges matching the tokens; or * SUGGESTED: match ranges for words not matching the tokens and the * endings of words that start with a token. + * ALL: match all ranges of str. * @returns {Array} An array: [ * [matchIndex_0, matchLength_0], * [matchIndex_1, matchLength_1], @@ -416,6 +418,14 @@ export var UrlbarUtils = { * The array is sorted by match indexes ascending. */ getTokenMatches(tokens, str, highlightType) { + if (highlightType == this.HIGHLIGHT.ALL) { + return [[0, str.length]]; + } + + if (!tokens?.length) { + return []; + } + // Only search a portion of the string, because not more than a certain // amount of characters are visible in the UI, matching over what is visible // would be expensive and pointless. diff --git a/browser/components/urlbar/private/ImportantDatesSuggestions.sys.mjs b/browser/components/urlbar/private/ImportantDatesSuggestions.sys.mjs @@ -212,6 +212,7 @@ export class ImportantDatesSuggestions extends SuggestProvider { let dateString = this.#formatDateOrRange(eventDateOrRange); return new lazy.UrlbarResult({ + queryContext, type: lazy.UrlbarUtils.RESULT_TYPE.SEARCH, source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH, isBestMatch: true, @@ -230,11 +231,8 @@ export class ImportantDatesSuggestions extends SuggestProvider { isManageable: true, isBlockable: true, }, - payloadHighlights: { - title: [ - // Make whole title bold. - [0, dateString.length], - ], + highlights: { + title: lazy.UrlbarUtils.HIGHLIGHT.ALL, }, }); } diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js @@ -499,13 +499,25 @@ async function checkDatesResults(query, expected) { expected, }) ); + + let queryContext = createContext(query, { + providers: [UrlbarProviderQuickSuggest.name], + isPrivate: false, + }); await check_results({ - context: createContext(query, { - providers: [UrlbarProviderQuickSuggest.name], - isPrivate: false, - }), + context: queryContext, matches: expected ? [expected].flat() : [], }); + + if (expected?.payload) { + info("Check the highligts"); + let { value, highlights } = + queryContext.results[0].getDisplayableValueAndHighlights("title", { + tokens: queryContext.tokens, + }); + Assert.equal(expected.payload.title, value); + Assert.deepEqual([[0, expected.payload.title.length]], highlights); + } } function makeExpectedResult({ diff --git a/browser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js b/browser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js @@ -32,13 +32,38 @@ add_task(function type_suggested() { let queryContext = createContext("test"); let result = new UrlbarResult({ queryContext, + type: UrlbarUtils.RESULT_TYPE.SEARCH, + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + payload: { + suggestion: "test search test", + }, + highlights: { + suggestion: UrlbarUtils.HIGHLIGHT.SUGGESTED, + }, + }); + + doTest({ + result, + target: "suggestion", + options: { tokens: queryContext.tokens }, + expected: { + value: "test search test", + highlights: [[4, 8]], + }, + }); +}); + +add_task(function type_all() { + let queryContext = createContext("test"); + let result = new UrlbarResult({ + queryContext, type: UrlbarUtils.RESULT_TYPE.URL, source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, payload: { url: "https://test.example.com/", }, highlights: { - url: UrlbarUtils.HIGHLIGHT.SUGGESTED, + url: UrlbarUtils.HIGHLIGHT.ALL, }, }); diff --git a/browser/components/urlbar/tests/unit/test_UrlbarUtils_getTokenMatches.js b/browser/components/urlbar/tests/unit/test_UrlbarUtils_getTokenMatches.js @@ -7,7 +7,7 @@ "use strict"; -add_task(function test() { +add_task(function testTyped() { const tests = [ { tokens: ["mozilla", "is", "i"], @@ -292,3 +292,62 @@ add_task(function testSuggestions() { ); } }); + +/** + * Tests ALL highlighting. + */ +add_task(function testAll() { + const tests = [ + { + tokens: [], + phrase: "mozilla is for the Open Web", + expected: [[0, "mozilla is for the Open Web".length]], + }, + { + tokens: undefined, + phrase: "mozilla is for the Open Web", + expected: [[0, "mozilla is for the Open Web".length]], + }, + ]; + for (let { tokens, phrase, expected } of tests) { + Assert.deepEqual( + UrlbarUtils.getTokenMatches(tokens, phrase, UrlbarUtils.HIGHLIGHT.ALL), + expected, + `Match "${tokens?.join(", ")}" on "${phrase}"` + ); + } +}); + +/** + * Tests no tokens parameter for TYPED or SUGGESTED type. + */ +add_task(function testNoTokensWithTypedOrSuggetion() { + const tests = [ + { + tokens: [], + phrase: "mozilla is for the Open Web", + expected: [], + }, + { + tokens: undefined, + phrase: "mozilla is for the Open Web", + expected: [], + }, + ]; + for (let { tokens, phrase, expected } of tests) { + Assert.deepEqual( + UrlbarUtils.getTokenMatches(tokens, phrase, UrlbarUtils.HIGHLIGHT.TYPED), + expected, + `Match "${tokens?.join(", ")}" on "${phrase} for TYPED"` + ); + Assert.deepEqual( + UrlbarUtils.getTokenMatches( + tokens, + phrase, + UrlbarUtils.HIGHLIGHT.SUGGESTED + ), + expected, + `Match "${tokens?.join(", ")}" on "${phrase} for SUGGESTED"` + ); + } +});