tor-browser

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

commit c78b1f62b6378f02ed007667d88271c1772e0501
parent 8610ad3d4a568fd4e5684fc3a97cecd615e3a9a2
Author: Daisuke Akatsuka <daisuke@birchill.co.jp>
Date:   Thu, 11 Dec 2025 01:12:03 +0000

Bug 1995227: Make displayUrl calculation lazily r=adw

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

Diffstat:
Mbrowser/components/urlbar/UrlbarResult.sys.mjs | 73++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
Mbrowser/components/urlbar/UrlbarUtils.sys.mjs | 15---------------
Mbrowser/components/urlbar/UrlbarView.sys.mjs | 15+++++++++------
Mbrowser/components/urlbar/private/AddonSuggestions.sys.mjs | 4----
Mbrowser/components/urlbar/private/DynamicSuggestions.sys.mjs | 4----
Mbrowser/components/urlbar/tests/browser-updateResults/browser_appendSpanCount.js | 1-
Mbrowser/components/urlbar/tests/browser-updateResults/head.js | 2--
Mbrowser/components/urlbar/tests/browser/browser_bestMatch.js | 6+-----
Mbrowser/components/urlbar/tests/browser/browser_oneOffs_heuristicRestyle.js | 4+++-
Mbrowser/components/urlbar/tests/browser/browser_results_format_displayValue.js | 2+-
Mbrowser/components/urlbar/tests/browser/browser_searchMode_localOneOffs_actionText.js | 3++-
Mbrowser/components/urlbar/tests/quicksuggest/QuickSuggestTestUtils.sys.mjs | 4----
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest.js | 2+-
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_dynamicSuggestions.js | 1-
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js | 1-
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_impressionCaps.js | 2--
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_merino.js | 7-------
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_scoreMap.js | 1-
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_topPicks.js | 2--
Mbrowser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_yelp_ml.js | 2--
Mbrowser/components/urlbar/tests/unit/head.js | 2--
Abrowser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js | 231+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mbrowser/components/urlbar/tests/unit/test_autofill_originsAndQueries.js | 2--
Mbrowser/components/urlbar/tests/unit/test_autofill_urls.js | 1-
Mbrowser/components/urlbar/tests/unit/test_providerHeuristicFallback.js | 1-
Mbrowser/components/urlbar/tests/unit/test_provider_notification.js | 2--
Mbrowser/components/urlbar/tests/unit/xpcshell.toml | 2++
27 files changed, 316 insertions(+), 76 deletions(-)

diff --git a/browser/components/urlbar/UrlbarResult.sys.mjs b/browser/components/urlbar/UrlbarResult.sys.mjs @@ -15,6 +15,7 @@ const lazy = {}; ChromeUtils.defineESModuleGetters(lazy, { JsonSchemaValidator: "resource://gre/modules/components-utils/JsonSchemaValidator.sys.mjs", + ObjectUtils: "resource://gre/modules/ObjectUtils.sys.mjs", UrlbarUtils: "moz-src:///browser/components/urlbar/UrlbarUtils.sys.mjs", }); @@ -128,6 +129,7 @@ export class UrlbarResult { tokens: queryContext.tokens, highlightTargets: highlights, }); + this.#highlights = Object.freeze(highlights); } this.#payload = this.#validatePayload(payload); @@ -326,6 +328,67 @@ export class UrlbarResult { } /** + * Get value and highlights of given payloadName that can display in the view. + * + * @param {string} payloadName + * The payload name to want to get the value. + * @param {object} options + * @param {object} [options.tokens] + * Make highlighting that matches this tokens. + * If no specific tokens, this function returns only value. + * @param {object} [options.isURL] + * If true, the value will be from UrlbarUtils.prepareUrlForDisplay(). + */ + getDisplayableValueAndHighlights(payloadName, options = {}) { + if (!this.#displayValuesCache) { + this.#displayValuesCache = new Map(); + } + + if (this.#displayValuesCache.has(payloadName)) { + let cached = this.#displayValuesCache.get(payloadName); + // If the different options are specified, ignore the cache. + // NOTE: If options.tokens is undefined, use cache as it is. + if ( + options.isURL == cached.options.isURL && + (options.tokens == undefined || + lazy.ObjectUtils.deepEqual(options.tokens, cached.options.tokens)) + ) { + return this.#displayValuesCache.get(payloadName); + } + } + + let value = this.payload[payloadName]; + if (!value) { + return {}; + } + + if (options.isURL) { + value = lazy.UrlbarUtils.prepareUrlForDisplay(value); + } + + if (typeof value == "string") { + value = value.substring(0, lazy.UrlbarUtils.MAX_TEXT_LENGTH); + } + + if (!options.tokens?.length || !this.#highlights?.[payloadName]) { + let cached = { value, options }; + this.#displayValuesCache.set(payloadName, cached); + return cached; + } + + let type = this.#highlights[payloadName]; + let highlights = Array.isArray(value) + ? value.map(subval => + lazy.UrlbarUtils.getTokenMatches(options.tokens, subval, type) + ) + : lazy.UrlbarUtils.getTokenMatches(options.tokens, value || "", type); + + let cached = { value, highlights, options }; + this.#displayValuesCache.set(payloadName, cached); + return cached; + } + + /** * Returns title or highlights of given payload or payloadHighlights. * * @param {object} target payload or payloadHighlights @@ -415,16 +478,10 @@ export class UrlbarResult { } catch (e) {} } - if (payload.url) { - // For display purposes we need to unescape the url. - payload.displayUrl = lazy.UrlbarUtils.prepareUrlForDisplay(payload.url); - highlightTargets.displayUrl = highlightTargets.url; - } - // For performance reasons limit excessive string lengths, to reduce the // amount of string matching we do here, and avoid wasting resources to // handle long textruns that the user would never see anyway. - for (let prop of ["displayUrl", "title", "suggestion"]) { + for (let prop of ["title", "suggestion"]) { let value = payload[prop]; if (typeof value == "string") { payload[prop] = value.substring(0, lazy.UrlbarUtils.MAX_TEXT_LENGTH); @@ -548,5 +605,7 @@ export class UrlbarResult { #suggestedIndex; #payload; #payloadHighlights; + #highlights; + #displayValuesCache; #testForceNewContent; } diff --git a/browser/components/urlbar/UrlbarUtils.sys.mjs b/browser/components/urlbar/UrlbarUtils.sys.mjs @@ -1869,9 +1869,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = { }, }, }, - displayUrl: { - type: "string", - }, frecency: { type: "number", }, @@ -1909,9 +1906,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = { type: "string", }, descriptionL10n: L10N_SCHEMA, - displayUrl: { - type: "string", - }, engine: { type: "string", }, @@ -1996,9 +1990,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = { dismissalKey: { type: "string", }, - displayUrl: { - type: "string", - }, dupedHeuristic: { type: "boolean", }, @@ -2103,9 +2094,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = { type: "object", required: ["keyword", "url"], properties: { - displayUrl: { - type: "string", - }, icon: { type: "string", }, @@ -2155,9 +2143,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = { device: { type: "string", }, - displayUrl: { - type: "string", - }, icon: { type: "string", }, diff --git a/browser/components/urlbar/UrlbarView.sys.mjs b/browser/components/urlbar/UrlbarView.sys.mjs @@ -2209,7 +2209,6 @@ export class UrlbarView { break; case lazy.UrlbarUtils.RESULT_TYPE.URL: if (result.providerName == "UrlbarProviderClipboard") { - result.payload.displayUrl = ""; actionSetter = () => { this.#l10nCache.setElementL10n(action, { id: "urlbar-result-action-visit-from-clipboard", @@ -2273,21 +2272,25 @@ export class UrlbarView { item.toggleAttribute("has-url", setURL); let url = item._elements.get("url"); if (setURL) { - let displayedUrl = result.payload.displayUrl; - let urlHighlights = result.payloadHighlights.displayUrl || []; + let { value: displayedUrl, highlights } = + result.getDisplayableValueAndHighlights("url", { + tokens: this.#queryContext.tokens, + isURL: true, + }); + this.#updateOverflowTooltip(url, displayedUrl); + if (lazy.UrlbarUtils.isTextDirectionRTL(displayedUrl, this.window)) { // Stripping the url prefix may change the initial text directionality, // causing parts of it to jump to the end. To prevent that we insert a // LRM character in place of the prefix. displayedUrl = "\u200e" + displayedUrl; - urlHighlights = this.#offsetHighlights(urlHighlights, 1); + highlights = this.#offsetHighlights(highlights, 1); } lazy.UrlbarUtils.addTextContentWithHighlights( url, displayedUrl, - urlHighlights + highlights ); - this.#updateOverflowTooltip(url, result.payload.displayUrl); } else { url.textContent = ""; this.#updateOverflowTooltip(url, ""); diff --git a/browser/components/urlbar/private/AddonSuggestions.sys.mjs b/browser/components/urlbar/private/AddonSuggestions.sys.mjs @@ -109,10 +109,6 @@ export class AddonSuggestions extends SuggestProvider { }, helpUrl: lazy.QuickSuggest.HELP_URL, }, - // TODO: We have to set highlights to calculate displayUrl now. But, since - // it is not related to the higilights, think about it with another - // way. - highlights: {}, }); } diff --git a/browser/components/urlbar/private/DynamicSuggestions.sys.mjs b/browser/components/urlbar/private/DynamicSuggestions.sys.mjs @@ -114,10 +114,6 @@ export class DynamicSuggestions extends SuggestProvider { source: lazy.UrlbarUtils.RESULT_SOURCE.SEARCH, ...resultProperties, payload, - // TODO: We have to set highlights to calculate displayUrl now. But, since - // it is not related to the higilights, think about it with another - // way. - highlights: {}, }); } diff --git a/browser/components/urlbar/tests/browser-updateResults/browser_appendSpanCount.js b/browser/components/urlbar/tests/browser-updateResults/browser_appendSpanCount.js @@ -87,7 +87,6 @@ add_task(async function viewUpdateAppendHidden() { payload: { title, url, - displayUrl: "http://example.com/" + title, }, }); }); diff --git a/browser/components/urlbar/tests/browser-updateResults/head.js b/browser/components/urlbar/tests/browser-updateResults/head.js @@ -86,7 +86,6 @@ function makeSuggestedIndexResult(suggestedIndex, resultSpan = 1) { resultSpan, payload: { url: "http://example.com/si", - displayUrl: "http://example.com/si", title: "suggested index", helpUrl: "http://example.com/", isBlockable: true, @@ -161,7 +160,6 @@ function makeProviderResults({ count = 0, type = undefined, specs = [] }) { source: UrlbarUtils.RESULT_SOURCE.HISTORY, payload: { url: "http://example.com/" + i, - displayUrl: "http://example.com/" + i, title: str, helpUrl: "http://example.com/", isBlockable: true, diff --git a/browser/components/urlbar/tests/browser/browser_bestMatch.js b/browser/components/urlbar/tests/browser/browser_bestMatch.js @@ -143,7 +143,7 @@ async function checkBestMatchRow({ result, hasHelpUrl = false }) { Assert.ok(url.textContent, "Row URL has non-empty textContext"); Assert.equal( url.textContent, - result.payload.displayUrl, + result.getDisplayableValueAndHighlights("url", { isURL: true }).value, "Row URL is correct" ); @@ -187,9 +187,5 @@ function makeBestMatchResult(payloadExtra = {}) { url: "https://example.com/best-match", ...payloadExtra, }, - // TODO: We have to set highlights to calculate displayUrl now. But, since - // it is not related to the higilights, think about it with another - // way. - highlights: {}, }); } diff --git a/browser/components/urlbar/tests/browser/browser_oneOffs_heuristicRestyle.js b/browser/components/urlbar/tests/browser/browser_oneOffs_heuristicRestyle.js @@ -88,7 +88,9 @@ async function heuristicIsNotRestyled(expectedType, resultDetails) { ) { Assert.equal( resultDetails.displayed.url, - resultDetails.result.payload.displayUrl + resultDetails.result.getDisplayableValueAndHighlights("url", { + isURL: true, + }).value ); } else { Assert.equal( diff --git a/browser/components/urlbar/tests/browser/browser_results_format_displayValue.js b/browser/components/urlbar/tests/browser/browser_results_format_displayValue.js @@ -44,7 +44,7 @@ add_task(async function test_receive_punycode_result() { let row = await UrlbarTestUtils.waitForAutocompleteResultAt(window, 0); is(row.result.type, UrlbarUtils.RESULT_TYPE.URL, "row.result.type"); is( - row.result.payload.displayUrl, + row.result.getDisplayableValueAndHighlights("url", { isURL: true }).value, "اختبار.اختبار.org:5000", "Result is trimmed and formatted correctly." ); diff --git a/browser/components/urlbar/tests/browser/browser_searchMode_localOneOffs_actionText.js b/browser/components/urlbar/tests/browser/browser_searchMode_localOneOffs_actionText.js @@ -289,7 +289,8 @@ add_task(async function localOneOff_withVisit() { Assert.equal(result.type, UrlbarUtils.RESULT_TYPE.URL); Assert.equal( result.displayed.url, - result.result.payload.displayUrl, + result.result.getDisplayableValueAndHighlights("url", { isURL: true }) + .value, "Check the heuristic action" ); Assert.notEqual( diff --git a/browser/components/urlbar/tests/quicksuggest/QuickSuggestTestUtils.sys.mjs b/browser/components/urlbar/tests/quicksuggest/QuickSuggestTestUtils.sys.mjs @@ -487,7 +487,6 @@ class _QuickSuggestTestUtils { requestId, source, provider, - displayUrl: url.replace(/^https:\/\//, ""), isSponsored: true, qsSuggestion: fullKeyword ?? keyword, sponsoredImpressionUrl: impressionUrl, @@ -595,7 +594,6 @@ class _QuickSuggestTestUtils { source, provider, telemetryType, - displayUrl: url.replace(/^https:\/\//, ""), isSponsored: false, qsSuggestion: fullKeyword ?? keyword, isBlockable: true, @@ -941,7 +939,6 @@ class _QuickSuggestTestUtils { url, originalUrl, icon, - displayUrl: url.replace(/^https:\/\//, ""), isSponsored: false, shouldShowUrl: true, bottomTextL10n: { @@ -996,7 +993,6 @@ class _QuickSuggestTestUtils { title, url: finalUrl.href, originalUrl: url, - displayUrl: finalUrl.href.replace(/^https:\/\//, ""), isSponsored: false, description, icon: "chrome://global/skin/icons/mdn.svg", diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest.js @@ -1197,7 +1197,7 @@ add_task(async function dedupeAgainstURL_timestamps() { // Check the quick suggest's payload excluding the timestamp-related // properties. let actualQuickSuggest = context.results[QUICK_SUGGEST_INDEX]; - let ignore = ["displayUrl", "sponsoredClickUrl", "url", "urlTimestampIndex"]; + let ignore = ["sponsoredClickUrl", "url", "urlTimestampIndex"]; Assert.deepEqual( getPayload(actualQuickSuggest, { ignore }), getPayload(expectedQuickSuggest, { ignore }), diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_dynamicSuggestions.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_dynamicSuggestions.js @@ -626,7 +626,6 @@ function makeExpectedResult({ isSponsored, telemetryType, suggestionType, - displayUrl: url.replace(/^https:\/\//, ""), source: "rust", provider: "Dynamic", isManageable: true, diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_importantDatesSuggestions.js @@ -351,7 +351,6 @@ add_task(async function testTwoSuggestions() { title: "Top Pick Suggestion 1", url: "https://foo.com/", telemetryType: "other_suggestions", - displayUrl: "foo.com", description: "A suggestion that just so happens to have the same keyword", isManageable: true, isSponsored: false, diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_impressionCaps.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_impressionCaps.js @@ -42,7 +42,6 @@ const EXPECTED_SPONSORED_URLBAR_RESULT = { telemetryType: "adm_sponsored", url: "http://example.com/sponsored", originalUrl: "http://example.com/sponsored", - displayUrl: "http://example.com/sponsored", title: "Sponsored suggestion", qsSuggestion: "sponsored", icon: null, @@ -71,7 +70,6 @@ const EXPECTED_NONSPONSORED_URLBAR_RESULT = { telemetryType: "adm_nonsponsored", url: "http://example.com/nonsponsored", originalUrl: "http://example.com/nonsponsored", - displayUrl: "http://example.com/nonsponsored", title: "Non-sponsored suggestion", qsSuggestion: "nonsponsored", icon: null, diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_merino.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_merino.js @@ -528,7 +528,6 @@ add_task(async function dismissals_amp() { title: suggestion.title, url: suggestion.url, originalUrl: suggestion.original_url || suggestion.url, - displayUrl: suggestion.url.replace(/^https:\/\//, ""), dismissalKey: suggestion.dismissal_key, requestId: suggestion.request_id, sponsoredImpressionUrl: suggestion.impression_url, @@ -559,7 +558,6 @@ add_task(async function dismissals_amp() { conditionalPayloadProperties: { url: { ignore: true }, urlTimestampIndex: { ignore: true }, - displayUrl: { ignore: true }, }, }); @@ -616,7 +614,6 @@ add_task(async function dismissals_amp() { conditionalPayloadProperties: { url: { ignore: true }, urlTimestampIndex: { ignore: true }, - displayUrl: { ignore: true }, }, }); } @@ -696,7 +693,6 @@ add_task(async function dismissals_unmanaged_1() { title: "example.com", url: suggestion.url, originalUrl: suggestion.original_url, - displayUrl: suggestion.url.replace(/^https:\/\//, ""), dismissalKey: suggestion.dismissal_key, source: "merino", isSponsored: false, @@ -817,7 +813,6 @@ add_task(async function dismissals_unmanaged_2() { provider, title: "example.com", url: "https://example.com/url", - displayUrl: "example.com/url", source: "merino", isSponsored: false, shouldShowUrl: true, @@ -999,7 +994,6 @@ add_task(async function bestMatch() { isSponsored: false, isBlockable: true, isManageable: true, - displayUrl: "url", source: "merino", provider, }, @@ -1091,7 +1085,6 @@ async function doUnmanagedTest({ pref, suggestion, shouldBeAdded }) { payload: { title: suggestion.title, url: suggestion.url, - displayUrl: suggestion.url.substring("https://".length), provider: suggestion.provider, telemetryType: suggestion.provider, isSponsored: !!suggestion.is_sponsored, diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_scoreMap.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_scoreMap.js @@ -680,7 +680,6 @@ function makeExpectedDefaultMerinoResult({ suggestion }) { isSponsored: !!suggestion.is_sponsored, title: suggestion.title, url: suggestion.url, - displayUrl: suggestion.url.replace(/^https:\/\//, ""), icon: suggestion.icon, descriptionL10n: suggestion.is_sponsored ? { id: "urlbar-result-action-sponsored" } diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_topPicks.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_topPicks.js @@ -20,7 +20,6 @@ const SUGGESTION_SEARCH_STRING = "example"; const SUGGESTION_URL = "http://example.com/"; const SUGGESTION_URL_WWW = "http://www.example.com/"; -const SUGGESTION_URL_DISPLAY = "http://example.com"; const MERINO_SUGGESTIONS = [ { @@ -171,7 +170,6 @@ function makeExpectedResult({ telemetryType, title: "title", url: SUGGESTION_URL, - displayUrl: SUGGESTION_URL_DISPLAY, icon: "icon", isSponsored: false, shouldShowUrl: true, diff --git a/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_yelp_ml.js b/browser/components/urlbar/tests/quicksuggest/unit/test_quicksuggest_yelp_ml.js @@ -559,7 +559,6 @@ function makeExpectedResult({ source = "ml", provider = "yelp_intent", originalUrl = undefined, - displayUrl = undefined, // Expect index -1 for amp results because we test // without the search suggestions provider. suggestedIndex = -1, @@ -570,7 +569,6 @@ function makeExpectedResult({ source, provider, originalUrl, - displayUrl, suggestedIndex, }); } diff --git a/browser/components/urlbar/tests/unit/head.js b/browser/components/urlbar/tests/unit/head.js @@ -810,8 +810,6 @@ function makeSearchResult( isPrivateEngine, }; - // Passing even an undefined URL in the payload creates a potentially-unwanted - // displayUrl parameter, so we add it only if specified. if (uri) { payload.url = uri; } diff --git a/browser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js b/browser/components/urlbar/tests/unit/test_UrlbarResult_getDisplayableValueAndHighlights.js @@ -0,0 +1,231 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +add_task(function type_typed() { + 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.TYPED, + }, + }); + + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens }, + expected: { + value: "https://test.example.com/", + highlights: [[8, 4]], + }, + }); +}); + +add_task(function type_suggested() { + 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, + }, + }); + + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens }, + expected: { + value: "https://test.example.com/", + highlights: [[0, 25]], + }, + }); +}); + +add_task(function option_isURL() { + 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.TYPED, + }, + }); + + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens, isURL: true }, + expected: { + value: "test.example.com", + highlights: [[0, 4]], + }, + }); +}); + +add_task(function option_no_tokens() { + let queryContext = createContext(""); + 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.TYPED, + }, + }); + + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens }, + expected: { + value: "https://test.example.com/", + highlights: undefined, + }, + }); +}); + +add_task(function option_nothing() { + let result = new UrlbarResult({ + queryContext: createContext(""), + type: UrlbarUtils.RESULT_TYPE.URL, + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + payload: { + url: "https://test.example.com/", + }, + highlights: { + url: UrlbarUtils.HIGHLIGHT.TYPED, + }, + }); + + doTest({ + result, + target: "url", + expected: { + value: "https://test.example.com/", + highlights: undefined, + }, + }); +}); + +add_task(function invalid_target() { + 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.TYPED, + }, + }); + + doTest({ + result, + target: "invalid", + options: { tokens: queryContext.tokens }, + expected: { + value: undefined, + highlights: undefined, + }, + }); +}); + +add_task(function cache() { + 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.TYPED, + }, + }); + + info("Get without any options"); + doTest({ + result, + target: "url", + expected: { + value: "https://test.example.com/", + highlights: undefined, + }, + }); + + info("Get with tokens"); + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens }, + expected: { + value: "https://test.example.com/", + highlights: [[8, 4]], + }, + }); + + info("Get with different isURL"); + doTest({ + result, + target: "url", + options: { tokens: queryContext.tokens, isURL: true }, + expected: { + value: "test.example.com", + highlights: [[0, 4]], + }, + }); + + info("Get without tokens"); + doTest({ + result, + target: "url", + options: { isURL: true }, + expected: { + value: "test.example.com", + highlights: [[0, 4]], + }, + }); + + info("Get without different tokens"); + let anotherQueryContext = createContext("example"); + doTest({ + result, + target: "url", + options: { tokens: anotherQueryContext.tokens }, + expected: { + value: "https://test.example.com/", + highlights: [[13, 7]], + }, + }); +}); + +function doTest({ result, target, options, expected }) { + let { value, highlights } = result.getDisplayableValueAndHighlights( + target, + options + ); + Assert.equal(value, expected.value); + Assert.deepEqual(highlights, expected.highlights); +} diff --git a/browser/components/urlbar/tests/unit/test_autofill_originsAndQueries.js b/browser/components/urlbar/tests/unit/test_autofill_originsAndQueries.js @@ -172,7 +172,6 @@ add_autofill_task(async function wwwShouldNotMatchNoWWW() { source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, uri: "http://www." + search + "/", fallbackTitle: "www." + search + "/", - displayUrl: "http://www." + search, heuristic: true, providerName: HEURISTIC_FALLBACK_PROVIDERNAME, }), @@ -409,7 +408,6 @@ add_autofill_task(async function httpsWWWShouldNotMatchNoWWW() { source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, uri: "http://www." + search + "/", fallbackTitle: "www." + search + "/", - displayUrl: "http://www." + search, heuristic: true, providerName: HEURISTIC_FALLBACK_PROVIDERNAME, }), diff --git a/browser/components/urlbar/tests/unit/test_autofill_urls.js b/browser/components/urlbar/tests/unit/test_autofill_urls.js @@ -907,7 +907,6 @@ add_task(async function formatPunycodeResultCorrectly() { makeVisitResult(context, { uri: "http://test.xn--e1afmkfd.com/", title: "test visit for http://test.xn--e1afmkfd.com/", - displayUrl: "http://test.пример.com", heuristic: true, }), ], diff --git a/browser/components/urlbar/tests/unit/test_providerHeuristicFallback.js b/browser/components/urlbar/tests/unit/test_providerHeuristicFallback.js @@ -761,7 +761,6 @@ add_task(async function () { makeVisitResult(context, { source: UrlbarUtils.RESULT_SOURCE.HISTORY, uri: `http://test.xn--e1afmkfd.com/`, - displayUrl: `test.пример.com`, heuristic: true, iconUri: "page-icon:http://test.xn--e1afmkfd.com/", }), diff --git a/browser/components/urlbar/tests/unit/test_provider_notification.js b/browser/components/urlbar/tests/unit/test_provider_notification.js @@ -17,7 +17,6 @@ add_setup(async function () { url: "https://mozilla.com/", tags: [], title: "mozilla.com", - displayUrl: "mozilla.com", }, }), ], @@ -39,7 +38,6 @@ add_setup(async function () { url: "https://example.com/", tags: [], title: "example.com", - displayUrl: "example.com", }, }), ], diff --git a/browser/components/urlbar/tests/unit/xpcshell.toml b/browser/components/urlbar/tests/unit/xpcshell.toml @@ -24,6 +24,8 @@ support-files = ["data/engine.xml"] ["test_UrlbarQueryContext_restrictSource.js"] +["test_UrlbarResult_getDisplayableValueAndHighlights.js"] + ["test_UrlbarSearchUtils.js"] ["test_UrlbarUtils_addToUrlbarHistory.js"]