Skip to content

Commit

Permalink
Fix trailing dot. resolve #10
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Jan 27, 2025
1 parent 8500ff3 commit 43845f0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
21 changes: 15 additions & 6 deletions src/Autolink.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,32 @@ public function convert(string $text, array $attribs = []): string
return preg_replace_callback(
$regex,
function ($matches) use ($attribs, $linkNoScheme) {
preg_match('/[a-zA-Z]*\=\"(.*)/', $matches[0], $inElements);
$url = $matches[0];

preg_match('/[a-zA-Z]*\=\"(.*)/', $url, $inElements);

if ($inElements) {
return $matches[0];
return $url;
}

if (
$linkNoScheme
&& (
str_starts_with($matches[0], '://')
|| str_starts_with($matches[0], '@')
str_starts_with($url, '://')
|| str_starts_with($url, '@')
)
) {
return $matches[0];
return $url;
}

$suffix = '';

if (str_ends_with($url, '.')) {
$suffix = '.';
$url = substr($url, 0, -1);
}

return $this->link($matches[0], $attribs);
return $this->link($url, $attribs) . $suffix;
},
$text
);
Expand Down
26 changes: 24 additions & 2 deletions test/AutolinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Windwalker\Test\Traits\BaseAssertionTrait;
use function PHPUnit\Framework\assertEquals;

/**
* The AutolinkTest class.
Expand Down Expand Up @@ -157,7 +158,7 @@ public function testTextLimit()
);

$this->instance->textLimit(function ($url) {
return \Asika\Autolink\Autolink::shortenUrl($url);
return Autolink::shortenUrl($url);
});

self::assertEquals(
Expand Down Expand Up @@ -385,6 +386,27 @@ public function testGetAndSetLinkBuilder()
self::assertInstanceOf('Closure', $this->instance->getLinkBuilder());
}

public function testIgnoreTrailingDot(): void
{
$txt = 'Link to https://google.com.';

$html = $this->instance->convert($txt);

assertEquals(
'Link to <a href="https://google.com">https://google.com</a>.',
$html,
);

$txt = 'Link to https://google.com/search?foo=yoo.';

$html = $this->instance->convert($txt);

assertEquals(
'Link to <a href="https://google.com/search?foo=yoo">https://google.com/search?foo=yoo</a>.',
$html,
);
}

/**
* urlProvider
*
Expand Down Expand Up @@ -431,6 +453,6 @@ public static function urlProvider()
#[DataProvider('urlProvider')]
public function testShortenUrl($url, $expect, $limit, $dots)
{
self::assertEquals($expect, \Asika\Autolink\Autolink::shortenUrl($url, $limit, $dots));
self::assertEquals($expect, Autolink::shortenUrl($url, $limit, $dots));
}
}

0 comments on commit 43845f0

Please sign in to comment.