From 6bf0a5cb5034a7e684dcc3500e841785237ce2dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 19:32:43 +0200 Subject: Adding upstream version 1:115.7.0. Signed-off-by: Daniel Baumann --- .../urlbar/UrlbarProviderBookmarkKeywords.sys.mjs | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 browser/components/urlbar/UrlbarProviderBookmarkKeywords.sys.mjs (limited to 'browser/components/urlbar/UrlbarProviderBookmarkKeywords.sys.mjs') diff --git a/browser/components/urlbar/UrlbarProviderBookmarkKeywords.sys.mjs b/browser/components/urlbar/UrlbarProviderBookmarkKeywords.sys.mjs new file mode 100644 index 0000000000..416e6238ba --- /dev/null +++ b/browser/components/urlbar/UrlbarProviderBookmarkKeywords.sys.mjs @@ -0,0 +1,119 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +/** + * This module exports a provider that offers bookmarks with keywords. + */ + +import { + UrlbarProvider, + UrlbarUtils, +} from "resource:///modules/UrlbarUtils.sys.mjs"; + +const lazy = {}; + +ChromeUtils.defineESModuleGetters(lazy, { + KeywordUtils: "resource://gre/modules/KeywordUtils.sys.mjs", + UrlbarResult: "resource:///modules/UrlbarResult.sys.mjs", + UrlbarTokenizer: "resource:///modules/UrlbarTokenizer.sys.mjs", +}); + +/** + * Class used to create the provider. + */ +class ProviderBookmarkKeywords extends UrlbarProvider { + /** + * Returns the name of this provider. + * + * @returns {string} the name of this provider. + */ + get name() { + return "BookmarkKeywords"; + } + + /** + * Returns the type of this provider. + * + * @returns {integer} one of the types from UrlbarUtils.PROVIDER_TYPE.* + */ + get type() { + return UrlbarUtils.PROVIDER_TYPE.HEURISTIC; + } + + /** + * Whether this provider should be invoked for the given context. + * If this method returns false, the providers manager won't start a query + * with this provider, to save on resources. + * + * @param {UrlbarQueryContext} queryContext The query context object + * @returns {boolean} Whether this provider should be invoked for the search. + */ + isActive(queryContext) { + return ( + (!queryContext.restrictSource || + queryContext.restrictSource == + lazy.UrlbarTokenizer.RESTRICT.BOOKMARK) && + !queryContext.searchMode && + queryContext.tokens.length + ); + } + + /** + * Starts querying. + * + * @param {object} queryContext The query context object + * @param {Function} addCallback Callback invoked by the provider to add a new + * result. + */ + async startQuery(queryContext, addCallback) { + let keyword = queryContext.tokens[0]?.value; + + let searchString = UrlbarUtils.substringAfter( + queryContext.searchString, + keyword + ).trim(); + let { entry, url, postData } = await lazy.KeywordUtils.getBindableKeyword( + keyword, + searchString + ); + if (!entry || !url) { + return; + } + + let title; + if (entry.url.host && searchString) { + // If we have a search string, the result has the title + // "host: searchString". + title = UrlbarUtils.strings.formatStringFromName( + "bookmarkKeywordSearch", + [ + entry.url.host, + queryContext.tokens + .slice(1) + .map(t => t.value) + .join(" "), + ] + ); + } else { + title = UrlbarUtils.unEscapeURIForUI(url); + } + + let result = new lazy.UrlbarResult( + UrlbarUtils.RESULT_TYPE.KEYWORD, + UrlbarUtils.RESULT_SOURCE.BOOKMARKS, + ...lazy.UrlbarResult.payloadAndSimpleHighlights(queryContext.tokens, { + title: [title, UrlbarUtils.HIGHLIGHT.TYPED], + url: [url, UrlbarUtils.HIGHLIGHT.TYPED], + keyword: [keyword, UrlbarUtils.HIGHLIGHT.TYPED], + input: queryContext.searchString, + postData, + icon: UrlbarUtils.getIconForUrl(entry.url), + }) + ); + result.heuristic = true; + addCallback(this, result); + } +} + +export var UrlbarProviderBookmarkKeywords = new ProviderBookmarkKeywords(); -- cgit v1.2.3