Guard against invalid selection ranges
authorAlexander Ebert <ebert@woltlab.com>
Wed, 7 Jun 2017 10:08:25 +0000 (12:08 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Wed, 7 Jun 2017 10:08:25 +0000 (12:08 +0200)
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js

index 23332b2ed9eaa0f38f8c8d23072c9adbcabcf9ea..5e8c2bad4d0da2b2cf5b23463ee4db7d3ec89950 100644 (file)
 define(['Dom/Util'], function(DomUtil) {
        "use strict";
        
+       var _isValidSelection = function(editorElement) {
+               var element = window.getSelection().anchorNode;
+               while (element) {
+                       if (element === editorElement) {
+                               return true;
+                       }
+                       
+                       element = element.parentNode;
+               }
+               
+               return false;
+       };
+       
        /**
         * @exports     WoltLabSuite/Core/Ui/Redactor/Format
         */
@@ -29,6 +42,11 @@ define(['Dom/Util'], function(DomUtil) {
                                return;
                        }
                        
+                       if (!_isValidSelection(editorElement)) {
+                               console.error("Invalid selection, range exists outside of the editor:", selection.anchorNode);
+                               return;
+                       }
+                       
                        var range = selection.getRangeAt(0);
                        var markerStart = null, markerEnd = null, tmpElement = null;
                        if (range.collapsed) {
@@ -190,6 +208,15 @@ define(['Dom/Util'], function(DomUtil) {
                 * @param       {string}        property        CSS property that should be removed
                 */
                removeFormat: function(editorElement, property) {
+                       var selection = window.getSelection();
+                       if (!selection.rangeCount) {
+                               return;
+                       }
+                       else if (!_isValidSelection(editorElement)) {
+                               console.error("Invalid selection, range exists outside of the editor:", selection.anchorNode);
+                               return;
+                       }
+                       
                        var strikeElements = elByTag('strike', editorElement);
                        
                        // remove any <strike> element first, all though there shouldn't be any at all