Stripping invalid icon classes from editor html input
authorAlexander Ebert <ebert@woltlab.com>
Wed, 4 Jan 2017 10:18:00 +0000 (11:18 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 4 Jan 2017 10:59:17 +0000 (11:59 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabSource.js

index 1471a0c6f438f37c0dd58dae79344c5267d31bad..66ffc4f6a14a6df52dd4518ec4244b42676c01f2 100644 (file)
@@ -24,12 +24,41 @@ $.Redactor.prototype.WoltLabSource = function() {
                                });
                        };
                        
+                       function stripIcons(div) {
+                               elBySelAll('.icon, .fa', div, function (element) {
+                                       var classNames = element.className.split(' ');
+                                       classNames = classNames.filter(function (value) {
+                                               if (value === 'fa' || value === 'icon') {
+                                                       return false;
+                                               }
+                                               else if (value.match(/^icon\d{2}$/)) {
+                                                       return false;
+                                               }
+                                               else if (value.match(/^fa-[a-z\-]+$/)) {
+                                                       return false;
+                                               }
+                                               
+                                               return true;
+                                       });
+                                       
+                                       element.className = classNames.join(' ');
+                                       if (element.className.trim() === '' && element.innerHTML === '') {
+                                               elRemove(element);
+                                       }
+                               });
+                       }
+                       
                        // disable caret position in source mode
                        this.source.setCaretOnShow = function () {};
                        this.source.setCaretOnHide = function (html) { return html; };
                        
                        var mpHide = this.source.hide;
                        this.source.hide = (function () {
+                               // use jQuery to parse, its parser is much more graceful
+                               var div = $('<div />').html(this.source.$textarea.val());
+                               stripIcons(div[0]);
+                               this.source.$textarea.val(div[0].innerHTML);
+                               
                                mpHide.call(this);
                                
                                setTimeout((function() {