summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/InputListAutoComplete.sys.mjs
blob: 61623df09d0644e72f7144075ce2dbfdafe12a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* 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/. */

import { FormAutoCompleteResult } from "resource://gre/modules/nsFormAutoCompleteResult.sys.mjs";

export class InputListAutoComplete {
  classID = Components.ID("{bf1e01d0-953e-11df-981c-0800200c9a66}");
  QueryInterface = ChromeUtils.generateQI(["nsIInputListAutoComplete"]);

  autoCompleteSearch(aUntrimmedSearchString, aField) {
    const items = this.getListSuggestions(aField);
    const searchResult = items.length
      ? Ci.nsIAutoCompleteResult.RESULT_SUCCESS
      : Ci.nsIAutoCompleteResult.RESULT_NOMATCH;
    const defaultIndex = items.length ? 0 : -1;

    return new FormAutoCompleteResult(
      aUntrimmedSearchString,
      searchResult,
      defaultIndex,
      "",
      items,
      null
    );
  }

  getListSuggestions(aField) {
    const items = [];

    if (!aField?.list) {
      return items;
    }

    const lowerFieldValue = aField.value.toLowerCase();

    for (const option of aField.list.options) {
      const label = option.label || option.text || option.value || "";

      if (!label.toLowerCase().includes(lowerFieldValue)) {
        continue;
      }

      items.push({
        label,
        value: option.value,
        comment: "",
        removable: false,
      });
    }

    return items;
  }
}