jscoverage.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  1. /*
  2. jscoverage.js - code coverage for JavaScript
  3. Copyright (C) 2007, 2008, 2009, 2010 siliconforks.com
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. function jscoverage_openWarningDialog() {
  17. var id;
  18. if (jscoverage_isReport) {
  19. id = 'reportWarningDialog';
  20. }
  21. else {
  22. id = 'warningDialog';
  23. }
  24. var dialog = document.getElementById(id);
  25. dialog.style.display = 'block';
  26. }
  27. function jscoverage_closeWarningDialog() {
  28. var id;
  29. if (jscoverage_isReport) {
  30. id = 'reportWarningDialog';
  31. }
  32. else {
  33. id = 'warningDialog';
  34. }
  35. var dialog = document.getElementById(id);
  36. dialog.style.display = 'none';
  37. }
  38. /**
  39. Initializes the _$jscoverage object in a window. This should be the first
  40. function called in the page.
  41. @param w this should always be the global window object
  42. */
  43. function jscoverage_init(w) {
  44. try {
  45. // in Safari, "import" is a syntax error
  46. Components.utils['import']('resource://app/modules/jscoverage.jsm');
  47. jscoverage_isInvertedMode = true;
  48. return;
  49. }
  50. catch (e) {}
  51. // check if we are in inverted mode
  52. if (w.opener) {
  53. try {
  54. if (w.opener.top._$jscoverage) {
  55. jscoverage_isInvertedMode = true;
  56. if (! w._$jscoverage) {
  57. w._$jscoverage = w.opener.top._$jscoverage;
  58. }
  59. }
  60. else {
  61. jscoverage_isInvertedMode = false;
  62. }
  63. }
  64. catch (e) {
  65. try {
  66. if (w.opener._$jscoverage) {
  67. jscoverage_isInvertedMode = true;
  68. if (! w._$jscoverage) {
  69. w._$jscoverage = w.opener._$jscoverage;
  70. }
  71. }
  72. else {
  73. jscoverage_isInvertedMode = false;
  74. }
  75. }
  76. catch (e2) {
  77. jscoverage_isInvertedMode = false;
  78. }
  79. }
  80. }
  81. else {
  82. jscoverage_isInvertedMode = false;
  83. }
  84. if (! jscoverage_isInvertedMode) {
  85. if (! w._$jscoverage) {
  86. w._$jscoverage = {};
  87. }
  88. }
  89. }
  90. var jscoverage_currentFile = null;
  91. var jscoverage_currentLine = null;
  92. var jscoverage_inLengthyOperation = false;
  93. /*
  94. Possible states:
  95. isInvertedMode isServer isReport tabs
  96. normal false false false Browser
  97. inverted true false false
  98. server, normal false true false Browser, Store
  99. server, inverted true true false Store
  100. report false false true
  101. */
  102. var jscoverage_isInvertedMode = false;
  103. var jscoverage_isServer = false;
  104. var jscoverage_isReport = false;
  105. jscoverage_init(window);
  106. function jscoverage_createRequest() {
  107. // Note that the IE7 XMLHttpRequest does not support file URL's.
  108. // http://xhab.blogspot.com/2006/11/ie7-support-for-xmlhttprequest.html
  109. // http://blogs.msdn.com/ie/archive/2006/12/06/file-uris-in-windows.aspx
  110. //#JSCOVERAGE_IF
  111. if (window.ActiveXObject) {
  112. return new ActiveXObject("Microsoft.XMLHTTP");
  113. }
  114. else {
  115. return new XMLHttpRequest();
  116. }
  117. }
  118. // http://www.quirksmode.org/js/findpos.html
  119. function jscoverage_findPos(obj) {
  120. var result = 0;
  121. do {
  122. result += obj.offsetTop;
  123. obj = obj.offsetParent;
  124. }
  125. while (obj);
  126. return result;
  127. }
  128. // http://www.quirksmode.org/viewport/compatibility.html
  129. function jscoverage_getViewportHeight() {
  130. //#JSCOVERAGE_IF /MSIE/.test(navigator.userAgent)
  131. if (self.innerHeight) {
  132. // all except Explorer
  133. return self.innerHeight;
  134. }
  135. else if (document.documentElement && document.documentElement.clientHeight) {
  136. // Explorer 6 Strict Mode
  137. return document.documentElement.clientHeight;
  138. }
  139. else if (document.body) {
  140. // other Explorers
  141. return document.body.clientHeight;
  142. }
  143. else {
  144. throw "Couldn't calculate viewport height";
  145. }
  146. //#JSCOVERAGE_ENDIF
  147. }
  148. /**
  149. Indicates visually that a lengthy operation has begun. The progress bar is
  150. displayed, and the cursor is changed to busy (on browsers which support this).
  151. */
  152. function jscoverage_beginLengthyOperation() {
  153. jscoverage_inLengthyOperation = true;
  154. var progressBar = document.getElementById('progressBar');
  155. progressBar.style.visibility = 'visible';
  156. ProgressBar.setPercentage(progressBar, 0);
  157. var progressLabel = document.getElementById('progressLabel');
  158. progressLabel.style.visibility = 'visible';
  159. /* blacklist buggy browsers */
  160. //#JSCOVERAGE_IF
  161. if (! /Opera|WebKit/.test(navigator.userAgent)) {
  162. /*
  163. Change the cursor style of each element. Note that changing the class of the
  164. element (to one with a busy cursor) is buggy in IE.
  165. */
  166. var tabs = document.getElementById('tabs').getElementsByTagName('div');
  167. var i;
  168. for (i = 0; i < tabs.length; i++) {
  169. tabs.item(i).style.cursor = 'wait';
  170. }
  171. }
  172. }
  173. /**
  174. Removes the progress bar and busy cursor.
  175. */
  176. function jscoverage_endLengthyOperation() {
  177. var progressBar = document.getElementById('progressBar');
  178. ProgressBar.setPercentage(progressBar, 100);
  179. setTimeout(function() {
  180. jscoverage_inLengthyOperation = false;
  181. progressBar.style.visibility = 'hidden';
  182. var progressLabel = document.getElementById('progressLabel');
  183. progressLabel.style.visibility = 'hidden';
  184. progressLabel.innerHTML = '';
  185. var tabs = document.getElementById('tabs').getElementsByTagName('div');
  186. var i;
  187. for (i = 0; i < tabs.length; i++) {
  188. tabs.item(i).style.cursor = '';
  189. }
  190. }, 50);
  191. }
  192. function jscoverage_setSize() {
  193. //#JSCOVERAGE_IF /MSIE/.test(navigator.userAgent)
  194. var viewportHeight = jscoverage_getViewportHeight();
  195. /*
  196. border-top-width: 1px
  197. padding-top: 10px
  198. padding-bottom: 10px
  199. border-bottom-width: 1px
  200. margin-bottom: 10px
  201. ----
  202. 32px
  203. */
  204. var tabPages = document.getElementById('tabPages');
  205. var tabPageHeight = (viewportHeight - jscoverage_findPos(tabPages) - 32) + 'px';
  206. var nodeList = tabPages.childNodes;
  207. var length = nodeList.length;
  208. for (var i = 0; i < length; i++) {
  209. var node = nodeList.item(i);
  210. if (node.nodeType !== 1) {
  211. continue;
  212. }
  213. node.style.height = tabPageHeight;
  214. }
  215. var iframeDiv = document.getElementById('iframeDiv');
  216. // may not exist if we have removed the first tab
  217. if (iframeDiv) {
  218. iframeDiv.style.height = (viewportHeight - jscoverage_findPos(iframeDiv) - 21) + 'px';
  219. }
  220. var summaryDiv = document.getElementById('summaryDiv');
  221. summaryDiv.style.height = (viewportHeight - jscoverage_findPos(summaryDiv) - 21) + 'px';
  222. var sourceDiv = document.getElementById('sourceDiv');
  223. sourceDiv.style.height = (viewportHeight - jscoverage_findPos(sourceDiv) - 21) + 'px';
  224. var storeDiv = document.getElementById('storeDiv');
  225. if (storeDiv) {
  226. storeDiv.style.height = (viewportHeight - jscoverage_findPos(storeDiv) - 21) + 'px';
  227. }
  228. //#JSCOVERAGE_ENDIF
  229. }
  230. /**
  231. Returns the boolean value of a string. Values 'false', 'f', 'no', 'n', 'off',
  232. and '0' (upper or lower case) are false.
  233. @param s the string
  234. @return a boolean value
  235. */
  236. function jscoverage_getBooleanValue(s) {
  237. s = s.toLowerCase();
  238. if (s === 'false' || s === 'f' || s === 'no' || s === 'n' || s === 'off' || s === '0') {
  239. return false;
  240. }
  241. return true;
  242. }
  243. function jscoverage_removeTab(id) {
  244. var tab = document.getElementById(id + 'Tab');
  245. tab.parentNode.removeChild(tab);
  246. var tabPage = document.getElementById(id + 'TabPage');
  247. tabPage.parentNode.removeChild(tabPage);
  248. }
  249. function jscoverage_isValidURL(url) {
  250. // RFC 3986
  251. var matches = /^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/.exec(url);
  252. if (matches === null) {
  253. return false;
  254. }
  255. var scheme = matches[1];
  256. if (typeof scheme === 'string') {
  257. scheme = scheme.toLowerCase();
  258. return scheme === '' || scheme === 'file:' || scheme === 'http:' || scheme === 'https:';
  259. }
  260. return true;
  261. }
  262. /**
  263. Initializes the contents of the tabs. This sets the initial values of the
  264. input field and iframe in the "Browser" tab and the checkbox in the "Summary"
  265. tab.
  266. @param queryString this should always be location.search
  267. */
  268. function jscoverage_initTabContents(queryString) {
  269. var showMissingColumn = false;
  270. var url = null;
  271. var windowURL = null;
  272. var parameters, parameter, i, index, name, value;
  273. if (queryString.length > 0) {
  274. // chop off the question mark
  275. queryString = queryString.substring(1);
  276. parameters = queryString.split(/&|;/);
  277. for (i = 0; i < parameters.length; i++) {
  278. parameter = parameters[i];
  279. index = parameter.indexOf('=');
  280. if (index === -1) {
  281. // still works with old syntax
  282. url = decodeURIComponent(parameter);
  283. }
  284. else {
  285. name = parameter.substr(0, index);
  286. value = decodeURIComponent(parameter.substr(index + 1));
  287. if (name === 'missing' || name === 'm') {
  288. showMissingColumn = jscoverage_getBooleanValue(value);
  289. }
  290. else if (name === 'url' || name === 'u' || name === 'frame' || name === 'f') {
  291. url = value;
  292. }
  293. else if (name === 'window' || name === 'w') {
  294. windowURL = value;
  295. }
  296. }
  297. }
  298. }
  299. var checkbox = document.getElementById('checkbox');
  300. checkbox.checked = showMissingColumn;
  301. if (showMissingColumn) {
  302. jscoverage_appendMissingColumn();
  303. }
  304. var isValidURL = function (url) {
  305. var result = jscoverage_isValidURL(url);
  306. if (! result) {
  307. alert('Invalid URL: ' + url);
  308. }
  309. return result;
  310. };
  311. if (url !== null && isValidURL(url)) {
  312. // this will automatically propagate to the input field
  313. frames[0].location = url;
  314. }
  315. else if (windowURL !== null && isValidURL(windowURL)) {
  316. window.open(windowURL);
  317. }
  318. // if the browser tab is absent, we have to initialize the summary tab
  319. if (! document.getElementById('browserTab')) {
  320. jscoverage_recalculateSummaryTab();
  321. }
  322. }
  323. function jscoverage_body_load() {
  324. // check if this is a file: URL
  325. if (window.location && window.location.href && /^file:/i.test(window.location.href)) {
  326. var warningDiv = document.getElementById('warningDiv');
  327. warningDiv.style.display = 'block';
  328. }
  329. var progressBar = document.getElementById('progressBar');
  330. ProgressBar.init(progressBar);
  331. function reportError(e) {
  332. jscoverage_endLengthyOperation();
  333. var summaryThrobber = document.getElementById('summaryThrobber');
  334. summaryThrobber.style.visibility = 'hidden';
  335. var div = document.getElementById('summaryErrorDiv');
  336. div.innerHTML = 'Error: ' + e;
  337. }
  338. if (jscoverage_isReport) {
  339. jscoverage_beginLengthyOperation();
  340. var summaryThrobber = document.getElementById('summaryThrobber');
  341. summaryThrobber.style.visibility = 'visible';
  342. var request = jscoverage_createRequest();
  343. try {
  344. request.open('GET', 'jscoverage.json', true);
  345. request.onreadystatechange = function (event) {
  346. if (request.readyState === 4) {
  347. try {
  348. if (request.status !== 0 && request.status !== 200) {
  349. throw request.status;
  350. }
  351. var response = request.responseText;
  352. if (response === '') {
  353. throw 404;
  354. }
  355. var json;
  356. if (window.JSON && window.JSON.parse) {
  357. json = window.JSON.parse(response);
  358. }
  359. else {
  360. json = eval('(' + response + ')');
  361. }
  362. var file;
  363. for (file in json) {
  364. if (! json.hasOwnProperty(file)) {
  365. continue;
  366. }
  367. var fileCoverage = json[file];
  368. _$jscoverage[file] = fileCoverage.coverage;
  369. _$jscoverage[file].source = fileCoverage.source;
  370. }
  371. jscoverage_recalculateSummaryTab();
  372. summaryThrobber.style.visibility = 'hidden';
  373. }
  374. catch (e) {
  375. reportError(e);
  376. }
  377. }
  378. };
  379. request.send(null);
  380. }
  381. catch (e) {
  382. reportError(e);
  383. }
  384. jscoverage_removeTab('browser');
  385. jscoverage_removeTab('store');
  386. }
  387. else {
  388. if (jscoverage_isInvertedMode) {
  389. jscoverage_removeTab('browser');
  390. }
  391. if (! jscoverage_isServer) {
  392. jscoverage_removeTab('store');
  393. }
  394. }
  395. jscoverage_initTabControl();
  396. jscoverage_initTabContents(location.search);
  397. }
  398. function jscoverage_body_resize() {
  399. if (/MSIE/.test(navigator.userAgent)) {
  400. jscoverage_setSize();
  401. }
  402. }
  403. // -----------------------------------------------------------------------------
  404. // tab 1
  405. function jscoverage_updateBrowser() {
  406. var input = document.getElementById("location");
  407. frames[0].location = input.value;
  408. }
  409. function jscoverage_openWindow() {
  410. var input = document.getElementById("location");
  411. var url = input.value;
  412. window.open(url);
  413. }
  414. function jscoverage_input_keypress(e) {
  415. if (e.keyCode === 13) {
  416. if (e.shiftKey) {
  417. jscoverage_openWindow();
  418. }
  419. else {
  420. jscoverage_updateBrowser();
  421. }
  422. }
  423. }
  424. function jscoverage_openInFrameButton_click() {
  425. jscoverage_updateBrowser();
  426. }
  427. function jscoverage_openInWindowButton_click() {
  428. jscoverage_openWindow();
  429. }
  430. function jscoverage_browser_load() {
  431. /* update the input box */
  432. var input = document.getElementById("location");
  433. /* sometimes IE seems to fire this after the tab has been removed */
  434. if (input) {
  435. input.value = frames[0].location;
  436. }
  437. }
  438. // -----------------------------------------------------------------------------
  439. // tab 2
  440. function jscoverage_createHandler(file, line) {
  441. return function () {
  442. jscoverage_get(file, line);
  443. return false;
  444. };
  445. }
  446. function jscoverage_createLink(file, line) {
  447. var link = document.createElement("a");
  448. link.href = '#';
  449. link.onclick = jscoverage_createHandler(file, line);
  450. var text;
  451. if (line) {
  452. text = line.toString();
  453. }
  454. else {
  455. text = file;
  456. }
  457. link.appendChild(document.createTextNode(text));
  458. return link;
  459. }
  460. function jscoverage_recalculateSummaryTab(cc) {
  461. var checkbox = document.getElementById('checkbox');
  462. var showMissingColumn = checkbox.checked;
  463. if (! cc) {
  464. cc = window._$jscoverage;
  465. }
  466. if (! cc) {
  467. //#JSCOVERAGE_IF 0
  468. throw "No coverage information found.";
  469. //#JSCOVERAGE_ENDIF
  470. }
  471. var tbody = document.getElementById("summaryTbody");
  472. while (tbody.hasChildNodes()) {
  473. tbody.removeChild(tbody.firstChild);
  474. }
  475. var totals = { files:0, statements:0, executed:0 };
  476. var file;
  477. var files = [];
  478. for (file in cc) {
  479. if (! cc.hasOwnProperty(file)) {
  480. continue;
  481. }
  482. files.push(file);
  483. }
  484. files.sort();
  485. var rowCounter = 0;
  486. for (var f = 0; f < files.length; f++) {
  487. file = files[f];
  488. var lineNumber;
  489. var num_statements = 0;
  490. var num_executed = 0;
  491. var missing = [];
  492. var fileCC = cc[file];
  493. var length = fileCC.length;
  494. var currentConditionalEnd = 0;
  495. var conditionals = null;
  496. if (fileCC.conditionals) {
  497. conditionals = fileCC.conditionals;
  498. }
  499. for (lineNumber = 0; lineNumber < length; lineNumber++) {
  500. var n = fileCC[lineNumber];
  501. if (lineNumber === currentConditionalEnd) {
  502. currentConditionalEnd = 0;
  503. }
  504. else if (currentConditionalEnd === 0 && conditionals && conditionals[lineNumber]) {
  505. currentConditionalEnd = conditionals[lineNumber];
  506. }
  507. if (currentConditionalEnd !== 0) {
  508. continue;
  509. }
  510. if (n === undefined || n === null) {
  511. continue;
  512. }
  513. if (n === 0) {
  514. missing.push(lineNumber);
  515. }
  516. else {
  517. num_executed++;
  518. }
  519. num_statements++;
  520. }
  521. var percentage = ( num_statements === 0 ? 0 : parseInt(100 * num_executed / num_statements) );
  522. var row = document.createElement("tr");
  523. row.className = ( rowCounter++ % 2 == 0 ? "odd" : "even" );
  524. var cell = document.createElement("td");
  525. cell.className = 'leftColumn';
  526. var link = jscoverage_createLink(file);
  527. cell.appendChild(link);
  528. row.appendChild(cell);
  529. cell = document.createElement("td");
  530. cell.className = 'numeric';
  531. cell.appendChild(document.createTextNode(num_statements));
  532. row.appendChild(cell);
  533. cell = document.createElement("td");
  534. cell.className = 'numeric';
  535. cell.appendChild(document.createTextNode(num_executed));
  536. row.appendChild(cell);
  537. // new coverage td containing a bar graph
  538. cell = document.createElement("td");
  539. cell.className = 'coverage';
  540. var pctGraph = document.createElement("div"),
  541. covered = document.createElement("div"),
  542. pct = document.createElement("span");
  543. pctGraph.className = "pctGraph";
  544. if( num_statements === 0 ) {
  545. covered.className = "skipped";
  546. pct.appendChild(document.createTextNode("N/A"));
  547. } else {
  548. covered.className = "covered";
  549. covered.style.width = percentage + "px";
  550. pct.appendChild(document.createTextNode(percentage + '%'));
  551. }
  552. pct.className = "pct";
  553. pctGraph.appendChild(covered);
  554. cell.appendChild(pctGraph);
  555. cell.appendChild(pct);
  556. row.appendChild(cell);
  557. if (showMissingColumn) {
  558. cell = document.createElement("td");
  559. for (var i = 0; i < missing.length; i++) {
  560. if (i !== 0) {
  561. cell.appendChild(document.createTextNode(", "));
  562. }
  563. link = jscoverage_createLink(file, missing[i]);
  564. // group contiguous missing lines; e.g., 10, 11, 12 -> 10-12
  565. var j, start = missing[i];
  566. for (;;) {
  567. j = 1;
  568. while (i + j < missing.length && missing[i + j] == missing[i] + j) {
  569. j++;
  570. }
  571. var nextmissing = missing[i + j], cur = missing[i] + j;
  572. if (isNaN(nextmissing)) {
  573. break;
  574. }
  575. while (cur < nextmissing && ! fileCC[cur]) {
  576. cur++;
  577. }
  578. if (cur < nextmissing || cur >= length) {
  579. break;
  580. }
  581. i += j;
  582. }
  583. if (start != missing[i] || j > 1) {
  584. i += j - 1;
  585. link.innerHTML += "-" + missing[i];
  586. }
  587. cell.appendChild(link);
  588. }
  589. row.appendChild(cell);
  590. }
  591. tbody.appendChild(row);
  592. totals['files'] ++;
  593. totals['statements'] += num_statements;
  594. totals['executed'] += num_executed;
  595. // write totals data into summaryTotals row
  596. var tr = document.getElementById("summaryTotals");
  597. if (tr) {
  598. var tds = tr.getElementsByTagName("td");
  599. tds[0].getElementsByTagName("span")[1].firstChild.nodeValue = totals['files'];
  600. tds[1].firstChild.nodeValue = totals['statements'];
  601. tds[2].firstChild.nodeValue = totals['executed'];
  602. var coverage = parseInt(100 * totals['executed'] / totals['statements']);
  603. if( isNaN( coverage ) ) {
  604. coverage = 0;
  605. }
  606. tds[3].getElementsByTagName("span")[0].firstChild.nodeValue = coverage + '%';
  607. tds[3].getElementsByTagName("div")[1].style.width = coverage + 'px';
  608. }
  609. }
  610. jscoverage_endLengthyOperation();
  611. }
  612. function jscoverage_appendMissingColumn() {
  613. var headerRow = document.getElementById('headerRow');
  614. var missingHeader = document.createElement('th');
  615. missingHeader.id = 'missingHeader';
  616. missingHeader.innerHTML = '<abbr title="List of statements missed during execution">Missing</abbr>';
  617. headerRow.appendChild(missingHeader);
  618. var summaryTotals = document.getElementById('summaryTotals');
  619. var empty = document.createElement('td');
  620. empty.id = 'missingCell';
  621. summaryTotals.appendChild(empty);
  622. }
  623. function jscoverage_removeMissingColumn() {
  624. var missingNode;
  625. missingNode = document.getElementById('missingHeader');
  626. missingNode.parentNode.removeChild(missingNode);
  627. missingNode = document.getElementById('missingCell');
  628. missingNode.parentNode.removeChild(missingNode);
  629. }
  630. function jscoverage_checkbox_click() {
  631. if (jscoverage_inLengthyOperation) {
  632. return false;
  633. }
  634. jscoverage_beginLengthyOperation();
  635. var checkbox = document.getElementById('checkbox');
  636. var showMissingColumn = checkbox.checked;
  637. setTimeout(function() {
  638. if (showMissingColumn) {
  639. jscoverage_appendMissingColumn();
  640. }
  641. else {
  642. jscoverage_removeMissingColumn();
  643. }
  644. jscoverage_recalculateSummaryTab();
  645. }, 50);
  646. return true;
  647. }
  648. // -----------------------------------------------------------------------------
  649. // tab 3
  650. function jscoverage_makeTable() {
  651. var coverage = _$jscoverage[jscoverage_currentFile];
  652. var lines = coverage.source;
  653. // this can happen if there is an error in the original JavaScript file
  654. if (! lines) {
  655. lines = [];
  656. }
  657. var rows = ['<table id="sourceTable">'];
  658. var i = 0;
  659. var progressBar = document.getElementById('progressBar');
  660. var tableHTML;
  661. var currentConditionalEnd = 0;
  662. function joinTableRows() {
  663. tableHTML = rows.join('');
  664. ProgressBar.setPercentage(progressBar, 60);
  665. /*
  666. This may be a long delay, so set a timeout of 100 ms to make sure the
  667. display is updated.
  668. */
  669. setTimeout(appendTable, 100);
  670. }
  671. function appendTable() {
  672. var sourceDiv = document.getElementById('sourceDiv');
  673. sourceDiv.innerHTML = tableHTML;
  674. ProgressBar.setPercentage(progressBar, 80);
  675. setTimeout(jscoverage_scrollToLine, 0);
  676. }
  677. while (i < lines.length) {
  678. var lineNumber = i + 1;
  679. if (lineNumber === currentConditionalEnd) {
  680. currentConditionalEnd = 0;
  681. }
  682. else if (currentConditionalEnd === 0 && coverage.conditionals && coverage.conditionals[lineNumber]) {
  683. currentConditionalEnd = coverage.conditionals[lineNumber];
  684. }
  685. var row = '<tr>';
  686. row += '<td class="numeric">' + lineNumber + '</td>';
  687. var timesExecuted = coverage[lineNumber];
  688. if (timesExecuted !== undefined && timesExecuted !== null) {
  689. if (currentConditionalEnd !== 0) {
  690. row += '<td class="y numeric">';
  691. }
  692. else if (timesExecuted === 0) {
  693. row += '<td class="r numeric" id="line-' + lineNumber + '">';
  694. }
  695. else {
  696. row += '<td class="g numeric">';
  697. }
  698. row += timesExecuted;
  699. row += '</td>';
  700. }
  701. else {
  702. row += '<td></td>';
  703. }
  704. row += '<td><pre>' + lines[i] + '</pre></td>';
  705. row += '</tr>';
  706. row += '\n';
  707. rows[lineNumber] = row;
  708. i++;
  709. }
  710. rows[i + 1] = '</table>';
  711. ProgressBar.setPercentage(progressBar, 40);
  712. setTimeout(joinTableRows, 0);
  713. }
  714. function jscoverage_scrollToLine() {
  715. jscoverage_selectTab('sourceTab');
  716. if (! window.jscoverage_currentLine) {
  717. jscoverage_endLengthyOperation();
  718. return;
  719. }
  720. var div = document.getElementById('sourceDiv');
  721. if (jscoverage_currentLine === 1) {
  722. div.scrollTop = 0;
  723. }
  724. else {
  725. var cell = document.getElementById('line-' + jscoverage_currentLine);
  726. // this might not be there if there is an error in the original JavaScript
  727. if (cell) {
  728. var divOffset = jscoverage_findPos(div);
  729. var cellOffset = jscoverage_findPos(cell);
  730. div.scrollTop = cellOffset - divOffset;
  731. }
  732. }
  733. jscoverage_currentLine = 0;
  734. jscoverage_endLengthyOperation();
  735. }
  736. /**
  737. Loads the given file (and optional line) in the source tab.
  738. */
  739. function jscoverage_get(file, line) {
  740. if (jscoverage_inLengthyOperation) {
  741. return;
  742. }
  743. jscoverage_beginLengthyOperation();
  744. setTimeout(function() {
  745. var sourceDiv = document.getElementById('sourceDiv');
  746. sourceDiv.innerHTML = '';
  747. jscoverage_selectTab('sourceTab');
  748. if (file === jscoverage_currentFile) {
  749. jscoverage_currentLine = line;
  750. jscoverage_recalculateSourceTab();
  751. }
  752. else {
  753. if (jscoverage_currentFile === null) {
  754. var tab = document.getElementById('sourceTab');
  755. tab.className = '';
  756. tab.onclick = jscoverage_tab_click;
  757. }
  758. jscoverage_currentFile = file;
  759. jscoverage_currentLine = line || 1; // when changing the source, always scroll to top
  760. var fileDiv = document.getElementById('fileDiv');
  761. fileDiv.innerHTML = jscoverage_currentFile;
  762. jscoverage_recalculateSourceTab();
  763. return;
  764. }
  765. }, 50);
  766. }
  767. /**
  768. Calculates coverage statistics for the current source file.
  769. */
  770. function jscoverage_recalculateSourceTab() {
  771. if (! jscoverage_currentFile) {
  772. jscoverage_endLengthyOperation();
  773. return;
  774. }
  775. var progressLabel = document.getElementById('progressLabel');
  776. progressLabel.innerHTML = 'Calculating coverage ...';
  777. var progressBar = document.getElementById('progressBar');
  778. ProgressBar.setPercentage(progressBar, 20);
  779. setTimeout(jscoverage_makeTable, 0);
  780. }
  781. // -----------------------------------------------------------------------------
  782. // tabs
  783. /**
  784. Initializes the tab control. This function must be called when the document is
  785. loaded.
  786. */
  787. function jscoverage_initTabControl() {
  788. var tabs = document.getElementById('tabs');
  789. var i;
  790. var child;
  791. var tabNum = 0;
  792. for (i = 0; i < tabs.childNodes.length; i++) {
  793. child = tabs.childNodes.item(i);
  794. if (child.nodeType === 1) {
  795. if (child.className !== 'disabled') {
  796. child.onclick = jscoverage_tab_click;
  797. }
  798. tabNum++;
  799. }
  800. }
  801. jscoverage_selectTab(0);
  802. }
  803. /**
  804. Selects a tab.
  805. @param tab the integer index of the tab (0, 1, 2, or 3)
  806. OR
  807. the ID of the tab element
  808. OR
  809. the tab element itself
  810. */
  811. function jscoverage_selectTab(tab) {
  812. if (typeof tab !== 'number') {
  813. tab = jscoverage_tabIndexOf(tab);
  814. }
  815. var tabs = document.getElementById('tabs');
  816. var tabPages = document.getElementById('tabPages');
  817. var nodeList;
  818. var tabNum;
  819. var i;
  820. var node;
  821. nodeList = tabs.childNodes;
  822. tabNum = 0;
  823. for (i = 0; i < nodeList.length; i++) {
  824. node = nodeList.item(i);
  825. if (node.nodeType !== 1) {
  826. continue;
  827. }
  828. if (node.className !== 'disabled') {
  829. if (tabNum === tab) {
  830. node.className = 'selected';
  831. }
  832. else {
  833. node.className = '';
  834. }
  835. }
  836. tabNum++;
  837. }
  838. nodeList = tabPages.childNodes;
  839. tabNum = 0;
  840. for (i = 0; i < nodeList.length; i++) {
  841. node = nodeList.item(i);
  842. if (node.nodeType !== 1) {
  843. continue;
  844. }
  845. if (tabNum === tab) {
  846. node.className = 'selected TabPage';
  847. }
  848. else {
  849. node.className = 'TabPage';
  850. }
  851. tabNum++;
  852. }
  853. }
  854. /**
  855. Returns an integer (0, 1, 2, or 3) representing the index of a given tab.
  856. @param tab the ID of the tab element
  857. OR
  858. the tab element itself
  859. */
  860. function jscoverage_tabIndexOf(tab) {
  861. if (typeof tab === 'string') {
  862. tab = document.getElementById(tab);
  863. }
  864. var tabs = document.getElementById('tabs');
  865. var i;
  866. var child;
  867. var tabNum = 0;
  868. for (i = 0; i < tabs.childNodes.length; i++) {
  869. child = tabs.childNodes.item(i);
  870. if (child.nodeType === 1) {
  871. if (child === tab) {
  872. return tabNum;
  873. }
  874. tabNum++;
  875. }
  876. }
  877. //#JSCOVERAGE_IF 0
  878. throw "Tab not found";
  879. //#JSCOVERAGE_ENDIF
  880. }
  881. function jscoverage_tab_click(e) {
  882. if (jscoverage_inLengthyOperation) {
  883. return;
  884. }
  885. var target;
  886. //#JSCOVERAGE_IF
  887. if (e) {
  888. target = e.target;
  889. }
  890. else if (window.event) {
  891. // IE
  892. target = window.event.srcElement;
  893. }
  894. if (target.className === 'selected') {
  895. return;
  896. }
  897. jscoverage_beginLengthyOperation();
  898. setTimeout(function() {
  899. if (target.id === 'summaryTab') {
  900. var tbody = document.getElementById("summaryTbody");
  901. while (tbody.hasChildNodes()) {
  902. tbody.removeChild(tbody.firstChild);
  903. }
  904. }
  905. else if (target.id === 'sourceTab') {
  906. var sourceDiv = document.getElementById('sourceDiv');
  907. sourceDiv.innerHTML = '';
  908. }
  909. jscoverage_selectTab(target);
  910. if (target.id === 'summaryTab') {
  911. jscoverage_recalculateSummaryTab();
  912. }
  913. else if (target.id === 'sourceTab') {
  914. jscoverage_recalculateSourceTab();
  915. }
  916. else {
  917. jscoverage_endLengthyOperation();
  918. }
  919. }, 50);
  920. }
  921. // -----------------------------------------------------------------------------
  922. // progress bar
  923. var ProgressBar = {
  924. init: function(element) {
  925. element._percentage = 0;
  926. /* doing this via JavaScript crashes Safari */
  927. /*
  928. var pctGraph = document.createElement('div');
  929. pctGraph.className = 'pctGraph';
  930. element.appendChild(pctGraph);
  931. var covered = document.createElement('div');
  932. covered.className = 'covered';
  933. pctGraph.appendChild(covered);
  934. var pct = document.createElement('span');
  935. pct.className = 'pct';
  936. element.appendChild(pct);
  937. */
  938. ProgressBar._update(element);
  939. },
  940. setPercentage: function(element, percentage) {
  941. element._percentage = percentage;
  942. ProgressBar._update(element);
  943. },
  944. _update: function(element) {
  945. var pctGraph = element.getElementsByTagName('div').item(0);
  946. var covered = pctGraph.getElementsByTagName('div').item(0);
  947. var pct = element.getElementsByTagName('span').item(0);
  948. pct.innerHTML = element._percentage.toString() + '%';
  949. covered.style.width = element._percentage + 'px';
  950. }
  951. };
  952. // -----------------------------------------------------------------------------
  953. // reports
  954. function jscoverage_pad(s) {
  955. return '0000'.substr(s.length) + s;
  956. }
  957. function jscoverage_quote(s) {
  958. return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
  959. switch (c) {
  960. case '\b':
  961. return '\\b';
  962. case '\f':
  963. return '\\f';
  964. case '\n':
  965. return '\\n';
  966. case '\r':
  967. return '\\r';
  968. case '\t':
  969. return '\\t';
  970. // IE doesn't support this
  971. /*
  972. case '\v':
  973. return '\\v';
  974. */
  975. case '"':
  976. return '\\"';
  977. case '\\':
  978. return '\\\\';
  979. default:
  980. return '\\u' + jscoverage_pad(c.charCodeAt(0).toString(16));
  981. }
  982. }) + '"';
  983. }
  984. function jscoverage_serializeCoverageToJSON() {
  985. var json = [];
  986. for (var file in _$jscoverage) {
  987. if (! _$jscoverage.hasOwnProperty(file)) {
  988. continue;
  989. }
  990. var coverage = _$jscoverage[file];
  991. var array = [];
  992. var length = coverage.length;
  993. for (var line = 0; line < length; line++) {
  994. var value = coverage[line];
  995. if (value === undefined || value === null) {
  996. value = 'null';
  997. }
  998. array.push(value);
  999. }
  1000. var source = coverage.source;
  1001. var lines = [];
  1002. length = source.length;
  1003. for (var line = 0; line < length; line++) {
  1004. lines.push(jscoverage_quote(source[line]));
  1005. }
  1006. json.push(jscoverage_quote(file) + ':{"coverage":[' + array.join(',') + '],"source":[' + lines.join(',') + ']}');
  1007. }
  1008. return '{' + json.join(',') + '}';
  1009. }
  1010. function jscoverage_storeButton_click() {
  1011. if (jscoverage_inLengthyOperation) {
  1012. return;
  1013. }
  1014. jscoverage_beginLengthyOperation();
  1015. var img = document.getElementById('storeImg');
  1016. img.style.visibility = 'visible';
  1017. var request = jscoverage_createRequest();
  1018. request.open('POST', '/jscoverage-store', true);
  1019. request.onreadystatechange = function (event) {
  1020. if (request.readyState === 4) {
  1021. var message;
  1022. try {
  1023. if (request.status !== 200 && request.status !== 201 && request.status !== 204) {
  1024. throw request.status;
  1025. }
  1026. message = request.responseText;
  1027. }
  1028. catch (e) {
  1029. if (e.toString().search(/^\d{3}$/) === 0) {
  1030. message = e + ': ' + request.responseText;
  1031. }
  1032. else {
  1033. message = 'Could not connect to server: ' + e;
  1034. }
  1035. }
  1036. jscoverage_endLengthyOperation();
  1037. var img = document.getElementById('storeImg');
  1038. img.style.visibility = 'hidden';
  1039. var div = document.getElementById('storeDiv');
  1040. div.appendChild(document.createTextNode(new Date() + ': ' + message));
  1041. div.appendChild(document.createElement('br'));
  1042. }
  1043. };
  1044. request.setRequestHeader('Content-Type', 'application/json');
  1045. var json = jscoverage_serializeCoverageToJSON();
  1046. request.setRequestHeader('Content-Length', json.length.toString());
  1047. request.send(json);
  1048. }