Edge yields a strange selection range when selecting an entire line
authorAlexander Ebert <ebert@woltlab.com>
Sat, 17 Feb 2018 17:21:05 +0000 (18:21 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sat, 17 Feb 2018 17:21:05 +0000 (18:21 +0100)
wcfsetup/install/files/js/3rdParty/redactor2/plugins/WoltLabCaret.js

index 529c9718ef237bee318a62779e909aa0bb7636bb..a4624627172d6e3e48990bb1e27be2276a062860 100644 (file)
@@ -148,6 +148,85 @@ $.Redactor.prototype.WoltLabCaret = function() {
                                        }
                                }
                        }).bind(this);
+                       
+                       this.selection.nodes = (function(tag) {
+                               var filter = (typeof tag === 'undefined') ? [] : (($.isArray(tag)) ? tag : [tag]);
+                               
+                               var sel = this.selection.get();
+                               var range = this.selection.range(sel);
+                               var nodes = [];
+                               var resultNodes = [];
+                               
+                               if (this.utils.isCollapsed()) {
+                                       nodes = [this.selection.current()];
+                               }
+                               else {
+                                       var node = range.startContainer;
+                                       var endNode = range.endContainer;
+                                       
+                                       // single node
+                                       if (node === endNode) {
+                                               return [node];
+                                       }
+                                       
+                                       // iterate
+                                       var commonAncestorContainer = range.commonAncestorContainer;
+                                       while (node && node !== endNode) {
+                                               // WoltLab modification: prevent `node` from breaking out of the `commonAncestorContainer`
+                                               //nodes.push(node = this.selection.nextNode(node));
+                                               nodes.push(node = this.selection.nextNode(node, commonAncestorContainer));
+                                       }
+                                       
+                                       // partially selected nodes
+                                       node = range.startContainer;
+                                       while (node && node !== commonAncestorContainer) {
+                                               nodes.unshift(node);
+                                               node = node.parentNode;
+                                       }
+                               }
+                               
+                               // remove service nodes
+                               $.each(nodes, function (i, s) {
+                                       if (s) {
+                                               var tagName = (s.nodeType !== 1) ? false : s.tagName.toLowerCase();
+                                               
+                                               if ($(s).hasClass('redactor-script-tag') || $(s).hasClass('redactor-selection-marker')) {
+                                                       return;
+                                               }
+                                               else if (tagName && filter.length !== 0 && $.inArray(tagName, filter) === -1) {
+                                                       return;
+                                               }
+                                               else {
+                                                       resultNodes.push(s);
+                                               }
+                                       }
+                               });
+                               
+                               return (resultNodes.length === 0) ? [] : resultNodes;
+                       }).bind(this);
+                       
+                       // WoltLab modification: Added the `container` parameter
+                       this.selection.nextNode = function(node, container) {
+                               if (node.hasChildNodes()) {
+                                       return node.firstChild;
+                               }
+                               else {
+                                       while (node && !node.nextSibling) {
+                                               node = node.parentNode;
+                                               
+                                               // WoltLab modification: do not allow the `node` to escape `container`
+                                               if (container && node === container) {
+                                                       return null;
+                                               }
+                                       }
+                                       
+                                       if (!node) {
+                                               return null;
+                                       }
+                                       
+                                       return node.nextSibling;
+                               }
+                       };
                },
                
                paragraphAfterBlock: function (block) {