-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket-pull-request-helper.js
159 lines (145 loc) · 4.83 KB
/
bitbucket-pull-request-helper.js
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
// ==UserScript==
// @name Bitbucket Pull-Request Helper
// @namespace bitbucket
// @description Show whitespaces in Bitbucket pull-requests
// @grant none
// @author sgrigorjev
// @license MIT
// @version 1.1.0
// @include https://bitbucket.org/*
// ==/UserScript==
(function(window, undefined){
var whitespaceStyle = 'color:#999; line-height: inherit';
var YAMLErrorLineColor = 'red';
var initInterval = 200;
var expandSectionInterval = 100;
var codeBlockMask = 'section.commentable-diff, section.bb-udiff';
var expandBtnMask = 'span.ellipsis';
var files = {};
function encodeLine() {
var i = 1,
state = true,
source = this.innerHTML,
leadsymbol = source[0],
leadspaces = 0,
leadstabs = 0,
opentag = null,
closetag = null,
repLength = 0;
repSource = '';
do {
switch (source[i]) {
case '\t':
repSource += '\u2192 ';
repLength++;
leadstabs++;
i++;
break;
case ' ':
repSource += '\u00B7';
repLength++;
leadspaces++;
i++;
break;
case '<':
closetag = source.substring(i, 6);
opentag = closetag.substring(0, 5);
if (opentag === '<ins>' || opentag === '<del>') {
repSource += opentag;
repLength += 5;
i += 5;
} else if (closetag === '</ins>' || closetag === '</del>') {
repSource += closetag;
repLength += 6;
i += 6;
} else {
state = false;
}
break;
default:
state = false;
break;
}
} while(state);
if (repLength > 0) {
this.innerHTML = leadsymbol + '<span style="' + whitespaceStyle + '">' + repSource + '</span>' + source.substring(repLength + 1);
}
this.setAttribute('data-encoded', 'true');
return {
leadsymbol: leadsymbol,
leadstabs: leadstabs,
leadspaces: leadspaces
};
}
function encodeSection($section) {
$section.find('pre.source:not([data-encoded])').each(function(){
var filename = $section.attr('id');
var file = files[filename];
var info = encodeLine.call(this);
file.leadstabs += info.leadstabs;
file.leadspaces += info.leadspaces;
// highlight tabs in YAML files
if (info.leadstabs > 0 && /\.yml$/.test(filename)) {
$(this).siblings('.gutter').css('background-color', YAMLErrorLineColor);
}
});
}
function init() {
$(codeBlockMask).each(function(){
var $section = $(this);
files[$section.attr('id')] = {
leadstabs: 0,
leadspaces: 0
};
encodeSection($section);
$section.on('click', expandBtnMask, function(){
waitExpandSection($section, $section.height());
});
});
}
/**
* @deprecated
*/
function waitInit() {
window.setTimeout(function(){
if ($(codeBlockMask).length) {
init();
} else {
waitInit();
}
}, initInterval);
}
function waitExpandSection($section, height) {
window.setTimeout(function(){
if ($section.height() > height) {
encodeSection($section);
} else {
waitExpandSection($section, height);
}
}, expandSectionInterval);
}
switch (true) {
// pull-request page
case /^\/.+\/.+\/pull-requests\/\d+/.test(window.location.pathname):
$('#pr-tab-content').on('diff:loaded', function(){
init();
});
break;
// new pull-request page
case /^\/.+\/.+\/pull-requests\/new/.test(window.location.pathname):
$(document).on("ajaxComplete", function(event, response, info){
if (/^\/.+\/.+\/compare\//.test(info.url)) {
init();
}
});
break;
// branch page
case /^\/.+\/.+\/branch\/.+/.test(window.location.pathname):
$(document).on("ajaxComplete", function(event, response, info){
if (/^\/.+\/.+\/branches\/diff\/.+/.test(info.url)) {
init();
}
});
break;
}
})(window);