From 9317bafd74c91159374903ac69cb25af9e3f2447 Mon Sep 17 00:00:00 2001 From: Wyatt Allen Date: Fri, 15 Jul 2016 17:08:22 -0700 Subject: [PATCH] Upgrades and tests for GrDiffBuilder utility methods A new method is added to the GrDiffBuilder class named `_renderContentByRange`. It will replace DIV.contentText elements with newly-rendered versions for the given line-range and side. Two utility methods found on GR-DIFF-BUILDER are pushed down into the GrDiffBuilder class. In particular, `getContentByLine` is moved as-is and a wrapper is added to original place. `getContentsByLineRange` is moved and converted to be more general. A wrapper is put in its original location which follows the original signature. These moves make the functions available to other methods of the class. Tests are added for these new and existing methods. Change-Id: I77634d05828756c46b802f9ec789ab767faca3cf --- .../diff/gr-diff-builder/gr-diff-builder.html | 22 ++--- .../diff/gr-diff-builder/gr-diff-builder.js | 60 +++++++++++++ .../gr-diff-builder/gr-diff-builder_test.html | 88 +++++++++++++++++++ 3 files changed, 153 insertions(+), 17 deletions(-) diff --git a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.html b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.html index 5f1fd90178..bf787088a3 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.html +++ b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.html @@ -142,10 +142,7 @@ limitations under the License. }, getContentByLine: function(lineNumber, opt_side, opt_root) { - var root = Polymer.dom(opt_root || this.diffElement); - var sideSelector = !!opt_side ? ('.' + opt_side) : ''; - return root.querySelector('td.lineNum[data-value="' + lineNumber + - '"]' + sideSelector + ' ~ td.content .contentText'); + return this._builder.getContentByLine(lineNumber, opt_side, opt_root); }, getContentByLineEl: function(lineEl) { @@ -162,19 +159,10 @@ limitations under the License. }, getContentsByLineRange: function(startLine, endLine, opt_side) { - var groups = - this._builder.getGroupsByLineRange(startLine, endLine, opt_side); - // In each group, search Element for lines in range. - return groups.reduce((function(acc, group) { - for (var line = startLine; line <= endLine; line++) { - var content = - this.getContentByLine(line, opt_side, group.element); - if (content) { - acc.push(content); - } - } - return acc; - }).bind(this), []); + var result = []; + this._builder.findLinesByRange(startLine, endLine, opt_side, null, + result); + return result; }, getCommentThreadByLine: function(lineNumber, opt_side, opt_root) { diff --git a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.js b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.js index 79188373f9..fb2d230a6e 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.js +++ b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder.js @@ -109,6 +109,66 @@ return groups; }; + GrDiffBuilder.prototype.getContentByLine = function(lineNumber, opt_side, + opt_root) { + var root = Polymer.dom(opt_root || this._outputEl); + var sideSelector = !!opt_side ? ('.' + opt_side) : ''; + return root.querySelector('td.lineNum[data-value="' + lineNumber + + '"]' + sideSelector + ' ~ td.content .contentText'); + }; + + /** + * Find line elements or line objects by a range of line numbers and a side. + * + * @param {Number} start The first line number + * @param {Number} end The last line number + * @param {String} opt_side The side of the range. Either 'left' or 'right'. + * @param {Array} out_lines The output list of line objects. Use + * null if not desired. + * @param {Array} out_elements The output list of line elements. + * Use null if not desired. + */ + GrDiffBuilder.prototype.findLinesByRange = function(start, end, opt_side, + out_lines, out_elements) { + var groups = this.getGroupsByLineRange(start, end, opt_side); + + groups.forEach(function(group) { + group.lines.forEach(function(line) { + if ((opt_side === 'left' && line.type === GrDiffLine.Type.ADD) || + (opt_side === 'right' && line.type === GrDiffLine.Type.REMOVE)) { + return; + } + var lineNumber = opt_side === 'left' ? + line.beforeNumber : line.afterNumber; + if (lineNumber < start || lineNumber > end) { return; } + + if (out_lines) { out_lines.push(line); } + if (out_elements) { + var content = this.getContentByLine(lineNumber, opt_side, + group.element); + if (content) { out_elements.push(content); } + } + }.bind(this)); + }.bind(this)); + }; + + /** + * Re-renders the DIV.contentText alement for the given side and range of diff + * content. + */ + GrDiffBuilder.prototype._renderContentByRange = function(start, end, side) { + var lines = []; + var elements = []; + var line; + var el; + this.findLinesByRange(start, end, side, lines, elements); + for (var i = 0; i < lines.length; i++) { + line = lines[i]; + el = elements[i]; + el.parentElement.replaceChild(this._createTextEl(line).firstChild, el); + } + }; + GrDiffBuilder.prototype.getSectionsByLineRange = function( startLine, endLine, opt_side) { return this.getGroupsByLineRange(startLine, endLine, opt_side).map( diff --git a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder_test.html b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder_test.html index 727ae9bc89..12c8f3c5ca 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder_test.html +++ b/polygerrit-ui/app/elements/diff/gr-diff-builder/gr-diff-builder_test.html @@ -23,7 +23,9 @@ limitations under the License. + + @@ -40,6 +42,14 @@ limitations under the License. + + + +