Skip to content

Commit 43845f0

Browse files
committed
Fix trailing dot. resolve #10
1 parent 8500ff3 commit 43845f0

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/Autolink.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,23 +99,32 @@ public function convert(string $text, array $attribs = []): string
9999
return preg_replace_callback(
100100
$regex,
101101
function ($matches) use ($attribs, $linkNoScheme) {
102-
preg_match('/[a-zA-Z]*\=\"(.*)/', $matches[0], $inElements);
102+
$url = $matches[0];
103+
104+
preg_match('/[a-zA-Z]*\=\"(.*)/', $url, $inElements);
103105

104106
if ($inElements) {
105-
return $matches[0];
107+
return $url;
106108
}
107109

108110
if (
109111
$linkNoScheme
110112
&& (
111-
str_starts_with($matches[0], '://')
112-
|| str_starts_with($matches[0], '@')
113+
str_starts_with($url, '://')
114+
|| str_starts_with($url, '@')
113115
)
114116
) {
115-
return $matches[0];
117+
return $url;
118+
}
119+
120+
$suffix = '';
121+
122+
if (str_ends_with($url, '.')) {
123+
$suffix = '.';
124+
$url = substr($url, 0, -1);
116125
}
117126

118-
return $this->link($matches[0], $attribs);
127+
return $this->link($url, $attribs) . $suffix;
119128
},
120129
$text
121130
);

test/AutolinkTest.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PHPUnit\Framework\Attributes\DataProvider;
77
use PHPUnit\Framework\TestCase;
88
use Windwalker\Test\Traits\BaseAssertionTrait;
9+
use function PHPUnit\Framework\assertEquals;
910

1011
/**
1112
* The AutolinkTest class.
@@ -157,7 +158,7 @@ public function testTextLimit()
157158
);
158159

159160
$this->instance->textLimit(function ($url) {
160-
return \Asika\Autolink\Autolink::shortenUrl($url);
161+
return Autolink::shortenUrl($url);
161162
});
162163

163164
self::assertEquals(
@@ -385,6 +386,27 @@ public function testGetAndSetLinkBuilder()
385386
self::assertInstanceOf('Closure', $this->instance->getLinkBuilder());
386387
}
387388

389+
public function testIgnoreTrailingDot(): void
390+
{
391+
$txt = 'Link to https://google.com.';
392+
393+
$html = $this->instance->convert($txt);
394+
395+
assertEquals(
396+
'Link to <a href="https://google.com">https://google.com</a>.',
397+
$html,
398+
);
399+
400+
$txt = 'Link to https://google.com/search?foo=yoo.';
401+
402+
$html = $this->instance->convert($txt);
403+
404+
assertEquals(
405+
'Link to <a href="https://google.com/search?foo=yoo">https://google.com/search?foo=yoo</a>.',
406+
$html,
407+
);
408+
}
409+
388410
/**
389411
* urlProvider
390412
*
@@ -431,6 +453,6 @@ public static function urlProvider()
431453
#[DataProvider('urlProvider')]
432454
public function testShortenUrl($url, $expect, $limit, $dots)
433455
{
434-
self::assertEquals($expect, \Asika\Autolink\Autolink::shortenUrl($url, $limit, $dots));
456+
self::assertEquals($expect, Autolink::shortenUrl($url, $limit, $dots));
435457
}
436458
}

0 commit comments

Comments
 (0)