From 8de66c02742ae3a953cdb0e1fcbd32726d9e081b Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 17 Jul 2020 17:16:22 -0400 Subject: [PATCH] Add shortened URL link label stripping to PageInfo::stripTrailingUrlFromBody - Add test cases for shortened URL link labels --- src/Content/PageInfo.php | 23 ++++++++++++++++++----- tests/src/Content/PageInfoTest.php | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Content/PageInfo.php b/src/Content/PageInfo.php index 642c57938..86663a833 100644 --- a/src/Content/PageInfo.php +++ b/src/Content/PageInfo.php @@ -252,22 +252,35 @@ class PageInfo /** * Remove the provided URL from the body if it is at the end of it. - * Keep the link label if it isn't the full URL. + * Keep the link label if it isn't the full URL or a shortened version of it. * * @param string $body * @param string $url - * @return string|string[]|null + * @return string */ protected static function stripTrailingUrlFromBody(string $body, string $url) { $quotedUrl = preg_quote($url, '#'); - $body = preg_replace("#(?: + $body = preg_replace_callback("#(?: \[url]$quotedUrl\[/url]| \[url=$quotedUrl]$quotedUrl\[/url]| \[url=$quotedUrl]([^[]*?)\[/url]| $quotedUrl - )$#isx", '$1', $body); + )$#isx", function ($match) use ($url) { + // Stripping URLs with no label + if (!isset($match[1])) { + return ''; + } - return $body; + // Stripping link labels that include a shortened version of the URL + if (strpos($url, trim($match[1], '.…')) !== false) { + return ''; + } + + // Keep all other labels + return $match[1]; + }, $body); + + return rtrim($body); } } diff --git a/tests/src/Content/PageInfoTest.php b/tests/src/Content/PageInfoTest.php index 6f9641564..3604348ff 100644 --- a/tests/src/Content/PageInfoTest.php +++ b/tests/src/Content/PageInfoTest.php @@ -108,6 +108,21 @@ class PageInfoTest extends MockedTest 'body' => '[url=https://example.com]link label[/url]', 'url' => 'https://example.com', ], + 'task-8797-shortened-link-label' => [ + 'expected' => 'content', + 'body' => 'content [url=https://example.com/page]example.com/[/url]', + 'url' => 'https://example.com/page', + ], + 'task-8797-shortened-link-label-ellipsis' => [ + 'expected' => 'content', + 'body' => 'content [url=https://example.com/page]example.com…[/url]', + 'url' => 'https://example.com/page', + ], + 'task-8797-shortened-link-label-dots' => [ + 'expected' => 'content', + 'body' => 'content [url=https://example.com/page]example.com...[/url]', + 'url' => 'https://example.com/page', + ], ]; }