Skip to content

Commit

Permalink
Merge pull request #12 from JJJ/issue/2796
Browse files Browse the repository at this point in the history
Issue/2796
  • Loading branch information
JJJ authored Jul 22, 2020
2 parents e2f4245 + ae6fa13 commit dc88363
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
16 changes: 14 additions & 2 deletions coffee/lib/abstract-chosen.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -168,9 +169,11 @@ class AbstractChosen

results = 0

searchMatchFromValue = false
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

Expand All @@ -196,11 +199,15 @@ class AbstractChosen
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 query.length
startpos = search_match.index
if query.length and not searchMatchFromValue
startpos = search_match.index highlightRegex
prefix = text.slice(0, startpos)
fix = text.slice(startpos, startpos + query.length)
suffix = text.slice(startpos + query.length)
Expand Down Expand Up @@ -228,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
Expand Down
5 changes: 5 additions & 0 deletions public/options.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ <h3>Example:</h3>
<td>false</td>
<td>By default, Chosen’s search matches starting at the beginning of a word. Setting this option to <code class="language-javascript">true</code> 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.</td>
</tr>
<tr>
<td>search_in_values</td>
<td>false</td>
<td>By default, Chosen’s search matches in content of HTML&lt;option&gt; element. Setting this option to <code class="language-javascript">true</code> allows matches in <code>value</code> attribute of the &lt;option&gt; element.</td>
</tr>
<tr>
<td>group_search</td>
<td>true</td>
Expand Down

0 comments on commit dc88363

Please sign in to comment.