Properly check if selection is within the $messageBody element
authorAlexander Ebert <ebert@woltlab.com>
Fri, 27 Feb 2015 10:40:18 +0000 (11:40 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Fri, 27 Feb 2015 10:40:18 +0000 (11:40 +0100)
wcfsetup/install/files/js/WCF.Message.js

index 9c5bb032f14be00e721a1bbc7b3a414783d11e30..5232d5f30f68958eff7bfbca55fc76ae7c334f27 100644 (file)
@@ -2088,6 +2088,14 @@ WCF.Message.Quote.Handler = Class.extend({
                        $element = $element.parentElement;
                }
                
+               // check if selection starts and ends within the $messageBody element
+               var $range = window.getSelection().getRangeAt(0);
+               if (!this._elementInsideContainer($range.startContainer, $messageBody) || !this._elementInsideContainer($range.endContainer, $messageBody)) {
+                       this._copyQuote.hide();
+                       
+                       return;
+               }
+               
                // compare selection with message text of given container
                var $messageText = this._getNodeText($messageBody);
                
@@ -2133,6 +2141,27 @@ WCF.Message.Quote.Handler = Class.extend({
                }, 10);
        },
        
+       /**
+        * Returns true if given element is a child element of given container element.
+        * 
+        * @param       Node            element
+        * @param       Element         container
+        * @return      boolean
+        */
+       _elementInsideContainer: function(element, container) {
+               if (element.nodeType === Node.TEXT_NODE) element = element.parentElement;
+               
+               while (element) {
+                       if (element === container) {
+                               return true;
+                       }
+                       
+                       element = element.parentElement;
+               }
+               
+               return false;
+       },
+       
        /**
         * Normalizes a text for comparison.
         *