Skip to content

Commit

Permalink
fix: add support links without scheme (//)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Aug 17, 2023
1 parent 5fd5f91 commit 9a0ea44
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ protected function parseUrl(string $url): string
$out = '';
if (isset($parse['scheme'])) {
$out .= $parse['scheme'].'://';
} elseif (0 === \strpos($url, '//')) {
$out .= '//';
}

if (isset($parse['user'], $parse['pass'])) {
$out .= \rawurlencode($parse['user']).':'.\rawurlencode($parse['pass']).'@';
} elseif (isset($parse['user'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Xbbcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ class Xbbcode
* @param string $webPath Web path
* @param array|null $allowed Allowed tags
*/
public function __construct(string $webPath = '', ?array $allowed = null)
public function __construct(string $webPath = '', array $allowed = null)
{
$this->webPath = $webPath;
$this->reloadSmiles();
Expand Down
31 changes: 30 additions & 1 deletion tests/Tag/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class ATest extends \PHPUnit\Framework\TestCase
{
public function testTag()
public function testTag(): void
{
$text = 'test [url=https://github.com/Gemorroj/xBBCode]xBBCode - привет мир[/url].';
$result = 'test <a class="bb" href="https://github.com/Gemorroj/xBBCode">xBBCode - привет мир</a>.';
Expand Down Expand Up @@ -34,4 +34,33 @@ public function testTag()
$xbbcode->parse($text);
self::assertEquals($result, $xbbcode->getHtml());
}

public function testTagWithoutSchema(): void
{
$text = 'test [url=//github.com/Gemorroj/xBBCode]xBBCode - привет мир[/url].';
$result = 'test <a class="bb" href="//github.com/Gemorroj/xBBCode">xBBCode - привет мир</a>.';

$xbbcode = new Xbbcode();
$xbbcode->setAutoLinks(true);
$xbbcode->parse($text);
self::assertEquals($result, $xbbcode->getHtml());

$xbbcode = new Xbbcode();
$xbbcode->setAutoLinks(false);
$xbbcode->parse($text);
self::assertEquals($result, $xbbcode->getHtml());

$text = 'test [url]//github.com/Gemorroj/xBBCode#привет[/url].';
$result = 'test <a class="bb" href="//github.com/Gemorroj/xBBCode#%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82">//github.com/Gemorroj/xBBCode#привет</a>.';

$xbbcode = new Xbbcode();
$xbbcode->setAutoLinks(true);
$xbbcode->parse($text);
self::assertEquals($result, $xbbcode->getHtml());

$xbbcode = new Xbbcode();
$xbbcode->setAutoLinks(false);
$xbbcode->parse($text);
self::assertEquals($result, $xbbcode->getHtml());
}
}

0 comments on commit 9a0ea44

Please sign in to comment.