Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Link for channels are not rendering correctly #8985

Merged
merged 3 commits into from
Dec 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions packages/rocketchat-mentions/Mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class {
return new RegExp(`@(${ this.pattern })`, 'gm');
}
get channelMentionRegex() {
return new RegExp(`#(${ this.pattern })`, 'gm');
return new RegExp(`^#(${ this.pattern })| #(${ this.pattern })`, 'gm');
}
replaceUsers(str, message, me) {
return str.replace(this.userMentionRegex, (match, username) => {
Expand All @@ -50,18 +50,25 @@ export default class {
}
replaceChannels(str, message) {
//since apostrophe escaped contains # we need to unescape it
return str.replace(/'/g, '\'').replace(this.channelMentionRegex, (match, name) => {
return str.replace(/'/g, '\'').replace(this.channelMentionRegex, (match, n1, n2) => {
const name = n1 || n2;
if (message.temp == null && _.findWhere(message.channels, {name}) == null) {
return match;
}

// remove the link from inside the link and put before
if (/^\s/.test(match)) {
return ` <a class="mention-link" data-channel="${ name }">${ match.trim() }</a>`;
}

return `<a class="mention-link" data-channel="${ name }">${ match }</a>`;
});
}
getUserMentions(str) {
return str.match(this.userMentionRegex) || [];
}
getChannelMentions(str) {
return str.match(this.channelMentionRegex) || [];
return (str.match(this.channelMentionRegex) || []).map(match => match.trim());
}
parse(message) {
let msg = (message && message.html) || '';
Expand Down
86 changes: 53 additions & 33 deletions packages/rocketchat-mentions/tests/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ describe('Mention', function() {
assert.deepEqual(result, mention.getUserMentions(text));
});
});
it.skip('shoud return without the "." from "@rocket.cat."', () => {
it.skip('should return without the "." from "@rocket.cat."', () => {
assert.deepEqual(result, mention.getUserMentions('@rocket.cat.'));
});
it.skip('shoud return without the "_" from "@rocket.cat_"', () => {
it.skip('should return without the "_" from "@rocket.cat_"', () => {
assert.deepEqual(result, mention.getUserMentions('@rocket.cat_'));
});
it.skip('shoud return without the "-" from "@rocket.cat."', () => {
it.skip('should return without the "-" from "@rocket.cat."', () => {
assert.deepEqual(result, mention.getUserMentions('@rocket.cat-'));
});
});
Expand Down Expand Up @@ -135,13 +135,13 @@ describe('Mention', function() {
assert.deepEqual(result, mention.getChannelMentions(text));
});
});
it.skip('shoud return without the "." from "#general."', () => {
it.skip('should return without the "." from "#general."', () => {
assert.deepEqual(result, mention.getUserMentions('#general.'));
});
it.skip('shoud return without the "_" from "#general_"', () => {
it.skip('should return without the "_" from "#general_"', () => {
assert.deepEqual(result, mention.getUserMentions('#general_'));
});
it.skip('shoud return without the "-" from "#general."', () => {
it.skip('should return without the "-" from "#general."', () => {
assert.deepEqual(result, mention.getUserMentions('#general-'));
});
});
Expand All @@ -159,7 +159,26 @@ describe('Mention', function() {
});
});
});

describe('for url with fragments', () => {
const result = [];
[
'http://localhost/#general'
].forEach(text => {
it(`should return nothing from "${ text }"`, () => {
assert.deepEqual(result, mention.getChannelMentions(text));
});
});
});
describe('for messages with url and channels', () => {
const result = ['#general'];
[
'http://localhost/#general #general'
].forEach(text => {
it(`should return "${ JSON.stringify(result) }" from "${ text }"`, () => {
assert.deepEqual(result, mention.getChannelMentions(text));
});
});
});
});

});
Expand All @@ -169,25 +188,25 @@ const message = {
};
describe('replace methods', function() {
describe('replaceUsers', () => {
it('shoud render for @all', () => {
it('should render for @all', () => {
const result = mention.replaceUsers('@all', message, 'me');
assert.equal('<a class="mention-link mention-link-me mention-link-all background-attention-color">@all</a>', result);
});
const str2 = '@rocket.cat';
it(`shoud render for ${ str2 }`, () => {
it(`should render for ${ str2 }`, () => {
const result = mention.replaceUsers('@rocket.cat', message, 'me');
assert.equal(result, `<a class="mention-link " data-username="${ str2.replace('@', '') }" title="">${ str2 }</a>`);
});

it(`shoud render for "hello ${ str2 }"`, () => {
it(`should render for "hello ${ str2 }"`, () => {
const result = mention.replaceUsers(`hello ${ str2 }`, message, 'me');
assert.equal(result, `hello <a class="mention-link " data-username="${ str2.replace('@', '') }" title="">${ str2 }</a>`);
});
it('shoud render for unknow/private user "hello @unknow"', () => {
it('should render for unknow/private user "hello @unknow"', () => {
const result = mention.replaceUsers('hello @unknow', message, 'me');
assert.equal(result, 'hello @unknow');
});
it('shoud render for me', () => {
it('should render for me', () => {
const result = mention.replaceUsers('hello @me', message, 'me');
assert.equal(result, 'hello <a class="mention-link mention-link-me background-primary-action-color" data-username="me" title="">@me</a>');
});
Expand All @@ -197,69 +216,70 @@ describe('replace methods', function() {
beforeEach(() => {
mention.useRealName = () => true;
});
it('shoud render for @all', () => {
it('should render for @all', () => {
const result = mention.replaceUsers('@all', message, 'me');
assert.equal('<a class="mention-link mention-link-me mention-link-all background-attention-color">@all</a>', result);
});

const str2 = '@rocket.cat';
const str2Name = 'Rocket.Cat';
it(`shoud render for ${ str2 }`, () => {
it(`should render for ${ str2 }`, () => {
const result = mention.replaceUsers('@rocket.cat', message, 'me');
assert.equal(result, `<a class="mention-link " data-username="${ str2.replace('@', '') }" title="${ str2.replace('@', '') }">${ str2Name }</a>`);
});

it(`shoud render for "hello ${ str2 }"`, () => {
it(`should render for "hello ${ str2 }"`, () => {
const result = mention.replaceUsers(`hello ${ str2 }`, message, 'me');
assert.equal(result, `hello <a class="mention-link " data-username="${ str2.replace('@', '') }" title="${ str2.replace('@', '') }">${ str2Name }</a>`);
});
it('shoud render for unknow/private user "hello @unknow"', () => {
it('should render for unknow/private user "hello @unknow"', () => {
const result = mention.replaceUsers('hello @unknow', message, 'me');
assert.equal(result, 'hello @unknow');
});
it('shoud render for me', () => {
it('should render for me', () => {
const result = mention.replaceUsers('hello @me', message, 'me');
assert.equal(result, 'hello <a class="mention-link mention-link-me background-primary-action-color" data-username="me" title="me">Me</a>');
});
});

describe('replaceChannels', () => {
it('shoud render for #general', () => {
const result = mention.replaceChannels('#general', message, 'me');
it('should render for #general', () => {
const result = mention.replaceChannels('#general', message);
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result);
});
const str2 = '#rocket.cat';
it(`shoud render for ${ str2 }`, () => {
const result = mention.replaceChannels(str2, message, 'me');
it(`should render for ${ str2 }`, () => {
const result = mention.replaceChannels(str2, message);
assert.equal(result, `<a class="mention-link" data-channel="${ str2.replace('#', '') }">${ str2 }</a>`);
});
it(`shoud render for "hello ${ str2 }"`, () => {
const result = mention.replaceChannels(`hello ${ str2 }`, message, 'me');
it(`should render for "hello ${ str2 }"`, () => {
const result = mention.replaceChannels(`hello ${ str2 }`, message);
console.log('result', result);
assert.equal(result, `hello <a class="mention-link" data-channel="${ str2.replace('#', '') }">${ str2 }</a>`);
});
it('shoud render for unknow/private channel "hello #unknow"', () => {
const result = mention.replaceChannels('hello #unknow', message, 'me');
it('should render for unknow/private channel "hello #unknow"', () => {
const result = mention.replaceChannels('hello #unknow', message);
assert.equal(result, 'hello #unknow');
});
});

describe('parse all', () => {
it('shoud render for #general', () => {
it('should render for #general', () => {
message.html = '#general';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result.html);
});
it('shoud render for "#general and @rocket.cat', () => {
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a> and <a class="mention-link " data-username="rocket.cat" title="">@rocket.cat</a>', result.html);
});
it('shoud render for "', () => {
it('should render for "', () => {
message.html = '';
const result = mention.parse(message, 'me');
assert.equal('', result.html);
});
it('shoud render for "simple text', () => {
it('should render for "simple text', () => {
message.html = 'simple text';
const result = mention.parse(message, 'me');
assert.equal('simple text', result.html);
Expand All @@ -270,22 +290,22 @@ describe('replace methods', function() {
beforeEach(() => {
mention.useRealName = () => true;
});
it('shoud render for #general', () => {
it('should render for #general', () => {
message.html = '#general';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result.html);
});
it('shoud render for "#general and @rocket.cat', () => {
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a> and <a class="mention-link " data-username="rocket.cat" title="rocket.cat">Rocket.Cat</a>', result.html);
});
it('shoud render for "', () => {
it('should render for "', () => {
message.html = '';
const result = mention.parse(message, 'me');
assert.equal('', result.html);
});
it('shoud render for "simple text', () => {
it('should render for "simple text', () => {
message.html = 'simple text';
const result = mention.parse(message, 'me');
assert.equal('simple text', result.html);
Expand Down