diff --git a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.html b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.html index 6a02a2dd00..bfddf89367 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.html +++ b/polygerrit-ui/app/elements/diff/gr-diff-selection/gr-diff-selection.html @@ -31,8 +31,8 @@ limitations under the License. :host-context(.selected-left:not(.selected-comment)) .contentWrapper ::content .unified .left.lineNum ~ .content:not(.both) .contentText, :host-context(.selected-right:not(.selected-comment)) .contentWrapper ::content .unified .right.lineNum ~ .content .contentText, :host-context(.selected-left.selected-comment) .contentWrapper ::content .side-by-side .left + .content .message, - :host-context(.selected-right.selected-comment) .contentWrapper ::content .side-by-side .right + .content .message, - :host-context(.selected-comment) .contentWrapper ::content .unified .message { + :host-context(.selected-right.selected-comment) .contentWrapper ::content .side-by-side .right + .content .message :not(.collapsedContent), + :host-context(.selected-comment) .contentWrapper ::content .unified .message :not(.collapsedContent){ -webkit-user-select: text; -moz-user-select: text; -ms-user-select: text; 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 6ba3ff83e4..f0f2c3b242 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 @@ -213,7 +213,7 @@ * @return {string} The selected comment text. */ _getCommentLines: function(sel, side) { - var range = sel.getRangeAt(0); + var range = GrRangeNormalizer.normalize(sel.getRangeAt(0)); var content = []; // Query the diffElement for comments. var messages = this.diffBuilder.diffElement.querySelectorAll( @@ -232,18 +232,45 @@ } } - if (!el.children.length) { - content.push(el.textContent); + if (el.id === 'output' && + !this._elementDescendedFromClass(el, 'collapsed')) { + content.push(this._getTextContentForRange(el, sel, range)); } } } - if (range.endOffset) { - content[content.length - 1] = - content[content.length - 1].substring(0, range.endOffset); - } - content[0] = content[0].substring(range.startOffset); return content.join('\n'); }, + + /** + * Given a DOM node, a selection, and a selection range, recursively get all + * of the text content within that selection. + * Using a domNode that isn't in the selection returns an empty string. + * + * @param {Element} domNode The root DOM node. + * @param {Selection} sel The selection. + * @param {Range} range The normalized selection range. + * @return {string} The text within the selection. + */ + _getTextContentForRange: function(domNode, sel, range) { + if (!sel.containsNode(domNode, true)) { return ''; } + + var text = ''; + if (domNode instanceof Text) { + text = domNode.textContent; + if (domNode === range.endContainer) { + text = text.substring(0, range.endOffset); + } + if (domNode === range.startContainer) { + text = text.substring(range.startOffset); + } + } else { + for (var i = 0; i < domNode.childNodes.length; i++) { + text += this._getTextContentForRange(domNode.childNodes[i], + sel, range); + } + } + return text; + }, }); })(); 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 a5c26e198a..76bf4ec1c5 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 @@ -35,7 +35,7 @@ limitations under the License.