Skip to content

Commit

Permalink
fix(NcRichText): Make URL_PATTERN match localhost and URLs with ports
Browse files Browse the repository at this point in the history
E.g. the file reference widget should work with links to Nextcloud on
localhost.

Also add some basic unit tests to prevent regressions with the regexes
in the future.

Signed-off-by: Jonas <jonas@freesources.org>
  • Loading branch information
mejo- committed Jan 29, 2024
1 parent a0c316f commit 11aa3db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/components/NcRichText/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
*
* @type {RegExp}
*/
export const URL_PATTERN = /(\s|^)(https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z]+(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig
export const URL_PATTERN = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(\s|$)/ig
export const RN_AUTOLINK = /(\s|^)(https?:\/\/)([-A-Z0-9+_.]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*)(?=\s|\)|$)/ig

/**
* Regex pattern to identify strings as links and then making them clickable
* Opposed to the above regex this one also matches IP addresses, which we would like to be clickable,
* but in general resolving references for them might mostly not work,
* as the link provider checks for local addresses and does not resolve them.
*
* @type {RegExp}
*/
export const URL_PATTERN_AUTOLINK = /(\s|\(|^)((https?:\/\/)((?:[-A-Z0-9+_]+\.)+[-A-Z0-9]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\s|\)|$)/ig
export const URL_PATTERN_AUTOLINK = /(\s|\(|^)((https?:\/\/)([-A-Z0-9+_.]+[-A-Z0-9]+(?::[0-9]+)?(?:\/[-A-Z0-9+&@#%?=~_|!:,.;()]*)*))(?=\s|\)|$)/ig
21 changes: 21 additions & 0 deletions tests/unit/components/NcRichText/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { URL_PATTERN, URL_PATTERN_AUTOLINK } from '../../../../src/components/NcRichText/helpers.js'

describe('URL_PATTERN', () => {
const urls = [
'http://example.org',
'https://example.org/',
'https://cloud.example.org/index.php/apps/files/',
'http://localhost/',
'http://localhost:8080/index.php',
'http://192.168.1.3/',
]

for (const url of urls) {
it(`URL_PATTERN regex matches URL ${url}`, () => {
expect(new RegExp(URL_PATTERN).exec(url)).toBeTruthy()
})
it(`URL_PATTERN_AUTOLINK regex matches URL ${url}`, () => {
expect(new RegExp(URL_PATTERN_AUTOLINK).exec(url)).toBeTruthy()
})
}
})

0 comments on commit 11aa3db

Please sign in to comment.