Skip to content
Open
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: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
.phpunit.result.cache
.phpunit.cache

# PHPUnit
/app/phpunit.xml
/phpunit.xml

# Logs
/logs/
*.log
Expand Down
2 changes: 1 addition & 1 deletion src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ private function quoteLineState(string $content, bool $paragraphOpen, bool $fenc
}
$isText = $this->isParagraphContinuation($content)
|| ($paragraphOpen && $content !== '' && !ctype_space($content) && !preg_match(self::PATTERN_HEADING, $content));
return [$isText && !preg_match(self::PATTERN_TABLE_SEPARATOR, $content), false];
return [$isText, false];
}

private function matchLine(string $line): Token
Expand Down
11 changes: 10 additions & 1 deletion src/MarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function __construct(
}

/**
* @throws ParseException if input exceeds maxBytes or is not valid UTF-8.
* @throws ParseException if input exceeds maxBytes (before or after NFC normalization)
* or is not valid UTF-8.
*/
private function guard(string &$markdown): void
{
Expand All @@ -63,6 +64,14 @@ private function guard(string &$markdown): void
}
$normalized = $this->normalizer->normalize($markdown);
$markdown = $normalized !== false ? $normalized : $markdown;

// NFC can grow the input (e.g. U+0958 has no composed form and doubles in size):
// the limit applies to what the parser actually processes.
if (strlen($markdown) > $this->maxBytes) {
throw new ParseException(
sprintf('Input exceeds maximum allowed size of %d bytes after Unicode normalization.', $this->maxBytes),
);
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/Integration/MarkdownParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ public function testMaxBytesAllowsInputAtExactByteLimit(): void
$this->assertNotEmpty($html);
}

public function testMaxBytesIsEnforcedAfterNormalization(): void
{
// U+0958 (3 bytes) has no composed form: NFC turns it into 2 code points (6 bytes).
// 10 × 3 = 30 bytes fits the limit before normalization, 60 bytes does not after.
$parser = new MarkdownParser(maxBytes: 40);

$this->expectException(ParseException::class);
$this->expectExceptionMessage('after Unicode normalization');
$parser->parse(str_repeat("\u{0958}", 10));
}

// ── NFC normalisation ─────────────────────────────────────────────────────

public function testNfcNormalisationConvertsNfdInput(): void
Expand Down
Loading