From 3b9b1b9fb697c1ecf7375f138021ef248c82f6d3 Mon Sep 17 00:00:00 2001 From: Roman Parpalak Date: Sat, 6 Apr 2024 01:27:25 +0300 Subject: [PATCH] Fixed skipping escaped backslashes when dealing with formatting. --- src/S2/Rose/Helper/StringHelper.php | 3 +-- tests/unit/Rose/Helper/StringHelperTest.php | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/S2/Rose/Helper/StringHelper.php b/src/S2/Rose/Helper/StringHelper.php index 6dbfe94..86c7f2f 100644 --- a/src/S2/Rose/Helper/StringHelper.php +++ b/src/S2/Rose/Helper/StringHelper.php @@ -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); diff --git a/tests/unit/Rose/Helper/StringHelperTest.php b/tests/unit/Rose/Helper/StringHelperTest.php index be617a3..cf9a710 100644 --- a/tests/unit/Rose/Helper/StringHelperTest.php +++ b/tests/unit/Rose/Helper/StringHelperTest.php @@ -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.', + [], + ], [ '', '', @@ -133,6 +138,16 @@ public function unbalancedInternalFormattingDataProvider(): array '\\i456789\\I', ['i' => -1], ], + [ + '456789\\\\I', + '456789\\\\I', + [], + ], + [ + '456789\\\\\\I', + '\\i456789\\\\\\I', + ['i' => -1], + ], [ '\\u456789', '\\u456789\\U', @@ -140,8 +155,8 @@ public function unbalancedInternalFormattingDataProvider(): array ], [ '\\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], ], ]; }