From fe46802aed8803b93396fcfa485eaea6468f142c Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Tue, 5 Mar 2019 08:54:03 +0800 Subject: [PATCH] fix intermittent crash when selecting a link (#2620) (#2622) --- .../markdown/markdown_link/markdown_link.js | 2 ++ app/components/post_list/post_list_base.js | 2 ++ app/utils/url.js | 4 +++ app/utils/url.test.js | 29 +++++++++++++++++++ 4 files changed, 37 insertions(+) diff --git a/app/components/markdown/markdown_link/markdown_link.js b/app/components/markdown/markdown_link/markdown_link.js index 8aae75fa1..03e0efc7a 100644 --- a/app/components/markdown/markdown_link/markdown_link.js +++ b/app/components/markdown/markdown_link/markdown_link.js @@ -26,6 +26,8 @@ export default class MarkdownLink extends PureComponent { static defaultProps = { onPermalinkPress: () => true, + serverURL: '', + siteURL: '', }; static contextTypes = { diff --git a/app/components/post_list/post_list_base.js b/app/components/post_list/post_list_base.js index 9a48f8750..dade435c6 100644 --- a/app/components/post_list/post_list_base.js +++ b/app/components/post_list/post_list_base.js @@ -51,6 +51,8 @@ export default class PostListBase extends PureComponent { onLoadMoreUp: () => true, renderFooter: () => null, refreshing: false, + serverURL: '', + siteURL: '', }; componentWillMount() { diff --git a/app/utils/url.js b/app/utils/url.js index 4881437db..5f1cc0737 100644 --- a/app/utils/url.js +++ b/app/utils/url.js @@ -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); } diff --git a/app/utils/url.test.js b/app/utils/url.test.js index 2a7acc15e..088bc4244 100644 --- a/app/utils/url.test.js +++ b/app/utils/url.test.js @@ -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); + } + }); + } + }); });