From 293c5dcaa0b6118f9f4ca1ed74ea66b7767409fe Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 21 Nov 2014 00:30:20 +0100 Subject: [PATCH] Improved parsing of source BBCodes --- .../js/3rdParty/redactor/plugins/wbbcode.js | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index 9700d2e536..7019563ffa 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -570,7 +570,7 @@ RedactorPlugins.wbbcode = function() { //html = html.replace(/%20/g, ' '); // cache source code tags to preserve leading tabs - var $cachedCodes = { }; + /*var $cachedCodes = { }; for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) { var $bbcode = __REDACTOR_SOURCE_BBCODES[$i]; @@ -580,17 +580,20 @@ RedactorPlugins.wbbcode = function() { $cachedCodes[$key] = match.replace(/\$/g, '$$$$'); return '@@' + $key + '@@'; }); - } + }*/ + + // ensure that [/code] is always followed by at least one empty line + html = html.replace(/\[\/code\]\n\n?/g, '[/code]\n\n'); WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'afterConvertFromHtml', { html: html }); // insert codes - if ($.getLength($cachedCodes)) { + /*if ($.getLength($cachedCodes)) { for (var $key in $cachedCodes) { var $regex = new RegExp('@@' + $key + '@@', 'g'); html = html.replace($regex, $cachedCodes[$key]); } - } + }*/ // remove all leading and trailing whitespaces, but add one empty line at the end html = $.trim(html); @@ -620,17 +623,13 @@ RedactorPlugins.wbbcode = function() { data = data.replace(/>/g, '>'); // cache source code tags - var $cachedCodes = { }; - for (var $i = 0, $length = __REDACTOR_SOURCE_BBCODES.length; $i < $length; $i++) { - var $bbcode = __REDACTOR_SOURCE_BBCODES[$i]; - - var $regExp = new RegExp('\\[' + $bbcode + '([\\S\\s]+?)\\[\\/' + $bbcode + '\\]', 'gi'); - data = data.replace($regExp, function(match) { - var $key = match.hashCode(); - $cachedCodes[$key] = match.replace(/\$/g, '$$$$'); - return '@@' + $key + '@@'; - }); - } + var $cachedCodes = [ ]; + var $regExp = new RegExp('\\[(' + __REDACTOR_SOURCE_BBCODES.join('|') + ')([\\S\\s]+?)\\[\\/\\1\\]', 'gi'); + data = data.replace($regExp, function(match) { + var $key = match.hashCode(); + $cachedCodes.push({ key: $key, value: match.replace(/\$/g, '$$$$') }); + return '@@' + $key + '@@'; + }); // [url] data = data.replace(/\[url\]([^"]+?)\[\/url]/gi, '$1' + this.opts.invisibleSpace); @@ -960,19 +959,23 @@ RedactorPlugins.wbbcode = function() { } // insert codes - if ($.getLength($cachedCodes)) { - for (var $key in $cachedCodes) { - var $regex = new RegExp('@@' + $key + '@@', 'g'); - data = data.replace($regex, $cachedCodes[$key]); + if ($cachedCodes.length) { + for (var $i = $cachedCodes.length - 1; $i >= 0; $i--) { + var $cachedCode = $cachedCodes[$i]; + var $regex = new RegExp('@@' + $cachedCode.key + '@@', 'g'); + + var $value = $cachedCode.value; + + // [tt] + $value = $value.replace(/^\[tt\](.*)\[\/tt\]/, '$1'); + + // preserve leading whitespaces in [code] tags + $value = $value.replace(/^\[code[^\]]*\][\S\s]*\[\/code\]$/, '
$&
'); + + data = data.replace($regex, $value); } - - // [tt] - data = data.replace(/\[tt\](.*?)\[\/tt\]/gi, '$1'); } - // preserve leading whitespaces in [code] tags - data = data.replace(/\[code\][\S\s]*?\[\/code\]/g, '
$&
'); - WCF.System.Event.fireEvent('com.woltlab.wcf.redactor', 'afterConvertToHtml', { data: data }); return data; -- 2.20.1