fix intermittent crash when selecting a link (#2620) (#2622)

This commit is contained in:
Saturnino Abril 2019-03-05 08:54:03 +08:00 committed by GitHub
parent 9253134612
commit fe46802aed
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 0 deletions

View file

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

View file

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

View file

@ -97,6 +97,10 @@ export function getScheme(url) {
}
export function matchPermalink(link, rootURL) {
if (!link || !rootURL) {
return null;
}
return new RegExp('^' + escapeRegex(rootURL) + '\\/([^\\/]+)\\/pl\\/(\\w+)').exec(link);
}

View file

@ -1,6 +1,8 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import assert from 'assert';
import * as UrlUtils from 'app/utils/url';
/* eslint-disable max-nested-callbacks */
@ -86,4 +88,31 @@ describe('UrlUtils', () => {
expect(UrlUtils.stripTrailingSlashes(url)).toEqual(expected);
});
});
describe('matchPermalink', () => {
const ROOT_URL = 'http://localhost:8065';
const tests = [
{name: 'should return null if all inputs are empty', input: {link: '', rootURL: ''}, expected: null},
{name: 'should return null if any of the inputs is null', input: {link: '', rootURL: null}, expected: null},
{name: 'should return null if any of the inputs is null', input: {link: null, rootURL: ''}, expected: null},
{name: 'should return null for not supported link', input: {link: 'https://mattermost.com', rootURL: ROOT_URL}, expected: null},
{name: 'should match permalink', input: {link: ROOT_URL + '/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsgy', rootURL: ROOT_URL}, expected: ['http://localhost:8065/ad-1/pl/qe93kkfd7783iqwuwfcwcxbsgy', 'ad-1', 'qe93kkfd7783iqwuwfcwcxbsgy']},
];
for (const test of tests) {
const {name, input, expected} = test;
it(name, () => {
const actual = UrlUtils.matchPermalink(input.link, input.rootURL);
if (actual) {
assert.equal(actual[0], expected[0]);
assert.equal(actual[1], expected[1]);
assert.equal(actual[2], expected[2]);
} else {
assert.equal(actual, expected);
}
});
}
});
});