Skip to content

Commit

Permalink
Issue friendica#14079: Shorten the displayed URL
Browse files Browse the repository at this point in the history
  • Loading branch information
annando committed Apr 7, 2024
1 parent ad65e56 commit f41cb9d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 19 deletions.
24 changes: 23 additions & 1 deletion src/Content/Text/BBCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
class BBCode
{
// Update this value to the current date whenever changes are made to BBCode::convert
const VERSION = '2021-07-28';
const VERSION = '2024-04-07';

const INTERNAL = 0;
const EXTERNAL = 1;
Expand Down Expand Up @@ -1988,6 +1988,12 @@ function ($match) {
return $text;
});

$text = preg_replace("/\[url=[^\[\]]*\](.*)\[\/url\]/Usi", '[url=$1]$1[/url]', $text);

if (!$for_plaintext && in_array($simple_html, [self::INTERNAL, self::EXTERNAL, self::DIASPORA])) {
$text = self::shortenLinkDescription($text);
}

// We need no target="_blank" rel="noopener noreferrer" for local links
// convert links start with DI::baseUrl() as local link without the target="_blank" rel="noopener noreferrer" attribute
$escapedBaseUrl = preg_quote(DI::baseUrl(), '/');
Expand Down Expand Up @@ -2112,6 +2118,22 @@ function ($matches) {
return trim($text);
}

private static function shortenLinkDescription(string $text): string
{
$text = preg_replace_callback(
"/\[url\=(.*?)\](.*?)\[\/url\]/ism",
function ($match) {
if ($match[1] == $match[2]) {
return "[url=" . $match[1] . "]" . Strings::getStyledURL($match[1]) . "[/url]";
} else {
return $match[0];
}
},
$text
);
return $text;
}

/**
* Strips the "abstract" tag from the provided text
*
Expand Down
4 changes: 4 additions & 0 deletions src/Util/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ public static function getBytesFromShorthand(string $shorthand): int
public static function getStyledURL(string $url): string
{
$parts = parse_url($url);
if (empty($parts['scheme'])) {
return $url;
}

$scheme = [$parts['scheme'] . '://www.', $parts['scheme'] . '://'];
$styled_url = str_replace($scheme, '', $url);

Expand Down
55 changes: 37 additions & 18 deletions tests/src/Content/Text/BBCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Friendica\DI;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Test\FixtureTest;
use Friendica\Util\Strings;

class BBCodeTest extends FixtureTest
{
Expand Down Expand Up @@ -61,76 +62,94 @@ public function dataLinks()
'bug-2487-1' => [
'data' => 'https://de.wikipedia.org/wiki/Juha_Sipilä',
'assertHTML' => true,
'styled' => true,
],
'bug-2487-2' => [
'data' => 'https://de.wikipedia.org/wiki/Dnepr_(Motorradmarke)',
'assertHTML' => true,
'styled' => true,
],
'bug-2487-3' => [
'data' => 'https://friendica.wäckerlin.ch/friendica',
'assertHTML' => true,
'styled' => true,
],
'bug-2487-4' => [
'data' => 'https://mastodon.social/@morevnaproject',
'assertHTML' => true,
'styled' => true,
],
/** @see https://github.com/friendica/friendica/issues/5795 */
'bug-5795' => [
'data' => 'https://social.nasqueron.org/@liw/100798039015010628',
'assertHTML' => true,
'styled' => true,
],
/** @see https://github.com/friendica/friendica/issues/6095 */
'bug-6095' => [
'data' => 'https://en.wikipedia.org/wiki/Solid_(web_decentralization_project)',
'assertHTML' => true,
'styled' => true,
],
'no-protocol' => [
'data' => 'example.com/path',
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'wrong-protocol' => [
'data' => 'ftp://example.com',
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'wrong-domain-without-path' => [
'data' => 'http://example',
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'wrong-domain-with-path' => [
'data' => 'http://example/path',
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-6857-domain-start' => [
'data' => "http://\nexample.com",
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-6857-domain-end' => [
'data' => "http://example\n.com",
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-6857-tld' => [
'data' => "http://example.\ncom",
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-6857-end' => [
'data' => "http://example.com\ntest",
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-6901' => [
'data' => "http://example.com<ul>",
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-7150' => [
'data' => html_entity_decode('http://example.com&nbsp;', ENT_QUOTES, 'UTF-8'),
'assertHTML' => false
'assertHTML' => false,
'styled' => true,
],
'bug-7271-query-string-brackets' => [
'data' => 'https://example.com/search?q=square+brackets+[url]',
'assertHTML' => true
'assertHTML' => true,
'styled' => false,
],
'bug-7271-path-brackets' => [
'data' => 'http://example.com/path/to/file[3].html',
'assertHTML' => true
'assertHTML' => true,
'styled' => false,
],
];
}
Expand All @@ -145,10 +164,10 @@ public function dataLinks()
*
* @throws InternalServerErrorException
*/
public function testAutoLinking(string $data, bool $assertHTML)
public function testAutoLinking(string $data, bool $assertHTML, bool $styled)
{
$output = BBCode::convert($data);
$assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . $data . '</a>');
$assert = $this->HTMLPurifier->purify('<a href="' . $data . '" target="_blank" rel="noopener noreferrer">' . ($styled ? Strings::getStyledURL($data) : $data) . '</a>');
if ($assertHTML) {
self::assertEquals($assert, $output);
} else {
Expand All @@ -160,21 +179,21 @@ public function dataBBCodes()
{
return [
'bug-7271-condensed-space' => [
'expectedHtml' => '<ol><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ol>',
'expectedHtml' => '<ol><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ol>',
'text' => '[ol][li] http://example.com/[/ol]',
],
'bug-7271-condensed-nospace' => [
'expectedHtml' => '<ol><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ol>',
'expectedHtml' => '<ol><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ol>',
'text' => '[ol][li]http://example.com/[/ol]',
],
'bug-7271-indented-space' => [
'expectedHtml' => '<ul><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
'expectedHtml' => '<ul><li> <a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ul>',
'text' => '[ul]
[li] http://example.com/
[/ul]',
],
'bug-7271-indented-nospace' => [
'expectedHtml' => '<ul><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">http://example.com/</a></li></ul>',
'expectedHtml' => '<ul><li><a href="http://example.com/" target="_blank" rel="noopener noreferrer">example.com/</a></li></ul>',
'text' => '[ul]
[li]http://example.com/
[/ul]',
Expand Down

0 comments on commit f41cb9d

Please sign in to comment.