fix intermittent crash when selecting a link (#2620)

This commit is contained in:
Saturnino Abril 2019-03-05 02:28:40 +08:00 committed by GitHub
parent c1a079a850
commit 8a4c6de86f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View file

@ -30,6 +30,8 @@ export default class MarkdownLink extends PureComponent {
static defaultProps = {
onPermalinkPress: () => true,
serverURL: '',
siteURL: '',
};
static contextTypes = {

View file

@ -53,6 +53,8 @@ export default class PostListBase extends PureComponent {
onLoadMoreUp: () => true,
renderFooter: () => null,
refreshing: false,
serverURL: '',
siteURL: '',
};
componentWillMount() {

View file

@ -99,6 +99,10 @@ export function getScheme(url) {
}
export function matchDeepLink(url, serverURL, siteURL) {
if (!url || !serverURL || !siteURL) {
return null;
}
const linkRoot = `(?:${escapeRegex(serverURL)}|${escapeRegex(siteURL)})?`;
let match = new RegExp('^' + linkRoot + '\\/([^\\/]+)\\/channels\\/(\\S+)').exec(url);

View file

@ -86,4 +86,26 @@ describe('UrlUtils', () => {
expect(UrlUtils.stripTrailingSlashes(url)).toEqual(expected);
});
});
describe('matchDeepLink', () => {
const SITE_URL = 'http://localhost:8065';
const SERVER_URL = 'http://localhost:8065';
const tests = [
{name: 'should return null if all inputs are empty', input: {url: '', serverURL: '', siteURL: ''}, expected: null},
{name: 'should return null if any of the input is null', input: {url: '', serverURL: '', siteURL: null}, expected: null},
{name: 'should return null if any of the input is null', input: {url: '', serverURL: null, siteURL: ''}, expected: null},
{name: 'should return null if any of the input is null', input: {url: null, serverURL: '', siteURL: ''}, expected: null},
{name: 'should return null for not supported link', input: {url: 'https://mattermost.com', serverURL: SERVER_URL, siteURL: SITE_URL}, expected: null},
{name: 'should match channel link', input: {url: SITE_URL + '/ad-1/channels/town-square', serverURL: SERVER_URL, siteURL: SITE_URL}, expected: {channelName: 'town-square', teamName: 'ad-1', type: 'channel'}},
{name: 'should match permalink', input: {url: SITE_URL + '/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsgy', serverURL: SERVER_URL, siteURL: SITE_URL}, expected: {postId: 'qe93kkfd7783iqwuwfcwcxbsgy', teamName: 'ad-1', type: 'permalink'}},
];
for (const test of tests) {
const {name, input, expected} = test;
it(name, () => {
expect(UrlUtils.matchDeepLink(input.url, input.serverURL, input.siteURL)).toEqual(expected);
});
}
});
});