PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 211 lines | 166 code | 18 blank | 27 comment | 22 complexity | 2e43bb66a482b49e20eab1ce61b7edb6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /*
  2. jscoverage-overlay.js - script for XUL overlay
  3. Copyright (C) 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. Components.utils.import('resource://gre/modules/jscoverage.jsm');
  17. // https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
  18. function openAndReuseOneTabPerURL(url) {
  19. var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  20. .getService(Components.interfaces.nsIWindowMediator);
  21. var browserEnumerator = wm.getEnumerator("navigator:browser");
  22. // Check each browser instance for our URL
  23. var found = false;
  24. while (!found && browserEnumerator.hasMoreElements()) {
  25. var browserWin = browserEnumerator.getNext();
  26. var tabbrowser = browserWin.getBrowser();
  27. // Check each tab of this browser instance
  28. var numTabs = tabbrowser.browsers.length;
  29. for(var index=0; index<numTabs; index++) {
  30. var currentBrowser = tabbrowser.getBrowserAtIndex(index);
  31. if (url == currentBrowser.currentURI.spec) {
  32. // The URL is already opened. Select this tab.
  33. tabbrowser.selectedTab = tabbrowser.mTabs[index];
  34. // Focus *this* browser-window
  35. browserWin.focus();
  36. found = true;
  37. break;
  38. }
  39. }
  40. }
  41. // Our URL isn't open. Open it now.
  42. if (!found) {
  43. var recentWindow = wm.getMostRecentWindow("navigator:browser");
  44. if (recentWindow) {
  45. // Use an existing browser window
  46. recentWindow.delayedOpenTab(url, null, null, null, null);
  47. }
  48. else {
  49. // No browser windows are open, so open a new one.
  50. window.open(url);
  51. }
  52. }
  53. }
  54. function jscoverage_view() {
  55. openAndReuseOneTabPerURL('chrome://jscoverage/content/jscoverage.html');
  56. }
  57. function jscoverage_pad(s) {
  58. return '0000'.substr(s.length) + s;
  59. }
  60. function jscoverage_quote(s) {
  61. return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
  62. switch (c) {
  63. case '\b':
  64. return '\\b';
  65. case '\f':
  66. return '\\f';
  67. case '\n':
  68. return '\\n';
  69. case '\r':
  70. return '\\r';
  71. case '\t':
  72. return '\\t';
  73. case '\v':
  74. return '\\v';
  75. case '"':
  76. return '\\"';
  77. case '\\':
  78. return '\\\\';
  79. default:
  80. return '\\u' + jscoverage_pad(c.charCodeAt(0).toString(16));
  81. }
  82. }) + '"';
  83. }
  84. function jscoverage_store() {
  85. try {
  86. const Cc = Components.classes;
  87. const Ci = Components.interfaces;
  88. var directoryService = Cc['@mozilla.org/file/directory_service;1'].getService(Ci.nsIProperties);
  89. var reportDirectory = directoryService.get('CurWorkD', Ci.nsILocalFile);
  90. reportDirectory.appendRelativePath('jscoverage-report');
  91. if (! reportDirectory.exists()) {
  92. reportDirectory.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
  93. }
  94. var ioService = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
  95. var copyChrome = function(filename) {
  96. var channel = ioService.newChannel('chrome://jscoverage/content/' + filename, null, null);
  97. var binaryInputStream = Cc['@mozilla.org/binaryinputstream;1'].createInstance(Ci.nsIBinaryInputStream);
  98. try {
  99. binaryInputStream.setInputStream(channel.open());
  100. var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
  101. file.initWithFile(reportDirectory);
  102. file.appendRelativePath(filename);
  103. var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
  104. fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
  105. var binaryOutputStream = Cc['@mozilla.org/binaryoutputstream;1'].createInstance(Ci.nsIBinaryOutputStream);
  106. try {
  107. binaryOutputStream.setOutputStream(fileOutputStream);
  108. for (;;) {
  109. var available = binaryInputStream.available();
  110. if (available === 0) {
  111. break;
  112. }
  113. var bytes = binaryInputStream.readBytes(available);
  114. binaryOutputStream.writeBytes(bytes, bytes.length);
  115. }
  116. if (filename === 'jscoverage.js') {
  117. var s = 'jscoverage_isReport = true;\n';
  118. binaryOutputStream.write(s, s.length);
  119. }
  120. }
  121. finally {
  122. binaryOutputStream.close();
  123. }
  124. }
  125. finally {
  126. binaryInputStream.close();
  127. }
  128. };
  129. copyChrome('jscoverage.html');
  130. copyChrome('jscoverage.js');
  131. copyChrome('jscoverage.css');
  132. copyChrome('jscoverage-throbber.gif');
  133. copyChrome('jscoverage-highlight.css');
  134. // write the coverage data
  135. var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
  136. file.initWithFile(reportDirectory);
  137. file.appendRelativePath('jscoverage.json');
  138. var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
  139. try {
  140. fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
  141. function write(s) {
  142. fileOutputStream.write(s, s.length);
  143. }
  144. write('{');
  145. var first = true;
  146. for (var file in _$jscoverage) {
  147. if (first) {
  148. first = false;
  149. }
  150. else {
  151. write(',');
  152. }
  153. write(jscoverage_quote(file));
  154. write(':{"coverage":[');
  155. var coverage = _$jscoverage[file];
  156. var length = coverage.length;
  157. for (var line = 0; line < length; line++) {
  158. if (line > 0) {
  159. write(',');
  160. }
  161. var value = coverage[line];
  162. if (value === undefined || value === null) {
  163. value = 'null';
  164. }
  165. write(value.toString());
  166. }
  167. write('],"source":[');
  168. var source = coverage.source;
  169. length = source.length;
  170. for (line = 0; line < length; line++) {
  171. if (line > 0) {
  172. write(',');
  173. }
  174. write(jscoverage_quote(source[line]));
  175. }
  176. write(']}');
  177. }
  178. write('}');
  179. alert('Coverage data stored.');
  180. }
  181. finally {
  182. fileOutputStream.close();
  183. }
  184. }
  185. catch (e) {
  186. alert(e);
  187. dump(e);
  188. dump('\n');
  189. }
  190. }