1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-11 18:24:43 +02:00

Desktop: Fix highlighting in GotoAnything dialogue (#7592)

This commit is contained in:
andy1631 2023-02-08 15:20:54 +01:00 committed by GitHub
parent 631c41a1ff
commit 2d673902a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 14 deletions

View File

@ -9,13 +9,18 @@ describe('StringUtils', function() {
it('should surround keywords with strings', (async () => {
const testCases = [
[[], 'test', 'a', 'b', 'test'],
[['test'], 'test', 'a', 'b', 'atestb'],
[['test'], 'Test', 'a', 'b', 'aTestb'],
[['te[]st'], 'Te[]st', 'a', 'b', 'aTe[]stb'],
[[], 'test', 'a', 'b', null, 'test'],
[['test'], 'test', 'a', 'b', null, 'atestb'],
[['test'], 'Test', 'a', 'b', null, 'aTestb'],
[['te[]st'], 'Te[]st', 'a', 'b', null, 'aTe[]stb'],
// [['test1', 'test2'], 'bla test1 blabla test1 bla test2 not this one - test22', 'a', 'b', 'bla atest1b blabla atest1b bla atest2b not this one - test22'],
[['test1', 'test2'], 'bla test1 test1 bla test2', '<span class="highlighted-keyword">', '</span>', 'bla <span class="highlighted-keyword">test1</span> <span class="highlighted-keyword">test1</span> bla <span class="highlighted-keyword">test2</span>'],
[['test1', 'test2'], 'bla test1 test1 bla test2', '<span class="highlighted-keyword">', '</span>', null, 'bla <span class="highlighted-keyword">test1</span> <span class="highlighted-keyword">test1</span> bla <span class="highlighted-keyword">test2</span>'],
// [[{ type:'regex', value:'test.*?'}], 'bla test1 test1 bla test2 test tttest', 'a', 'b', 'bla atest1b atest1b bla atest2b atestb tttest'],
[['test'], 'testTest', 'a', 'b', { escapeHtml: true }, 'atestbaTestb'],
[['test'], 'test test Test', 'a', 'b', { escapeHtml: true }, 'atestb atestb aTestb'],
[['d'], 'dfasdf', '[', ']', { escapeHtml: true }, '[d]fas[d]f'],
[[{ scriptType: 'en', type: 'regex', value: 'd*', valueRegex: 'd[^ \t\n\r,\\.,\\+\\-\\*\\?\\!\\=\\{\\}\\<\\>\\|\\:"\'\\(\\)\\[\\]]*?' }], '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&lt;img src=q onerror=eval(&quot;require(&apos;child_process&apos;).exec(&apos;mate-calc&apos;);&quot;);&gt;'],
];
for (let i = 0; i < testCases.length; i++) {
@ -25,9 +30,10 @@ describe('StringUtils', function() {
const input = t[1];
const prefix = t[2];
const suffix = t[3];
const expected = t[4];
const options = t[4];
const expected = t[5];
const actual = StringUtils.surroundKeywords(keywords, input, prefix, suffix);
const actual = StringUtils.surroundKeywords(keywords, input, prefix, suffix, options);
expect(actual).toBe(expected, `Test case ${i}`);
}

View File

@ -301,20 +301,17 @@ function surroundKeywords(keywords, text, prefix, suffix, options = null) {
escapeHtml: false,
}, options);
if (!keywords.length) return text;
text = options.escapeHtml ? htmlentities(text) : text;
function escapeHtml(s) {
if (!options.escapeHtml) return s;
return htmlentities(s);
}
if (!keywords.length) return text;
let regexString = keywords
.map(k => {
if (k.type === 'regex') {
return escapeHtml(stringUtilsCommon.replaceRegexDiacritics(k.valueRegex));
return stringUtilsCommon.replaceRegexDiacritics(k.valueRegex);
} else {
const value = typeof k === 'string' ? k : k.value;
return escapeHtml(stringUtilsCommon.replaceRegexDiacritics(stringUtilsCommon.pregQuote(value)));
return stringUtilsCommon.replaceRegexDiacritics(stringUtilsCommon.pregQuote(value));
}
})
.join('|');