This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
jscoverage.js
1024 lines (915 loc) · 28.9 KB
/
jscoverage.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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
jscoverage.js - code coverage for JavaScript
Copyright (C) 2007, 2008 siliconforks.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
Initializes the _$jscoverage object in a window. This should be the first
function called in the page.
@param w this should always be the global window object
*/
function jscoverage_init(w) {
try {
// in Safari, "import" is a syntax error
Components.utils['import']('resource://gre/modules/jscoverage.jsm');
jscoverage_isInvertedMode = true;
return;
}
catch (e) {}
if (w.opener && w.opener.top._$jscoverage) {
// we are in inverted mode
jscoverage_isInvertedMode = true;
if (! w._$jscoverage) {
w._$jscoverage = w.opener.top._$jscoverage;
}
}
else {
// we are not in inverted mode
jscoverage_isInvertedMode = false;
if (! w._$jscoverage) {
w._$jscoverage = {};
}
}
}
var jscoverage_currentFile = null;
var jscoverage_currentLine = null;
var jscoverage_inLengthyOperation = false;
/*
Possible states:
isInvertedMode isServer isReport tabs
normal false false false Browser
inverted true false false
server, normal false true false Browser, Store
server, inverted true true false Store
report false false true
*/
var jscoverage_isInvertedMode = false;
var jscoverage_isServer = false;
var jscoverage_isReport = false;
jscoverage_init(window);
function jscoverage_createRequest() {
// Note that the IE7 XMLHttpRequest does not support file URL's.
// http://xhab.blogspot.com/2006/11/ie7-support-for-xmlhttprequest.html
// http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx
//#JSCOVERAGE_IF
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
else {
return new XMLHttpRequest();
}
}
// http://www.quirksmode.org/js/findpos.html
function jscoverage_findPos(obj) {
var result = 0;
do {
result += obj.offsetTop;
obj = obj.offsetParent;
}
while (obj);
return result;
}
// http://www.quirksmode.org/viewport/compatibility.html
function jscoverage_getViewportHeight() {
//#JSCOVERAGE_IF /MSIE/.test(navigator.userAgent)
if (self.innerHeight) {
// all except Explorer
return self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight) {
// Explorer 6 Strict Mode
return document.documentElement.clientHeight;
}
else if (document.body) {
// other Explorers
return document.body.clientHeight;
}
else {
throw "Couldn't calculate viewport height";
}
//#JSCOVERAGE_ENDIF
}
/**
Indicates visually that a lengthy operation has begun. The progress bar is
displayed, and the cursor is changed to busy (on browsers which support this).
*/
function jscoverage_beginLengthyOperation() {
jscoverage_inLengthyOperation = true;
var progressBar = document.getElementById('progressBar');
progressBar.style.visibility = 'visible';
ProgressBar.setPercentage(progressBar, 0);
var progressLabel = document.getElementById('progressLabel');
progressLabel.style.visibility = 'visible';
/* blacklist buggy browsers */
//#JSCOVERAGE_IF
if (! /Opera|WebKit/.test(navigator.userAgent)) {
/*
Change the cursor style of each element. Note that changing the class of the
element (to one with a busy cursor) is buggy in IE.
*/
var tabs = document.getElementById('tabs').getElementsByTagName('div');
var i;
for (i = 0; i < tabs.length; i++) {
tabs.item(i).style.cursor = 'wait';
}
}
}
/**
Removes the progress bar and busy cursor.
*/
function jscoverage_endLengthyOperation() {
var progressBar = document.getElementById('progressBar');
ProgressBar.setPercentage(progressBar, 100);
setTimeout(function() {
jscoverage_inLengthyOperation = false;
progressBar.style.visibility = 'hidden';
var progressLabel = document.getElementById('progressLabel');
progressLabel.style.visibility = 'hidden';
progressLabel.innerHTML = '';
var tabs = document.getElementById('tabs').getElementsByTagName('div');
var i;
for (i = 0; i < tabs.length; i++) {
tabs.item(i).style.cursor = '';
}
}, 50);
}
function jscoverage_setSize() {
//#JSCOVERAGE_IF /MSIE/.test(navigator.userAgent)
var viewportHeight = jscoverage_getViewportHeight();
/*
border-top-width: 1px
padding-top: 10px
padding-bottom: 10px
border-bottom-width: 1px
margin-bottom: 10px
----
32px
*/
var tabPages = document.getElementById('tabPages');
var tabPageHeight = (viewportHeight - jscoverage_findPos(tabPages) - 32) + 'px';
var nodeList = tabPages.childNodes;
var length = nodeList.length;
for (var i = 0; i < length; i++) {
var node = nodeList.item(i);
if (node.nodeType !== 1) {
continue;
}
node.style.height = tabPageHeight;
}
var iframeDiv = document.getElementById('iframeDiv');
// may not exist if we have removed the first tab
if (iframeDiv) {
iframeDiv.style.height = (viewportHeight - jscoverage_findPos(iframeDiv) - 21) + 'px';
}
var summaryDiv = document.getElementById('summaryDiv');
summaryDiv.style.height = (viewportHeight - jscoverage_findPos(summaryDiv) - 21) + 'px';
var sourceDiv = document.getElementById('sourceDiv');
sourceDiv.style.height = (viewportHeight - jscoverage_findPos(sourceDiv) - 21) + 'px';
var storeDiv = document.getElementById('storeDiv');
if (storeDiv) {
storeDiv.style.height = (viewportHeight - jscoverage_findPos(storeDiv) - 21) + 'px';
}
//#JSCOVERAGE_ENDIF
}
/**
Returns the boolean value of a string. Values 'false', 'f', 'no', 'n', 'off',
and '0' (upper or lower case) are false.
@param s the string
@return a boolean value
*/
function jscoverage_getBooleanValue(s) {
s = s.toLowerCase();
if (s === 'false' || s === 'f' || s === 'no' || s === 'n' || s === 'off' || s === '0') {
return false;
}
return true;
}
function jscoverage_removeTab(id) {
var tab = document.getElementById(id + 'Tab');
tab.parentNode.removeChild(tab);
var tabPage = document.getElementById(id + 'TabPage');
tabPage.parentNode.removeChild(tabPage);
}
/**
Initializes the contents of the tabs. This sets the initial values of the
input field and iframe in the "Browser" tab and the checkbox in the "Summary"
tab.
@param queryString this should always be location.search
*/
function jscoverage_initTabContents(queryString) {
var showMissingColumn = false;
var parameters, parameter, i, index, url, name, value;
if (queryString.length > 0) {
// chop off the question mark
queryString = queryString.substring(1);
parameters = queryString.split(/&|;/);
for (i = 0; i < parameters.length; i++) {
parameter = parameters[i];
index = parameter.indexOf('=');
if (index === -1) {
// still works with old syntax
url = parameter;
}
else {
name = parameter.substr(0, index);
value = parameter.substr(index + 1);
if (name === 'missing' || name === 'm') {
showMissingColumn = jscoverage_getBooleanValue(value);
}
else if (name === 'url' || name === 'u') {
url = value;
}
}
}
}
var checkbox = document.getElementById('checkbox');
checkbox.checked = showMissingColumn;
if (showMissingColumn) {
jscoverage_appendMissingColumn();
}
// this will automatically propagate to the input field
if (url) {
frames[0].location = url;
}
// if the browser tab is absent, we have to initialize the summary tab
if (! document.getElementById('browserTab')) {
jscoverage_recalculateSummaryTab();
}
}
function jscoverage_body_load() {
var progressBar = document.getElementById('progressBar');
ProgressBar.init(progressBar);
function reportError(e) {
jscoverage_endLengthyOperation();
var summaryThrobber = document.getElementById('summaryThrobber');
summaryThrobber.style.visibility = 'hidden';
var div = document.getElementById('summaryErrorDiv');
div.innerHTML = 'Error: ' + e;
}
if (jscoverage_isReport) {
jscoverage_beginLengthyOperation();
var summaryThrobber = document.getElementById('summaryThrobber');
summaryThrobber.style.visibility = 'visible';
var request = jscoverage_createRequest();
try {
request.open('GET', 'jscoverage.json', true);
request.onreadystatechange = function (event) {
if (request.readyState === 4) {
try {
if (request.status !== 0 && request.status !== 200) {
throw request.status;
}
var response = request.responseText;
if (response === '') {
throw 404;
}
var json = eval('(' + response + ')');
var file;
for (file in json) {
var fileCoverage = json[file];
_$jscoverage[file] = fileCoverage.coverage;
_$jscoverage[file].source = fileCoverage.source;
}
jscoverage_recalculateSummaryTab();
summaryThrobber.style.visibility = 'hidden';
}
catch (e) {
reportError(e);
}
}
};
request.send(null);
}
catch (e) {
reportError(e);
}
jscoverage_removeTab('browser');
jscoverage_removeTab('store');
}
else {
if (jscoverage_isInvertedMode) {
jscoverage_removeTab('browser');
}
if (! jscoverage_isServer) {
jscoverage_removeTab('store');
}
}
jscoverage_initTabControl();
jscoverage_initTabContents(location.search);
}
function jscoverage_body_resize() {
if (/MSIE/.test(navigator.userAgent)) {
jscoverage_setSize();
}
}
// -----------------------------------------------------------------------------
// tab 1
function jscoverage_updateBrowser() {
var input = document.getElementById("location");
frames[0].location = input.value;
}
function jscoverage_input_keypress(e) {
if (e.keyCode === 13) {
jscoverage_updateBrowser();
}
}
function jscoverage_button_click() {
jscoverage_updateBrowser();
}
function jscoverage_browser_load() {
/* update the input box */
var input = document.getElementById("location");
/* sometimes IE seems to fire this after the tab has been removed */
if (input) {
input.value = frames[0].location;
}
}
// -----------------------------------------------------------------------------
// tab 2
function jscoverage_createLink(file, line) {
var link = document.createElement("a");
var url;
var call;
var text;
if (line) {
url = file + ".jscoverage.html?" + line;
call = "jscoverage_get('" + file + "', " + line + ");";
text = line.toString();
}
else {
url = file + ".jscoverage.html";
call = "jscoverage_get('" + file + "');";
text = file;
}
link.setAttribute('href', 'javascript:' + call);
link.appendChild(document.createTextNode(text));
return link;
}
function jscoverage_recalculateSummaryTab(cc) {
var checkbox = document.getElementById('checkbox');
var showMissingColumn = checkbox.checked;
if (! cc) {
cc = window._$jscoverage;
}
if (! cc) {
//#JSCOVERAGE_IF 0
throw "No coverage information found.";
//#JSCOVERAGE_ENDIF
}
var tbody = document.getElementById("summaryTbody");
while (tbody.hasChildNodes()) {
tbody.removeChild(tbody.firstChild);
}
var totals = { files:0, statements:0, executed:0, coverage:0, skipped:0 };
var file;
var files = [];
for (file in cc) {
files.push(file);
}
files.sort();
var rowCounter = 0;
for (var f = 0; f < files.length; f++) {
file = files[f];
var lineNumber;
var num_statements = 0;
var num_executed = 0;
var missing = [];
var fileCC = cc[file];
var length = fileCC.length;
var currentConditionalEnd = 0;
var conditionals = null;
if (fileCC.conditionals) {
conditionals = fileCC.conditionals;
}
for (lineNumber = 0; lineNumber < length; lineNumber++) {
var n = fileCC[lineNumber];
if (lineNumber === currentConditionalEnd) {
currentConditionalEnd = 0;
}
else if (currentConditionalEnd === 0 && conditionals && conditionals[lineNumber]) {
currentConditionalEnd = conditionals[lineNumber];
}
if (currentConditionalEnd !== 0) {
continue;
}
if (n === undefined || n === null) {
continue;
}
if (n === 0) {
missing.push(lineNumber);
}
else {
num_executed++;
}
num_statements++;
}
var percentage = ( num_statements === 0 ? 0 : parseInt(100 * num_executed / num_statements) );
var row = document.createElement("tr");
row.className = ( rowCounter++ % 2 == 0 ? "odd" : "even" );
var cell = document.createElement("td");
cell.className = 'leftColumn';
var link = jscoverage_createLink(file);
cell.appendChild(link);
row.appendChild(cell);
cell = document.createElement("td");
cell.className = 'numeric';
cell.appendChild(document.createTextNode(num_statements));
row.appendChild(cell);
cell = document.createElement("td");
cell.className = 'numeric';
cell.appendChild(document.createTextNode(num_executed));
row.appendChild(cell);
// new coverage td containing a bar graph
cell = document.createElement("td");
cell.className = 'coverage';
var pctGraph = document.createElement("div"),
covered = document.createElement("div"),
pct = document.createElement("span");
pctGraph.className = "pctGraph";
if( num_statements === 0 ) {
covered.className = "skipped";
pct.appendChild(document.createTextNode("N/A"));
} else {
covered.className = "covered";
covered.style.width = percentage + "px";
pct.appendChild(document.createTextNode(percentage + '%'));
}
pct.className = "pct";
pctGraph.appendChild(covered);
cell.appendChild(pctGraph);
cell.appendChild(pct);
row.appendChild(cell);
if (showMissingColumn) {
cell = document.createElement("td");
for (var i = 0; i < missing.length; i++) {
if (i !== 0) {
cell.appendChild(document.createTextNode(", "));
}
link = jscoverage_createLink(file, missing[i]);
cell.appendChild(link);
}
row.appendChild(cell);
}
tbody.appendChild(row);
totals['files'] ++;
totals['statements'] += num_statements;
totals['executed'] += num_executed;
totals['coverage'] += percentage;
if( num_statements === 0 ) {
totals['skipped']++;
}
// write totals data into summaryTotals row
var tr = document.getElementById("summaryTotals");
if (tr) {
var tds = tr.getElementsByTagName("td");
tds[0].getElementsByTagName("span")[1].firstChild.nodeValue = totals['files'];
tds[1].firstChild.nodeValue = totals['statements'];
tds[2].firstChild.nodeValue = totals['executed'];
var coverage = parseInt(totals['coverage'] / ( totals['files'] - totals['skipped'] ) );
if( isNaN( coverage ) ) {
coverage = 0;
}
tds[3].getElementsByTagName("span")[0].firstChild.nodeValue = coverage + '%';
tds[3].getElementsByTagName("div")[1].style.width = coverage + 'px';
}
}
jscoverage_endLengthyOperation();
}
function jscoverage_appendMissingColumn() {
var headerRow = document.getElementById('headerRow');
var missingHeader = document.createElement('th');
missingHeader.id = 'missingHeader';
missingHeader.innerHTML = '<abbr title="List of statements missed during execution">Missing</abbr>';
headerRow.appendChild(missingHeader);
var summaryTotals = document.getElementById('summaryTotals');
var empty = document.createElement('td');
empty.id = 'missingCell';
summaryTotals.appendChild(empty);
}
function jscoverage_removeMissingColumn() {
var missingNode;
missingNode = document.getElementById('missingHeader');
missingNode.parentNode.removeChild(missingNode);
missingNode = document.getElementById('missingCell');
missingNode.parentNode.removeChild(missingNode);
}
function jscoverage_checkbox_click() {
if (jscoverage_inLengthyOperation) {
return false;
}
jscoverage_beginLengthyOperation();
var checkbox = document.getElementById('checkbox');
var showMissingColumn = checkbox.checked;
setTimeout(function() {
if (showMissingColumn) {
jscoverage_appendMissingColumn();
}
else {
jscoverage_removeMissingColumn();
}
jscoverage_recalculateSummaryTab();
}, 50);
return true;
}
// -----------------------------------------------------------------------------
// tab 3
function jscoverage_makeTable() {
var coverage = _$jscoverage[jscoverage_currentFile];
var lines = coverage.source;
// this can happen if there is an error in the original JavaScript file
if (! lines) {
lines = [];
}
var rows = ['<table id="sourceTable">'];
var i = 0;
var progressBar = document.getElementById('progressBar');
var tableHTML;
var currentConditionalEnd = 0;
function joinTableRows() {
tableHTML = rows.join('');
ProgressBar.setPercentage(progressBar, 60);
/*
This may be a long delay, so set a timeout of 100 ms to make sure the
display is updated.
*/
setTimeout(appendTable, 100);
}
function appendTable() {
var sourceDiv = document.getElementById('sourceDiv');
sourceDiv.innerHTML = tableHTML;
ProgressBar.setPercentage(progressBar, 80);
setTimeout(jscoverage_scrollToLine, 0);
}
while (i < lines.length) {
var lineNumber = i + 1;
if (lineNumber === currentConditionalEnd) {
currentConditionalEnd = 0;
}
else if (currentConditionalEnd === 0 && coverage.conditionals && coverage.conditionals[lineNumber]) {
currentConditionalEnd = coverage.conditionals[lineNumber];
}
var row = '<tr>';
row += '<td class="numeric">' + lineNumber + '</td>';
var timesExecuted = coverage[lineNumber];
if (timesExecuted !== undefined && timesExecuted !== null) {
if (currentConditionalEnd !== 0) {
row += '<td class="y numeric">';
}
else if (timesExecuted === 0) {
row += '<td class="r numeric" id="line-' + lineNumber + '">';
}
else {
row += '<td class="g numeric">';
}
row += timesExecuted;
row += '</td>';
}
else {
row += '<td></td>';
}
row += '<td><pre>' + lines[i] + '</pre></td>';
row += '</tr>';
row += '\n';
rows[lineNumber] = row;
i++;
}
rows[i + 1] = '</table>';
ProgressBar.setPercentage(progressBar, 40);
setTimeout(joinTableRows, 0);
}
function jscoverage_scrollToLine() {
jscoverage_selectTab('sourceTab');
if (! window.jscoverage_currentLine) {
jscoverage_endLengthyOperation();
return;
}
var div = document.getElementById('sourceDiv');
if (jscoverage_currentLine === 1) {
div.scrollTop = 0;
}
else {
var cell = document.getElementById('line-' + jscoverage_currentLine);
// this might not be there if there is an error in the original JavaScript
if (cell) {
var divOffset = jscoverage_findPos(div);
var cellOffset = jscoverage_findPos(cell);
div.scrollTop = cellOffset - divOffset;
}
}
jscoverage_currentLine = 0;
jscoverage_endLengthyOperation();
}
/**
Loads the given file (and optional line) in the source tab.
*/
function jscoverage_get(file, line) {
if (jscoverage_inLengthyOperation) {
return;
}
jscoverage_beginLengthyOperation();
setTimeout(function() {
var sourceDiv = document.getElementById('sourceDiv');
sourceDiv.innerHTML = '';
jscoverage_selectTab('sourceTab');
if (file === jscoverage_currentFile) {
jscoverage_currentLine = line;
jscoverage_recalculateSourceTab();
}
else {
if (jscoverage_currentFile === null) {
var tab = document.getElementById('sourceTab');
tab.className = '';
tab.onclick = jscoverage_tab_click;
}
jscoverage_currentFile = file;
jscoverage_currentLine = line || 1; // when changing the source, always scroll to top
var fileDiv = document.getElementById('fileDiv');
fileDiv.innerHTML = jscoverage_currentFile;
jscoverage_recalculateSourceTab();
return;
}
}, 50);
}
/**
Calculates coverage statistics for the current source file.
*/
function jscoverage_recalculateSourceTab() {
if (! jscoverage_currentFile) {
jscoverage_endLengthyOperation();
return;
}
var progressLabel = document.getElementById('progressLabel');
progressLabel.innerHTML = 'Calculating coverage ...';
var progressBar = document.getElementById('progressBar');
ProgressBar.setPercentage(progressBar, 20);
setTimeout(jscoverage_makeTable, 0);
}
// -----------------------------------------------------------------------------
// tabs
/**
Initializes the tab control. This function must be called when the document is
loaded.
*/
function jscoverage_initTabControl() {
var tabs = document.getElementById('tabs');
var i;
var child;
var tabNum = 0;
for (i = 0; i < tabs.childNodes.length; i++) {
child = tabs.childNodes.item(i);
if (child.nodeType === 1) {
if (child.className !== 'disabled') {
child.onclick = jscoverage_tab_click;
}
tabNum++;
}
}
jscoverage_selectTab(0);
}
/**
Selects a tab.
@param tab the integer index of the tab (0, 1, 2, or 3)
OR
the ID of the tab element
OR
the tab element itself
*/
function jscoverage_selectTab(tab) {
if (typeof tab !== 'number') {
tab = jscoverage_tabIndexOf(tab);
}
var tabs = document.getElementById('tabs');
var tabPages = document.getElementById('tabPages');
var nodeList;
var tabNum;
var i;
var node;
nodeList = tabs.childNodes;
tabNum = 0;
for (i = 0; i < nodeList.length; i++) {
node = nodeList.item(i);
if (node.nodeType !== 1) {
continue;
}
if (node.className !== 'disabled') {
if (tabNum === tab) {
node.className = 'selected';
}
else {
node.className = '';
}
}
tabNum++;
}
nodeList = tabPages.childNodes;
tabNum = 0;
for (i = 0; i < nodeList.length; i++) {
node = nodeList.item(i);
if (node.nodeType !== 1) {
continue;
}
if (tabNum === tab) {
node.className = 'selected TabPage';
}
else {
node.className = 'TabPage';
}
tabNum++;
}
}
/**
Returns an integer (0, 1, 2, or 3) representing the index of a given tab.
@param tab the ID of the tab element
OR
the tab element itself
*/
function jscoverage_tabIndexOf(tab) {
if (typeof tab === 'string') {
tab = document.getElementById(tab);
}
var tabs = document.getElementById('tabs');
var i;
var child;
var tabNum = 0;
for (i = 0; i < tabs.childNodes.length; i++) {
child = tabs.childNodes.item(i);
if (child.nodeType === 1) {
if (child === tab) {
return tabNum;
}
tabNum++;
}
}
//#JSCOVERAGE_IF 0
throw "Tab not found";
//#JSCOVERAGE_ENDIF
}
function jscoverage_tab_click(e) {
if (jscoverage_inLengthyOperation) {
return;
}
var target;
//#JSCOVERAGE_IF
if (e) {
target = e.target;
}
else if (window.event) {
// IE
target = window.event.srcElement;
}
if (target.className === 'selected') {
return;
}
jscoverage_beginLengthyOperation();
setTimeout(function() {
if (target.id === 'summaryTab') {
var tbody = document.getElementById("summaryTbody");
while (tbody.hasChildNodes()) {
tbody.removeChild(tbody.firstChild);
}
}
else if (target.id === 'sourceTab') {
var sourceDiv = document.getElementById('sourceDiv');
sourceDiv.innerHTML = '';
}
jscoverage_selectTab(target);
if (target.id === 'summaryTab') {
jscoverage_recalculateSummaryTab();
}
else if (target.id === 'sourceTab') {
jscoverage_recalculateSourceTab();
}
else {
jscoverage_endLengthyOperation();
}
}, 50);
}
// -----------------------------------------------------------------------------
// progress bar
var ProgressBar = {
init: function(element) {
element._percentage = 0;
/* doing this via JavaScript crashes Safari */
/*
var pctGraph = document.createElement('div');
pctGraph.className = 'pctGraph';
element.appendChild(pctGraph);
var covered = document.createElement('div');
covered.className = 'covered';
pctGraph.appendChild(covered);
var pct = document.createElement('span');
pct.className = 'pct';
element.appendChild(pct);
*/
ProgressBar._update(element);
},
setPercentage: function(element, percentage) {
element._percentage = percentage;
ProgressBar._update(element);
},
_update: function(element) {
var pctGraph = element.getElementsByTagName('div').item(0);
var covered = pctGraph.getElementsByTagName('div').item(0);
var pct = element.getElementsByTagName('span').item(0);
pct.innerHTML = element._percentage.toString() + '%';
covered.style.width = element._percentage + 'px';
}
};
// -----------------------------------------------------------------------------
// reports
function jscoverage_pad(s) {
return '0000'.substr(s.length) + s;
}
function jscoverage_quote(s) {
return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
switch (c) {
case '\b':
return '\\b';
case '\f':
return '\\f';
case '\n':
return '\\n';
case '\r':
return '\\r';
case '\t':
return '\\t';
// IE doesn't support this
/*
case '\v':
return '\\v';
*/
case '"':
return '\\"';
case '\\':
return '\\\\';
default:
return '\\u' + jscoverage_pad(c.charCodeAt(0).toString(16));
}
}) + '"';
}
function jscoverage_serializeCoverageToJSON() {
var json = [];
for (var file in _$jscoverage) {
var coverage = _$jscoverage[file];
var array = [];
var length = coverage.length;
for (var line = 0; line < length; line++) {
var value = coverage[line];
if (value === undefined || value === null) {
value = 'null';
}
array.push(value);
}
json.push(jscoverage_quote(file) + ':[' + array.join(',') + ']');
}
return '{' + json.join(',') + '}';
}
function jscoverage_storeButton_click() {
if (jscoverage_inLengthyOperation) {
return;
}
jscoverage_beginLengthyOperation();
var img = document.getElementById('storeImg');
img.style.visibility = 'visible';
var request = jscoverage_createRequest();
request.open('POST', '/jscoverage-store', true);
request.onreadystatechange = function (event) {
if (request.readyState === 4) {
var message;
try {
if (request.status !== 200 && request.status !== 201 && request.status !== 204) {
throw request.status;
}
message = request.responseText;