Skip to content

Commit

Permalink
Issue #11248: Avoiding links with highlighted words (#12123)
Browse files Browse the repository at this point in the history
  • Loading branch information
rssilva authored and rodrigok committed Dec 21, 2018
1 parent 41d9305 commit 070ce9f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
11 changes: 3 additions & 8 deletions packages/rocketchat-highlight-words/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Meteor } from 'meteor/meteor';
import { RocketChat } from 'meteor/rocketchat:lib';
import _ from 'underscore';
import s from 'underscore.string';
import { highlightWords } from './helper';

function HighlightWordsClient(message) {
let msg = message;
Expand All @@ -17,14 +18,8 @@ function HighlightWordsClient(message) {
}
}

const to_highlight = RocketChat.getUserPreference(Meteor.userId(), 'highlights');
if (Array.isArray(to_highlight)) {
to_highlight.forEach((highlight) => {
if (!s.isBlank(highlight)) {
return msg = msg.replace(new RegExp(`(^|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(${ s.escapeRegExp(highlight) })($|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(?![^<]*>|[^<>]*<\\/)`, 'gmi'), '$1<span class="highlight-text">$2</span>$3');
}
});
}
const to_highlight = RocketChat.getUserPreference(Meteor.user(), 'highlights');
msg = highlightWords(msg, to_highlight);

message.html = msg;
return message;
Expand Down
42 changes: 42 additions & 0 deletions packages/rocketchat-highlight-words/client/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import s from 'underscore.string';

export const checkHighlightedWordsInUrls = (msg, highlight) => {
const urlRegex = new RegExp(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)(${ s.escapeRegExp(highlight) })\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)`, 'gmi');
const urlMatches = msg.match(urlRegex);

return urlMatches;
};

export const removeHighlightedUrls = (msg, highlight, urlMatches) => {

urlMatches.forEach((match) => {
const highlightRegex = new RegExp(highlight, 'gmi');
const withTemplate = match.replace(highlightRegex, `<span class="highlight-text">${ highlight }</span>`);
const regexWithTemplate = new RegExp(withTemplate, 'i');

msg = msg.replace(regexWithTemplate, match);
});

return msg;
};

export const highlightWords = (msg, to_highlight) => {
const highlightTemplate = '$1<span class="highlight-text">$2</span>$3';

if (Array.isArray(to_highlight)) {
to_highlight.forEach((highlight) => {
if (!s.isBlank(highlight)) {
const regex = new RegExp(`(^|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(${ s.escapeRegExp(highlight) })($|\\b|[\\s\\n\\r\\t.,،'\\\"\\+!?:-])(?![^<]*>|[^<>]*<\\/)`, 'gmi');
const urlMatches = checkHighlightedWordsInUrls(msg, highlight);

msg = msg.replace(regex, highlightTemplate);

if (urlMatches) {
msg = removeHighlightedUrls(msg, highlight, urlMatches);
}
}
});
}

return msg;
};
36 changes: 36 additions & 0 deletions packages/rocketchat-highlight-words/tests/helper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* eslint-env mocha */

import 'babel-polyfill';
import assert from 'assert';
import { highlightWords } from '../client/helper';

describe('helper', () => {
describe('highlightWords', () => {
it('highlights the correct words', () => {
const res = highlightWords('here is some word', ['word']);

assert.equal(res, 'here is some <span class="highlight-text">word</span>');
});

describe('handles links', () => {
it('not highlighting one link', () => {
const res = highlightWords('here we go https://somedomain.com/here-some.word/pulls more words after', ['word']);

assert.equal(res, 'here we go https://somedomain.com/here-some.word/pulls more words after');
});

it('not highlighting two links', () => {
const msg = 'here https://somedomain.com/here-some-foo/pulls more words after http://www.domain.com/some.foo/bar words after';
const res = highlightWords(msg, ['foo']);

assert.equal(res, msg);
});

it('not highlighting link but keep words on message highlighted', () => {
const res = highlightWords('here we go https://somedomain.com/here-some.foo/pulls more foo after', ['foo']);

assert.equal(res, 'here we go https://somedomain.com/here-some.foo/pulls more <span class="highlight-text">foo</span> after');
});
});
});
});

0 comments on commit 070ce9f

Please sign in to comment.