2016-01-15 10:02:48 -05:00
|
|
|
module Gitlab
|
|
|
|
module Diff
|
|
|
|
class InlineDiffMarker
|
2016-04-06 15:37:09 -04:00
|
|
|
MARKDOWN_SYMBOLS = {
|
|
|
|
addition: "+",
|
|
|
|
deletion: "-"
|
2017-02-21 18:32:18 -05:00
|
|
|
}.freeze
|
2016-04-06 15:37:09 -04:00
|
|
|
|
2016-01-15 10:02:48 -05:00
|
|
|
attr_accessor :raw_line, :rich_line
|
|
|
|
|
|
|
|
def initialize(raw_line, rich_line = raw_line)
|
|
|
|
@raw_line = raw_line
|
2016-01-29 13:37:17 -05:00
|
|
|
@rich_line = ERB::Util.html_escape(rich_line)
|
2016-01-15 10:02:48 -05:00
|
|
|
end
|
|
|
|
|
2016-04-06 15:37:09 -04:00
|
|
|
def mark(line_inline_diffs, mode: nil, markdown: false)
|
2016-01-29 13:37:17 -05:00
|
|
|
return rich_line unless line_inline_diffs
|
|
|
|
|
2016-01-20 12:44:27 -05:00
|
|
|
marker_ranges = []
|
2016-01-15 10:02:48 -05:00
|
|
|
line_inline_diffs.each do |inline_diff_range|
|
2016-01-15 10:29:26 -05:00
|
|
|
# Map the inline-diff range based on the raw line to character positions in the rich line
|
2016-01-20 09:26:44 -05:00
|
|
|
inline_diff_positions = position_mapping[inline_diff_range].flatten
|
2016-01-15 10:29:26 -05:00
|
|
|
# Turn the array of character positions into ranges
|
2016-01-20 12:44:27 -05:00
|
|
|
marker_ranges.concat(collapse_ranges(inline_diff_positions))
|
|
|
|
end
|
2016-01-15 10:02:48 -05:00
|
|
|
|
2016-01-20 12:44:27 -05:00
|
|
|
offset = 0
|
2016-01-29 13:37:17 -05:00
|
|
|
|
2016-04-06 15:37:09 -04:00
|
|
|
# Mark each range
|
|
|
|
marker_ranges.each_with_index do |range, index|
|
2016-05-18 14:20:46 -04:00
|
|
|
before_content =
|
|
|
|
if markdown
|
|
|
|
"{#{MARKDOWN_SYMBOLS[mode]}"
|
|
|
|
else
|
|
|
|
"<span class='#{html_class_names(marker_ranges, mode, index)}'>"
|
|
|
|
end
|
|
|
|
after_content =
|
|
|
|
if markdown
|
|
|
|
"#{MARKDOWN_SYMBOLS[mode]}}"
|
|
|
|
else
|
|
|
|
"</span>"
|
|
|
|
end
|
2016-04-06 15:37:09 -04:00
|
|
|
offset = insert_around_range(rich_line, range, before_content, after_content, offset)
|
2016-01-15 10:02:48 -05:00
|
|
|
end
|
|
|
|
|
2016-01-29 13:37:17 -05:00
|
|
|
rich_line.html_safe
|
2016-01-15 10:02:48 -05:00
|
|
|
end
|
|
|
|
|
2016-01-15 10:29:26 -05:00
|
|
|
private
|
|
|
|
|
2016-04-06 15:37:09 -04:00
|
|
|
def html_class_names(marker_ranges, mode, index)
|
|
|
|
class_names = ["idiff"]
|
|
|
|
class_names << "left" if index == 0
|
|
|
|
class_names << "right" if index == marker_ranges.length - 1
|
|
|
|
class_names << mode if mode
|
|
|
|
class_names.join(" ")
|
|
|
|
end
|
|
|
|
|
2016-01-15 10:29:26 -05:00
|
|
|
# Mapping of character positions in the raw line, to the rich (highlighted) line
|
2016-01-15 10:02:48 -05:00
|
|
|
def position_mapping
|
|
|
|
@position_mapping ||= begin
|
|
|
|
mapping = []
|
|
|
|
rich_pos = 0
|
|
|
|
(0..raw_line.length).each do |raw_pos|
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
|
2016-01-15 10:29:26 -05:00
|
|
|
# The raw and rich lines are the same except for HTML tags,
|
|
|
|
# so skip over any `<...>` segment
|
2016-01-15 10:02:48 -05:00
|
|
|
while rich_char == '<'
|
|
|
|
until rich_char == '>'
|
|
|
|
rich_pos += 1
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
end
|
|
|
|
|
|
|
|
rich_pos += 1
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
end
|
|
|
|
|
2016-01-20 09:26:44 -05:00
|
|
|
# multi-char HTML entities in the rich line correspond to a single character in the raw line
|
|
|
|
if rich_char == '&'
|
|
|
|
multichar_mapping = [rich_pos]
|
|
|
|
until rich_char == ';'
|
|
|
|
rich_pos += 1
|
|
|
|
multichar_mapping << rich_pos
|
|
|
|
rich_char = rich_line[rich_pos]
|
|
|
|
end
|
|
|
|
|
|
|
|
mapping[raw_pos] = multichar_mapping
|
|
|
|
else
|
|
|
|
mapping[raw_pos] = rich_pos
|
|
|
|
end
|
2016-01-15 10:02:48 -05:00
|
|
|
|
|
|
|
rich_pos += 1
|
|
|
|
end
|
|
|
|
|
|
|
|
mapping
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-15 10:29:26 -05:00
|
|
|
# Takes an array of integers, and returns an array of ranges covering the same integers
|
2016-01-15 10:02:48 -05:00
|
|
|
def collapse_ranges(positions)
|
|
|
|
return [] if positions.empty?
|
|
|
|
ranges = []
|
|
|
|
|
|
|
|
start = prev = positions[0]
|
|
|
|
range = start..prev
|
|
|
|
positions[1..-1].each do |pos|
|
|
|
|
if pos == prev + 1
|
|
|
|
range = start..pos
|
|
|
|
prev = pos
|
|
|
|
else
|
|
|
|
ranges << range
|
|
|
|
start = prev = pos
|
|
|
|
range = start..prev
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ranges << range
|
|
|
|
|
|
|
|
ranges
|
|
|
|
end
|
|
|
|
|
2016-01-15 10:29:26 -05:00
|
|
|
# Inserts tags around the characters identified by the given range
|
2016-01-15 10:02:48 -05:00
|
|
|
def insert_around_range(text, range, before, after, offset = 0)
|
2016-01-20 13:33:34 -05:00
|
|
|
# Just to be sure
|
|
|
|
return offset if offset + range.end + 1 > text.length
|
|
|
|
|
2016-01-15 10:02:48 -05:00
|
|
|
text.insert(offset + range.begin, before)
|
|
|
|
offset += before.length
|
|
|
|
|
|
|
|
text.insert(offset + range.end + 1, after)
|
|
|
|
offset += after.length
|
|
|
|
|
|
|
|
offset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|