From: Alexander Ebert Date: Mon, 30 Jun 2014 18:27:03 +0000 (+0200) Subject: Improved HTML -> BBCode, no longer relying on editor manipulation X-Git-Tag: 2.1.0_Alpha_1~641^2~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=60bf3754e5b2c84c6b6b936bfcc50b80e5c162cf;p=GitHub%2FWoltLab%2FWCF.git Improved HTML -> BBCode, no longer relying on editor manipulation --- diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js index d03a6ee914..6d8aa998b1 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js @@ -29,6 +29,13 @@ RedactorPlugins.wbbcode = { this.opts.pasteBeforeCallback = $.proxy(this._wPasteBeforeCallback, this); this.opts.pasteAfterCallback = $.proxy(this._wPasteAfterCallback, this); + + var $mpSyncClean = this.syncClean; + var self = this; + this.syncClean = function(html) { + html = html.replace(/

]+)?><\/p>/g, '

@@@wcf_empty_line@@@

'); + return $mpSyncClean.call(self, html); + }; }, /** @@ -110,7 +117,6 @@ RedactorPlugins.wbbcode = { */ toggle: function(direct) { if (this.opts.visual) { - this._convertParagraphs(); this.toggleCode(direct); this._convertFromHtml(); @@ -124,54 +130,49 @@ RedactorPlugins.wbbcode = { } }, - _convertParagraphs: function() { - this.$editor.find('p').replaceWith(function() { - var $html = $(this).html(); - if ($html == '
') { - // an empty line is presented by


but in the textarea this equals only a single new line - return $html; - } - - return $html + '
'; - }); - this.sync(); - }, - /** * Converts source contents from HTML into BBCode. */ _convertFromHtml: function() { var html = this.$source.val(); - // drop line break right before/after a
 tag (used by [code]-BBCode)
-		html = html.replace(/
\n
\n/g, '');
-		html = html.replace(/<\/pre>\n
\n/g, ''); - - html = html.replace(/
(?:\n
)+/g, function(match) { - var $count = match.match(/
/g); - return '@@@' + $count.length + '@@@'; - }); - html = html.replace(/\n@@@/g, '@@@'); - html = html.replace(/@@@(\d+)@@@/g, function(match, times) { - var $tmp = '
'; - for (var $i = 1; $i < times; $i++) { - $tmp += '\n
'; + // drop all new lines + html = html.replace(/\r?\n/g, ''); + + // convert paragraphs into single lines + var $parts = html.split(/(<\/?p>)/); + var $tmp = ''; + var $buffer = ''; + for (var $i = 1; $i < $parts.length; $i++) { + var $part = $parts[$i]; + if ($part == '

') { + continue; } - - return $tmp; - }); + else if ($part == '

') { + $buffer = $.trim($buffer); + if ($buffer != '@@@wcf_empty_line@@@') { + $buffer += "\n"; + } + + $tmp += $buffer; + $buffer = ''; + } + else { + $buffer += $part; + } + } + html = $tmp; - // drop line break if there is a succeeding
- /*html = html.replace(/(
)?\n
/g, function(match, br) { - console.debug("br was matched:"); - console.debug(match); - - return br ? match : '
'; - });*/ + html = html.replace(/@@@wcf_empty_line@@@/g, '\n'); + html = html.replace(/\n\n$/, '\n'); + + // convert all
into \n + html = html.replace(/
$/, ''); + html = html.replace(/
/g, '\n'); // drop
, they are pointless because the editor already adds a newline after them html = html.replace(/
/g, ''); - html = html.replace(/ /gi," "); + html = html.replace(/ /gi, " "); // [email] html = html.replace(/]*?href=(["'])mailto:(.+?)\1.*?>([\s\S]+?)<\/a>/gi, '[email=$2]$3[/email]'); @@ -397,20 +398,12 @@ RedactorPlugins.wbbcode = { // remove 0x200B (unicode zero width space) data = this.removeZeroWidthSpace(data); - //if (!$pasted) { - // Convert & to its HTML entity. - data = data.replace(/&/g, '&'); - - // Convert < and > to their HTML entities. - data = data.replace(//g, '>'); - //} + // Convert & to its HTML entity. + data = data.replace(/&/g, '&'); - /*if ($pasted) { - $pasted = false; - // skip - return data; - }*/ + // Convert < and > to their HTML entities. + data = data.replace(//g, '>'); // cache source code tags var $cachedCodes = { }; @@ -570,6 +563,14 @@ RedactorPlugins.wbbcode = { }); html = html.replace(/<\/h[1-6]>/g, '[/size]'); + // convert block-level elements + html = html.replace(/<(article|header)[^>]+>/g, '
'); + html = html.replace(/<\/(article|header)>/g, '
'); + + // replace nested elements e.g.

...

+ html = html.replace(/<(div|p)([^>]+)?><(div|p)([^>]+)?>/g, '

'); + html = html.replace(/<\/(div|p)><\/(div|p)>/g, '

@@@wcf_break@@@'); + return html; }, @@ -580,22 +581,27 @@ RedactorPlugins.wbbcode = { * @return string */ _wPasteAfterCallback: function(html) { - // restore font size - html = html.replace(/\[size=(\d+)\]/g, '

'); - html = html.replace(/\[\/size\]/g, '

'); - - // replace

with

...

- html = html.replace(/

([\s\S]*?)<\/p>/g, '

$1

'); + // replace

with

...

+ html = html.replace(/

([\s\S]*?)<\/p>/g, '

$1

'); // drop
html = html.replace(/]*>/g, ''); html = html.replace(/<\/header>/g, ''); - html = html.replace(/
.*?<\/div>/g, '

$1

'); + html = html.replace(/
.*?<\/div>/g, '

$1

'); // drop lonely divs html = html.replace(/<\/?div>/g, ''); + html = html.replace(/@@@wcf_break@@@/g, '


'); + + // drop lonely

opening tags + html = html.replace(/

/g, '

'); + + // restore font size + html = html.replace(/\[size=(\d+)\]/g, '


'); + html = html.replace(/\[\/size\]/g, '


'); + return html; } }; diff --git a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js index d1a894466d..7849c99c12 100644 --- a/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js +++ b/wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js @@ -66,8 +66,6 @@ RedactorPlugins.wmonkeypatch = { this.setOption('modalOpenedCallback', $.proxy(this.modalOpenedCallback, this)); this.modalTemplatesInit(); - - window.dtdesign = this; }, cleanRemoveSpaces: function(html, buffer) {