forked from visionmedia/node-jscoverage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jscoverage-overlay.js
211 lines (190 loc) · 6.6 KB
/
jscoverage-overlay.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
/*
jscoverage-overlay.js - script for XUL overlay
Copyright (C) 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.
*/
Components.utils.import('resource://gre/modules/jscoverage.jsm');
// https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
function openAndReuseOneTabPerURL(url) {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser");
// Check each browser instance for our URL
var found = false;
while (!found && browserEnumerator.hasMoreElements()) {
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.getBrowser();
// Check each tab of this browser instance
var numTabs = tabbrowser.browsers.length;
for(var index=0; index<numTabs; index++) {
var currentBrowser = tabbrowser.getBrowserAtIndex(index);
if (url == currentBrowser.currentURI.spec) {
// The URL is already opened. Select this tab.
tabbrowser.selectedTab = tabbrowser.mTabs[index];
// Focus *this* browser-window
browserWin.focus();
found = true;
break;
}
}
}
// Our URL isn't open. Open it now.
if (!found) {
var recentWindow = wm.getMostRecentWindow("navigator:browser");
if (recentWindow) {
// Use an existing browser window
recentWindow.delayedOpenTab(url, null, null, null, null);
}
else {
// No browser windows are open, so open a new one.
window.open(url);
}
}
}
function jscoverage_view() {
openAndReuseOneTabPerURL('chrome://jscoverage/content/jscoverage.html');
}
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';
case '\v':
return '\\v';
case '"':
return '\\"';
case '\\':
return '\\\\';
default:
return '\\u' + jscoverage_pad(c.charCodeAt(0).toString(16));
}
}) + '"';
}
function jscoverage_store() {
try {
const Cc = Components.classes;
const Ci = Components.interfaces;
var directoryService = Cc['@mozilla.org/file/directory_service;1'].getService(Ci.nsIProperties);
var reportDirectory = directoryService.get('CurWorkD', Ci.nsILocalFile);
reportDirectory.appendRelativePath('jscoverage-report');
if (! reportDirectory.exists()) {
reportDirectory.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
}
var ioService = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
var copyChrome = function(filename) {
var channel = ioService.newChannel('chrome://jscoverage/content/' + filename, null, null);
var binaryInputStream = Cc['@mozilla.org/binaryinputstream;1'].createInstance(Ci.nsIBinaryInputStream);
try {
binaryInputStream.setInputStream(channel.open());
var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
file.initWithFile(reportDirectory);
file.appendRelativePath(filename);
var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
var binaryOutputStream = Cc['@mozilla.org/binaryoutputstream;1'].createInstance(Ci.nsIBinaryOutputStream);
try {
binaryOutputStream.setOutputStream(fileOutputStream);
for (;;) {
var available = binaryInputStream.available();
if (available === 0) {
break;
}
var bytes = binaryInputStream.readBytes(available);
binaryOutputStream.writeBytes(bytes, bytes.length);
}
if (filename === 'jscoverage.js') {
var s = 'jscoverage_isReport = true;\n';
binaryOutputStream.write(s, s.length);
}
}
finally {
binaryOutputStream.close();
}
}
finally {
binaryInputStream.close();
}
};
copyChrome('jscoverage.html');
copyChrome('jscoverage.js');
copyChrome('jscoverage.css');
copyChrome('jscoverage-throbber.gif');
copyChrome('jscoverage-highlight.css');
// write the coverage data
var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
file.initWithFile(reportDirectory);
file.appendRelativePath('jscoverage.json');
var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
try {
fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
function write(s) {
fileOutputStream.write(s, s.length);
}
write('{');
var first = true;
for (var file in _$jscoverage) {
if (first) {
first = false;
}
else {
write(',');
}
write(jscoverage_quote(file));
write(':{"coverage":[');
var coverage = _$jscoverage[file];
var length = coverage.length;
for (var line = 0; line < length; line++) {
if (line > 0) {
write(',');
}
var value = coverage[line];
if (value === undefined || value === null) {
value = 'null';
}
write(value.toString());
}
write('],"source":[');
var source = coverage.source;
length = source.length;
for (line = 0; line < length; line++) {
if (line > 0) {
write(',');
}
write(jscoverage_quote(source[line]));
}
write(']}');
}
write('}');
alert('Coverage data stored.');
}
finally {
fileOutputStream.close();
}
}
catch (e) {
alert(e);
dump(e);
dump('\n');
}
}