From 5bd70e26ed0430bbb8be489d5d75acf434e6177b Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 25 May 2014 19:36:06 +0200 Subject: [PATCH] Fix for nested span tags --- .../js/3rdParty/redactor/plugins/wbbcode.js | 82 ++++++++++++++++--- 1 file changed, 69 insertions(+), 13 deletions(-) diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index dff66094ba..f78f2afc4e 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -194,21 +194,77 @@ RedactorPlugins.wbbcode = { // html = html.replace(/
/gi, '[quote]'); // html = html.replace(/\n*<\/blockquote>/gi, '[/quote]'); - // [color] - html = html.replace(/([\s\S]*?)<\/span>/gi, function(match, r, g, b, text) { - var $hex = ("0123456789ABCDEF".charAt((r - r % 16) / 16) + '' + "0123456789ABCDEF".charAt(r % 16)) + '' + ("0123456789ABCDEF".charAt((g - g % 16) / 16) + '' + "0123456789ABCDEF".charAt(g % 16)) + '' + ("0123456789ABCDEF".charAt((b - b % 16) / 16) + '' + "0123456789ABCDEF".charAt(b % 16)); - - return "[color=#" + $hex + "]" + text + "[/color]"; - }); - html = html.replace(/([\s\S]*?)<\/span>/gi, "[color=$1]$2[/color]"); + // handle [color], [size] and [font] + var $components = html.split(/(<\/?span[^>]*>)/); - // [size] - html = html.replace(/([\s\S]*?)<\/span>/gi, "[size=$1]$2[/size]"); + var $buffer = [ ]; + var $openElements = [ ]; + var $result = ''; - // [font] - html = html.replace(/([\s\S]*?)<\/span>/gi, function(match, fontFamily, text) { - return "[font='" + fontFamily.replace(/'/g, '') + "']" + text + "[/font]"; - }); + for (var $i = 0; $i < $components.length; $i++) { + var $value = $components[$i]; + + if ($value == '') { + var $opening = $openElements.pop(); + var $tmp = $opening.start + $buffer.pop() + $opening.end; + + if ($buffer.length) { + $buffer[$buffer.length - 1] += $tmp; + } + else { + $result += $tmp; + } + } + else { + if ($value.match(/^/)) { + var $style = RegExp.$1; + var $start; + var $end; + + if ($style.match(/^color: ?rgb\((\d{1,3}), ?(\d{1,3}), ?(\d{1,3})\);?$/i)) { + var $r = RegExp.$1; + var $g = RegExp.$2; + var $b = RegExp.$3; + + var $hex = ("0123456789ABCDEF".charAt(($r - $r % 16) / 16) + '' + "0123456789ABCDEF".charAt($r % 16)) + '' + ("0123456789ABCDEF".charAt(($g - $g % 16) / 16) + '' + "0123456789ABCDEF".charAt($g % 16)) + '' + ("0123456789ABCDEF".charAt(($b - $b % 16) / 16) + '' + "0123456789ABCDEF".charAt($b % 16)); + $start = '[color=#' + $hex + ']'; + $end = '[/color]'; + } + else if ($style.match(/^color: ?(.*?);?$/i)) { + $start = '[color=' + RegExp.$1 + ']'; + $end = '[/color]'; + } + else if ($style.match(/^font-size: ?(\d+)pt;?$/i)) { + $start = '[size=' + RegExp.$1 + ']'; + $end = '[/size]'; + } + else if ($style.match(/^font-family: ?(.*?);?$/)) { + $start = '[font=' + RegExp.$1.replace(/'/g, '') + ']'; + $end = '[/font]'; + } + else { + $start = ''; + $end = ''; + } + + $buffer[$buffer.length] = ''; + $openElements[$buffer.length] = { + start: $start, + end: $end + }; + } + else { + if ($buffer.length) { + $buffer[$buffer.length - 1] += $value; + } + else { + $result += $value; + } + } + } + } + + html = $result; // [align] html = html.replace(/
([\s\S]*?)<\/div>/gi, "[align=$1]$2[/align]"); -- 2.20.1