[ticket/16512] Fix make_clickable() to make use of custom link classes

PHPBB3-16512
This commit is contained in:
rxu 2020-06-04 15:08:06 +07:00
parent d7ccd22383
commit da4b446ac6
No known key found for this signature in database
GPG key ID: 8EF9758E0A54D19E
2 changed files with 156 additions and 85 deletions

View file

@ -927,7 +927,7 @@ function make_clickable_callback($type, $whitespace, $url, $relative_url, $class
* Cuts down displayed size of link if over 50 chars, turns absolute links * Cuts down displayed size of link if over 50 chars, turns absolute links
* into relative versions when the server/script path matches the link * into relative versions when the server/script path matches the link
*/ */
function make_clickable($text, $server_url = false, $class = 'postlink') function make_clickable($text, $server_url = false, string $class = 'postlink')
{ {
if ($server_url === false) if ($server_url === false)
{ {
@ -948,29 +948,51 @@ function make_clickable($text, $server_url = false, $class = 'postlink')
$magic_url_match_args = array(); $magic_url_match_args = array();
} }
// relative urls for this board // Check if tne match for this $server_url and $class already exists
$magic_url_match_args[$server_url][] = array( $element_exists = false;
'#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#iu', if (isset($magic_url_match_args[$server_url]))
MAGIC_URL_LOCAL, {
$local_class, array_walk_recursive($magic_url_match_args[$server_url], function($value, $key) use (&$element_exists, $static_class)
); {
if ($value == $static_class)
{
$element_exists = true;
return;
}
}
);
}
// matches a xxxx://aaaaa.bbb.cccc. ... // Only add new $server_url and $class matches if not exist
$magic_url_match_args[$server_url][] = array( if (!$element_exists)
'#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#iu', {
MAGIC_URL_FULL, // relative urls for this board
$class, $magic_url_match_args[$server_url][] = array(
); '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#iu',
MAGIC_URL_LOCAL,
$local_class,
$static_class,
);
// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing // matches a xxxx://aaaaa.bbb.cccc. ...
$magic_url_match_args[$server_url][] = array( $magic_url_match_args[$server_url][] = array(
'#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#iu', '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#iu',
MAGIC_URL_WWW, MAGIC_URL_FULL,
$class, $class,
); $static_class,
);
// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
$magic_url_match_args[$server_url][] = array(
'#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#iu',
MAGIC_URL_WWW,
$class,
$static_class,
);
}
// matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode. // matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
$magic_url_match_args[$server_url][] = array( $magic_url_match_args[$server_url]['email'] = array(
'/(^|[\n\t (>])(' . get_preg_expression('email') . ')/iu', '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/iu',
MAGIC_URL_EMAIL, MAGIC_URL_EMAIL,
'', '',
@ -981,6 +1003,12 @@ function make_clickable($text, $server_url = false, $class = 'postlink')
{ {
if (preg_match($magic_args[0], $text, $matches)) if (preg_match($magic_args[0], $text, $matches))
{ {
// Only apply $class from the correcponding function call argument (excepting emails which never has a class)
if ($magic_args[3] != $static_class && $magic_args[1] != MAGIC_URL_EMAIL)
{
continue;
}
$text = preg_replace_callback($magic_args[0], function($matches) use ($magic_args) $text = preg_replace_callback($magic_args[0], function($matches) use ($magic_args)
{ {
$relative_url = isset($matches[3]) ? $matches[3] : ''; $relative_url = isset($matches[3]) ? $matches[3] : '';

View file

@ -27,131 +27,173 @@ class phpbb_functions_make_clickable_test extends phpbb_test_case
**/ **/
public function data_test_make_clickable_url_positive() public function data_test_make_clickable_url_positive()
{ {
return array( return [
array( [
'http://www.phpbb.com/community/', 'http://www.phpbb.com/community/',
'<!-- m --><a class="postlink" href="http://www.phpbb.com/community/">http://www.phpbb.com/community/</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.phpbb.com/community/">http://www.phpbb.com/community/</a><!-- m -->'
), ],
array( [
'http://www.phpbb.com/path/file.ext#section', 'http://www.phpbb.com/path/file.ext#section',
'<!-- m --><a class="postlink" href="http://www.phpbb.com/path/file.ext#section">http://www.phpbb.com/path/file.ext#section</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.phpbb.com/path/file.ext#section">http://www.phpbb.com/path/file.ext#section</a><!-- m -->'
), ],
array( [
'ftp://ftp.phpbb.com/', 'ftp://ftp.phpbb.com/',
'<!-- m --><a class="postlink" href="ftp://ftp.phpbb.com/">ftp://ftp.phpbb.com/</a><!-- m -->' '<!-- m --><a class="postlink" href="ftp://ftp.phpbb.com/">ftp://ftp.phpbb.com/</a><!-- m -->'
), ],
array( [
'sip://bantu@phpbb.com', 'sip://bantu@phpbb.com',
'<!-- m --><a class="postlink" href="sip://bantu@phpbb.com">sip://bantu@phpbb.com</a><!-- m -->' '<!-- m --><a class="postlink" href="sip://bantu@phpbb.com">sip://bantu@phpbb.com</a><!-- m -->'
), ],
array( [
'www.phpbb.com/community/', 'www.phpbb.com/community/',
'<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->' '<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->'
), ],
array( [
'http://testhost/viewtopic.php?t=1', 'http://testhost/viewtopic.php?t=1',
'<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->' '<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
), ],
array( [
'javascript://testhost/viewtopic.php?t=1', 'javascript://testhost/viewtopic.php?t=1',
'javascript://testhost/viewtopic.php?t=1' 'javascript://testhost/viewtopic.php?t=1'
), ],
array( [
"java\nscri\npt://testhost/viewtopic.php?t=1", "java\nscri\npt://testhost/viewtopic.php?t=1",
"java\nscri\n<!-- m --><a class=\"postlink\" href=\"pt://testhost/viewtopic.php?t=1\">pt://testhost/viewtopic.php?t=1</a><!-- m -->" "java\nscri\n<!-- m --><a class=\"postlink\" href=\"pt://testhost/viewtopic.php?t=1\">pt://testhost/viewtopic.php?t=1</a><!-- m -->"
), ],
array( [
'email@domain.com', 'email@domain.com',
'<!-- e --><a href="mailto:email@domain.com">email@domain.com</a><!-- e -->' '<!-- e --><a href="mailto:email@domain.com">email@domain.com</a><!-- e -->'
), ],
// Test appending punctuation mark to the URL // Test appending punctuation mark to the URL
array( [
'http://testhost/viewtopic.php?t=1!', 'http://testhost/viewtopic.php?t=1!',
'<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!' '<!-- l --><a class="postlink-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!'
), ],
array( [
'www.phpbb.com/community/?', 'www.phpbb.com/community/?',
'<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->?' '<!-- w --><a class="postlink" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->?'
), ],
// Test shortened text for URL > 55 characters long // Test shortened text for URL > 55 characters long
// URL text should be turned into: first 39 chars + ' ... ' + last 10 chars // URL text should be turned into: first 39 chars + ' ... ' + last 10 chars
array( [
'http://www.phpbb.com/community/path/to/long/url/file.ext#section', 'http://www.phpbb.com/community/path/to/long/url/file.ext#section',
'<!-- m --><a class="postlink" href="http://www.phpbb.com/community/path/to/long/url/file.ext#section">http://www.phpbb.com/community/path/to/ ... xt#section</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.phpbb.com/community/path/to/long/url/file.ext#section">http://www.phpbb.com/community/path/to/ ... xt#section</a><!-- m -->'
), ],
); ];
} }
public function data_test_make_clickable_url_idn() public function data_test_make_clickable_url_idn()
{ {
return array( return [
array( [
'http://www.täst.de/community/', 'http://www.täst.de/community/',
'<!-- m --><a class="postlink" href="http://www.täst.de/community/">http://www.täst.de/community/</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.täst.de/community/">http://www.täst.de/community/</a><!-- m -->'
), ],
array( [
'http://www.täst.de/path/file.ext#section', 'http://www.täst.de/path/file.ext#section',
'<!-- m --><a class="postlink" href="http://www.täst.de/path/file.ext#section">http://www.täst.de/path/file.ext#section</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.täst.de/path/file.ext#section">http://www.täst.de/path/file.ext#section</a><!-- m -->'
), ],
array( [
'ftp://ftp.täst.de/', 'ftp://ftp.täst.de/',
'<!-- m --><a class="postlink" href="ftp://ftp.täst.de/">ftp://ftp.täst.de/</a><!-- m -->' '<!-- m --><a class="postlink" href="ftp://ftp.täst.de/">ftp://ftp.täst.de/</a><!-- m -->'
), ],
array( [
'javascript://täst.de/', 'javascript://täst.de/',
'javascript://täst.de/' 'javascript://täst.de/'
), ],
array( [
'sip://bantu@täst.de', 'sip://bantu@täst.de',
'<!-- m --><a class="postlink" href="sip://bantu@täst.de">sip://bantu@täst.de</a><!-- m -->' '<!-- m --><a class="postlink" href="sip://bantu@täst.de">sip://bantu@täst.de</a><!-- m -->'
), ],
array( [
'www.täst.de/community/', 'www.täst.de/community/',
'<!-- w --><a class="postlink" href="http://www.täst.de/community/">www.täst.de/community/</a><!-- w -->' '<!-- w --><a class="postlink" href="http://www.täst.de/community/">www.täst.de/community/</a><!-- w -->'
), ],
// Test appending punctuation mark to the URL // Test appending punctuation mark to the URL
array( [
'http://домен.рф/viewtopic.php?t=1!', 'http://домен.рф/viewtopic.php?t=1!',
'<!-- m --><a class="postlink" href="http://домен.рф/viewtopic.php?t=1">http://домен.рф/viewtopic.php?t=1</a><!-- m -->!' '<!-- m --><a class="postlink" href="http://домен.рф/viewtopic.php?t=1">http://домен.рф/viewtopic.php?t=1</a><!-- m -->!'
), ],
array( [
'www.домен.рф/сообщество/?', 'www.домен.рф/сообщество/?',
'<!-- w --><a class="postlink" href="http://www.домен.рф/сообщество/">www.домен.рф/сообщество/</a><!-- w -->?' '<!-- w --><a class="postlink" href="http://www.домен.рф/сообщество/">www.домен.рф/сообщество/</a><!-- w -->?'
), ],
// Test shortened text for URL > 55 characters long // Test shortened text for URL > 55 characters long
// URL text should be turned into: first 39 chars + ' ... ' + last 10 chars // URL text should be turned into: first 39 chars + ' ... ' + last 10 chars
array( [
'http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section', 'http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section',
'<!-- m --><a class="postlink" href="http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section">http://www.домен.рф/сообщество/путь/по/ ... xt#section</a><!-- m -->' '<!-- m --><a class="postlink" href="http://www.домен.рф/сообщество/путь/по/длинной/ссылке/file.ext#section">http://www.домен.рф/сообщество/путь/по/ ... xt#section</a><!-- m -->'
), ],
// IDN with invalid characters shouldn't be parsed correctly (only 'valid' part) // IDN with invalid characters shouldn't be parsed correctly (only 'valid' part)
array( [
'http://www.täst╫.de', 'http://www.täst╫.de',
'<!-- m --><a class="postlink" href="http://www.täst">http://www.täst</a><!-- m -->╫.de' '<!-- m --><a class="postlink" href="http://www.täst">http://www.täst</a><!-- m -->╫.de'
), ],
// IDN in emails is unsupported yet // IDN in emails is unsupported yet
array('почта@домен.рф', 'почта@домен.рф'), ['почта@домен.рф', 'почта@домен.рф'],
); ];
} }
public function data_test_make_clickable_local_url_idn() public function data_test_make_clickable_local_url_idn()
{ {
return array( return [
array( [
'http://www.домен.рф/viewtopic.php?t=1', 'http://www.домен.рф/viewtopic.php?t=1',
'<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->' '<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
), ],
// Test appending punctuation mark to the URL // Test appending punctuation mark to the URL
array( [
'http://www.домен.рф/viewtopic.php?t=1!', 'http://www.домен.рф/viewtopic.php?t=1!',
'<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!' '<!-- l --><a class="postlink-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->!'
), ],
array( [
'http://www.домен.рф/сообщество/?', 'http://www.домен.рф/сообщество/?',
'<!-- l --><a class="postlink-local" href="http://www.домен.рф/сообщество/">сообщество/</a><!-- l -->?' '<!-- l --><a class="postlink-local" href="http://www.домен.рф/сообщество/">сообщество/</a><!-- l -->?'
), ],
); ];
}
public function data_test_make_clickable_custom_classes()
{
return [
[
'http://www.домен.рф/viewtopic.php?t=1',
'http://www.домен.рф',
'class1',
'<!-- l --><a class="class1-local" href="http://www.домен.рф/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
],
[
'http://www.домен.рф/viewtopic.php?t=1!',
false,
'class2',
'<!-- m --><a class="class2" href="http://www.домен.рф/viewtopic.php?t=1">http://www.домен.рф/viewtopic.php?t=1</a><!-- m -->!'
],
[
'http://www.домен.рф/сообщество/?',
false,
'class3',
'<!-- m --><a class="class3" href="http://www.домен.рф/сообщество/">http://www.домен.рф/сообщество/</a><!-- m -->?'
],
[
'www.phpbb.com/community/',
false,
'class2',
'<!-- w --><a class="class2" href="http://www.phpbb.com/community/">www.phpbb.com/community/</a><!-- w -->'
],
[
'http://testhost/viewtopic.php?t=1',
false,
'class1',
'<!-- l --><a class="class1-local" href="http://testhost/viewtopic.php?t=1">viewtopic.php?t=1</a><!-- l -->'
],
[
'email@domain.com',
false,
'class-email',
'<!-- e --><a href="mailto:email@domain.com">email@domain.com</a><!-- e -->'
],
];
} }
protected function setUp(): void protected function setUp(): void
@ -166,16 +208,9 @@ class phpbb_functions_make_clickable_test extends phpbb_test_case
/** /**
* @dataProvider data_test_make_clickable_url_positive * @dataProvider data_test_make_clickable_url_positive
*/
public function test_urls_matching_positive($url, $expected)
{
$this->assertSame($expected, make_clickable($url));
}
/**
* @dataProvider data_test_make_clickable_url_idn * @dataProvider data_test_make_clickable_url_idn
*/ */
public function test_urls_matching_idn($url, $expected) public function test_urls_matching_positive($url, $expected)
{ {
$this->assertSame($expected, make_clickable($url)); $this->assertSame($expected, make_clickable($url));
} }
@ -187,4 +222,12 @@ class phpbb_functions_make_clickable_test extends phpbb_test_case
{ {
$this->assertSame($expected, make_clickable($url, "http://www.домен.рф")); $this->assertSame($expected, make_clickable($url, "http://www.домен.рф"));
} }
/**
* @dataProvider data_test_make_clickable_custom_classes
*/
public function test_make_clickable_custom_classes($url, $server_url, $class, $expected)
{
$this->assertSame($expected, make_clickable($url, $server_url, $class));
}
} }