commit 90d46ae1bbeec8a80cf439689ec24982e9948bf2
parent cbcf596518dd7593c8a2bced50e500d51f735a34
Author: Mark Banner <standard8@mozilla.com>
Date: Wed, 5 Nov 2025 10:53:24 +0000
Bug 1996979 - Simplify UrlbarTokenizer.tokenize to not modify the object in-place, so that it can be used in more cases. r=urlbar-reviewers,jteow
This avoids having to somehow indicate to TypeScript that the original object is modified, and makes the code more
explicit about what is happening.
Differential Revision: https://phabricator.services.mozilla.com/D270415
Diffstat:
6 files changed, 35 insertions(+), 36 deletions(-)
diff --git a/browser/components/urlbar/UrlbarProviderPlaces.sys.mjs b/browser/components/urlbar/UrlbarProviderPlaces.sys.mjs
@@ -476,7 +476,7 @@ class Search {
// Use the original string here, not the stripped one, so the tokenizer can
// properly recognize token types.
- let { tokens } = lazy.UrlbarTokenizer.tokenize({
+ let tokens = lazy.UrlbarTokenizer.tokenize({
searchString: unescapedSearchString,
trimmedSearchString: unescapedSearchString.trim(),
});
diff --git a/browser/components/urlbar/UrlbarProvidersManager.sys.mjs b/browser/components/urlbar/UrlbarProvidersManager.sys.mjs
@@ -8,8 +8,9 @@
*/
/**
- * @import { UrlbarProvider, UrlbarSearchStringTokenData } from "UrlbarUtils.sys.mjs"
+ * @import { UrlbarProvider } from "UrlbarUtils.sys.mjs"
* @import { UrlbarMuxer } from "UrlbarUtils.sys.mjs"
+ * @import { UrlbarSearchStringTokenData } from "UrlbarTokenizer.sys.mjs"
*/
const lazy = {};
@@ -425,7 +426,8 @@ export class ProvidersManager {
}
// Apply tokenization.
- lazy.UrlbarTokenizer.tokenize(queryContext);
+ let tokens = lazy.UrlbarTokenizer.tokenize(queryContext);
+ queryContext.tokens = tokens;
// If there's a single source, we are in restriction mode.
if (queryContext.sources && queryContext.sources.length == 1) {
diff --git a/browser/components/urlbar/UrlbarTokenizer.sys.mjs b/browser/components/urlbar/UrlbarTokenizer.sys.mjs
@@ -25,6 +25,16 @@ ChromeUtils.defineLazyGetter(lazy, "gFluentStrings", function () {
});
/**
+ * @typedef UrlbarSearchStringTokenData
+ * @property {Values<typeof lazy.UrlbarTokenizer.TYPE>} type
+ * The type of the token.
+ * @property {string} value
+ * The value of the token.
+ * @property {string} lowerCaseValue
+ * The lower case version of the value.
+ */
+
+/**
* This Map stores key-value pairs where each key is a restrict token
* and each value is an array containing the localized keyword and the
* english keyword.
@@ -132,23 +142,22 @@ export var UrlbarTokenizer = {
/**
* Tokenizes the searchString from a UrlbarQueryContext.
*
- * @param {UrlbarQueryContext} queryContext
- * The query context object to tokenize
- * @returns {UrlbarQueryContext} the same query context object with a new
- * tokens property.
+ * @param {object} context
+ * @param {string} context.searchString
+ * @param {string} [context.searchMode]
+ * @param {string} context.trimmedSearchString
+ * @returns {UrlbarSearchStringTokenData[]}
+ * The tokens associated with the query.
*/
- tokenize(queryContext) {
+ tokenize(context) {
lazy.logger.debug("Tokenizing search string", {
- searchString: queryContext.searchString,
+ searchString: context.searchString,
});
- if (!queryContext.trimmedSearchString) {
- queryContext.tokens = [];
- return queryContext;
+ if (!context.trimmedSearchString) {
+ return [];
}
- let unfiltered = splitString(queryContext);
- let tokens = filterTokens(unfiltered);
- queryContext.tokens = tokens;
- return queryContext;
+ let unfiltered = splitString(context);
+ return filterTokens(unfiltered);
},
/**
@@ -177,8 +186,9 @@ const CHAR_TO_TYPE_MAP = new Map(
/**
* Given a queryContext object, splits its searchString into string tokens.
*
- * @param {UrlbarQueryContext} queryContext
- * The query context object to tokenize.
+ * @param {object} context
+ * @param {string} context.searchString
+ * @param {string} [context.searchMode]
* @returns {string[]} An array of string tokens.
*/
function splitString({ searchString, searchMode }) {
diff --git a/browser/components/urlbar/UrlbarUtils.sys.mjs b/browser/components/urlbar/UrlbarUtils.sys.mjs
@@ -9,6 +9,7 @@
/**
* @import {Query} from "UrlbarProvidersManager.sys.mjs"
+ * @import {UrlbarSearchStringTokenData} from "UrlbarTokenizer.sys.mjs"
*/
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
@@ -2311,16 +2312,6 @@ UrlbarUtils.RESULT_PAYLOAD_SCHEMA = {
*/
/**
- * @typedef UrlbarSearchStringTokenData
- * @property {Values<typeof lazy.UrlbarTokenizer.TYPE>} type
- * The type of the token.
- * @property {string} value
- * The value of the token.
- * @property {string} lowerCaseValue
- * The lower case version of the value.
- */
-
-/**
* UrlbarQueryContext defines a user's autocomplete input from within the urlbar.
* It supplements it with details of how the search results should be obtained
* and what they consist of.
diff --git a/browser/components/urlbar/tests/unit/head.js b/browser/components/urlbar/tests/unit/head.js
@@ -135,7 +135,8 @@ function createContext(searchString = "foo", properties = {}) {
properties
)
);
- UrlbarTokenizer.tokenize(context);
+ let tokens = UrlbarTokenizer.tokenize(context);
+ context.tokens = tokens;
return context;
}
diff --git a/browser/components/urlbar/tests/unit/test_tokenizer.js b/browser/components/urlbar/tests/unit/test_tokenizer.js
@@ -511,14 +511,9 @@ add_task(async function test_tokenizer() {
);
}
- let newQueryContext = UrlbarTokenizer.tokenize(queryContext);
- Assert.equal(
- queryContext,
- newQueryContext,
- "The queryContext object is the same"
- );
+ let tokens = UrlbarTokenizer.tokenize(queryContext);
Assert.deepEqual(
- queryContext.tokens,
+ tokens,
queryContext.expectedTokens,
"Check the expected tokens"
);