From faeffff8a39aae0d8e0ca6c07a9ad6724570b5a7 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 23 Jun 2020 07:53:18 -0400 Subject: [PATCH] [pre] blocks now preserve spaces - Added test case - Added English documentation --- doc/BBCode.md | 8 ++++++++ src/Content/Text/BBCode.php | 6 +++++- tests/src/Content/Text/BBCodeTest.php | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/doc/BBCode.md b/doc/BBCode.md index 59f7414491..08c43636f8 100644 --- a/doc/BBCode.md +++ b/doc/BBCode.md @@ -633,6 +633,14 @@ On Mastodon this field is used for the content warning. @user@domain.tld #hashtag + + Additionally, [pre] blocks preserve spaces: + + +       Spaces + [nosmile] is used to disable smilies on a post by post basis

diff --git a/src/Content/Text/BBCode.php b/src/Content/Text/BBCode.php index 95484de720..ba5bde045d 100644 --- a/src/Content/Text/BBCode.php +++ b/src/Content/Text/BBCode.php @@ -1878,7 +1878,11 @@ class BBCode // Remove escaping tags $text = preg_replace("/\[noparse\](.*?)\[\/noparse\]/ism", '\1', $text); $text = preg_replace("/\[nobb\](.*?)\[\/nobb\]/ism", '\1', $text); - $text = preg_replace("/\[pre\](.*?)\[\/pre\]/ism", '\1', $text); + + // Additionally, [pre] tags preserve spaces + $text = preg_replace_callback("/\[pre\](.*?)\[\/pre\]/ism", function ($match) { + return str_replace(' ', ' ', $match[1]); + }, $text); return $text; }); // Escaped code diff --git a/tests/src/Content/Text/BBCodeTest.php b/tests/src/Content/Text/BBCodeTest.php index 82e2853e71..77613e891a 100644 --- a/tests/src/Content/Text/BBCodeTest.php +++ b/tests/src/Content/Text/BBCodeTest.php @@ -241,6 +241,10 @@ class BBCodeTest extends MockedTest 'expectedHtml' => '[test] Space', 'text' => '[test] Space', ], + 'task-8800-pre-spaces' => [ + 'expectedHtml' => '    Spaces', + 'text' => '[pre] Spaces[/pre]', + ], ]; }