Fix DOM traversal of copy process for comments

The comment selection process was being performed incorrectly due to
lack of utilization of Polymer.dom. This uncovered a few more issues
with the way the fake copy events in testing were being constructed and
a base case in comment text selection.

Bug: Issue 4674
Change-Id: I7e05b7ebe0bfd60bc9cc8973166685c24e25ab37
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js
index f69eddd..e6d3653 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.js
@@ -86,6 +86,10 @@
       }
     },
 
+    _getCopyEventTarget: function(e) {
+      return Polymer.dom(e).rootTarget;
+    },
+
     /**
      * Utility function to determine whether an element is a descendant of
      * another element with the particular className.
@@ -107,14 +111,15 @@
 
     _handleCopy: function(e) {
       var commentSelected = false;
-      if (this._elementDescendedFromClass(e.target, SelectionClass.COMMENT)) {
+      var target = this._getCopyEventTarget(e);
+      if (this.classList.contains(SelectionClass.COMMENT)) {
         commentSelected = true;
       } else {
-        if (!this._elementDescendedFromClass(e.target, 'content')) {
+        if (!this._elementDescendedFromClass(target, 'content')) {
           return;
         }
       }
-      var lineEl = this.diffBuilder.getLineElByChild(e.target);
+      var lineEl = this.diffBuilder.getLineElByChild(target);
       if (!lineEl) {
         return;
       }
@@ -214,6 +219,9 @@
       var content = [];
       // Fall back to default copy behavior if the selection lies within one
       // comment body.
+      if (range.startContainer === range.endContainer) {
+        return;
+      }
       if (this._elementDescendedFromClass(range.commonAncestorContainer,
           'message')) {
         return;
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html
index f44d349..71fa233 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html
+++ b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection_test.html
@@ -87,6 +87,7 @@
 <script>
   suite('gr-diff-selection', function() {
     var element;
+    var copyEventStub;
 
     var emulateCopyOn = function(target) {
       var fakeEvent = {
@@ -96,12 +97,14 @@
           setData: sinon.stub(),
         },
       };
+      element._getCopyEventTarget.returns(target);
       element._handleCopy(fakeEvent);
       return fakeEvent;
     };
 
     setup(function() {
       element = fixture('basic');
+      sinon.stub(element, '_getCopyEventTarget');
       element._cachedDiffBuilder = {
         getLineElByChild: sinon.stub().returns({}),
         getSideByLineEl: sinon.stub(),