-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdiff-highlighter.coffee
More file actions
454 lines (358 loc) · 15.8 KB
/
diff-highlighter.coffee
File metadata and controls
454 lines (358 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# Mike Solomon 2014
## Helper functions
logMessages = (messages...) ->
# console.log '[GitHub diff highlighter]', messages...
longestCommonPrefix = (strings...) ->
prefix = []
firstString = strings[0]
combinePrefix = => prefix.join('')
if strings.length < 2
return combinePrefix()
for i in [0...firstString.length]
for string in strings[1..]
if string[i] != firstString[i]
return combinePrefix()
prefix.push firstString[i]
return combinePrefix()
dropNonexisting = (array) ->
(element for element in array when element?) || []
notEmpty = (array) ->
if array.length > 0 then array else null
unique = (array) ->
array.filter((value, index, self) -> self.indexOf(value) == index)
addedElements = (oldArray, newArray) ->
for element in newArray
if oldArray.indexOf(element) == -1
return true
return false
urlEncodeFilePath = (filePath) ->
encodeURIComponent(filePath).replace(/%2F/g, '/')
# An HTML tag that is being merged in with text and other HTML tags
class HtmlTag
tagTypeRegex = /<(\/?)\s*(\w+).*?(\/?)\w*>/
constructor: (@tag) ->
match = tagTypeRegex.exec(@tag)
@isClosing = (match[1] || null)?
@tagType = match[2].toLowerCase()
@isSelfContained = (match[3] || null)?
isClosingTag: -> @isClosing
isOpeningTag: -> !@isClosingTag() and !@isSelfContained
canClose: (otherTag) -> @tagType == otherTag.tagType
getText: -> @tag
getClosingTagText: -> "</#{@tagType}>"
# A stack of HtmlTags to assist merging two strings of tags
class TagStack
constructor: ->
@tagStack = []
addTag: (tag) ->
if tag.isClosingTag()
openingTag = @tagStack.pop()
unless tag.canClose(openingTag) and openingTag.isOpeningTag()
throw 'Tags do not match: ' + tag.tag + " cannot close " + openingTag.tag
else
@tagStack.push tag
addTagFromStack: (tag, stack) ->
tag.fromStack = stack
@addTag tag
calculateTagDepth: (index) -> @tagStack.length - index
getPrematurelyClosedTagsIndex: (closingTag) ->
if closingTag.isClosingTag() and @tagStack.length > 0
for tag, i in @tagStack by -1
if closingTag.canClose tag
return i
return null
getPrematurelyClosedTagsFromIndex: (index) ->
return @tagStack.slice(index).reverse()
getPrematurelyClosedTags: (tag, notEarlyStack, earlyStack) ->
if tag.isClosingTag()
earlyStackTagIndex = earlyStack.getPrematurelyClosedTagsIndex tag
if earlyStackTagIndex?
notEarlyStackTagIndex = notEarlyStack.getPrematurelyClosedTagsIndex tag
if notEarlyStackTagIndex?
if earlyStack.calculateTagDepth(earlyStackTagIndex) >= notEarlyStack.calculateTagDepth(notEarlyStackTagIndex)
return earlyStack.getPrematurelyClosedTagsFromIndex earlyStackTagIndex
return []
# Splits an HTML line into tags and text while tracking the current position
class HtmlLineSplitter
constructor: (@line) ->
@tagRegex = /(<[^>]*?>)|([^<]*)/g
@advance()
advance: ->
@top = @tagRegex.exec(@line)
getContent: -> @top[0] || null
setContent: (content) -> @top[0] = content
getTag: -> new HtmlTag(@getContent())
popContent: (value) ->
value = @getContent()
@advance()
value
isTag: -> (@top[1] || null)?
isText: -> (@top[2] || null)?
hasContent: -> @getContent()?
isClosingTag: -> @isTag() and @getContent().indexOf('/') != -1
removeTextPrefix: (prefix) ->
previous = @getContent()
if previous.length == prefix.length
@advance()
else
@setContent previous.substr(prefix.length)
# merge two HTML strings that differ only by HTML tags
class LineMerger
constructor: (highlightedLine, diffLine) ->
@highlighted = new HtmlLineSplitter highlightedLine
@diff = new HtmlLineSplitter diffLine
@highlightedStack = new TagStack()
@diffStack = new TagStack()
@output = []
appendPopped: (splitter) -> @output.push splitter.popContent()
mergeTextIntoOutput: ->
prefix = longestCommonPrefix(@highlighted.getContent(), @diff.getContent())
if prefix.length > 0
@output.push prefix
@highlighted.removeTextPrefix prefix
@diff.removeTextPrefix prefix
else
throw 'Could not find prefix:\n' + @highlighted.getContent() + '\n' + @diff.getContent() + '\n'
mergeTagIntoOutput: (splitter, stack) ->
otherStack = if stack isnt @diffStack then @diffStack else @highlightedStack
tag = splitter.getTag()
prematurelyClosedTags = stack.getPrematurelyClosedTags tag, stack, otherStack
stack.addTag(tag)
@output.push t.getClosingTagText() for t in prematurelyClosedTags
if tag.tagType == 'br'
splitter.popContent()
else
@appendPopped(splitter)
@output.push t.getText() for t in prematurelyClosedTags
combineIntoHtml: ->
while @highlighted.hasContent() or @diff.hasContent()
# merge tags in before anything else
if @highlighted.isTag()
@mergeTagIntoOutput @highlighted, @highlightedStack
else if @diff.isTag()
@mergeTagIntoOutput @diff, @diffStack
# merge in text common to both stacks
else if @highlighted.isText() and @diff.isText()
@mergeTextIntoOutput()
# dump the rest in at the end
else if @highlighted.hasContent()
@appendPopped(@highlighted)
else if @diff.hasContent()
@appendPopped(@diff)
@output.join('')
# A text file made of Lines
class File
constructor: (@path) ->
@lines = []
storeLine: (lineElement, line, lineNumber) ->
line = new Line(lineElement, line, lineNumber)
@lines[lineNumber] = line
line
storeDiffLine: (lineElement, line, lineNumberPrevious, lineNumberCurrent) ->
storedLine = new Line(lineElement, line, lineNumberCurrent || lineNumberPrevious)
storedLine.setIsDiff lineNumberPrevious
@lines.push storedLine
storedLine
getLine: (index) -> @lines[index]
# A line in a text file
class Line
nonBreakingSpaceRegex = / /g
constructor: (@lineElement, @line, @lineNumber) ->
@diffMarker = ''
setIsDiff: (@lineNumberPrevious)->
@line = @line.replace(nonBreakingSpaceRegex, ' ')
@diffMarker = @line.substr(0, 1)
@line = @line.substr(1)
diffAdded: -> @diffMarker == '+'
diffRemoved: -> @diffMarker == '-'
diffUnchanged: -> !@diffAdded() && !@diffRemoved() && @diffMarker != '@'
# Fetches, stores, and highlights diffs on a GitHub page
class DiffProcessor
endsInShaRegex = /[0-9a-fA-F]{40}$/
binRegex = /bin/i
permalinkShasRegex = /compare\/[^:]*:([^.]*)...[^:]*:([^\/]*)/
constructor: ->
@currentRepoPath = ''
@updateCurrentRepoPath()
@currentCommitIdentifier = ''
@parentCommitIdentifiers = ''
@updateCommitIdentifiers()
@changedFilePaths = []
@updateChangedFilePaths()
@diffData = @getRegularDiffData()
@parentData = {}
@currentData = {}
@mutationObserver = @getMutationObserver()
getMutationObserver: ->
mutationObserver = new MutationObserver @refreshDataAndHighlight
observerConfig = {childList: true, characterData: true, subtree: true}
mutationObserver.observe document, observerConfig
mutationObserver
getPartialShaFromMergingPermalink: (index) ->
document.querySelector('a.js-permalink-shortcut')?.href?.match(permalinkShasRegex)?[index + 1]
getMergingBranchCommitIdentifier: (index) ->
element = document.querySelectorAll('#js-discussion-header .gh-header-meta span.commit-ref.current-branch span')[index] ||
document.querySelectorAll('.branch-name span.js-selectable-text')[index]
element?.textContent?.trim()
getGuessAtCurrentCommitIdentifier: ->
result = @getMergingBranchFromFromComment()
result ||= document.querySelector('.commit a')?.href?.match(/[a-f0-9]{40}$/)?[0]
result ||= @getPartialShaFromMergingPermalink 1
result ||= @getMergingBranchCommitIdentifier 1
result || document.body.innerHTML.match(/commit\/([a-f0-9]{40})/)?[1]
getMergingBranchFromFromComment: ->
result = /head sha1: "([0-9a-fA-F]{40})"/.exec(document.body.innerHTML)?[1]
getMergingBranchToFromComment: ->
result = /base sha1: "([0-9a-fA-F]{40})"/.exec(document.body.innerHTML)?[1]
getMergingBranchTo: -> @getMergingBranchToFromComment()
getMergingBranchFrom: -> @getMergingBranchFromFromComment()
getParentCommitIdentifiers: ->
notEmpty(dropNonexisting([@getMergingBranchTo()])) ||
notEmpty(endsInShaRegex.exec(e.href)?[0] for e in document.querySelectorAll('.commit-meta .sha-block a.sha')) ||
dropNonexisting([@getPartialShaFromMergingPermalink 0]) ||
dropNonexisting([@getMergingBranchCommitIdentifier 0])
updateCurrentRepoPath: ->
changed = false
currentRepoPath = window.location.pathname.match(/\/([^\/]*\/[^\/]*)/)[1]
if currentRepoPath != @currentRepoPath
@currentRepoPath = currentRepoPath
changed = true
changed
updateCommitIdentifiers: ->
changed = false
currentCommitIdentifier = @getGuessAtCurrentCommitIdentifier()
if currentCommitIdentifier != @currentCommitIdentifier
@currentCommitIdentifier = currentCommitIdentifier
changed = true
parentCommitIdentifiers = @getParentCommitIdentifiers()
if parentCommitIdentifiers.toString() != @parentCommitIdentifiers.toString()
@parentCommitIdentifiers = parentCommitIdentifiers
changed = true
changed
updateChangedFilePaths: ->
changed = false
changedFilePaths = @getChangedFilePaths()
if addedElements(@changedFilePaths, changedFilePaths)
changed = true
@changedFilePaths = changedFilePaths
changed
getChangedFilePaths: ->
changedFileLinks = document.querySelectorAll('.file .info span.js-selectable-text')
for link in changedFileLinks
if binRegex.test link.parentElement?.childNodes[1].textContent
continue # exclude binary files
path = link.textContent?.trim()
if path?.indexOf('→') != -1
# file was renamed. we don't usually have enough info to get the LHS of the diff, unfortunately
path = link.parentElement?.parentElement?.getAttribute('data-path')
path
getLinesToMerge: (filePath, line) ->
if line.diffAdded() or line.diffUnchanged()
dropNonexisting [@currentData[filePath]?.getLine(line.lineNumber)]
else if line.diffRemoved()
dropNonexisting (@parentData[id]?[filePath]?.getLine(line.lineNumberPrevious) for id in @parentCommitIdentifiers)
else
[]
highlight: ->
@highlightDiffData @diffData
highlightDiffData: (diffData) ->
for filePath, fileList of diffData
for file in fileList
for key, line of file.lines
if line?.lineElement.getAttribute('github-diff-highlighter-highlighted')?
file[key] = null
continue
for mergeLine in @getLinesToMerge filePath, line
try
mergeLineText = mergeLine.line.replace / /g, ' '
line.lineElement.innerHTML = line.diffMarker + new LineMerger(line.line, mergeLineText).combineIntoHtml()
line.lineElement.setAttribute('github-diff-highlighter-highlighted', true)
break
catch e
# console.log e
fetchPageHtml: (url, callback) ->
xhr = new XMLHttpRequest()
xhr.onerror = =>
logMessages xhr.status, xhr
xhr.onload = =>
if xhr.status == 200
callback null, xhr.responseText
@highlight()
else
xhr.onerror xhr
xhr.open "GET", url
xhr.send()
buildBlobPrefixFromCommitIdentifier: (sha) -> "https://github.com/#{@currentRepoPath}/tree/#{sha}/"
buildBlobPrefixFromCommitUrl: (commitUrl) -> @buildCommitPathFromSha @endsInShaRegex.exec(commitUrl)[0]
fetchCurrentHtml: ->
blobPrefix = @buildBlobPrefixFromCommitIdentifier(@currentCommitIdentifier)
for filePath in @changedFilePaths
@currentData[filePath] = file = new File(filePath)
@fetchPageHtml blobPrefix + urlEncodeFilePath(filePath), @getStoreHtml(file)
getStoreHtml: (htmlFile) ->
(error, html) =>
if error
return
files = @getFilesFromHtmlText html
for file in files
for line in file.querySelectorAll('.line')
lineNumber = parseInt(line.id.substr(2))
lineContents = line.innerHTML
htmlFile.storeLine line, lineContents, lineNumber
fetchParentHtml: ->
for parent in @parentCommitIdentifiers
blobPrefix = @buildBlobPrefixFromCommitIdentifier(parent)
@parentData[parent] ||= {}
for filePath in @changedFilePaths
@parentData[parent][filePath] = file = new File(filePath)
@fetchPageHtml blobPrefix + urlEncodeFilePath(filePath), @getStoreHtml(file)
getFilesFromHtmlText: (htmlText) ->
asHtml = document.createElement('html')
asHtml.innerHTML = htmlText
asHtml.querySelectorAll('.file')
getRegularDiffData: ->
@getDiffData document.querySelectorAll('.file'), @changedFilePaths
getDiffData: (changedFileElements, changedFilePaths) ->
diffData = {}
for path, i in changedFilePaths
diffData[path] ||= []
file = new File(path)
diffData[path].push file
lines = changedFileElements[i]?.querySelectorAll('.file-diff-line')
if lines?
for line in lines
lineNumberElements = line.querySelectorAll('.diff-line-num')
[lineNumberPrevious, lineNumberCurrent] = (parseInt(e.getAttribute('data-line-number')) for e in lineNumberElements)
lineNumberCurrent ||= parseInt(line.getAttribute('data-line'))
lineContainer = line.querySelector('.diff-line-code')
if lineContainer
lineContents = lineContainer.innerText
file.storeDiffLine lineContainer, lineContents, lineNumberPrevious, lineNumberCurrent
diffData
fetchAndHighlight: ->
@fetchCurrentHtml()
@fetchParentHtml()
refreshDataAndHighlight: (mutations) =>
changed = @updateCurrentRepoPath()
changed ||= @updateChangedFilePaths()
changed ||= @updateCommitIdentifiers()
if changed
# we appear to be on a new page. reset entirely
@mutationObserver.disconnect()
window.diffProcessor.constructor()
window.diffProcessor.fetchAndHighlight()
return
@diffData = @getRegularDiffData()
@highlight()
## Bootstrap and run
# fix GitHub's css to not force black text on character-by-character diffs
style = document.createElement('style')
style.type = 'text/css'
style.innerHTML = '''
.highlight span.x {
color: inherit !important;
}
'''
document.getElementsByTagName('head')[0].appendChild(style)
window.diffProcessor = new DiffProcessor()
window.diffProcessor.fetchAndHighlight()