From acf93eac4b20030cd098c8f5d170ec369ab02b28 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 6 May 2024 13:20:39 -0700 Subject: [PATCH] bootstrap_typeahead: Store values in a WeakMap. Signed-off-by: Anders Kaseorg --- web/src/bootstrap_typeahead.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/web/src/bootstrap_typeahead.ts b/web/src/bootstrap_typeahead.ts index 0e15bb2d96..46206acc94 100644 --- a/web/src/bootstrap_typeahead.ts +++ b/web/src/bootstrap_typeahead.ts @@ -225,6 +225,7 @@ class Typeahead { stopAdvance: boolean; advanceKeyCodes: number[]; parentElement?: string; + values: WeakMap; constructor(input_element: TypeaheadInputElement, options: TypeaheadOptions) { this.input_element = input_element; @@ -259,6 +260,7 @@ class Typeahead { this.helpOnEmptyStrings = options.helpOnEmptyStrings ?? false; this.naturalSearch = options.naturalSearch ?? false; this.parentElement = options.parentElement; + this.values = new WeakMap(); if (this.fixed) { this.$container.css("position", "fixed"); @@ -270,7 +272,8 @@ class Typeahead { } select(e?: JQuery.ClickEvent | JQuery.KeyUpEvent | JQuery.KeyDownEvent): this { - const val = this.$menu.find(".active").data("typeahead-value"); + const val = this.values.get(this.$menu.find(".active")[0]); + assert(val !== undefined); if (this.input_element.type === "contenteditable") { this.input_element.$element .text(this.updater(val, this.query, this.input_element, e) ?? "") @@ -294,7 +297,8 @@ class Typeahead { } set_value(): void { - const val = this.$menu.find(".active").data("typeahead-value"); + const val = this.values.get(this.$menu.find(".active")[0]); + assert(typeof val === "string"); if (this.input_element.type === "contenteditable") { this.input_element.$element.text(val); } else { @@ -408,7 +412,8 @@ class Typeahead { render(final_items: ItemType[], matching_items: ItemType[]): this { const $items: JQuery[] = final_items.map((item) => { - const $i = $(ITEM_HTML).data("typeahead-value", item); + const $i = $(ITEM_HTML); + this.values.set($i[0], item); const item_html = this.highlighter_html(item, this.query) ?? ""; const $item_html = $i.find("a").html(item_html);