Skip to content

Commit 20d5858

Browse files
committed
Supports localhost #6
1 parent 43845f0 commit 20d5858

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/Autolink.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,18 @@ public function __construct(array $options = [], array $schemes = [])
8585
public function convert(string $text, array $attribs = []): string
8686
{
8787
$linkNoScheme = $this->getLinkNoScheme();
88+
$staticDomains = '|localhost';
8889

8990
if ($linkNoScheme) {
9091
$schemeRegex = "[(%s)\:\/\/@]*";
92+
$staticDomains = '';
9193
} else {
9294
$schemeRegex = "(%s)\:\/\/";
9395
}
9496

9597
$schemeRegex = sprintf($schemeRegex, $this->getSchemes(true));
9698

97-
$regex = '/(([a-zA-Z]*=")*' . $schemeRegex . "[\-\p{L}\p{N}\p{M}]+\.[\p{L}\p{M}]{2,}([\/\p{L}\p{N}\p{M}\-._~:?#\[\]@!$&'()*+,;=%\">]*)?)/u";
99+
$regex = '/(([a-zA-Z]*=")*' . $schemeRegex . "([\-\p{L}\p{N}\p{M}]+\.[\p{L}\p{M}]{2,}$staticDomains)([\/\p{L}\p{N}\p{M}\-._~:?#\[\]@!$&'()*+,;=%\">]*)?)/u";
98100

99101
return preg_replace_callback(
100102
$regex,

test/AutolinkTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,38 @@ public function testIgnoreTrailingDot(): void
407407
);
408408
}
409409

410+
public function testLocalhost(): void
411+
{
412+
$txt = 'Link to http://localhost with some text.';
413+
414+
$html = $this->instance->convert($txt);
415+
416+
assertEquals(
417+
'Link to <a href="http://localhost">http://localhost</a> with some text.',
418+
$html,
419+
);
420+
421+
$txt = 'Link to http://localhost.';
422+
423+
$html = $this->instance->convert($txt);
424+
425+
assertEquals(
426+
'Link to <a href="http://localhost">http://localhost</a>.',
427+
$html,
428+
);
429+
430+
// Localhost without scheme should be ignored.
431+
$txt = 'Link to localhost.';
432+
433+
$this->instance->linkNoScheme(true);
434+
$html = $this->instance->convert($txt);
435+
436+
assertEquals(
437+
'Link to localhost.',
438+
$html,
439+
);
440+
}
441+
410442
/**
411443
* urlProvider
412444
*

0 commit comments

Comments
 (0)