From 83769f27b5656b45db91430b26132f13768b8176 Mon Sep 17 00:00:00 2001 From: Paul Naumov Date: Fri, 17 Mar 2017 00:09:10 +0300 Subject: [PATCH 1/5] Update abstract-chosen.coffee --- coffee/lib/abstract-chosen.coffee | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index f6f337ed5eb..a02a68273f0 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -26,6 +26,7 @@ class AbstractChosen @disable_search = @options.disable_search || false @enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true @group_search = if @options.group_search? then @options.group_search else true + @search_in_values = @options.search_in_values || false @search_contains = @options.search_contains || false @single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true @max_selected_options = @options.max_selected_options || Infinity @@ -165,6 +166,7 @@ class AbstractChosen results = 0 searchText = this.get_search_text() + searchMatchFromValue = null escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") regex = this.get_search_regex(escapedSearchText) highlightRegex = this.get_highlight_regex(escapedSearchText) @@ -189,10 +191,15 @@ class AbstractChosen unless option.group and not @group_search option.search_match = this.search_string_match(option.search_text, regex) + + if !option.search_match and @search_in_values + option.search_match = this.search_string_match(option.value, regex) + searchMatchFromValue = true + results += 1 if option.search_match and not option.group if option.search_match - if searchText.length + if searchText.length and !searchMatchFromValue startpos = option.search_text.search highlightRegex text = option.search_text.substr(0, startpos + searchText.length) + '' + option.search_text.substr(startpos + searchText.length) option.search_text = text.substr(0, startpos) + '' + text.substr(startpos) From 9cde3c42ac8dadde530446e20c49294045ef704c Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 17 Mar 2017 01:22:35 +0300 Subject: [PATCH 2/5] New "search_in_values" option, which allows you to perform search in "value" attribute of the ' + option.search_text.substr(startpos + searchText.length) option.search_text = text.substr(0, startpos) + '' + text.substr(startpos) From 2338e90af069fd2c44660199ac7b94ddde42cfd4 Mon Sep 17 00:00:00 2001 From: paul Date: Fri, 17 Mar 2017 01:34:01 +0300 Subject: [PATCH 3/5] Description of the new 'search_in_values' option --- public/options.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/public/options.html b/public/options.html index c64d5a6747f..45d4aa9f424 100644 --- a/public/options.html +++ b/public/options.html @@ -86,6 +86,11 @@

Example:

false By default, Chosen’s search matches starting at the beginning of a word. Setting this option to true allows matches starting from anywhere within a word. This is especially useful for options that include a lot of special characters or phrases in ()s and []s. + + search_in_values + false + By default, Chosen’s search matches in content of HTML<option> element. Setting this option to true allows matches in value attribute of the <option> element. + single_backstroke_delete true From 8093522724d55ad6dc16f3baba456940cbd5f6c4 Mon Sep 17 00:00:00 2001 From: John James Jacoby Date: Wed, 22 Jul 2020 18:20:26 -0500 Subject: [PATCH 4/5] Resolve merge conflict. --- coffee/lib/abstract-chosen.coffee | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index 85dd21fe061..f5482d8d19f 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -169,11 +169,11 @@ class AbstractChosen results = 0 - searchText = this.get_search_text() searchMatchFromValue = false - escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") - regex = this.get_search_regex(escapedSearchText) - highlightRegex = this.get_highlight_regex(escapedSearchText) + query = this.get_search_text() + escapedQuery = query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + regex = this.get_search_regex(escapedQuery) + highlightRegex = this.get_highlight_regex(escapedQuery) for option in @results_data @@ -196,26 +196,17 @@ class AbstractChosen text = if option.group then option.label else option.text unless option.group and not @group_search - option.search_match = this.search_string_match(option.search_text, regex) - + search_match = this.search_string_match(text, regex) + option.search_match = search_match? + if not option.search_match and @search_in_values option.search_match = this.search_string_match(option.value, regex) searchMatchFromValue = true - - results += 1 if option.search_match and not option.group - - if option.search_match - if searchText.length and not searchMatchFromValue - startpos = option.search_text.search highlightRegex - text = option.search_text.substr(0, startpos + searchText.length) + '
' + option.search_text.substr(startpos + searchText.length) - option.search_text = text.substr(0, startpos) + '' + text.substr(startpos) - search_match = this.search_string_match(text, regex) - option.search_match = search_match? results += 1 if option.search_match and not option.group if option.search_match - if query.length + if query.length and not searchMatchFromValue startpos = search_match.index prefix = text.slice(0, startpos) fix = text.slice(startpos, startpos + query.length) @@ -244,6 +235,11 @@ class AbstractChosen regex_flag = if @case_sensitive_search then "" else "i" new RegExp(regex_string, regex_flag) + get_highlight_regex: (escaped_search_string) -> + regex_anchor = if @search_contains then "" else "\\b" + regex_flag = if @case_sensitive_search then "" else "i" + new RegExp(regex_anchor + escaped_search_string, regex_flag) + search_string_match: (search_string, regex) -> match = regex.exec(search_string) match.index += 1 if !@search_contains && match?[1] # make up for lack of lookbehind operator in regex From ae6fa13d44c532c4b298d92a651df84d2026da57 Mon Sep 17 00:00:00 2001 From: John James Jacoby Date: Wed, 22 Jul 2020 18:22:36 -0500 Subject: [PATCH 5/5] Add back highlightRegex. --- coffee/lib/abstract-chosen.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coffee/lib/abstract-chosen.coffee b/coffee/lib/abstract-chosen.coffee index f5482d8d19f..2ad40bc9a17 100644 --- a/coffee/lib/abstract-chosen.coffee +++ b/coffee/lib/abstract-chosen.coffee @@ -207,7 +207,7 @@ class AbstractChosen if option.search_match if query.length and not searchMatchFromValue - startpos = search_match.index + startpos = search_match.index highlightRegex prefix = text.slice(0, startpos) fix = text.slice(startpos, startpos + query.length) suffix = text.slice(startpos + query.length)