PLT-1424 Added XRegExp library to fix search highlighting (#3787)

Этот коммит содержится в:
Harrison Healey
2016-08-12 07:49:19 -04:00
коммит произвёл Joram Wilander
родитель 96420542b1
Коммит c0a905c037
2 изменённых файлов: 8 добавлений и 10 удалений

Просмотреть файл

@@ -31,7 +31,8 @@
"react-textarea-autosize": "4.0.3", "react-textarea-autosize": "4.0.3",
"superagent": "2.1.0", "superagent": "2.1.0",
"twemoji": "2.0.5", "twemoji": "2.0.5",
"velocity-animate": "1.2.3" "velocity-animate": "1.2.3",
"xregexp": "3.1.1"
}, },
"devDependencies": { "devDependencies": {
"babel-core": "6.13.2", "babel-core": "6.13.2",

Просмотреть файл

@@ -11,6 +11,7 @@ import PreferenceStore from 'stores/preference_store.jsx';
import UserStore from 'stores/user_store.jsx'; import UserStore from 'stores/user_store.jsx';
import twemoji from 'twemoji'; import twemoji from 'twemoji';
import * as Utils from './utils.jsx'; import * as Utils from './utils.jsx';
import XRegExp from 'xregexp';
// pattern to detect the existance of a Chinese, Japanese, or Korean character in a string // pattern to detect the existance of a Chinese, Japanese, or Korean character in a string
// http://stackoverflow.com/questions/15033196/using-javascript-to-check-whether-a-string-contains-japanese-characters-includi // http://stackoverflow.com/questions/15033196/using-javascript-to-check-whether-a-string-contains-japanese-characters-includi
@@ -140,13 +141,9 @@ function autolinkEmails(text, tokens) {
return autolinker.link(text); return autolinker.link(text);
} }
function autolinkAtMentions(text, tokens) { const punctuation = XRegExp.cache('[^\\pL\\d]');
// Return true if provided character is punctuation
function isPunctuation(character) {
const re = /[\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]/g;
return re.test(character);
}
function autolinkAtMentions(text, tokens) {
// Test if provided text needs to be highlighted, special mention or current user // Test if provided text needs to be highlighted, special mention or current user
function mentionExists(u) { function mentionExists(u) {
return (Constants.SPECIAL_MENTIONS.indexOf(u) !== -1 || UserStore.getProfileByUsername(u)); return (Constants.SPECIAL_MENTIONS.indexOf(u) !== -1 || UserStore.getProfileByUsername(u));
@@ -176,7 +173,7 @@ function autolinkAtMentions(text, tokens) {
const originalUsername = usernameLower; const originalUsername = usernameLower;
for (let c = usernameLower.length; c > 0; c--) { for (let c = usernameLower.length; c > 0; c--) {
if (isPunctuation(usernameLower[c - 1])) { if (punctuation.test(usernameLower[c - 1])) {
usernameLower = usernameLower.substring(0, c - 1); usernameLower = usernameLower.substring(0, c - 1);
if (mentionExists(usernameLower)) { if (mentionExists(usernameLower)) {
@@ -299,8 +296,8 @@ function autolinkHashtags(text, tokens) {
return output.replace(/(^|\W)(#[a-zA-ZäöüÄÖÜß][a-zA-Z0-9äöüÄÖÜß.\-_]*)\b/g, replaceHashtagWithToken); return output.replace(/(^|\W)(#[a-zA-ZäöüÄÖÜß][a-zA-Z0-9äöüÄÖÜß.\-_]*)\b/g, replaceHashtagWithToken);
} }
const puncStart = /^[^a-zA-Z0-9#]+/; const puncStart = XRegExp.cache('^[^\\pL\\d\\s#]+');
const puncEnd = /[^a-zA-Z0-9]+$/; const puncEnd = XRegExp.cache('[^\\pL\\d\\s]+$');
function parseSearchTerms(searchTerm) { function parseSearchTerms(searchTerm) {
let terms = []; let terms = [];