Merge pull request #10697 from MrPetovan/bug/10692-api-expand-entities
Prevent expandTags to be performed on existing links in Module\Api\Mastodon\Statuses
This commit is contained in:
commit
5246b9c4b0
3 changed files with 35 additions and 3 deletions
|
@ -2294,14 +2294,14 @@ class BBCode
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expand tags to URLs
|
* Expand tags to URLs, checks the tag is at the start of a line or preceded by a non-word character
|
||||||
*
|
*
|
||||||
* @param string $body
|
* @param string $body
|
||||||
* @return string body with expanded tags
|
* @return string body with expanded tags
|
||||||
*/
|
*/
|
||||||
public static function expandTags(string $body)
|
public static function expandTags(string $body)
|
||||||
{
|
{
|
||||||
return preg_replace_callback("/([!#@])([^\^ \x0D\x0A,;:?\']*[^\^ \x0D\x0A,;:?!\'.])/",
|
return preg_replace_callback("/(?<=\W|^)([!#@])([^\^ \x0D\x0A,;:?'\"]*[^\^ \x0D\x0A,;:?!'\".])/",
|
||||||
function ($match) {
|
function ($match) {
|
||||||
switch ($match[1]) {
|
switch ($match[1]) {
|
||||||
case '!':
|
case '!':
|
||||||
|
@ -2314,6 +2314,7 @@ class BBCode
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
|
default:
|
||||||
return $match[1] . '[url=' . 'https://' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
|
return $match[1] . '[url=' . 'https://' . DI::baseUrl() . '/search?tag=' . $match[2] . ']' . $match[2] . '[/url]';
|
||||||
}
|
}
|
||||||
}, $body);
|
}, $body);
|
||||||
|
|
|
@ -63,7 +63,10 @@ class Statuses extends BaseApi
|
||||||
// The imput is defined as text. So we can use Markdown for some enhancements
|
// The imput is defined as text. So we can use Markdown for some enhancements
|
||||||
$body = Markdown::toBBCode($request['status']);
|
$body = Markdown::toBBCode($request['status']);
|
||||||
|
|
||||||
$body = BBCode::expandTags($body);
|
// Avoids potential double expansion of existing links
|
||||||
|
$body = BBCode::performWithEscapedTags($body, ['url'], function ($body) {
|
||||||
|
return BBCode::expandTags($body);
|
||||||
|
});
|
||||||
|
|
||||||
$item = [];
|
$item = [];
|
||||||
$item['uid'] = $uid;
|
$item['uid'] = $uid;
|
||||||
|
|
|
@ -75,6 +75,7 @@ class BBCodeTest extends MockedTest
|
||||||
->andReturn($baseUrlMock);
|
->andReturn($baseUrlMock);
|
||||||
$baseUrlMock->shouldReceive('getHostname')->withNoArgs()->andReturn('friendica.local');
|
$baseUrlMock->shouldReceive('getHostname')->withNoArgs()->andReturn('friendica.local');
|
||||||
$baseUrlMock->shouldReceive('getUrlPath')->withNoArgs()->andReturn('');
|
$baseUrlMock->shouldReceive('getUrlPath')->withNoArgs()->andReturn('');
|
||||||
|
$baseUrlMock->shouldReceive('__toString')->withNoArgs()->andReturn('friendica.local');
|
||||||
|
|
||||||
$config = \HTMLPurifier_HTML5Config::createDefault();
|
$config = \HTMLPurifier_HTML5Config::createDefault();
|
||||||
$config->set('HTML.Doctype', 'HTML5');
|
$config->set('HTML.Doctype', 'HTML5');
|
||||||
|
@ -339,4 +340,31 @@ class BBCodeTest extends MockedTest
|
||||||
|
|
||||||
self::assertEquals($expected, $actual);
|
self::assertEquals($expected, $actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function dataExpandTags()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'bug-10692-non-word' => [
|
||||||
|
'[url=https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160]https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160[/url]',
|
||||||
|
'[url=https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160]https://github.com/friendica/friendica/blob/2021.09-rc/src/Util/Logger/StreamLogger.php#L160[/url]',
|
||||||
|
],
|
||||||
|
'bug-10692-start-line' => [
|
||||||
|
'#[url=https://friendica.local/search?tag=L160]L160[/url]',
|
||||||
|
'#L160',
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider dataExpandTags
|
||||||
|
*
|
||||||
|
* @param string $expected Expected BBCode output
|
||||||
|
* @param string $text Input text
|
||||||
|
*/
|
||||||
|
public function testExpandTags(string $expected, string $text)
|
||||||
|
{
|
||||||
|
$actual = BBCode::expandTags($text);
|
||||||
|
|
||||||
|
self::assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue