Skip to content

Commit

Permalink
Formatting: Improve parenthesis handling in make_clickable().
Browse files Browse the repository at this point in the history
Improve the regular expression for making links clickable to account for parenthesis in links containing an extension, for example: `http://wordpress.org/my-image(2).jpg`.

Props coquardcyr, hellofromtonya, parthvataliya, rhellewellgmailcom.
Fixes #62037.


git-svn-id: https://develop.svn.wordpress.org/trunk@59143 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
peterwilsoncc committed Sep 30, 2024
1 parent cf53600 commit 9b6c034
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -2943,6 +2943,10 @@ function _make_url_clickable_cb( $matches ) {
$suffix = $matches[3];
}

if ( isset( $matches[4] ) && ! empty( $matches[4] ) ) {
$url .= $matches[4];
}

// Include parentheses in the URL only if paired.
while ( substr_count( $url, '(' ) < substr_count( $url, ')' ) ) {
$suffix = strrchr( $url, ')' ) . $suffix;
Expand Down Expand Up @@ -3115,6 +3119,7 @@ function make_clickable( $text ) {
)*
)
(\)?) # 3: Trailing closing parenthesis (for parenthesis balancing post processing).
(\\.\\w{2,6})? # 4: Allowing file extensions (e.g., .jpg, .png).
~xS';
/*
* The regex is a non-anchored pattern and does not have a single fixed starting character.
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/tests/formatting/makeClickable.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,16 @@ public function data_urls() {
Richard Hamming wrote about people getting more done with their doors closed, but',
),

// @ticket #62037
'URL with brackets in path before the extension' => array(
'text' => 'http://example-image(2).jpg',
'expected' => '<a href="http://example-image(2).jpg" rel="nofollow">http://example-image(2).jpg</a>',
),
'URL with brackets within path and with a extension' => array(
'text' => 'http://example-(2)-image.jpg',
'expected' => '<a href="http://example-(2)-image.jpg" rel="nofollow">http://example-(2)-image.jpg</a>',
),

// @ticket 11211
// Test with real comments which were incorrectly linked.
'real world: example.com text (.org URL)' => array(
Expand Down

0 comments on commit 9b6c034

Please sign in to comment.