PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/jscoverage.js

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