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
25 changes: 24 additions & 1 deletion src/Parser/InlineParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ protected function findBalancedBracketEnd(string $text, int $open): ?int
$length = strlen($text);
$at = $open + 1;
while ($at < $length) {
$at += strcspn($text, '[]\\', $at);
$at += strcspn($text, '[]\\`', $at);
if ($at >= $length) {
break;
}
Expand All @@ -1151,6 +1151,14 @@ protected function findBalancedBracketEnd(string $text, int $open): ?int

continue;
}
if ($char === '`') {
$codeEnd = $this->findCodeSpanEnd($text, $at);
if ($codeEnd !== null) {
$at = $codeEnd;

continue;
}
}
if ($char === '[') {
$starts[] = $at;
} elseif ($char === ']') {
Expand All @@ -1159,6 +1167,21 @@ protected function findBalancedBracketEnd(string $text, int $open): ?int
if ($starts === []) {
return $at;
}
if ($at + 1 < $length && $text[$at + 1] === '(') {
$destinationEnd = $this->cachedLinkDestinationEnd($text, $at + 1);
if ($destinationEnd !== null) {
$at = $destinationEnd;

continue;
}
} elseif ($at + 1 < $length && $text[$at + 1] === '{') {
$attributeEnd = $this->findAttributeEnd($text, $at + 1);
if ($attributeEnd !== null && $this->isValidAttrPayload(substr($text, $at + 2, $attributeEnd - $at - 2))) {
$at = $attributeEnd + 1;

continue;
}
}
}
$at++;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/DjotConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2345,6 +2345,26 @@ public function testTripleBackticksInline(): void

// Edge cases: Links and URLs

#[DataProvider('opaqueBracketContentCases')]
public function testOpaqueContentDoesNotCloseOuterBrackets(string $djot, string $expected): void
{
$this->assertSame($expected, $this->converter->convert($djot));
}

public static function opaqueBracketContentCases(): array
{
return [
['a [b [x](http://u/]) c', "<p>a [b <a href=\"http://u/]\">x</a> c</p>\n"],
['a [b [x](http://u/[a]) c', "<p>a [b <a href=\"http://u/[a]\">x</a> c</p>\n"],
['a [b ![x](http://u/]) c', "<p>a [b <img alt=\"x\" src=\"http://u/]\"> c</p>\n"],
['a [b [x](http://u/]) c](v)', "<p>a <a href=\"v\">b <a href=\"http://u/]\">x</a> c</a></p>\n"],
['[a [x](u\\]) c](v)', "<p><a href=\"v\">a <a href=\"u]\">x</a> c</a></p>\n"],
['[a [x]{title="]"} c](v)', "<p><a href=\"v\">a <span title=\"]\">x</span> c</a></p>\n"],
['[a `]` b](v)', "<p><a href=\"v\">a <code>]</code> b</a></p>\n"],
['[a `[x]` b](v)', "<p><a href=\"v\">a <code>[x]</code> b</a></p>\n"],
];
}

public function testLinkWithParenthesesInUrl(): void
{
$djot = '[Wikipedia](https://en.wikipedia.org/wiki/Example_(disambiguation))';
Expand Down
Loading