Fixed handling of quote/code bbcode
authorAlexander Ebert <ebert@woltlab.com>
Sun, 29 Mar 2015 14:12:35 +0000 (16:12 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 29 Mar 2015 14:13:03 +0000 (16:13 +0200)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js

index af343425d99a49db01ec66f3ed4dfc5b2abbbd9f..d56dd92d82845e68a5323925a9de923cbdd44b7e 100644 (file)
@@ -27,7 +27,7 @@ RedactorPlugins.wbbcode = function() {
                                // use stored editor contents
                                var $content = $.trim(this.wutil.getOption('woltlab.originalValue'));
                                if ($content.length) {
-                                       this.wutil.replaceText($content);
+                                       this.wutil.replaceText($content, false);
                                        
                                        // ensure that the caret is not within a quote tag
                                        this.wutil.selectionEndOfEditor();
@@ -1412,6 +1412,7 @@ RedactorPlugins.wbbcode = function() {
                                                + '</div>';
                                        }).bind(this));
                                        
+                                       data = data.replace(new RegExp('(?:<p>)?(@@' + $cachedCode.key + '@@)(?:<\/p>)?', 'g'), '$1');
                                        data = data.replace($regex, $value);
                                }
                        }
@@ -2213,6 +2214,7 @@ RedactorPlugins.wbbcode = function() {
                                        var $html = this.wbbcode.convertToHtml($bbcode);
                                        
                                        this.buffer.set();
+                                       
                                        this.insert.html($html, false);
                                        
                                        // set caret after code listing
@@ -2302,35 +2304,28 @@ RedactorPlugins.wbbcode = function() {
                },
                
                /**
-                * Ensures that there is a paragraph in front of each block-level element because you cannot click in between two of them.
+                * Inserting block-level elements (e.g. quotes or code bbcode) can lead to void paragraphs.
                 */
                fixBlockLevelElements: function() {
-                       return;
-                       var $addSpacing = (function(referenceElement, target) {
-                               var $tagName = 'P';
-                               
-                               // fix reference element if a block element is within a quote (wrapped by <div>...</div>)
-                               if (referenceElement.parentElement.tagName === 'DIV' && referenceElement.parentElement !== this.$editor[0]) {
-                                       referenceElement = referenceElement.parentElement;
-                                       $tagName = 'DIV';
-                               }
-                               
-                               // no previous/next element or it is not a <p> (default) or <div> (within quotes)
-                               if (referenceElement[target] === null || referenceElement[target].tagName !== $tagName) {
-                                       $('<' + $tagName + '>' + this.opts.invisibleSpace + '</' + $tagName + '>')[(target === 'previousElementSibling' ? 'insertBefore' : 'insertAfter')](referenceElement);
-                               }
-                               else if (referenceElement.previousElementSibling.tagName === $tagName) {
-                                       // previous/next element is empty or contains an empty <p></p> (block element is a direct children of the editor)
-                                       if (!referenceElement[target].innerHTML.length || referenceElement[target].innerHTML.toLowerCase() === '<p></p>') {
-                                               $(referenceElement[target]).html(this.opts.invisibleSpace);
+                       var $removeVoidElements = (function(referenceElement, position) {
+                               var $sibling = referenceElement[position];
+                               if ($sibling && $sibling.nodeType === Node.ELEMENT_NODE && $sibling.tagName === 'P') {
+                                       if (!$sibling.innerHTML.length) {
+                                               $sibling.parentElement.removeChild($sibling);
+                                       }
+                                       else if ($sibling.innerHTML === '\u200b') {
+                                               var $adjacentSibling = $sibling[position];
+                                               if ($adjacentSibling && $adjacentSibling.nodeType === Node.ELEMENT_NODE && $adjacentSibling.tagName === 'P' && $adjacentSibling.innerHTML.length) {
+                                                       $sibling.parentElement.removeChild($sibling);
+                                               }
                                        }
                                }
                        }).bind(this);
                        
-                       this.$editor.find('blockquote, .codeBox').each((function(index, blockElement) {
-                               $addSpacing(blockElement, 'previousElementSibling');
-                               $addSpacing(blockElement, 'nextElementSibling');
-                       }).bind(this));
+                       this.$editor.find('blockquote, .codeBox').each(function() {
+                               $removeVoidElements(this, 'previousElementSibling');
+                               $removeVoidElements(this, 'nextElementSibling');
+                       });
                },
                
                /**
index 7a9b9dc92f782a297e9dcdf3747405e206c71d52..813d1407d173f016b5a60b8b4877bf8cfece34b0 100644 (file)
@@ -763,9 +763,10 @@ RedactorPlugins.wutil = function() {
                                                this.wutil.selectionEndOfEditor();
                                        }
                                        else {
-                                               var $p = $('<p><br></p>').insertAfter($insertAfter);
+                                               this.caret.setAfter($insertAfter);
+                                               /*var $p = $('<p><br></p>').insertAfter($insertAfter);
                                                
-                                               this.caret.setEnd($p);
+                                               this.caret.setEnd($p);*/
                                        }
                                }
                        }
@@ -883,8 +884,9 @@ RedactorPlugins.wutil = function() {
                 * Replaces the current content with the provided value.
                 * 
                 * @param       string          value
+                * @param       boolean         addNewlines
                 */
-               replaceText: function(value) {
+               replaceText: function(value, addNewlines) {
                        var $document = $(document);
                        var $offsetTop = $document.scrollTop();
                        var $wasInWysiwygMode = false;
@@ -894,7 +896,10 @@ RedactorPlugins.wutil = function() {
                                $wasInWysiwygMode = true;
                        }
                        
-                       value = this.wutil.addNewlines(value);
+                       if (addNewlines !== false) {
+                               value = this.wutil.addNewlines(value);
+                       }
+                       
                        this.$textarea.val(value);
                        
                        if ($wasInWysiwygMode) {