PLT-7518 Added unit tests for channel linking (#7352)
* PLT-7518 Added unit tests for channel linking * Removed unused escaping function
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
8391ef6288
Коммит
670bfbf626
49
webapp/tests/utils/formatting_channel_links.test.jsx
Обычный файл
49
webapp/tests/utils/formatting_channel_links.test.jsx
Обычный файл
@@ -0,0 +1,49 @@
|
|||||||
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
import assert from 'assert';
|
||||||
|
|
||||||
|
import * as TextFormatting from 'utils/text_formatting.jsx';
|
||||||
|
|
||||||
|
describe('TextFormatting.ChannelLinks', () => {
|
||||||
|
it('Not channel links', (done) => {
|
||||||
|
assert.equal(
|
||||||
|
TextFormatting.formatText('~123').trim(),
|
||||||
|
'<p>~123</p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
TextFormatting.formatText('~town-square').trim(),
|
||||||
|
'<p>~town-square</p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Channel links', (done) => {
|
||||||
|
assert.equal(
|
||||||
|
TextFormatting.formatText('~town-square', {
|
||||||
|
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
|
||||||
|
team: {name: 'myteam'}
|
||||||
|
}).trim(),
|
||||||
|
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~Town Square</a></p>'
|
||||||
|
);
|
||||||
|
assert.equal(
|
||||||
|
TextFormatting.formatText('~town-square.', {
|
||||||
|
channelNamesMap: {'town-square': {display_name: 'Town Square'}},
|
||||||
|
team: {name: 'myteam'}
|
||||||
|
}).trim(),
|
||||||
|
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~Town Square</a>.</p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(
|
||||||
|
TextFormatting.formatText('~town-square', {
|
||||||
|
channelNamesMap: {'town-square': {display_name: '<b>Reception</b>'}},
|
||||||
|
team: {name: 'myteam'}
|
||||||
|
}).trim(),
|
||||||
|
'<p><a class="mention-link" href="/myteam/channels/town-square" data-channel-mention="town-square">~<b>Reception</b></a></p>'
|
||||||
|
);
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -185,7 +185,7 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tokens.set(alias, {
|
tokens.set(alias, {
|
||||||
value: `<a class='mention-link' href="${href}" data-channel-mention="${channelName}">${displayName}</a>`,
|
value: `<a class="mention-link" href="${href}" data-channel-mention="${channelName}">~${displayName}</a>`,
|
||||||
originalText: mention
|
originalText: mention
|
||||||
});
|
});
|
||||||
return alias;
|
return alias;
|
||||||
@@ -196,7 +196,7 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
|
|||||||
|
|
||||||
if (channelMentionExists(channelNameLower)) {
|
if (channelMentionExists(channelNameLower)) {
|
||||||
// Exact match
|
// Exact match
|
||||||
const alias = addToken(channelNameLower, mention, '~' + channelNamesMap[channelNameLower].display_name);
|
const alias = addToken(channelNameLower, mention, escapeHtml(channelNamesMap[channelNameLower].display_name));
|
||||||
return spacer + alias;
|
return spacer + alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +209,8 @@ function autolinkChannelMentions(text, tokens, channelNamesMap, team) {
|
|||||||
|
|
||||||
if (channelMentionExists(channelNameLower)) {
|
if (channelMentionExists(channelNameLower)) {
|
||||||
const suffix = originalChannelName.substr(c - 1);
|
const suffix = originalChannelName.substr(c - 1);
|
||||||
const alias = addToken(channelNameLower, '~' + channelNameLower, '~' + channelNamesMap[channelNameLower].display_name);
|
const alias = addToken(channelNameLower, '~' + channelNameLower,
|
||||||
|
escapeHtml(channelNamesMap[channelNameLower].display_name));
|
||||||
return spacer + alias + suffix;
|
return spacer + alias + suffix;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -231,6 +232,18 @@ export function escapeRegex(text) {
|
|||||||
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const htmlEntities = {
|
||||||
|
'&': '&',
|
||||||
|
'<': '<',
|
||||||
|
'>': '>',
|
||||||
|
'"': '"',
|
||||||
|
"'": '''
|
||||||
|
};
|
||||||
|
|
||||||
|
export function escapeHtml(text) {
|
||||||
|
return text.replace(/[&<>"']/g, (match) => htmlEntities[match]);
|
||||||
|
}
|
||||||
|
|
||||||
function highlightCurrentMentions(text, tokens, mentionKeys = []) {
|
function highlightCurrentMentions(text, tokens, mentionKeys = []) {
|
||||||
let output = text;
|
let output = text;
|
||||||
|
|
||||||
|
|||||||
@@ -242,10 +242,6 @@ export function extractFirstLink(text) {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function escapeRegExp(string) {
|
|
||||||
return string.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Taken from http://stackoverflow.com/questions/1068834/object-comparison-in-javascript and modified slightly
|
// Taken from http://stackoverflow.com/questions/1068834/object-comparison-in-javascript and modified slightly
|
||||||
export function areObjectsEqual(x, y) {
|
export function areObjectsEqual(x, y) {
|
||||||
let p;
|
let p;
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user