Skip to content

Commit

Permalink
Do not HTML-escape the markup for ellipses in html formatter (rubocop…
Browse files Browse the repository at this point in the history
…#3217)

There was a bug in the formatter causing HTML tags to appear
on the generated page when <span class="extra-code">...</span>
was added for offense locations spanning multiple lines.
  • Loading branch information
jonas054 authored and Neodelf committed Oct 15, 2016
1 parent 06fd4de commit adcde46
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* [#3157](https://github.com/bbatsov/rubocop/issues/3157): Don't let `LineEndConcatenation` and `UnneededInterpolation` make changes to the same string during auto-correct. ([@jonas054][])
* [#3187](https://github.com/bbatsov/rubocop/issues/3187): Let `Style/BlockDelimiters` ignore blocks in *all* method arguments. ([@jonas054][])
* Modify `Style/ParallelAssignment` to use implicit begins when parallel assignment uses a `rescue` modifier and is the only thing in the method. ([@rrosenblum][])
* [#3217](https://github.com/bbatsov/rubocop/pull/3217): Fix output of ellipses for multi-line offense ranges in HTML formatter. ([@jonas054][])

### Changes

Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/formatter/html_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,14 @@ def decorated_message(offense)

def highlighted_source_line(offense)
location = offense.location

source_line = if location.first_line == location.last_line
location.source_line
else
"#{location.source_line} #{ELLIPSES}"
end
source_line = location.source_line

escape(source_line[0...offense.highlighted_area.begin_pos]) +
"<span class=\"highlight #{offense.severity}\">" +
escape(offense.highlighted_area.source) +
'</span>' +
escape(source_line[offense.highlighted_area.end_pos..-1])
escape(source_line[offense.highlighted_area.end_pos..-1]) +
(location.first_line == location.last_line ? '' : " #{ELLIPSES}")
end

def escape(s)
Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/html_formatter/expected.html
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ <h1 class="title">RuboCop Inspection Report</h1>
<span class="message">Inconsistent indentation detected.</span>
</div>

<pre><code> <span class="highlight convention">def set_book</span> &lt;span class=&quot;extra-code&quot;&gt;...&lt;/span&gt;</code></pre>
<pre><code> <span class="highlight convention">def set_book</span> <span class="extra-code">...</span></code></pre>

</div>

Expand All @@ -494,7 +494,7 @@ <h1 class="title">RuboCop Inspection Report</h1>
<span class="message">Inconsistent indentation detected.</span>
</div>

<pre><code> <span class="highlight convention">def book_params</span> &lt;span class=&quot;extra-code&quot;&gt;...&lt;/span&gt;</code></pre>
<pre><code> <span class="highlight convention">def book_params</span> <span class="extra-code">...</span></code></pre>

</div>

Expand Down

0 comments on commit adcde46

Please sign in to comment.