Merge branch 'develop-ascraeus' into develop

# By Joas Schilling
# Via Dhruv Goel (1) and Joas Schilling (1)
* develop-ascraeus:
  [ticket/12705] Store the regular expression matches based on server_url
  [ticket/12705] Break calling make_clickable with a different server_url
This commit is contained in:
Dhruv 2014-06-17 14:58:17 +05:30
commit 05d63e35ef
2 changed files with 55 additions and 7 deletions

View file

@ -773,44 +773,47 @@ function make_clickable($text, $server_url = false, $class = 'postlink')
static $static_class; static $static_class;
static $magic_url_match_args; static $magic_url_match_args;
if (!is_array($magic_url_match_args) || $static_class != $class) if (!isset($magic_url_match_args[$server_url]) || $static_class != $class)
{ {
$static_class = $class; $static_class = $class;
$class = ($static_class) ? ' class="' . $static_class . '"' : ''; $class = ($static_class) ? ' class="' . $static_class . '"' : '';
$local_class = ($static_class) ? ' class="' . $static_class . '-local"' : ''; $local_class = ($static_class) ? ' class="' . $static_class . '-local"' : '';
$magic_url_match_args = array(); if (!is_array($magic_url_match_args))
{
$magic_url_match_args = array();
}
// relative urls for this board // relative urls for this board
$magic_url_match_args[] = array( $magic_url_match_args[$server_url][] = array(
'#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#i', '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#i',
MAGIC_URL_LOCAL, MAGIC_URL_LOCAL,
$local_class, $local_class,
); );
// matches a xxxx://aaaaa.bbb.cccc. ... // matches a xxxx://aaaaa.bbb.cccc. ...
$magic_url_match_args[] = array( $magic_url_match_args[$server_url][] = array(
'#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#i', '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#i',
MAGIC_URL_FULL, MAGIC_URL_FULL,
$class, $class,
); );
// matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing // matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
$magic_url_match_args[] = array( $magic_url_match_args[$server_url][] = array(
'#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#i', '#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#i',
MAGIC_URL_WWW, MAGIC_URL_WWW,
$class, $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[] = array( $magic_url_match_args[$server_url][] = array(
'/(^|[\n\t (>])(' . get_preg_expression('email') . ')/i', '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/i',
MAGIC_URL_EMAIL, MAGIC_URL_EMAIL,
'', '',
); );
} }
foreach ($magic_url_match_args as $magic_args) foreach ($magic_url_match_args[$server_url] as $magic_args)
{ {
if (preg_match($magic_args[0], $text, $matches)) if (preg_match($magic_args[0], $text, $matches))
{ {

View file

@ -104,5 +104,50 @@ class phpbb_text_processing_make_clickable_test extends phpbb_test_case
$this->assertEquals($expected, $result, $label); $this->assertEquals($expected, $result, $label);
} }
public function make_clickable_mixed_serverurl_data()
{
$urls = array(
'http://thisdomain.org' => array('tag' => 'm', 'url' => false, 'text' => false),
'http://thisdomain.org/' => array('tag' => 'm', 'url' => false, 'text' => false),
'http://thisdomain.org/1' => array('tag' => 'm', 'url' => false, 'text' => false),
'http://thisdomain.org/path/some?query=abc#test' => array('tag' => 'm', 'url' => false, 'text' => false),
'https://www.phpbb.com' => array('tag' => 'm', 'url' => false, 'text' => false),
'https://www.phpbb.com/' => array('tag' => 'm', 'url' => false, 'text' => false),
'https://www.phpbb.com/1' => array('tag' => 'l', 'url' => false, 'text' => '1'),
'https://www.phpbb.com/path/some?query=abc#test' => array('tag' => 'l', 'url' => false, 'text' => 'path/some?query=abc#test'),
);
$test_data = array();
// run the test for each combination
foreach ($urls as $url => $url_type)
{
// false means it's the same as the url, less typing
$url_type['url'] = ($url_type['url']) ? $url_type['url'] : $url;
$url_type['text'] = ($url_type['text']) ? $url_type['text'] : $url;
$class = ($url_type['tag'] === 'l') ? 'postlink-local' : 'postlink';
// replace the url with the desired output format
$output = '<!-- ' . $url_type['tag'] . ' --><a class="' . $class . '" href="' . $url_type['url'] . '">' . $url_type['text'] . '</a><!-- ' . $url_type['tag'] . ' -->';
$test_data[] = array($url, $output);
}
return $test_data;
}
/**
* @dataProvider make_clickable_mixed_serverurl_data
*/
public function test_make_clickable_mixed_serverurl($input, $expected)
{
$result = make_clickable($input, 'https://www.phpbb.com');
$label = 'Making text clickable: ' . $input;
$this->assertEquals($expected, $result, $label);
}
} }