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
6 changes: 4 additions & 2 deletions src/Parser/InlineParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1957,8 +1957,10 @@ protected function applyConsecutiveAttributes(Node $node, string $text, int $sta
*/
protected function parseFootnoteRef(string $text, int $pos): ?array
{
// Match [^label] - \G anchors at offset position, avoiding extra strpos check
if (!preg_match('/\G\[\^([^\]]+)\]/', $text, $matches, 0, $pos)) {
// A footnote label cannot cross a physical line. The definition marker
// is one line too, so accepting a newline here creates an identifier
// that no valid definition can bind (jgm/djot#208).
if (!preg_match('/\G\[\^([^\]\r\n]+)\]/', $text, $matches, 0, $pos)) {
return null;
}

Expand Down
71 changes: 71 additions & 0 deletions tests/TestCase/FootnoteLabelIsSingleLineTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Djot\Test\TestCase;

use Djot\DjotConverter;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class FootnoteLabelIsSingleLineTest extends TestCase
{
/**
* @return iterable<string, array{string}>
*/
public static function lineEndings(): iterable
{
yield 'LF' => ["\n"];
yield 'CRLF' => ["\r\n"];
yield 'CR' => ["\r"];
}

#[DataProvider('lineEndings')]
public function testAReferenceLabelDoesNotCrossALineEnding(string $ending): void
{
$source = "before[^two{$ending}words].\n";
$converter = new DjotConverter(warnings: true);
$document = $converter->parse($source);
$types = array_map(
static fn ($node): string => $node->getType(),
$document->getChildren()[0]->getChildren(),
);

self::assertNotContains('footnote_ref', $types);
self::assertContains('soft_break', $types);
self::assertStringNotContainsString('doc-noteref', $converter->convert($source));
self::assertSame([], array_filter(
$converter->getWarnings(),
static fn ($warning): bool => str_contains($warning->getMessage(), 'Undefined footnote'),
));
}

public function testAMultilineDefinitionMarkerDoesNotRegisterOrSwallowText(): void
{
$source = "see[^two words].\n\n[^two\nwords]: note.\n";
$converter = new DjotConverter();

$document = $converter->parse($source);
self::assertNotContains('footnote', array_map(
static fn ($node): string => $node->getType(),
$document->getChildren(),
));
self::assertStringContainsString("[^two\nwords]: note.", $converter->convert($source));
}

/**
* @return iterable<string, array{string}>
*/
public static function sameLineLabels(): iterable
{
yield 'space' => ['two words'];
yield 'tab' => ["two\twords"];
}

#[DataProvider('sameLineLabels')]
public function testSameLineWhitespaceStillResolves(string $label): void
{
$html = (new DjotConverter())->convert("see[^{$label}].\n\n[^{$label}]: note.\n");
self::assertStringContainsString('doc-noteref', $html);
}
}
Loading