mirror of
https://github.com/laurent22/joplin.git
synced 2024-11-27 08:21:03 +02:00
This commit is contained in:
parent
2aea7fcc25
commit
563b9d8f71
@ -421,10 +421,12 @@ class Dialog extends React.PureComponent<Props, State> {
|
||||
// make list scroll to top in every search
|
||||
this.makeItemIndexVisible(0);
|
||||
|
||||
const keywordsWithoutEmptyString = keywords?.filter(v => !!v);
|
||||
|
||||
this.setState({
|
||||
listType: listType,
|
||||
results: results,
|
||||
keywords: keywords ? keywords : await this.keywords(searchQuery),
|
||||
keywords: keywordsWithoutEmptyString ? keywordsWithoutEmptyString : await this.keywords(searchQuery),
|
||||
selectedItemId: results.length === 0 ? null : getResultId(results[0]),
|
||||
resultsInBody: resultsInBody,
|
||||
commandArgs: commandArgs,
|
||||
|
@ -24,6 +24,12 @@ describe('string-utils', () => {
|
||||
'dfasdf', '[', ']', { escapeHtml: true }, '[d]fas[d]f',
|
||||
],
|
||||
[['zzz'], 'zzz<img src=q onerror=eval("require(\'child_process\').exec(\'mate-calc\');");>', 'a', 'b', { escapeHtml: true }, 'azzzb<img src=q onerror=eval("require('child_process').exec('mate-calc');");>'],
|
||||
[['a'], 'non-latin-chars-éão', '<span>', '</span>', { escapeHtml: true }, 'non-l<span>a</span>tin-ch<span>a</span>rs-é<span>ã</span>o'],
|
||||
[['o'], 'Abrir diretório de perfis > (openProfileDirectory)', '<span>', '</span>', { escapeHtml: true },
|
||||
'Abrir diret<span>ó</span>ri<span>o</span> de perfis > (<span>o</span>penPr<span>o</span>fileDirect<span>o</span>ry)'],
|
||||
[[], '<>"\'&', 'a', 'b', { escapeHtml: true }, '<>"'&'],
|
||||
[[], '<>"\'&', 'a', 'b', { escapeHtml: false }, '<>"\'&'],
|
||||
[['a'], 'non-latin-chars-éão', '<<<', '>>>', { escapeHtml: false }, 'non-l<<<a>>>tin-ch<<<a>>>rs-é<<<ã>>>o'],
|
||||
])('should surround keywords with strings (case %#)', (async (keywords, input, prefix, suffix, options, expected) => {
|
||||
const actual = StringUtils.surroundKeywords(keywords, input, prefix, suffix, options);
|
||||
|
||||
|
@ -235,9 +235,9 @@ interface SurroundKeywordOptions {
|
||||
export function surroundKeywords(keywords: KeywordType, text: string, prefix: string, suffix: string, options: SurroundKeywordOptions|null = null) {
|
||||
options = { escapeHtml: false, ...options };
|
||||
|
||||
text = options.escapeHtml ? htmlentities(text) : text;
|
||||
|
||||
if (!keywords.length) return text;
|
||||
if (!keywords.length) {
|
||||
return options.escapeHtml ? htmlentities(text) : text;
|
||||
}
|
||||
|
||||
let regexString = keywords
|
||||
.map(k => {
|
||||
@ -251,8 +251,18 @@ export function surroundKeywords(keywords: KeywordType, text: string, prefix: st
|
||||
})
|
||||
.join('|');
|
||||
regexString = `(${regexString})`;
|
||||
const re = new RegExp(regexString, 'gi');
|
||||
return text.replace(re, `${prefix}$1${suffix}`);
|
||||
const keywordRegex = new RegExp(regexString, 'gi');
|
||||
|
||||
return text
|
||||
.split(keywordRegex)
|
||||
.map((textSegment) => {
|
||||
const encodedText = options.escapeHtml ? htmlentities(textSegment) : textSegment;
|
||||
if (keywordRegex.test(textSegment)) {
|
||||
return `${prefix}${encodedText}${suffix}`;
|
||||
} else {
|
||||
return encodedText;
|
||||
}
|
||||
}).join('');
|
||||
}
|
||||
|
||||
export function substrWithEllipsis(s: string, start: number, length: number) {
|
||||
|
Loading…
Reference in New Issue
Block a user