From 9d65beb9f405940b1f0f5e4a22e53570cc18c0f0 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Wed, 7 Jun 2017 12:08:25 +0200 Subject: [PATCH] Guard against invalid selection ranges --- .../WoltLabSuite/Core/Ui/Redactor/Format.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js index 23332b2ed9..5e8c2bad4d 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Redactor/Format.js @@ -11,6 +11,19 @@ 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 element first, all though there shouldn't be any at all -- 2.20.1