From 579c342b3da64e689aff3cdbfb08a7c513e5fb1b Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Tue, 4 Apr 2017 17:47:21 +0200 Subject: [PATCH] Avoid bbcode parsing inside source code elements --- .../system/bbcode/HtmlBBCodeParser.class.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php b/wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php index 79ccffd55a..3271d85e75 100644 --- a/wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/HtmlBBCodeParser.class.php @@ -27,6 +27,12 @@ class HtmlBBCodeParser extends BBCodeParser { */ public static $disallowNesting = ['attach', 'b', 'code', 'email', 'i', 'img', 'media', 's', 'tt', 'u', 'url', 'user', 'wsm', 'wsp']; + /** + * tag names used to isolate bbcodes contained in source code elements + * @var string[] + */ + public static $codeTagNames = ['kbd', 'pre']; + /** * list of open tags with name and uuid * @var array @@ -43,6 +49,16 @@ class HtmlBBCodeParser extends BBCodeParser { * @inheritDoc */ public function parse($text) { + $codeBlocks = []; + foreach (self::$codeTagNames as $tagName) { + $text = preg_replace_callback('~(<' . $tagName . '[^>]+>)([\s\S]+?)(<\/' . $tagName . ')~', function ($matches) use (&$codeBlocks) { + $uuid = StringUtil::getUUID(); + $codeBlocks[$uuid] = $matches[2]; + + return $matches[1] . '###' . $uuid . '###' . $matches[3]; + }, $text); + } + $this->setText($text); // difference to the original implementation: sourcecode bbcodes are handled too @@ -54,6 +70,10 @@ class HtmlBBCodeParser extends BBCodeParser { $this->buildParsedString(); + foreach ($codeBlocks as $uuid => $content) { + $this->parsedText = str_replace('###' . $uuid . '###', $content, $this->parsedText); + } + return $this->parsedText; } -- 2.20.1