Cleaned up code, resolved an issue with 0x200B
authorAlexander Ebert <ebert@woltlab.com>
Sun, 30 Mar 2014 14:53:54 +0000 (16:53 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 30 Mar 2014 14:53:54 +0000 (16:53 +0200)
wcfsetup/install/files/js/3rdParty/redactor/plugins/wbbcode.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wmonkeypatch.js
wcfsetup/install/files/js/3rdParty/redactor/plugins/wutil.js

index 31cd080b63588e57afd676673d2e0224bd759574..9f639705a9e9aaf61405a68700564eba088c1419 100644 (file)
@@ -119,25 +119,6 @@ RedactorPlugins.wbbcode = {
                }
        },
        
-       /**
-        * Removes the unicode zero width space (0x200B).
-        * 
-        * @param       string          string
-        * @return      string
-        */
-       _removeCrap: function(string) {
-               var $string = '';
-               
-               for (var $i = 0, $length = string.length; $i < $length; $i++) {
-                       var $byte = string.charCodeAt($i).toString(16);
-                       if ($byte != '200b') {
-                               $string += string[$i];
-                       }
-               }
-               
-               return $string;
-       },
-       
        _convertParagraphs: function() {
                this.$editor.find('p').replaceWith(function() {
                        var $html = $(this).html();
@@ -301,7 +282,7 @@ RedactorPlugins.wbbcode = {
                var data = this.$source.val();
                
                // remove 0x200B (unicode zero width space)
-               data = this._removeCrap(data);
+               data = this.removeZeroWidthSpace(data);
                
                //if (!$pasted) {
                        // Convert & to its HTML entity.
index 1c6f9374d5f9c686d887267613e4f7f616e2dfec..67e166d1d92905dbf143dc22d9402fff1c03fa31 100644 (file)
@@ -233,5 +233,46 @@ RedactorPlugins.wmonkeypatch = {
                        marginLeft: -1 * Math.round($dimensions.width / 2) + 'px',
                        marginTop: -1 * Math.round($dimensions.height / 2) + 'px'
                });
-       }
+       },
+       
+       /**
+        * Overwrites $.Redactor.inlineEachNodes(), the original method compares "selectionHtml"
+        * and "parentHtml" to check if the callback should be invoked. In some cases the "parentHtml"
+        * may contain a trailing unicode zero-width space and the comparision will fail, even though
+        * the "entire" node is selected.
+        * 
+        * @see $.Redactor.inlineEachNodes()
+        */
+       inlineEachNodes: function(callback)
+       {
+               var range = this.getRange(),
+                       node = this.getElement(),
+                       nodes = this.getNodes(),
+                       collapsed;
+
+               if (range.collapsed || range.startContainer === range.endContainer && node)
+               {
+                       nodes = $(node);
+                       collapsed = true;
+               }
+
+               $.each(nodes, $.proxy(function(i, node)
+               {
+                       if (!collapsed && node.tagName !== 'INLINE')
+                       {
+                               var selectionHtml = this.getSelectionText();
+                               var parentHtml = $(node).parent().text();
+                               // if parentHtml contains a trailing 0x200B, the comparison will most likely fail
+                               var selected = this.removeZeroWidthSpace(selectionHtml) == this.removeZeroWidthSpace(parentHtml);
+
+                               if (selected && node.parentNode.tagName === 'INLINE' && !$(node.parentNode).hasClass('redactor_editor'))
+                               {
+                                       node = node.parentNode;
+                               }
+                               else return;
+                       }
+                       callback.call(this, node);
+
+               }, this ) );
+       },
 }
\ No newline at end of file
index 0d52ee0cf6b69086c33a43dbb79cbe19b41326c0..fc5075c65e8b77c4f35276222b5cb31aca1566fe 100644 (file)
@@ -282,5 +282,24 @@ RedactorPlugins.wutil = {
                $target.parent().remove();
                
                return $button;
-       }
+       },
+       
+       /**
+        * Removes the unicode zero-width space (0x200B).
+        * 
+        * @param       string          string
+        * @return      string
+        */
+       removeZeroWidthSpace: function(string) {
+               var $string = '';
+               
+               for (var $i = 0, $length = string.length; $i < $length; $i++) {
+                       var $byte = string.charCodeAt($i).toString(16);
+                       if ($byte != '200b') {
+                               $string += string[$i];
+                       }
+               }
+               
+               return $string;
+       },
 };