Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion inline/LinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ protected function renderEmail($block)
protected function renderUrl($block)
{
$url = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($block[1]), ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
$decodedUrl = urldecode($block[1]);
$secureUrlText = preg_match('//u', $decodedUrl) ? $decodedUrl : $block[1];
$text = htmlspecialchars($secureUrlText, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
return "<a href=\"$url\">$text</a>";
}

Expand Down
4 changes: 3 additions & 1 deletion inline/UrlLinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ protected function parseUrl($markdown)
protected function renderAutoUrl($block)
{
$href = htmlspecialchars($block[1], ENT_COMPAT | ENT_HTML401, 'UTF-8');
$text = htmlspecialchars(urldecode($block[1]), ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
$decodedUrl = urldecode($block[1]);
$secureUrlText = preg_match('//u', $decodedUrl) ? $decodedUrl : $block[1];
$text = htmlspecialchars($secureUrlText, ENT_NOQUOTES | ENT_SUBSTITUTE, 'UTF-8');
return "<a href=\"$href\">$text</a>";
}
}
15 changes: 15 additions & 0 deletions tests/GithubMarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,19 @@ public function testKeepZeroAlive()
$this->assertEquals("0", $parser->parseParagraph("0"));
$this->assertEquals("<p>0</p>\n", $parser->parse("0"));
}

public function testAutoLinkLabelingWithEncodedUrl()
{
$parser = $this->createMarkdown();

$utfText = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a";
$utfNaturalUrl = "http://example.com/" . $utfText;
$utfEncodedUrl = "http://example.com/" . urlencode($utfText);
$eucEncodedUrl = "http://example.com/" . urlencode(mb_convert_encoding($utfText, 'EUC-JP', 'UTF-8'));

$this->assertStringEndsWith(">{$utfNaturalUrl}</a>", $parser->parseParagraph($utfNaturalUrl), "Natural UTF-8 URL needs no conversion.");
$this->assertStringEndsWith(">{$utfNaturalUrl}</a>", $parser->parseParagraph($utfEncodedUrl), "Encoded UTF-8 URL will be converted to readable format.");
$this->assertStringEndsWith(">{$eucEncodedUrl}</a>", $parser->parseParagraph($eucEncodedUrl), "Non UTF-8 URL should never be converted.");
// See: \cebe\markdown\inline\UrlLinkTrait::renderAutoUrl
}
}
15 changes: 15 additions & 0 deletions tests/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ public function testKeepZeroAlive()
$this->assertEquals("0", $parser->parseParagraph("0"));
$this->assertEquals("<p>0</p>\n", $parser->parse("0"));
}

public function testAutoLinkLabelingWithEncodedUrl()
{
$parser = $this->createMarkdown();

$utfText = "\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a";
$utfNaturalUrl = "http://example.com/" . $utfText;
$utfEncodedUrl = "http://example.com/" . urlencode($utfText);
$eucEncodedUrl = "http://example.com/" . urlencode(mb_convert_encoding($utfText, 'EUC-JP', 'UTF-8'));

$this->assertStringEndsWith(">{$utfNaturalUrl}</a>", $parser->parseParagraph("<{$utfNaturalUrl}>"), "Natural UTF-8 URL needs no conversion.");
$this->assertStringEndsWith(">{$utfNaturalUrl}</a>", $parser->parseParagraph("<{$utfEncodedUrl}>"), "Encoded UTF-8 URL will be converted to readable format.");
$this->assertStringEndsWith(">{$eucEncodedUrl}</a>", $parser->parseParagraph("<{$eucEncodedUrl}>"), "Non UTF-8 URL should never be converted.");
// See: \cebe\markdown\inline\LinkTrait::renderUrl
}
}