Fixed encoding issues with pasted code
authorAlexander Ebert <ebert@woltlab.com>
Wed, 24 Aug 2016 09:09:53 +0000 (11:09 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 24 Aug 2016 09:10:34 +0000 (11:10 +0200)
com.woltlab.wcf/templates/wysiwyg.tpl
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js [new file with mode: 0644]
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCode.js
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabPaste.js

index c43230fdea977f55edddcdeadb7ca32aa3e99eaa..aa9bf0fda4c7b9b8456dfe11e354ab7765f7b660 100644 (file)
@@ -14,6 +14,7 @@
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabBlock.js?v={@LAST_UPDATE_TIME}',
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabButton.js?v={@LAST_UPDATE_TIME}',
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabCaret.js?v={@LAST_UPDATE_TIME}',
+                       '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabClean.js?v={@LAST_UPDATE_TIME}',
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabCode.js?v={@LAST_UPDATE_TIME}',
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabColor.js?v={@LAST_UPDATE_TIME}',
                        '{@$__wcf->getPath()}js/3rdParty/redactor2/plugins/WoltLabDropdown.js?v={@LAST_UPDATE_TIME}',
                                        'WoltLabAttachment',
                                        'WoltLabAutosave',
                                        'WoltLabCaret',
+                                       'WoltLabClean',
                                        'WoltLabCode',
                                        'WoltLabColor',
                                        'WoltLabDropdown',
diff --git a/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js b/wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabClean.js
new file mode 100644 (file)
index 0000000..383a377
--- /dev/null
@@ -0,0 +1,59 @@
+$.Redactor.prototype.WoltLabClean = function() {
+       "use strict";
+       
+       return {
+               init: function () {
+                       var mpOnSet = this.clean.onSet;
+                       this.clean.onSet = (function (html) {
+                               return mpOnSet.call(this, html.replace(/\u200B/g, ''));
+                       }).bind(this);
+                       
+                       var mpOnSync = this.clean.onSync;
+                       this.clean.onSync = (function (html) {
+                               var div, replacements = {};
+                               if (html.indexOf('<pre') !== -1) {
+                                       div = elCreate('div');
+                                       div.innerHTML = html;
+                                       
+                                       elBySelAll('pre', div, function (pre) {
+                                               var uuid = WCF.getUUID();
+                                               
+                                               replacements[uuid] = pre.textContent;
+                                               pre.textContent = uuid;
+                                       });
+                                       
+                                       html = div.innerHTML;
+                               }
+                               
+                               html = mpOnSync.call(this, html);
+                               
+                               if (div) {
+                                       div.innerHTML = html;
+                                       
+                                       elBySelAll('pre', div, function (pre) {
+                                               pre.textContent = replacements[pre.textContent];
+                                       });
+                                       
+                                       html = div.innerHTML;
+                               }
+                               
+                               return html;
+                       }).bind(this);
+                       
+                       var mpSavePreFormatting = this.clean.savePreFormatting;
+                       this.clean.savePreFormatting = (function (html) {
+                               var mpCleanEncodeEntities = this.clean.encodeEntities;
+                               this.clean.encodeEntities = function(str) {
+                                       return WCF.String.escapeHTML(str);
+                               };
+                               
+                               html = mpSavePreFormatting.call(this, html);
+                               
+                               // revert to original method
+                               this.clean.encodeEntities = mpCleanEncodeEntities;
+                               
+                               return html;
+                       }).bind(this);
+               }
+       }
+};
index 2a96ea44911977795c5ace290af4e430c7fb49bb..3b708e084f6a79bfc84af8dfe6d18a7d180fd0a2 100644 (file)
@@ -3,6 +3,10 @@ $.Redactor.prototype.WoltLabCode = function() {
        
        return {
                init: function() {
+                       // disable caret position in source mode
+                       this.source.setCaretOnShow = function () {};
+                       this.source.setCaretOnHide = function (html) { return html; };
+                       
                        require(['WoltLabSuite/Core/Ui/Redactor/Code'], (function (UiRedactorCode) {
                                new UiRedactorCode(this);
                        }).bind(this));
index 2c2693794bdb4c6a99709a817b5b27db4734f30e..c4027eae5093d68a9850c8a815732b0725fa7fd7 100644 (file)
@@ -7,7 +7,18 @@ $.Redactor.prototype.WoltLabPaste = function() {
                        
                        var mpInit = this.paste.init;
                        this.paste.init = (function (e) {
-                               clipboardData = e.originalEvent.clipboardData.getData('text/plain');
+                               var isCode = (this.opts.type === 'pre' || this.utils.isCurrentOrParent('pre')) ? true : false;
+                               if (isCode) {
+                                       clipboardData = e.originalEvent.clipboardData.getData('text/plain');
+                                       
+                                       var mpCleanEncodeEntities = this.clean.encodeEntities;
+                                       this.clean.encodeEntities = (function(str) {
+                                               // revert to original method
+                                               this.clean.encodeEntities = mpCleanEncodeEntities;
+                                               
+                                               return WCF.String.escapeHTML(str);
+                                       }).bind(this);
+                               }
                                
                                mpInit.call(this, e);
                        }).bind(this);