Skip to content

Commit

Permalink
Merge pull request #5176 from nextcloud-libraries/fix/reference_url_p…
Browse files Browse the repository at this point in the history
…attern

fix(NcRichText): Make URL_PATTERN match localhost and URLs with ports
  • Loading branch information
mejo- authored Jan 30, 2024
2 parents c39a056 + 55aa36f commit 102db32
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/components/NcRichText/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
*
* @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

/**
* 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 102db32

Please sign in to comment.