Skip to content

Commit

Permalink
Fixed skipping escaped backslashes when dealing with formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
parpalak committed Apr 5, 2024
1 parent 58127db commit 3b9b1b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/S2/Rose/Helper/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ public static function clearInternalFormatting(string $text): string

public static function fixUnbalancedInternalFormatting(string $text, array &$tagsNum): string
{
preg_match_all('#\\\\([' . self::FORMATTING_SYMBOLS . '])#i', $text, $matches);
preg_match_all('#(?:\\\\{2})*(*SKIP)\K\\\\([' . self::FORMATTING_SYMBOLS . '])#i', $text, $matches);

// $tagsNum = [];
foreach ($matches[1] as $match) {
$lowerMatch = strtolower($match);
$tagsNum[$lowerMatch] = ($tagsNum[$lowerMatch] ?? 0) + ($match === $lowerMatch ? 1 : -1);
Expand Down
19 changes: 17 additions & 2 deletions tests/unit/Rose/Helper/StringHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public function unbalancedInternalFormattingDataProvider(): array
'\\iThis is \\bformatted text\\I with \\Bspecial characters\\i.\\I',
['i' => 1, 'b' => 0],
],
[
'Normal text with escaped formatting symbols like \\\\draw or \\\\inline or \\\\\\\\uuu.',
'Normal text with escaped formatting symbols like \\\\draw or \\\\inline or \\\\\\\\uuu.',
[],
],
[
'',
'',
Expand All @@ -133,15 +138,25 @@ public function unbalancedInternalFormattingDataProvider(): array
'\\i456789\\I',
['i' => -1],
],
[
'456789\\\\I',
'456789\\\\I',
[],
],
[
'456789\\\\\\I',
'\\i456789\\\\\\I',
['i' => -1],
],
[
'\\u456789',
'\\u456789\\U',
['u' => 1],
],
[
'\\u\\D\\\\I\\b',
'\\i\\d\\u\\D\\\\I\\b\\B\\U',
['i' => -1, 'd' => -1, 'u' => 1, 'b' => 1],
'\\d\\u\\D\\\\I\\b\\B\\U',
['d' => -1, 'u' => 1, 'b' => 1],
],
];
}
Expand Down

0 comments on commit 3b9b1b9

Please sign in to comment.